revert optimization that does not harmonize with realloc() cpp-ports

Wed, 08 Jun 2022 19:26:43 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 08 Jun 2022 19:26:43 +0200
branch
cpp-ports
changeset 365
2ea1ed291e9f
parent 364
e56f9c6f6b8f
child 386
b91f8efadb63

revert optimization that does not harmonize with realloc()

src/server/util/pblock.c file | annotate | diff | comparison | revisions
--- a/src/server/util/pblock.c	Wed Jun 08 19:14:57 2022 +0200
+++ b/src/server/util/pblock.c	Wed Jun 08 19:26:43 2022 +0200
@@ -85,7 +85,7 @@
 #define PB_KEY_LIST_BUCKET_COUNT 0x7f // has to be (2^n - 1)
 
 struct pb_key_list_bucket {
-    pb_key *elements;
+    pb_key **elements;
     unsigned size;
 };
 
@@ -93,30 +93,32 @@
     struct pb_key_list_bucket buckets[PB_KEY_LIST_BUCKET_COUNT + 1];
 };
 
-static const pb_key *_pb_key_list_insert(struct pb_key_list *hashList, unsigned int hashval, pb_key* element) {
+static void _pb_key_list_insert(struct pb_key_list *hashList, unsigned int hashval, pb_key* element) {
     struct pb_key_list_bucket *bucket = &hashList->buckets[hashval & PB_KEY_LIST_BUCKET_COUNT];
     size_t idx = bucket->size;
     bucket->size++;
-    bucket->elements = realloc(bucket->elements, sizeof(pb_key) * bucket->size);
+    bucket->elements = realloc(bucket->elements, sizeof(pb_key*) * bucket->size);
     if (bucket->elements == NULL) abort();
-    bucket->elements[idx] = *element;
-    return &bucket->elements[idx];
+    bucket->elements[idx] = element;
 }
 
 static struct pb_key_list _pbKeys;
 
 static const pb_key *_create_key(const char *name)
 {
-    /* Create a the new pb_key */
-    pb_key key;
-    key.name = STRDUP(name);
-    key.namelen = strlen(name);
-    key.hashval = PListHash(name);
-    key.sizendx = 0;
-    key.hashndx = key.hashval % PLSIZENDX(0);
+    /* Create a new pb_key */
+    pb_key *key = malloc(sizeof(pb_key));
+    if (key == NULL) abort();
+    key->name = STRDUP(name);
+    key->namelen = strlen(name);
+    key->hashval = PListHash(name);
+    key->sizendx = 0;
+    key->hashndx = key->hashval % PLSIZENDX(0);
 
     /* Group pb_keys by hashval for later retrieval */
-    return _pb_key_list_insert(&_pbKeys, key.hashval, &key);
+    _pb_key_list_insert(&_pbKeys, key->hashval, key);
+
+    return key;
 }
 
 const pb_key *pb_key_accept;
@@ -509,8 +511,14 @@
 
 NSAPI_PUBLIC void pblock_free_default_keys(void) {
     for (unsigned i = 0 ; i < PB_KEY_LIST_BUCKET_COUNT ; i++) {
-        // free(NULL) is defined, so don't worry here
-        free(_pbKeys.buckets[i].elements);
+        unsigned count = _pbKeys.buckets[i].size;
+        if (count > 0) {
+            pb_key **keys = _pbKeys.buckets[i].elements;
+            for (unsigned j = 0 ; j < count ; j++) {
+                free(keys[j]);
+            }
+            free(keys);
+        }
     }
 }
 
@@ -521,7 +529,7 @@
     /* Check to see if name corresponds to a pb_key */
     struct pb_key_list_bucket *bucket = &_pbKeys.buckets[hashval & PB_KEY_LIST_BUCKET_COUNT];
     for (unsigned i = 0 ; i < bucket->size ; i++) {
-        pb_key *key = &bucket->elements[i];
+        pb_key *key = bucket->elements[i];
         if (key->hashval == hashval && !strcmp(key->name, name)) {
             return key;
         }

mercurial