| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
memory_pool.c - description |
/* |
| 3 |
------------------- |
* memory_pool |
| 4 |
copyright : (C) 2004-2025 by Leaflet |
* - memory pool |
| 5 |
email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
|
#include "memory_pool.h" |
|
| 13 |
#include "log.h" |
#include "log.h" |
| 14 |
|
#include "memory_pool.h" |
| 15 |
#include <stdlib.h> |
#include <stdlib.h> |
| 16 |
#include <string.h> |
#include <string.h> |
| 17 |
|
|
| 94 |
} |
} |
| 95 |
|
|
| 96 |
p_node = p_pool->p_free; |
p_node = p_pool->p_free; |
| 97 |
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)); |
| 98 |
for (i = 0; i < p_pool->node_count_per_chunk - 1; i++) |
for (i = 0; i < p_pool->node_count_per_chunk - 1; i++) |
| 99 |
{ |
{ |
| 100 |
p_node = p_chunk + (i + 1) * p_pool->node_size; |
p_node = (char *)p_chunk + (i + 1) * p_pool->node_size; |
| 101 |
memcpy(p_chunk + i * p_pool->node_size, &p_node, sizeof(p_node)); |
memcpy((char *)p_chunk + i * p_pool->node_size, &p_node, sizeof(p_node)); |
| 102 |
} |
} |
| 103 |
|
|
| 104 |
p_pool->p_chunks[p_pool->chunk_count] = p_chunk; |
p_pool->p_chunks[p_pool->chunk_count] = p_chunk; |
| 145 |
} |
} |
| 146 |
|
|
| 147 |
// For test and debug |
// For test and debug |
| 148 |
|
#ifdef _DEBUG |
| 149 |
memory_pool_check_node(p_pool, p_node); |
memory_pool_check_node(p_pool, p_node); |
| 150 |
|
#endif |
| 151 |
|
|
| 152 |
memcpy(p_node, &(p_pool->p_free), sizeof(p_pool->p_free)); |
memcpy(p_node, &(p_pool->p_free), sizeof(p_pool->p_free)); |
| 153 |
p_pool->p_free = p_node; |
p_pool->p_free = p_node; |
| 171 |
|
|
| 172 |
for (i = 0; i < p_pool->chunk_count; i++) |
for (i = 0; i < p_pool->chunk_count; i++) |
| 173 |
{ |
{ |
| 174 |
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) |
| 175 |
{ |
{ |
| 176 |
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) |
| 177 |
{ |
{ |
| 178 |
return 0; // OK |
return 0; // OK |
| 179 |
} |
} |
| 180 |
else |
else |
| 181 |
{ |
{ |
| 182 |
log_error("Address of node (%p) is not aligned with border of chunk %d [%p, %p)\n", |
log_error("Address of node (%p) is not aligned with border of chunk %d [%p, %p)\n", |
| 183 |
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); |
| 184 |
return -3; |
return -3; |
| 185 |
} |
} |
| 186 |
} |
} |