make range allocation dynamic
authorSami Kerola <kerolasa@iki.fi>
Sun, 10 Apr 2011 17:12:29 +0000 (19:12 +0200)
committerSami Kerola <kerolasa@iki.fi>
Sun, 10 Apr 2011 17:12:29 +0000 (19:12 +0200)
This is a test fix after commit

5cbe8d07fb23db630e76d18022f20368312720ec

to see what can be done. Truth is that not much. I could fix how
ranges are allocated, but the fact there is pointers to shared
networks and network names reallocating the memory spaces is not
really going to work. The only way to truly fix this issue is to
create better data structures. As you can expect that is a major
change, and will take some time to implement.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>

src/defaults.h
src/dhcpd-pools.c
src/dhcpd-pools.h
src/getdata.c
src/other.c
src/sort.c

index c34e4be..1c81bfe 100644 (file)
@@ -13,7 +13,7 @@
 **
 ** You should have received a copy of the GNU General Public License
 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.   
-*/ 
+*/
 
 #ifndef DEFAULTS_H
 # define DEFAULTS_H 1
 static const int MAXLEN = 1024;
 
 /* Total number of characters in all shared network names */
-static const int SHARED_NETWORKS_NAMES = 65536;
+static const unsigned int SHARED_NETWORKS_NAMES = 65536;
 
 /* Maximum number of shared networks */
-static const int SHARED_NETWORKS = 8192;
+static const unsigned int SHARED_NETWORKS = 8192;
 
 /* Maximum number of ranges */
-static const unsigned int RANGES = 65536;
+unsigned int RANGES;
 
-/* Merge sort split size */
-static const int MIN_MERGE_SIZE = 8;
-
-#endif /* DEFAULTS_H */
+#endif                         /* DEFAULTS_H */
index 00b7efc..db8495b 100644 (file)
@@ -213,6 +213,7 @@ int main(int argc, char **argv)
 /* Global allocations, counter resets etc */
 int prepare_memory()
 {
+       RANGES = 64;
        num_ranges = num_shared_networks = 0;
        shared_networks =
            safe_malloc(sizeof(struct shared_network_t) * SHARED_NETWORKS);
index 4bb9d2b..e3835ad 100644 (file)
@@ -148,6 +148,7 @@ void *safe_malloc(const size_t size)
 #endif
 #endif
     ;
+void *safe_realloc(void *ptr, const size_t size);
 char *safe_strdup(const char *str) __attribute__ ((nonnull(1)));
 void print_version(void) __attribute__ ((noreturn));
 void usage(int status) __attribute__ ((noreturn));
index c57d2c7..1334855 100644 (file)
@@ -386,9 +386,14 @@ char *parse_config(int is_include, char *config_file,
                                range_p->backups = 0;
                                range_p->shared_net = shared_p;
                                num_ranges++;
-                               if (RANGES < num_ranges) {
-                                       errx(EXIT_FAILURE,
-                                            "parse_config: increase default.h RANGES and recompile.");
+                               if (RANGES < num_ranges + 1) {
+                                       RANGES *= 2;
+                                       ranges =
+                                           safe_realloc(ranges,
+                                                        sizeof(struct
+                                                               range_t) *
+                                                        RANGES);
+                                       range_p = ranges + num_ranges;
                                }
                                newclause = true;
                                break;
@@ -429,13 +434,14 @@ char *parse_config(int is_include, char *config_file,
                                        errx(EXIT_FAILURE,
                                             "parse_config: increase default.h SHARED_NETWORKS_NAMES and recompile");
                                }
-                               if (SHARED_NETWORKS < num_shared_networks) {
-                                       /* FIXME: make this go
+                               if (SHARED_NETWORKS <
+                                   num_shared_networks + 2) {
+                                       /* FIXME: make this
                                         * away by reallocationg
                                         * more space. */
                                        errx(EXIT_FAILURE,
                                             "parse_config: increase default.h SHARED_NETWORKS and recompile");
-                                }
+                               }
                                argument = 0;
                                braces_shared = braces;
                                break;
index d8f9ac4..21a763a 100644 (file)
@@ -41,7 +41,6 @@ extern char *malloc();
 void *safe_malloc(const size_t size)
 {
        void *ret = malloc(size);
-
        if (ret == NULL) {
                err(EXIT_FAILURE,
                    "safe_malloc: cannot allocate %lu bytes: ", size);
@@ -50,6 +49,17 @@ void *safe_malloc(const size_t size)
        return ret;
 }
 
+/* Simple memory reallocation wrapper */
+void *safe_realloc(void *ptr, const size_t size)
+{
+       void *ret = realloc(ptr, size);
+
+       if (!ret && size)
+               err(EXIT_FAILURE,
+                   "safe_realloc: cannot allocate %zu bytes", size);
+       return ret;
+}
+
 /* Simple strdup wrapper */
 char *safe_strdup(const char *str)
 {
@@ -108,7 +118,7 @@ void print_version(void)
                "This is free software: you are free to change and redistribute it.\n");
        fprintf(stdout,
                "There is NO WARRANTY, to the extent permitted by law.\n");
-        exit(EXIT_SUCCESS);
+       exit(EXIT_SUCCESS);
 }
 
 void usage(int status)
index c222c51..e436f5a 100644 (file)
@@ -26,7 +26,6 @@
 #include <err.h>
 
 #include "dhcpd-pools.h"
-#include "defaults.h"
 
 /* Sort functions for range sorting */
 int intcomp(const void *x, const void *y)
@@ -167,6 +166,8 @@ void mergesort_ranges(struct range_t *orig, int size, struct range_t *temp)
 {
        int left, right, i;
        struct range_t hold;
+       /* Merge sort split size */
+       static const int MIN_MERGE_SIZE = 8;
 
        if (size < MIN_MERGE_SIZE) {
                for (left = 0; left < size; left++) {