Fix op gcc error on old Debian.
[m6w6/libmemcached] / libhashkit / hashkit.cc
1 /* HashKit
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 */
8
9 #include <libhashkit/common.h>
10
11 static inline void _hashkit_init(hashkit_st *self)
12 {
13 self->base_hash.function= hashkit_one_at_a_time;
14 self->base_hash.context= NULL;
15
16 self->distribution_hash.function= hashkit_one_at_a_time;
17 self->distribution_hash.context= NULL;
18
19 self->flags.is_base_same_distributed= true;
20 self->_key= NULL;
21 }
22
23 static inline hashkit_st *_hashkit_create(hashkit_st *self)
24 {
25 if (self)
26 {
27 self->options.is_allocated= false;
28 }
29 else
30 {
31 self= (hashkit_st*)calloc(1, sizeof(hashkit_st));
32 if (self == NULL)
33 {
34 return NULL;
35 }
36
37 self->options.is_allocated= true;
38 }
39
40 return self;
41 }
42
43 hashkit_st *hashkit_create(hashkit_st *self)
44 {
45 self= _hashkit_create(self);
46 if (self == NULL)
47 {
48 return NULL;
49 }
50
51 _hashkit_init(self);
52
53 return self;
54 }
55
56
57 void hashkit_free(hashkit_st *self)
58 {
59 if (self and self->_key)
60 {
61 free(self->_key);
62 self->_key= NULL;
63 }
64
65 if (hashkit_is_allocated(self))
66 {
67 free(self);
68 }
69 }
70
71 hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *source)
72 {
73 if (source == NULL)
74 {
75 return hashkit_create(destination);
76 }
77
78 /* new_clone will be a pointer to destination */
79 destination= _hashkit_create(destination);
80
81 // Should only happen on allocation failure.
82 if (destination == NULL)
83 {
84 return NULL;
85 }
86
87 destination->base_hash= source->base_hash;
88 destination->distribution_hash= source->distribution_hash;
89 destination->flags= source->flags;
90 destination->_key= aes_clone_key(static_cast<aes_key_t*>(source->_key));
91
92 return destination;
93 }
94
95 bool hashkit_compare(const hashkit_st *first, const hashkit_st *second)
96 {
97 if (not first or not second)
98 return false;
99
100 if (first->base_hash.function == second->base_hash.function and
101 first->base_hash.context == second->base_hash.context and
102 first->distribution_hash.function == second->distribution_hash.function and
103 first->distribution_hash.context == second->distribution_hash.context and
104 first->flags.is_base_same_distributed == second->flags.is_base_same_distributed)
105 {
106 return true;
107 }
108
109 return false;
110 }