Merge in conversion to C++.
[awesomized/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 bool _hashkit_init(hashkit_st *self)
12 {
13 self->base_hash.function= hashkit_one_at_a_time;
14 self->base_hash.context= NULL;
15 self->distribution_hash.function= self->base_hash.function;
16 self->flags.is_base_same_distributed= false;
17
18 return true;
19 }
20
21 static inline hashkit_st *_hashkit_create(hashkit_st *self)
22 {
23 if (self == NULL)
24 {
25 self= (hashkit_st *)malloc(sizeof(hashkit_st));
26 if (self == NULL)
27 {
28 return NULL;
29 }
30
31 self->options.is_allocated= true;
32 }
33 else
34 {
35 self->options.is_allocated= false;
36 }
37
38 return self;
39 }
40
41 hashkit_st *hashkit_create(hashkit_st *self)
42 {
43 self= _hashkit_create(self);
44 if (! self)
45 return self;
46
47 if (! _hashkit_init(self))
48 {
49 hashkit_free(self);
50 }
51
52 return self;
53 }
54
55
56 void hashkit_free(hashkit_st *self)
57 {
58 if (hashkit_is_allocated(self))
59 {
60 free(self);
61 }
62 }
63
64 hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *source)
65 {
66 if (source == NULL)
67 {
68 return hashkit_create(destination);
69 }
70
71 /* new_clone will be a pointer to destination */
72 destination= _hashkit_create(destination);
73
74 // Should only happen on allocation failure.
75 if (destination == NULL)
76 {
77 return NULL;
78 }
79
80 destination->base_hash= source->base_hash;
81 destination->distribution_hash= source->distribution_hash;
82 destination->flags= source->flags;
83
84 return destination;
85 }
86
87 bool hashkit_compare(const hashkit_st *first, const hashkit_st *second)
88 {
89 if (first->base_hash.function == second->base_hash.function &&
90 first->base_hash.context == second->base_hash.context &&
91 first->distribution_hash.function == second->distribution_hash.function &&
92 first->distribution_hash.context == second->distribution_hash.context &&
93 first->flags.is_base_same_distributed == second->flags.is_base_same_distributed)
94 {
95 return true;
96 }
97
98 return false;
99 }