Forráskód Böngészése

handle null hash on HASH_OVERHEAD

Troy D. Hanson 10 éve
szülő
commit
08121e0a50
5 módosított fájl, 37 hozzáadás és 2 törlés
  1. 2 1
      src/uthash.h
  2. 1 1
      tests/Makefile
  3. 1 0
      tests/README
  4. 2 0
      tests/test85.ans
  5. 31 0
      tests/test85.c

+ 2 - 1
src/uthash.h

@@ -869,10 +869,11 @@ do {
 } while(0)
 } while(0)
 
 
 #define HASH_OVERHEAD(hh,head)                                                   \
 #define HASH_OVERHEAD(hh,head)                                                   \
+ ((head) ? (                                                                     \
  (size_t)((((head)->hh.tbl->num_items   * sizeof(UT_hash_handle))   +            \
  (size_t)((((head)->hh.tbl->num_items   * sizeof(UT_hash_handle))   +            \
            ((head)->hh.tbl->num_buckets * sizeof(UT_hash_bucket))   +            \
            ((head)->hh.tbl->num_buckets * sizeof(UT_hash_bucket))   +            \
             (sizeof(UT_hash_table))                                 +            \
             (sizeof(UT_hash_table))                                 +            \
-            (HASH_BLOOM_BYTELEN)))
+            (HASH_BLOOM_BYTELEN)))) : 0)
 
 
 #ifdef NO_DECLTYPE
 #ifdef NO_DECLTYPE
 #define HASH_ITER(hh,head,el,tmp)                                                \
 #define HASH_ITER(hh,head,el,tmp)                                                \

+ 1 - 1
tests/Makefile

@@ -13,7 +13,7 @@ PROGS = test1 test2 test3 test4 test5 test6 test7 test8 test9   \
         test58 test59 test60 test61 test62 test63 test64 test65 \
         test58 test59 test60 test61 test62 test63 test64 test65 \
         test66 test67 test68 test69 test70 test71 test72 test73 \
         test66 test67 test68 test69 test70 test71 test72 test73 \
         test74 test75 test76 test77 test78 test79 test80 test81 \
         test74 test75 test76 test77 test78 test79 test80 test81 \
-        test82 test83 test84
+        test82 test83 test84 test85
 CFLAGS = -I$(HASHDIR) 
 CFLAGS = -I$(HASHDIR) 
 #CFLAGS += -DHASH_BLOOM=16
 #CFLAGS += -DHASH_BLOOM=16
 #CFLAGS += -O2
 #CFLAGS += -O2

+ 1 - 0
tests/README

@@ -86,6 +86,7 @@ test81: test utarray_insert past end of array
 test82: test utarray_inserta past end of array
 test82: test utarray_inserta past end of array
 test83: test HASH_REPLACE_STR with char[] key
 test83: test HASH_REPLACE_STR with char[] key
 test84: test HASH_REPLACE_STR with char* key
 test84: test HASH_REPLACE_STR with char* key
+test85: test HASH_OVERHEAD on null and non null hash
 
 
 
 
 Other Make targets
 Other Make targets

+ 2 - 0
tests/test85.ans

@@ -0,0 +1,2 @@
+overhead non-zero
+overhead zero

+ 31 - 0
tests/test85.c

@@ -0,0 +1,31 @@
+#include "uthash.h"
+#include <stdlib.h>   /* malloc */
+#include <stdio.h>    /* printf */
+
+typedef struct example_user_t {
+    int id;
+    int cookie;
+    UT_hash_handle hh;
+} example_user_t;
+
+int main(int argc,char *argv[]) {
+    int i;
+    example_user_t *user, *users=NULL;
+
+    /* create elements */
+    for(i=0;i<10;i++) {
+        if ( (user = (example_user_t*)malloc(sizeof(example_user_t))) == NULL) exit(-1);
+        user->id = i;
+        user->cookie = i*i;
+        HASH_ADD_INT(users,id,user);
+    }
+
+   size_t s = HASH_OVERHEAD(hh,users);
+   printf("overhead %s\n", (s==0)?"zero":"non-zero");
+   HASH_CLEAR(hh,users);  
+   // should free those elements 
+   // but this test is not concerned with that
+   s = HASH_OVERHEAD(hh,users);
+   printf("overhead %s\n", (s==0)?"zero":"non-zero");
+   return 0;
+}