From 775157b1a5ae108e1828a6ada231fc4d4d56e01f Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sat, 22 Jan 2011 18:24:45 +0100 Subject: [PATCH] Optimization on basis of gcov Code blocks to more common first order. Also couple checks got to be refactored next to section where update happens, that will reduce unnecessary checking. Signed-off-by: Sami Kerola --- src/getdata.c | 91 +++++++++++++++++++++++++++------------------------------ 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/src/getdata.c b/src/getdata.c index 430468c..d887f0c 100644 --- a/src/getdata.c +++ b/src/getdata.c @@ -48,6 +48,7 @@ extern char *malloc(); #define _XOPEN_SOURCE 600 #include #include +#include #include #include "dhcpd-pools.h" @@ -120,10 +121,16 @@ int parse_leases(void) else if (strstr(line, "binding state active")) { leases[num_leases] = htonl(inp.s_addr); num_leases++; + if (leasesmallocsize < num_leases) { + errx(EXIT_FAILURE, "parse_leases: running out of lease memory, report a bug"); + } sw_active_lease = 1; } else if (strstr(line, " binding state free")) { touches[num_touches] = htonl(inp.s_addr); num_touches++; + if (touchesmallocsize < num_touches) { + errx(EXIT_FAILURE, "parse_leases: running out of touch memory, report a bug"); + } } else if (strstr(line, " binding state backup")) { if (num_backups == 0) { backups = @@ -132,6 +139,9 @@ int parse_leases(void) } backups[num_backups] = htonl(inp.s_addr); num_backups++; + if (backupsmallocsize < num_backups) { + errx(EXIT_FAILURE, "parse_leases: running out of backup IPs memory, report a bug"); + } } /* FIXME: move to output.c and use FILE *outfile */ @@ -145,22 +155,7 @@ int parse_leases(void) ("\n\t%s\n\t%s\n\n", ipstring, macstring); } - - if ((leasesmallocsize < num_leases) || - (touchesmallocsize < num_touches) || - (backupsmallocsize < num_backups)) { - printf("WARNING: running out of memory\n"); - printf("\tlease/touch/backup = %lu/%lu/%lu\n", - leasesmallocsize, touchesmallocsize, - backupsmallocsize); - printf("\tlease/touch/backup = %lu/%lu/%lu\n", - num_leases, num_touches, num_backups); - printf - ("Code should realloc() and init new memory, but no time to write that now!\n"); - exit(EXIT_FAILURE); - } } - return 0; } @@ -198,10 +193,10 @@ int nth_field(int n, char *dest, const char *src) /* dhcpd.conf interesting words */ int is_interesting_config_clause(char *s) { - if (strstr(s, "shared-network")) { - return 1; - } else if (strstr(s, "range")) { + if (strstr(s, "range")) { return 3; + } else if (strstr(s, "shared-network")) { + return 1; } else if (strstr(s, "include")) { return 4; } else { @@ -372,9 +367,33 @@ char *parse_config(int is_include, char *config_file, i = 0; switch (argument) { - case 0: - /* printf ("nothing interesting: %s\n", word); */ + case 2: + /* printf ("range 2nd ip: %s\n", word); */ + range_p = ranges + num_ranges; + inet_aton(word, &inp); argument = 0; + range_p->last_ip = htonl(inp.s_addr) + 1; + range_p->count = 0; + range_p->touched = 0; + range_p->backups = 0; + range_p->shared_net = shared_p; + num_ranges++; + if (RANGES < num_ranges) { + eprintf + ("parse_config: Range space full! Increase RANGES and recompile."); + exit(EXIT_FAILURE); + } + newclause = true; + break; + case 3: + /* printf ("range 1nd ip: %s\n", word); */ + range_p = ranges + num_ranges; + if (!(inet_aton(word, &inp))) { + /* word was not ip, try again */ + break; + } + range_p->first_ip = htonl(inp.s_addr) - 1; + argument = 2; break; case 1: /* printf ("shared-network named: %s\n", word); */ @@ -403,34 +422,6 @@ char *parse_config(int is_include, char *config_file, argument = 0; braces_shared = braces; break; - case 2: - /* printf ("range 2nd ip: %s\n", word); */ - range_p = ranges + num_ranges; - inet_aton(word, &inp); - argument = 0; - range_p->last_ip = htonl(inp.s_addr) + 1; - range_p->count = 0; - range_p->touched = 0; - range_p->backups = 0; - range_p->shared_net = shared_p; - num_ranges++; - if (RANGES < num_ranges) { - eprintf - ("parse_config: Range space full! Increase RANGES and recompile."); - exit(EXIT_FAILURE); - } - newclause = true; - break; - case 3: - /* printf ("range 1nd ip: %s\n", word); */ - range_p = ranges + num_ranges; - if (!(inet_aton(word, &inp))) { - /* word was not ip, try again */ - break; - } - range_p->first_ip = htonl(inp.s_addr) - 1; - argument = 2; - break; case 4: /* printf ("include file: %s\n", word); */ argument = 0; @@ -441,6 +432,10 @@ char *parse_config(int is_include, char *config_file, shared_p); newclause = true; break; + case 0: + /* printf ("nothing interesting: %s\n", word); */ + argument = 0; + break; default: eprintf ("parse_config: This cannot happen, report a bug!"); -- 1.7.10.4