--- lbbs/src/memory_pool.c 2025/06/14 02:02:54 1.3 +++ lbbs/src/memory_pool.c 2026/01/06 13:59:21 1.12 @@ -1,21 +1,17 @@ -/*************************************************************************** - memory_pool.c - description - ------------------- - copyright : (C) 2004-2025 by Leaflet - email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * memory_pool + * - memory pool + * + * Copyright (C) 2004-2026 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif -#include "memory_pool.h" #include "log.h" +#include "memory_pool.h" #include #include @@ -25,14 +21,14 @@ MEMORY_POOL *memory_pool_init(size_t nod if (node_size < sizeof(void *)) { - log_error("Error: node_size < sizeof(void *)\n"); + log_error("Error: node_size < sizeof(void *)"); return NULL; } p_pool = malloc(sizeof(MEMORY_POOL)); if (p_pool == NULL) { - log_error("malloc(MEMORY_POOL) error: OOM\n"); + log_error("malloc(MEMORY_POOL) error: OOM"); return NULL; } @@ -45,7 +41,7 @@ MEMORY_POOL *memory_pool_init(size_t nod p_pool->p_chunks = malloc(sizeof(void *) * (size_t)chunk_count_limit); if (p_pool->p_chunks == NULL) { - log_error("malloc(sizeof(void *) * %d) error: OOM\n", chunk_count_limit); + log_error("malloc(sizeof(void *) * %d) error: OOM", chunk_count_limit); free(p_pool); return NULL; } @@ -66,7 +62,7 @@ void memory_pool_cleanup(MEMORY_POOL *p_ if (p_pool->node_count_allocated > 0) { - log_error("Still have %d in-use nodes\n", p_pool->node_count_allocated); + log_error("Still have %d in-use nodes", p_pool->node_count_allocated); } while (p_pool->chunk_count > 0) @@ -84,25 +80,28 @@ inline static void *memory_pool_add_chun void *p_chunk; void *p_node; size_t i; + size_t chunk_size; if (p_pool->chunk_count >= p_pool->chunk_count_limit) { - log_error("Chunk count limit %d reached\n", p_pool->chunk_count); + log_error("Chunk count limit %d reached", p_pool->chunk_count); return NULL; } - p_chunk = malloc(p_pool->node_size * p_pool->node_count_per_chunk); + + chunk_size = p_pool->node_size * p_pool->node_count_per_chunk; + p_chunk = malloc(chunk_size); if (p_chunk == NULL) { - log_error("malloc(%d * %d) error: OOM\n", p_pool->node_size, p_pool->node_count_per_chunk); + log_error("malloc(%zu) error: OOM", chunk_size); return NULL; } p_node = p_pool->p_free; - memcpy(p_chunk + (p_pool->node_count_per_chunk - 1) * p_pool->node_size, &p_node, sizeof(p_node)); + memcpy((char *)p_chunk + (p_pool->node_count_per_chunk - 1) * p_pool->node_size, &p_node, sizeof(p_node)); for (i = 0; i < p_pool->node_count_per_chunk - 1; i++) { - p_node = p_chunk + (i + 1) * p_pool->node_size; - memcpy(p_chunk + i * p_pool->node_size, &p_node, sizeof(p_node)); + p_node = (char *)p_chunk + (i + 1) * p_pool->node_size; + memcpy((char *)p_chunk + i * p_pool->node_size, &p_node, sizeof(p_node)); } p_pool->p_chunks[p_pool->chunk_count] = p_chunk; @@ -121,13 +120,13 @@ void *memory_pool_alloc(MEMORY_POOL *p_p if (p_pool == NULL) { - log_error("NULL pointer error\n"); + log_error("NULL pointer error"); return NULL; } if (p_pool->p_free == NULL && memory_pool_add_chunk(p_pool) == NULL) { - log_error("Add chunk error\n"); + log_error("Add chunk error"); return NULL; } @@ -144,12 +143,14 @@ void memory_pool_free(MEMORY_POOL *p_poo { if (p_pool == NULL) { - log_error("NULL pointer error\n"); + log_error("NULL pointer error"); return; } // For test and debug - // memory_pool_check_node(p_pool, p_node); +#ifdef _DEBUG + memory_pool_check_node(p_pool, p_node); +#endif memcpy(p_node, &(p_pool->p_free), sizeof(p_pool->p_free)); p_pool->p_free = p_node; @@ -165,7 +166,7 @@ int memory_pool_check_node(MEMORY_POOL * if (p_pool == NULL || p_node == NULL) { - log_error("NULL pointer error\n"); + log_error("NULL pointer error"); return -1; } @@ -173,21 +174,21 @@ int memory_pool_check_node(MEMORY_POOL * for (i = 0; i < p_pool->chunk_count; i++) { - if (p_node >= p_pool->p_chunks[i] && p_node < p_pool->p_chunks[i] + chunk_size) + if (p_node >= p_pool->p_chunks[i] && (char *)p_node < (char *)(p_pool->p_chunks[i]) + chunk_size) { - if ((size_t)(p_node - p_pool->p_chunks[i]) % p_pool->node_size == 0) + if ((size_t)((char *)p_node - (char *)(p_pool->p_chunks[i])) % p_pool->node_size == 0) { return 0; // OK } else { - log_error("Address of node (%p) is not aligned with border of chunk %d [%p, %p)\n", - i, p_node >= p_pool->p_chunks[i], p_pool->p_chunks[i] + chunk_size); + log_error("Address of node (%p) is not aligned with border of chunk %d [%p, %p)", + p_node, i, p_pool->p_chunks[i], (char *)(p_pool->p_chunks[i]) + chunk_size); return -3; } } } - log_error("Address of node is not in range of chunks\n"); + log_error("Address of node is not in range of chunks"); return -2; }