mirror of
git://git.code.sf.net/p/dhcpd-pools/code
synced 2025-12-16 15:57:00 +00:00
getdata: new xstrstr function for performance
The strstr is changed to xstrstr, which short cuts to points in string where first differences are expected to found. This made the tool tiny bit quicker. Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
8c6a3d9a4a
commit
b1cd84e804
3 changed files with 32 additions and 5 deletions
|
|
@ -162,6 +162,7 @@ void *safe_malloc(const size_t size)
|
|||
;
|
||||
void *safe_realloc(void *ptr, const size_t size);
|
||||
char *safe_strdup(const char *str) __attribute__ ((nonnull(1)));
|
||||
int xstrstr(char *a, char *b, int len);
|
||||
void print_version(void) __attribute__ ((noreturn));
|
||||
void usage(int status) __attribute__ ((noreturn));
|
||||
/* qsort required functions... */
|
||||
|
|
|
|||
|
|
@ -134,14 +134,14 @@ int parse_leases(void)
|
|||
config.dhcpdlease_file);
|
||||
}
|
||||
/* It's a lease, save IP */
|
||||
if (strstr(line, "lease") == line) {
|
||||
if (xstrstr(line, "lease", 5)) {
|
||||
strncpy(ipstring, line, MAXLEN);
|
||||
nth_field(2, ipstring, ipstring);
|
||||
inet_aton(ipstring, &inp);
|
||||
sw_active_lease = 0;
|
||||
}
|
||||
/* Copy IP to correct array */
|
||||
else if (strstr(line, "binding state active")) {
|
||||
else if (xstrstr(line, " binding state active", 22)) {
|
||||
leases[num_leases] = htonl(inp.s_addr);
|
||||
num_leases++;
|
||||
if (leasesmallocsize < num_leases) {
|
||||
|
|
@ -151,7 +151,7 @@ int parse_leases(void)
|
|||
leasesmallocsize /= sizeof(uint32_t);
|
||||
}
|
||||
sw_active_lease = 1;
|
||||
} else if (strstr(line, " binding state free")) {
|
||||
} else if (xstrstr(line, " binding state free", 20)) {
|
||||
touches[num_touches] = htonl(inp.s_addr);
|
||||
num_touches++;
|
||||
if (touchesmallocsize < num_touches) {
|
||||
|
|
@ -161,7 +161,7 @@ int parse_leases(void)
|
|||
safe_realloc(touches, touchesmallocsize);
|
||||
touchesmallocsize /= sizeof(uint32_t);
|
||||
}
|
||||
} else if (strstr(line, " binding state backup")) {
|
||||
} else if (xstrstr(line, " binding state backup", 22)) {
|
||||
if (num_backups == 0) {
|
||||
backups =
|
||||
safe_malloc(sizeof(uint32_t) *
|
||||
|
|
@ -180,7 +180,7 @@ int parse_leases(void)
|
|||
|
||||
if ((macaddr != NULL)
|
||||
&& (sw_active_lease == 1)
|
||||
&& (strstr(line, "hardware ethernet"))) {
|
||||
&& (xstrstr(line, " hardware ethernet", 19))) {
|
||||
nth_field(3, macstring, line);
|
||||
macstring[17] = '\0';
|
||||
macaddr_p->ethernet = safe_strdup(macstring);
|
||||
|
|
|
|||
26
src/other.c
26
src/other.c
|
|
@ -78,6 +78,32 @@ void *safe_realloc(void *ptr, const size_t size)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
|
||||
__attribute__ ((hot))
|
||||
#endif
|
||||
xstrstr(char *a, char *b, int len)
|
||||
{
|
||||
int i;
|
||||
/* two spaces are very common in lease file, after them
|
||||
* nearly everything differs */
|
||||
if (a[2] != b[2]) {
|
||||
return false;
|
||||
}
|
||||
/* " binding state " == 16 chars, this will skip right
|
||||
* to first difering line. */
|
||||
if (17 < len && a[17] != b[17]) {
|
||||
return false;
|
||||
}
|
||||
/* looking good, double check the whole thing... */
|
||||
for (i = 0; a[i] != '\0' && b[i] != '\0'; i++) {
|
||||
if (a[i] != b[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Simple strdup wrapper */
|
||||
char *safe_strdup(const char *str)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue