| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
|
#include "memory_pool.h" |
|
| 17 |
#include "log.h" |
#include "log.h" |
| 18 |
|
#include "memory_pool.h" |
| 19 |
#include <stdlib.h> |
#include <stdlib.h> |
| 20 |
#include <string.h> |
#include <string.h> |
| 21 |
|
|
| 98 |
} |
} |
| 99 |
|
|
| 100 |
p_node = p_pool->p_free; |
p_node = p_pool->p_free; |
| 101 |
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)); |
| 102 |
for (i = 0; i < p_pool->node_count_per_chunk - 1; i++) |
for (i = 0; i < p_pool->node_count_per_chunk - 1; i++) |
| 103 |
{ |
{ |
| 104 |
p_node = p_chunk + (i + 1) * p_pool->node_size; |
p_node = (char *)p_chunk + (i + 1) * p_pool->node_size; |
| 105 |
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)); |
| 106 |
} |
} |
| 107 |
|
|
| 108 |
p_pool->p_chunks[p_pool->chunk_count] = p_chunk; |
p_pool->p_chunks[p_pool->chunk_count] = p_chunk; |
| 148 |
return; |
return; |
| 149 |
} |
} |
| 150 |
|
|
| 151 |
|
// For test and debug |
| 152 |
|
#ifdef _DEBUG |
| 153 |
|
memory_pool_check_node(p_pool, p_node); |
| 154 |
|
#endif |
| 155 |
|
|
| 156 |
memcpy(p_node, &(p_pool->p_free), sizeof(p_pool->p_free)); |
memcpy(p_node, &(p_pool->p_free), sizeof(p_pool->p_free)); |
| 157 |
p_pool->p_free = p_node; |
p_pool->p_free = p_node; |
| 158 |
|
|
| 175 |
|
|
| 176 |
for (i = 0; i < p_pool->chunk_count; i++) |
for (i = 0; i < p_pool->chunk_count; i++) |
| 177 |
{ |
{ |
| 178 |
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) |
| 179 |
{ |
{ |
| 180 |
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) |
| 181 |
{ |
{ |
| 182 |
return 0; // OK |
return 0; // OK |
| 183 |
} |
} |
| 184 |
else |
else |
| 185 |
{ |
{ |
| 186 |
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", |
| 187 |
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); |
| 188 |
return -3; |
return -3; |
| 189 |
} |
} |
| 190 |
} |
} |