mirror of
https://gitlab.alpinelinux.org/alpine/aports.git
synced 2026-01-04 08:12:06 +01:00
113 lines
3.7 KiB
Diff
113 lines
3.7 KiB
Diff
From f4e82c231bd5089db5ee8b8082489304264bc805 Mon Sep 17 00:00:00 2001
|
|
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
|
|
Date: Thu, 14 Nov 2013 14:35:18 +0200
|
|
Subject: [PATCH 1/2] avoid unneeded memory allocations
|
|
|
|
---
|
|
bin/mmdblookup.c | 18 ++++++------------
|
|
src/maxminddb.c | 14 +++++---------
|
|
2 files changed, 11 insertions(+), 21 deletions(-)
|
|
|
|
diff --git a/bin/mmdblookup.c b/bin/mmdblookup.c
|
|
index ac72a93..4b7733d 100644
|
|
--- a/bin/mmdblookup.c
|
|
+++ b/bin/mmdblookup.c
|
|
@@ -24,7 +24,7 @@ LOCAL int lookup_and_print(MMDB_s *mmdb, const char *ip_address,
|
|
int lookup_path_length);
|
|
LOCAL int benchmark(MMDB_s *mmdb, int iterations);
|
|
LOCAL MMDB_lookup_result_s lookup_or_die(MMDB_s *mmdb, const char *ipstr);
|
|
-LOCAL char *random_ipv4();
|
|
+LOCAL void random_ipv4(char *ip);
|
|
/* --prototypes end - don't remove this comment-- */
|
|
/* *INDENT-ON* */
|
|
|
|
@@ -304,13 +304,14 @@ LOCAL int lookup_and_print(MMDB_s *mmdb, const char *ip_address,
|
|
|
|
LOCAL int benchmark(MMDB_s *mmdb, int iterations)
|
|
{
|
|
+ char ip_address[16];
|
|
int exit_code = 0;
|
|
- srand( time(NULL) );
|
|
|
|
+ srand( time(NULL) );
|
|
clock_t time = clock();
|
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
- char *ip_address = random_ipv4();
|
|
+ random_ipv4(ip_address);
|
|
|
|
MMDB_lookup_result_s result = lookup_or_die(mmdb, ip_address);
|
|
MMDB_entry_data_list_s *entry_data_list = NULL;
|
|
@@ -324,13 +325,11 @@ LOCAL int benchmark(MMDB_s *mmdb, int iterations)
|
|
fprintf(stderr, "Got an error looking up the entry data - %s\n",
|
|
MMDB_strerror(status));
|
|
exit_code = 5;
|
|
- free(ip_address);
|
|
MMDB_free_entry_data_list(entry_data_list);
|
|
goto end;
|
|
}
|
|
}
|
|
|
|
- free(ip_address);
|
|
MMDB_free_entry_data_list(entry_data_list);
|
|
}
|
|
|
|
@@ -369,13 +368,8 @@ LOCAL MMDB_lookup_result_s lookup_or_die(MMDB_s *mmdb, const char *ipstr)
|
|
return result;
|
|
}
|
|
|
|
-LOCAL char *random_ipv4()
|
|
+LOCAL void random_ipv4(char *ip)
|
|
{
|
|
- int ip_int = rand();
|
|
- uint8_t *bytes = (uint8_t *)&ip_int;
|
|
-
|
|
- char *ip = malloc(16);
|
|
snprintf(ip, 16, "%u.%u.%u.%u",
|
|
- *bytes, *(bytes + 1), *(bytes + 2), *(bytes + 3));
|
|
- return ip;
|
|
+ rand()&0xff, rand()&0xff, rand()&0xff, rand()&0xff);
|
|
}
|
|
diff --git a/src/maxminddb.c b/src/maxminddb.c
|
|
index f3a7dfd..3bf7154 100644
|
|
--- a/src/maxminddb.c
|
|
+++ b/src/maxminddb.c
|
|
@@ -625,20 +625,18 @@ MMDB_lookup_result_s MMDB_lookup_sockaddr(MMDB_s *mmdb,
|
|
}
|
|
};
|
|
|
|
- uint8_t *address;
|
|
+ uint8_t mapped_address[16], *address;
|
|
if (mmdb->metadata.ip_version == 4) {
|
|
if (sockaddr->sa_family == AF_INET6) {
|
|
return result;
|
|
}
|
|
- address = malloc(4);
|
|
- memcpy(address, &((struct sockaddr_in *)sockaddr)->sin_addr.s_addr, 4);
|
|
+ address = (uint8_t*) &((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
|
|
} else {
|
|
- // We need calloc() here for the IPv4 case - the first 12 bytes must be 0
|
|
- address = calloc(1, 16);
|
|
if (sockaddr->sa_family == AF_INET6) {
|
|
- memcpy(address,
|
|
- ((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr, 16);
|
|
+ address = (uint8_t*) &((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
|
|
} else {
|
|
+ address = mapped_address;
|
|
+ memset(address, 0, 12);
|
|
memcpy(address + 12,
|
|
&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr, 4);
|
|
}
|
|
@@ -648,8 +646,6 @@ MMDB_lookup_result_s MMDB_lookup_sockaddr(MMDB_s *mmdb,
|
|
find_address_in_search_tree(mmdb, address, sockaddr->sa_family,
|
|
&result);
|
|
|
|
- free(address);
|
|
-
|
|
return result;
|
|
}
|
|
|
|
--
|
|
1.8.4.1
|
|
|