--- lbbs/src/memory_pool.c 2025/06/12 09:46:42 1.1 +++ lbbs/src/memory_pool.c 2025/11/11 00:28:05 1.9 @@ -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-2025 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif -#include "memory_pool.h" #include "log.h" +#include "memory_pool.h" #include #include @@ -98,11 +94,11 @@ inline static void *memory_pool_add_chun } 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; @@ -148,6 +144,11 @@ void memory_pool_free(MEMORY_POOL *p_poo return; } + // For test and debug +#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; @@ -170,16 +171,16 @@ 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); + i, p_node >= p_pool->p_chunks[i], (char *)(p_pool->p_chunks[i]) + chunk_size); return -3; } }