From 075c4f45834b2d9a6545e380dbfa0477432cce1a Mon Sep 17 00:00:00 2001 From: Evgeny Date: Mon, 8 Jun 2026 12:57:28 +0300 Subject: [PATCH] =?UTF-8?q?memory=5Fpool:=20name=20field=20=E2=80=94=20?= =?UTF-8?q?=D0=B8=D0=B4=D0=B5=D0=BD=D1=82=D0=B8=D1=84=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BF=D1=83=D0=BB=D0=B0=20=D0=B2=20=D0=B4?= =?UTF-8?q?=D0=B8=D0=B0=D0=B3=D0=BD=D0=BE=D1=81=D1=82=D0=B8=D0=BA=D0=B5=20?= =?UTF-8?q?=D0=BA=D0=BE=D1=80=D1=80=D0=B0=D0=BF=D1=88=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/memory_pool.c | 11 ++++++----- lib/memory_pool.h | 3 ++- lib/tcp_io.c | 4 ++-- lib/u_async.c | 2 +- src/dummynet.c | 2 +- src/etcp.c | 4 ++-- src/lwip_tcp/lwip_tcp.c | 6 +++--- src/proxy/tcp_proxy_client.c | 2 +- src/tun_if.c | 4 ++-- src/utun_instance.c | 6 +++--- tests/bbr_integration/test_bbr_integration.c | 6 +++--- tests/test_etcp_congestion.c | 6 +++--- tests/test_etcp_dummynet.c | 6 +++--- tests/test_etcp_reinit_inflight.c | 6 +++--- tests/test_intensive_memory_pool.c | 2 +- tests/test_ll_queue.c | 2 +- tests/test_pkt_normalizer_standalone.c | 4 ++-- 17 files changed, 39 insertions(+), 37 deletions(-) diff --git a/lib/memory_pool.c b/lib/memory_pool.c index 53c52421..8f49aee1 100644 --- a/lib/memory_pool.c +++ b/lib/memory_pool.c @@ -29,8 +29,8 @@ static void pool_check_and_clear_tags(struct memory_pool* pool, void* obj, const uint8_t* counter = (uint8_t*)obj + POOL_COUNTER_OFF; if (*canary != POOL_CANARY_VAL) { - DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "pool_free BUFFER OVERFLOW pool=%p sz=%zu allocs=%zu reuse=%zu free=%d", - pool, pool->object_size, pool->allocations, pool->reuse_count, pool->free_count); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "pool_free BUFFER OVERFLOW pool=%p name=%s sz=%zu allocs=%zu reuse=%zu free=%d", + pool, pool->name ? pool->name : "?", pool->object_size, pool->allocations, pool->reuse_count, pool->free_count); DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " obj=%p alloc=%s free=%s canary=0x%08x expected=0x%08x", obj, *loc ? *loc : "(null)", location, *canary, POOL_CANARY_VAL); if (pool->object_size) @@ -39,8 +39,8 @@ static void pool_check_and_clear_tags(struct memory_pool* pool, void* obj, const while (_halt) {} } if (*counter == 0) { - DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "pool_free DOUBLE FREE pool=%p sz=%zu allocs=%zu reuse=%zu alloc=%s free=%s", - pool, pool->object_size, pool->allocations, pool->reuse_count, + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "pool_free DOUBLE FREE pool=%p name=%s sz=%zu allocs=%zu reuse=%zu alloc=%s free=%s", + pool, pool->name ? pool->name : "?", pool->object_size, pool->allocations, pool->reuse_count, *loc ? *loc : "(null)", location); volatile int _halt = 1; while (_halt) {} @@ -55,7 +55,7 @@ size_t memory_pool_get_total_free_blocks(void) { } // Инициализировать пул памяти -struct memory_pool* memory_pool_init(size_t object_size) { +struct memory_pool* memory_pool_init(size_t object_size, const char* name) { struct memory_pool* pool = u_calloc(1, sizeof(struct memory_pool)); if (!pool) { return NULL; @@ -66,6 +66,7 @@ struct memory_pool* memory_pool_init(size_t object_size) { pool->allocations = 0; pool->reuse_count = 0; pool->alloc_tag_counter = 1; + pool->name = name; return pool; } diff --git a/lib/memory_pool.h b/lib/memory_pool.h index 7391344a..4cb0f474 100644 --- a/lib/memory_pool.h +++ b/lib/memory_pool.h @@ -18,11 +18,12 @@ struct memory_pool { size_t allocations; // Общее количество аллокаций (включая новые malloc) size_t reuse_count; // Количество повторных использований из пула uint8_t alloc_tag_counter; // монотонный счётчик для детекции double-free (1 байт) + const char* name; // Имя пула для диагностики (напр. "data_pool", "pkt_pool") }; // сам пул: // если используем с элементами ll_queue то не забываем что к object_size надо прибавить sizeof(struct ll_entry) -struct memory_pool* memory_pool_init(size_t object_size); +struct memory_pool* memory_pool_init(size_t object_size, const char* name); void memory_pool_destroy(struct memory_pool* pool); // элементы пула: diff --git a/lib/tcp_io.c b/lib/tcp_io.c index 5d02e4fc..bc9708a9 100644 --- a/lib/tcp_io.c +++ b/lib/tcp_io.c @@ -46,10 +46,10 @@ struct tcp_conn* tcp_conn_create( tc->on_error = on_error; tc->arg = arg; - tc->entry_pool = memory_pool_init(sizeof(struct ll_entry)); + tc->entry_pool = memory_pool_init(sizeof(struct ll_entry), "entry_pool"); { size_t ds = entry_data_size > write_chunk_size ? entry_data_size : write_chunk_size; - tc->data_pool = memory_pool_init(ds); + tc->data_pool = memory_pool_init(ds, "data_pool"); } if (!tc->entry_pool || !tc->data_pool) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "tcp_conn_create: memory_pool_init failed"); diff --git a/lib/u_async.c b/lib/u_async.c index 53316c53..c27759ef 100644 --- a/lib/u_async.c +++ b/lib/u_async.c @@ -1347,7 +1347,7 @@ struct UASYNC* uasync_create(void) { } // Initialize timeout pool - ua->timeout_pool = memory_pool_init(sizeof(struct timeout_node)); + ua->timeout_pool = memory_pool_init(sizeof(struct timeout_node), "timeout_pool"); if (!ua->timeout_pool) { timeout_heap_destroy(ua->timeout_heap); socket_array_destroy(ua->sockets); diff --git a/src/dummynet.c b/src/dummynet.c index dd728126..9a63a974 100644 --- a/src/dummynet.c +++ b/src/dummynet.c @@ -355,7 +355,7 @@ struct dummynet* dummynet_create(struct UASYNC* ua, const char* bind_ip, uint16_ dn->listen_port = listen_port; /* Создаём пул для пакетов */ - dn->pkt_pool = memory_pool_init(sizeof(struct ll_entry) + sizeof(struct dummynet_pkt)); + dn->pkt_pool = memory_pool_init(sizeof(struct ll_entry) + sizeof(struct dummynet_pkt), "pkt_pool"); if (!dn->pkt_pool) { DEBUG_ERROR(DEBUG_CATEGORY_DUMMYNET, "Failed to create packet pool"); u_free(dn); diff --git a/src/etcp.c b/src/etcp.c index 1803a0b7..acdf94d3 100644 --- a/src/etcp.c +++ b/src/etcp.c @@ -205,8 +205,8 @@ struct ETCP_CONN* etcp_connection_create(struct UTUN_INSTANCE* instance, char* n etcp->input_wait_ack = queue_new(instance->ua, INFLIGHT_INITIAL_HASH_SIZE, 0, 4, "input_wait_ack"); // Hash for wait_ack etcp->recv_q = queue_new(instance->ua, INFLIGHT_INITIAL_HASH_SIZE, 0, 4, "recv_q"); // Hash for recv_q etcp->ack_q = queue_new(instance->ua, INFLIGHT_INITIAL_HASH_SIZE, 0, 4, "ack_q"); - etcp->inflight_pool = memory_pool_init(sizeof(struct INFLIGHT_PACKET)); - etcp->io_pool = memory_pool_init(sizeof(struct ETCP_FRAGMENT)); + etcp->inflight_pool = memory_pool_init(sizeof(struct INFLIGHT_PACKET), "inflight_pool"); + etcp->io_pool = memory_pool_init(sizeof(struct ETCP_FRAGMENT), "io_pool"); etcp->optimal_inflight=100000; etcp->initialized=0; etcp->links_up=0; diff --git a/src/lwip_tcp/lwip_tcp.c b/src/lwip_tcp/lwip_tcp.c index 54e84bce..723a56c5 100644 --- a/src/lwip_tcp/lwip_tcp.c +++ b/src/lwip_tcp/lwip_tcp.c @@ -95,9 +95,9 @@ struct lwip_tcp_ctx *lwip_tcp_init(struct UASYNC *ua, tcp_output_fn output, void ctx->ua = ua; ctx->output = output; ctx->output_arg = output_arg; - ctx->pcb_pool = memory_pool_init(sizeof(struct tcp_pcb)); - ctx->pcb_listen_pool = memory_pool_init(sizeof(struct tcp_pcb_listen)); - ctx->seg_pool = memory_pool_init(sizeof(struct tcp_seg)); + ctx->pcb_pool = memory_pool_init(sizeof(struct tcp_pcb), "pcb_pool"); + ctx->pcb_listen_pool = memory_pool_init(sizeof(struct tcp_pcb_listen), "pcb_listen_pool"); + ctx->seg_pool = memory_pool_init(sizeof(struct tcp_seg), "seg_pool"); if (!ctx->pcb_pool || !ctx->pcb_listen_pool || !ctx->seg_pool) { lwip_tcp_destroy(ctx); return NULL; diff --git a/src/proxy/tcp_proxy_client.c b/src/proxy/tcp_proxy_client.c index 4c3752fe..a3e39c81 100644 --- a/src/proxy/tcp_proxy_client.c +++ b/src/proxy/tcp_proxy_client.c @@ -564,7 +564,7 @@ struct tcp_proxy_client* tcp_proxy_client_create(struct UTUN_INSTANCE* inst, str p->via_node_id = via_node_id; p->mappings = mappings; p->mapping_count = mapping_count; - p->entry_pool = memory_pool_init(sizeof(struct ll_entry)); + p->entry_pool = memory_pool_init(sizeof(struct ll_entry), "entry_pool"); if (!p->entry_pool) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "tcp_proxy_client_create: memory_pool_init failed"); u_free(p); return NULL; } p->tun = tun_init_nat(ua, tun_name, tun_ip, mtu > 0 ? mtu : 1500, test_mode); diff --git a/src/tun_if.c b/src/tun_if.c index e511d3b6..175bc7c8 100644 --- a/src/tun_if.c +++ b/src/tun_if.c @@ -161,7 +161,7 @@ struct tun_if* tun_init(struct UASYNC* ua, struct utun_config* config) tun->test_fd = fds[1]; } - tun->pool = memory_pool_init(sizeof(struct ll_entry)); + tun->pool = memory_pool_init(sizeof(struct ll_entry), "tun_pool"); if (!tun->pool) goto fail; tun->output_queue = queue_new(ua, 0, 0, 0, "TUN output"); @@ -253,7 +253,7 @@ struct tun_if* tun_init_nat(struct UASYNC* ua, const char* ifname, const char* i tun->test_fd = fds[1]; } - tun->pool = memory_pool_init(sizeof(struct ll_entry)); + tun->pool = memory_pool_init(sizeof(struct ll_entry), "tun_pool"); if (!tun->pool) goto fail2; tun->output_queue = queue_new(ua, 0, 0, 0, "NAT TUN output"); diff --git a/src/utun_instance.c b/src/utun_instance.c index f8588668..dd2275e5 100644 --- a/src/utun_instance.c +++ b/src/utun_instance.c @@ -83,9 +83,9 @@ static int instance_init_common(struct UTUN_INSTANCE* instance, struct UASYNC* u } // Create memory pools - instance->ack_pool = memory_pool_init(sizeof(struct ACK_PACKET)); - instance->data_pool = memory_pool_init(PACKET_DATA_SIZE); - instance->pkt_pool = memory_pool_init(sizeof(struct ETCP_DGRAM) + PACKET_DATA_SIZE); + instance->ack_pool = memory_pool_init(sizeof(struct ACK_PACKET), "ack_pool"); + instance->data_pool = memory_pool_init(PACKET_DATA_SIZE, "data_pool"); + instance->pkt_pool = memory_pool_init(sizeof(struct ETCP_DGRAM) + PACKET_DATA_SIZE, "pkt_pool"); // Create routing module if (routing_create(instance) != 0) { diff --git a/tests/bbr_integration/test_bbr_integration.c b/tests/bbr_integration/test_bbr_integration.c index 1b9796f1..07f6dec8 100644 --- a/tests/bbr_integration/test_bbr_integration.c +++ b/tests/bbr_integration/test_bbr_integration.c @@ -111,9 +111,9 @@ static struct UTUN_INSTANCE* create_instance(struct UASYNC* u, uint64_t node_id, inst->ua = u; inst->node_id = node_id; if (sc_init_local_keys(&inst->my_keys, pub_hex, priv_hex) != SC_OK) { u_free(inst); return NULL; } - inst->ack_pool = memory_pool_init(sizeof(struct ACK_PACKET)); - inst->data_pool = memory_pool_init(PACKET_DATA_SIZE); - inst->pkt_pool = memory_pool_init(sizeof(struct ETCP_DGRAM) + PACKET_DATA_SIZE); + inst->ack_pool = memory_pool_init(sizeof(struct ACK_PACKET), "ack_pool"); + inst->data_pool = memory_pool_init(PACKET_DATA_SIZE, "data_pool"); + inst->pkt_pool = memory_pool_init(sizeof(struct ETCP_DGRAM) + PACKET_DATA_SIZE, "pkt_pool"); if (!inst->ack_pool || !inst->data_pool || !inst->pkt_pool) { u_free(inst); return NULL; } struct utun_config* cfg = u_calloc(1, sizeof(*cfg)); if (!cfg) { u_free(inst); return NULL; } diff --git a/tests/test_etcp_congestion.c b/tests/test_etcp_congestion.c index 33a6e419..3f0bc85d 100644 --- a/tests/test_etcp_congestion.c +++ b/tests/test_etcp_congestion.c @@ -102,9 +102,9 @@ static struct UTUN_INSTANCE* create_instance(struct UASYNC* u, uint64_t node_id, inst->ua = u; inst->node_id = node_id; if (sc_init_local_keys(&inst->my_keys, pub_hex, priv_hex) != SC_OK) { u_free(inst); return NULL; } - inst->ack_pool = memory_pool_init(sizeof(struct ACK_PACKET)); - inst->data_pool = memory_pool_init(PACKET_DATA_SIZE); - inst->pkt_pool = memory_pool_init(sizeof(struct ETCP_DGRAM) + PACKET_DATA_SIZE); + inst->ack_pool = memory_pool_init(sizeof(struct ACK_PACKET), "ack_pool"); + inst->data_pool = memory_pool_init(PACKET_DATA_SIZE, "data_pool"); + inst->pkt_pool = memory_pool_init(sizeof(struct ETCP_DGRAM) + PACKET_DATA_SIZE, "pkt_pool"); if (!inst->ack_pool || !inst->data_pool || !inst->pkt_pool) { u_free(inst); return NULL; } struct utun_config* cfg = u_calloc(1, sizeof(*cfg)); if (!cfg) { u_free(inst); return NULL; } diff --git a/tests/test_etcp_dummynet.c b/tests/test_etcp_dummynet.c index ba5dd9b5..fa4111a0 100644 --- a/tests/test_etcp_dummynet.c +++ b/tests/test_etcp_dummynet.c @@ -111,9 +111,9 @@ static struct UTUN_INSTANCE* create_instance(struct UASYNC* u, uint64_t node_id, return NULL; } - inst->ack_pool = memory_pool_init(sizeof(struct ACK_PACKET)); - inst->data_pool = memory_pool_init(PACKET_DATA_SIZE); - inst->pkt_pool = memory_pool_init(sizeof(struct ETCP_DGRAM) + PACKET_DATA_SIZE); + inst->ack_pool = memory_pool_init(sizeof(struct ACK_PACKET), "ack_pool"); + inst->data_pool = memory_pool_init(PACKET_DATA_SIZE, "data_pool"); + inst->pkt_pool = memory_pool_init(sizeof(struct ETCP_DGRAM) + PACKET_DATA_SIZE, "pkt_pool"); if (!inst->ack_pool || !inst->data_pool || !inst->pkt_pool) { u_free(inst); diff --git a/tests/test_etcp_reinit_inflight.c b/tests/test_etcp_reinit_inflight.c index 0edf0c63..b72b7d44 100644 --- a/tests/test_etcp_reinit_inflight.c +++ b/tests/test_etcp_reinit_inflight.c @@ -53,9 +53,9 @@ static struct UTUN_INSTANCE* create_instance(struct UASYNC* u, uint64_t node_id, inst->ua = u; inst->node_id = node_id; if (sc_init_local_keys(&inst->my_keys, pub_hex, priv_hex) != SC_OK) { u_free(inst); return NULL; } - inst->ack_pool = memory_pool_init(sizeof(struct ACK_PACKET)); - inst->data_pool = memory_pool_init(PACKET_DATA_SIZE); - inst->pkt_pool = memory_pool_init(sizeof(struct ETCP_DGRAM) + PACKET_DATA_SIZE); + inst->ack_pool = memory_pool_init(sizeof(struct ACK_PACKET), "ack_pool"); + inst->data_pool = memory_pool_init(PACKET_DATA_SIZE, "data_pool"); + inst->pkt_pool = memory_pool_init(sizeof(struct ETCP_DGRAM) + PACKET_DATA_SIZE, "pkt_pool"); if (!inst->ack_pool || !inst->data_pool || !inst->pkt_pool) { u_free(inst); return NULL; } struct utun_config* cfg = u_calloc(1, sizeof(*cfg)); if (!cfg) { u_free(inst); return NULL; } diff --git a/tests/test_intensive_memory_pool.c b/tests/test_intensive_memory_pool.c index 9d917b9c..d1d9dc9c 100644 --- a/tests/test_intensive_memory_pool.c +++ b/tests/test_intensive_memory_pool.c @@ -65,7 +65,7 @@ static double test_with_pools(int iterations) { struct ll_queue* queue = queue_new(ua, 0, 0, 0,"q2"); // С пулами // Создать пул памяти для данных - struct memory_pool* pool = memory_pool_init(sizeof(struct ll_entry) + 64); + struct memory_pool* pool = memory_pool_init(sizeof(struct ll_entry) + 64, "test_pool"); if (!pool) { queue_free(queue); uasync_destroy(ua, 0); diff --git a/tests/test_ll_queue.c b/tests/test_ll_queue.c index af8c6718..73754600 100644 --- a/tests/test_ll_queue.c +++ b/tests/test_ll_queue.c @@ -354,7 +354,7 @@ static void test_limits_hash(void) { static void test_pool(void) { TEST("memory_pool integration + reuse"); struct UASYNC *ua = uasync_create(); - struct memory_pool *pool = memory_pool_init(sizeof(test_data_t)); + struct memory_pool *pool = memory_pool_init(sizeof(test_data_t), "test_data_pool"); struct ll_queue *q = queue_new(ua, 0, 0, 0, "q8"); size_t alloc1 = 0, reuse1 = 0; diff --git a/tests/test_pkt_normalizer_standalone.c b/tests/test_pkt_normalizer_standalone.c index ffdebbd5..1f8b5309 100644 --- a/tests/test_pkt_normalizer_standalone.c +++ b/tests/test_pkt_normalizer_standalone.c @@ -185,7 +185,7 @@ static int init_mock_etcp(void) { mock_etcp.instance = (struct UTUN_INSTANCE*)&mock_instance; // Create io_pool for ETCP_FRAGMENT allocation - mock_etcp.io_pool = memory_pool_init(sizeof(struct ETCP_FRAGMENT)); + mock_etcp.io_pool = memory_pool_init(sizeof(struct ETCP_FRAGMENT), "io_pool"); if (!mock_etcp.io_pool) { printf("Failed to create io_pool\n"); return -1; @@ -271,7 +271,7 @@ int main() { } // Initialize memory pool - mock_instance.data_pool = memory_pool_init(MTU_SIZE); + mock_instance.data_pool = memory_pool_init(MTU_SIZE, "data_pool"); if (!mock_instance.data_pool) { printf("Failed to create memory pool\n");