]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/hash.c
* return a const char instead
[citadel.git] / libcitadel / lib / hash.c
index f945fd5cb05e0c1ce91cc2b8237da19027ad7365..20e0f1222690510df5441ab5767ff96a0aee5e2c 100644 (file)
@@ -64,6 +64,9 @@ int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry
        void *Next;
        const char* KeyStr;
 
+       if (Hash == NULL)
+               return 0;
+
        for (i=0; i < Hash->nMembersUsed; i++) {
                if (i==0) {
                        Previous = NULL;
@@ -104,6 +107,10 @@ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Secon
        const char *bla = "";
        long key;
        long i;
+
+       if (Hash == NULL)
+               return 0;
+
        if (Hash->MyKeys != NULL)
                free (Hash->MyKeys);
 
@@ -125,8 +132,12 @@ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Secon
                        foo = Hash->LookupTable[i]->HashKey;
                        if (First != NULL)
                                bar = First(Hash->Members[Hash->LookupTable[i]->Position]->Data);
+                       else 
+                               bar = "";
                        if (Second != NULL)
                                bla = Second(Hash->Members[Hash->LookupTable[i]->Position]->Data);
+                       else
+                               bla = "";
                }
 #ifdef DEBUG
                printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
@@ -163,6 +174,13 @@ HashList *NewHash(int Uniq, HashFunc F)
        return NewList;
 }
 
+int GetCount(HashList *Hash)
+{
+       if(Hash==NULL) return 0;
+       return Hash->nMembersUsed;
+}
+
+
 /**
  * \brief private destructor for one hash element.
  * \param Data an element to free using the user provided destructor, or just plain free
@@ -224,6 +242,9 @@ static void IncreaseHashSize(HashList *Hash)
        Payload **NewPayloadArea;
        HashKey **NewTable;
        
+       if (Hash == NULL)
+               return ;
+
        /** double our payload area */
        NewPayloadArea = (Payload**) malloc(sizeof(Payload*) * Hash->MemberSize * 2);
        memset(&NewPayloadArea[Hash->MemberSize], 0, sizeof(Payload*) * Hash->MemberSize);
@@ -263,6 +284,9 @@ static void InsertHashItem(HashList *Hash,
        Payload *NewPayloadItem;
        HashKey *NewHashKey;
 
+       if (Hash == NULL)
+               return;
+
        if (Hash->nMembersUsed >= Hash->MemberSize)
                IncreaseHashSize (Hash);
 
@@ -282,16 +306,14 @@ static void InsertHashItem(HashList *Hash,
        /** but if we should be sorted into a specific place... */
        if ((Hash->nMembersUsed != 0) && 
            (HashPos != Hash->nMembersUsed) ) {
-               long InsertAt;
                long ItemsAfter;
 
                ItemsAfter = Hash->nMembersUsed - HashPos;
-               InsertAt = HashPos;
                /** make space were we can fill us in */
                if (ItemsAfter > 0)
                {
-                       memmove(&Hash->LookupTable[InsertAt + 1],
-                               &Hash->LookupTable[InsertAt],
+                       memmove(&Hash->LookupTable[HashPos + 1],
+                               &Hash->LookupTable[HashPos],
                                ItemsAfter * sizeof(HashKey*));
                } 
        }
@@ -312,6 +334,9 @@ static long FindInTaintedHash(HashList *Hash, long HashBinKey)
 {
        long SearchPos;
 
+       if (Hash == NULL)
+               return 0;
+
        for (SearchPos = 0; SearchPos < Hash->nMembersUsed; SearchPos ++) {
                if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
                        return SearchPos;
@@ -331,6 +356,9 @@ static long FindInHash(HashList *Hash, long HashBinKey)
        long SearchPos;
        long StepWidth;
 
+       if (Hash == NULL)
+               return 0;
+
        if (Hash->tainted)
                return FindInTaintedHash(Hash, HashBinKey);
 
@@ -378,6 +406,9 @@ static long FindInHash(HashList *Hash, long HashBinKey)
  */
 inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
 {
+       if (Hash == NULL)
+               return 0;
+
        if (Hash->Algorithm == NULL)
                return hashlittle(HKey, HKLen, 9283457);
        else
@@ -398,6 +429,9 @@ void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDat
        long HashBinKey;
        long HashAt;
 
+       if (Hash == NULL)
+               return;
+
        /** first, find out were we could fit in... */
        HashBinKey = CalcHashKey(Hash, HKey, HKLen);
        HashAt = FindInHash(Hash, HashBinKey);
@@ -443,6 +477,9 @@ int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data)
        long HashBinKey;
        long HashAt;
 
+       if (Hash == NULL)
+               return 0;
+
        if (HKLen <= 0) {
                *Data = NULL;
                return  0;
@@ -480,6 +517,8 @@ int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload)
 int GetHashKeys(HashList *Hash, char ***List)
 {
        long i;
+       if (Hash == NULL)
+               return 0;
        if (Hash->MyKeys != NULL)
                free (Hash->MyKeys);
 
@@ -510,8 +549,11 @@ HashPos *GetNewHashPos(void)
  */
 void DeleteHashPos(HashPos **DelMe)
 {
-       free(*DelMe);
-       *DelMe = NULL;
+       if (*DelMe != NULL)
+       {
+               free(*DelMe);
+               *DelMe = NULL;
+       }
 }
 
 
@@ -523,11 +565,11 @@ void DeleteHashPos(HashPos **DelMe)
  * \param Data returns the Data found at HashPos
  * \returns whether the item was found or not.
  */
-int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, char **HashKey, void **Data)
+int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
 {
        long PayloadPos;
 
-       if (Hash->nMembersUsed <= At->Position)
+       if ((Hash == NULL) || (Hash->nMembersUsed <= At->Position))
                return 0;
        *HKLen = Hash->LookupTable[At->Position]->HKLen;
        *HashKey = Hash->LookupTable[At->Position]->HashKey;
@@ -648,3 +690,16 @@ void SortByPayload(HashList *Hash, CompareFunc SortBy)
  *      return strcmp (a, b);
  * }
  */
+
+
+/*
+ * Generic function to free a pointer.  This can be used as a callback with the
+ * hash table, even on systems where free() is defined as a macro or has had other
+ * horrible things done to it.
+ */
+void generic_free_handler(void *ptr) {
+       free(ptr);
+}
+
+
+