From 931fb9e67b8647e1ce4467e22ebe3496e26a3bf2 Mon Sep 17 00:00:00 2001 From: lion Date: Tue, 23 Sep 2025 18:15:46 +0800 Subject: [PATCH] use the local stack region_buffer instead of heap allocation --- binding/c/xdb_api.h | 3 ++- binding/c/xdb_searcher.c | 27 ++++++--------------------- binding/c/xdb_util.c | 4 ++-- 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/binding/c/xdb_api.h b/binding/c/xdb_api.h index 2db43cf..6d69995 100644 --- a/binding/c/xdb_api.h +++ b/binding/c/xdb_api.h @@ -41,7 +41,8 @@ #define xdb_vector_index_rows 256 #define xdb_vector_index_cols 256 #define xdb_vector_index_size 8 -#define xdb_segment_index_size 14 +#define xdb_v4_index_size 14 // 4 + 4 + 2 + 4 +#define xdb_v6_index_size 38 // 16 + 16 + 2 + 4 // --- ip version info #define xdb_ipv4_id 4 diff --git a/binding/c/xdb_searcher.c b/binding/c/xdb_searcher.c index 8eba674..b2b5c04 100644 --- a/binding/c/xdb_searcher.c +++ b/binding/c/xdb_searcher.c @@ -132,7 +132,7 @@ XDB_PUBLIC(int) xdb_search(xdb_searcher_t *xdb, const bytes_ip_t *ip_bytes, int register int seg_index_size, l, h, m, p; unsigned int s_ptr, e_ptr, data_ptr, data_len; char vector_buffer[xdb_vector_index_size]; - char *segment_buffer = NULL; + char segment_buffer[xdb_v6_index_size]; // ip version check if (ip_len != xdb->version->bytes) { @@ -168,12 +168,8 @@ XDB_PUBLIC(int) xdb_search(xdb_searcher_t *xdb, const bytes_ip_t *ip_bytes, int // printf("s_ptr=%u, e_ptr=%u\n", s_ptr, e_ptr); // binary search to get the final region info + // segment_buffer = xdb_malloc(seg_index_size); seg_index_size = xdb->version->segment_index_size; - segment_buffer = xdb_malloc(seg_index_size); - if (segment_buffer == NULL) { - return -2; - } - data_len = 0, data_ptr = 0; l = 0, h = ((int) (e_ptr - s_ptr)) / seg_index_size; while (l <= h) { @@ -183,8 +179,7 @@ XDB_PUBLIC(int) xdb_search(xdb_searcher_t *xdb, const bytes_ip_t *ip_bytes, int // read the segment index item err = read(xdb, p, segment_buffer, seg_index_size); if (err != 0) { - err += 20; - goto defer; + return 20 + err; } // decode the data fields as needed @@ -201,28 +196,18 @@ XDB_PUBLIC(int) xdb_search(xdb_searcher_t *xdb, const bytes_ip_t *ip_bytes, int // printf("data_len=%u, data_ptr=%u\n", data_len, data_ptr); if (data_len == 0) { - err = 100; - goto defer; + return 100; } // buffer alloc checking err = xdb_region_buffer_alloc(region, data_len); if (err != 0) { - err += 100; - goto defer; + return 100 + err; } err = read(xdb, data_ptr, region->value, data_len); if (err != 0) { - err += 30; - goto defer; - } - -defer: - // checn and free the segment buffer - if (segment_buffer != NULL) { - xdb_free(segment_buffer); - segment_buffer = NULL; + return 30 + err; } return err; diff --git a/binding/c/xdb_util.c b/binding/c/xdb_util.c index c2e8ef9..1f21630 100644 --- a/binding/c/xdb_util.c +++ b/binding/c/xdb_util.c @@ -305,10 +305,10 @@ XDB_PRIVATE(int) _ipv4_sub_compare(const bytes_ip_t *ip_bytes, int bytes, const static xdb_version_t _ip_version_list[] = { // 14 = 4 + 4 + 2 + 4 - {xdb_ipv4_id, "IPv4", xdb_ipv4_bytes, 14, _ipv4_sub_compare}, + {xdb_ipv4_id, "IPv4", xdb_ipv4_bytes, xdb_v4_index_size, _ipv4_sub_compare}, // 38 = 16 + 16 + 2 + 4 - {xdb_ipv6_id, "IPv6", xdb_ipv6_bytes, 38, xdb_ip_sub_compare}, + {xdb_ipv6_id, "IPv6", xdb_ipv6_bytes, xdb_v6_index_size, xdb_ip_sub_compare}, // END {0, NULL, 0, 0, NULL}