--- lbbs/src/memory_pool.c 2025/06/21 02:15:18 1.5 +++ lbbs/src/memory_pool.c 2025/12/19 06:16:27 1.10 @@ -1,18 +1,14 @@ -/*************************************************************************** - 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 "log.h" #include "memory_pool.h" @@ -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) @@ -87,22 +83,22 @@ inline static void *memory_pool_add_chun 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); 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(%d * %d) error: OOM", p_pool->node_size, p_pool->node_count_per_chunk); 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 +117,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,7 +140,7 @@ void memory_pool_free(MEMORY_POOL *p_poo { if (p_pool == NULL) { - log_error("NULL pointer error\n"); + log_error("NULL pointer error"); return; } @@ -167,7 +163,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; } @@ -175,21 +171,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)", + i, p_node >= 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; }