use realloc instead of malloc in object_add_directive

Wed, 09 Nov 2022 10:37:52 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 09 Nov 2022 10:37:52 +0100
changeset 421
437562f5681d
parent 420
0b42a35d6add
child 422
76f2f5d532d0

use realloc instead of malloc in object_add_directive

src/server/util/object.c file | annotate | diff | comparison | revisions
src/server/util/object.h file | annotate | diff | comparison | revisions
--- a/src/server/util/object.c	Tue Nov 08 22:35:18 2022 +0100
+++ b/src/server/util/object.c	Wed Nov 09 10:37:52 2022 +0100
@@ -56,25 +56,22 @@
 }
 
 
-void object_add_directive(httpd_object *obj, directive *dir, int dt) {
+int object_add_directive(httpd_object *obj, directive *dir, int dt) {
     dtable *l = object_get_dtable(obj, dt);
     // allocate space for the new directive
-
-    //l->dirs = realloc(l->dirs, (l->ndir+1)*sizeof(void*));
-    // TODO: use realloc
-    
-    directive **drs = pool_malloc(obj->pool, (l->ndir+1)*sizeof(void*));
-    for(int i=0;i<l->ndir;i++) {
-        drs[i] = l->dirs[i];
+    if(l->ndir >= l->alloc) {
+        l->alloc += 8;
+        directive **drs = pool_realloc(obj->pool, l->dirs, (l->alloc)*sizeof(void*));
+        if(!drs) {
+            l->alloc -= 8;
+            return -1;
+        }
+        l->dirs = drs;
     }
-    if(l->dirs != NULL) {
-        pool_free(obj->pool, l->dirs);
-    }
-    l->dirs = drs;
-
+    
     // add directive
-    l->dirs[l->ndir] = dir;
-    l->ndir++;
+    l->dirs[l->ndir++] = dir;
+    return 0;
 }
 
 
--- a/src/server/util/object.h	Tue Nov 08 22:35:18 2022 +0100
+++ b/src/server/util/object.h	Wed Nov 09 10:37:52 2022 +0100
@@ -69,6 +69,7 @@
 struct dtable {
     directive **dirs;
     int       ndir;
+    int       alloc;
 };
 
 struct httpd_object {
@@ -132,7 +133,7 @@
 /*
  * adds a directive to the object with the type dt (enum RequestPhase)
  */
-void object_add_directive(httpd_object *obj, directive *dir, int dt);
+int object_add_directive(httpd_object *obj, directive *dir, int dt);
 
 /*
  * get a list of all directives with a specific type

mercurial