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