Merging bzr://gaz.tangent.org/libmemcached/build/ to Build branch
[m6w6/libmemcached] / libhashkit / hashkit.cc
index 201a6dff114ea5cd9153e3bff85cc6f06fa47325..e397871f491eea54fe99c592bf81f1e1a162e5f3 100644 (file)
@@ -8,21 +8,27 @@
 
 #include <libhashkit/common.h>
 
-static inline bool _hashkit_init(hashkit_st *self)
+static inline void _hashkit_init(hashkit_st *self)
 {
   self->base_hash.function= hashkit_one_at_a_time;
   self->base_hash.context= NULL;
-  self->distribution_hash.function= self->base_hash.function;
-  self->flags.is_base_same_distributed= false;
 
-  return true;
+  self->distribution_hash.function= hashkit_one_at_a_time;
+  self->distribution_hash.context= NULL;
+
+  self->flags.is_base_same_distributed= true;
+  self->_key= NULL;
 }
 
 static inline hashkit_st *_hashkit_create(hashkit_st *self)
 {
-  if (self == NULL)
+  if (self)
   {
-    self= (hashkit_st *)malloc(sizeof(hashkit_st));
+    self->options.is_allocated= false;
+  }
+  else
+  {
+    self= (hashkit_st*)calloc(1, sizeof(hashkit_st));
     if (self == NULL)
     {
       return NULL;
@@ -30,10 +36,6 @@ static inline hashkit_st *_hashkit_create(hashkit_st *self)
 
     self->options.is_allocated= true;
   }
-  else
-  {
-    self->options.is_allocated= false;
-  }
 
   return self;
 }
@@ -41,20 +43,25 @@ static inline hashkit_st *_hashkit_create(hashkit_st *self)
 hashkit_st *hashkit_create(hashkit_st *self)
 {
   self= _hashkit_create(self);
-  if (! self)
-    return self;
-
-  if (! _hashkit_init(self))
+  if (self == NULL)
   {
-    hashkit_free(self);
+    return NULL;
   }
 
+  _hashkit_init(self);
+
   return self;
 }
 
 
 void hashkit_free(hashkit_st *self)
 {
+  if (self and self->_key)
+  {
+    free(self->_key);
+    self->_key= NULL;
+  }
+
   if (hashkit_is_allocated(self))
   {
     free(self);
@@ -80,16 +87,20 @@ hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *source)
   destination->base_hash= source->base_hash;
   destination->distribution_hash= source->distribution_hash;
   destination->flags= source->flags;
+  destination->_key= aes_clone_key(static_cast<aes_key_t*>(source->_key));
 
   return destination;
 }
 
 bool hashkit_compare(const hashkit_st *first, const hashkit_st *second)
 {
-  if (first->base_hash.function == second->base_hash.function &&
-      first->base_hash.context == second->base_hash.context &&
-      first->distribution_hash.function == second->distribution_hash.function &&
-      first->distribution_hash.context == second->distribution_hash.context &&
+  if (not first or not second)
+    return false;
+
+  if (first->base_hash.function == second->base_hash.function and
+      first->base_hash.context == second->base_hash.context and
+      first->distribution_hash.function == second->distribution_hash.function and
+      first->distribution_hash.context == second->distribution_hash.context and
       first->flags.is_base_same_distributed == second->flags.is_base_same_distributed)
   {
     return true;