| 1 |
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
| 2 |
/*
|
| 3 |
* test_memory_pool
|
| 4 |
* - tester for memory pool
|
| 5 |
*
|
| 6 |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com>
|
| 7 |
*/
|
| 8 |
|
| 9 |
#ifdef HAVE_CONFIG_H
|
| 10 |
#include "config.h"
|
| 11 |
#endif
|
| 12 |
|
| 13 |
#include "log.h"
|
| 14 |
#include "memory_pool.h"
|
| 15 |
#include <stdio.h>
|
| 16 |
#include <string.h>
|
| 17 |
#include <unistd.h>
|
| 18 |
|
| 19 |
enum _test_memory_pool_constant_t
|
| 20 |
{
|
| 21 |
NODE_SIZE = 1023,
|
| 22 |
NODE_PER_CHUNK = 1000,
|
| 23 |
CHUNK_COUNT_LIMIT = 100,
|
| 24 |
};
|
| 25 |
|
| 26 |
int main(int argc, char *argv[])
|
| 27 |
{
|
| 28 |
MEMORY_POOL *p_pool;
|
| 29 |
void *p_nodes[NODE_PER_CHUNK * CHUNK_COUNT_LIMIT];
|
| 30 |
int i;
|
| 31 |
int j;
|
| 32 |
int k;
|
| 33 |
|
| 34 |
if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
|
| 35 |
{
|
| 36 |
printf("Open log error\n");
|
| 37 |
return -1;
|
| 38 |
}
|
| 39 |
|
| 40 |
log_common_redir(STDOUT_FILENO);
|
| 41 |
log_error_redir(STDERR_FILENO);
|
| 42 |
|
| 43 |
p_pool = memory_pool_init(NODE_SIZE, NODE_PER_CHUNK, CHUNK_COUNT_LIMIT);
|
| 44 |
if (p_pool == NULL)
|
| 45 |
{
|
| 46 |
return -2;
|
| 47 |
}
|
| 48 |
|
| 49 |
for (j = 0; j < 3; j++)
|
| 50 |
{
|
| 51 |
printf("Testing times #%d ...\n", j + 1);
|
| 52 |
|
| 53 |
for (i = 0; i < NODE_PER_CHUNK * CHUNK_COUNT_LIMIT; i++)
|
| 54 |
{
|
| 55 |
if (j == 0 && i % NODE_PER_CHUNK == 0)
|
| 56 |
{
|
| 57 |
printf("Allocating nodes, total=%d, allocated=%d, free=%d\n", p_pool->node_count_total, p_pool->node_count_allocated, p_pool->node_count_free);
|
| 58 |
}
|
| 59 |
|
| 60 |
p_nodes[i] = memory_pool_alloc(p_pool);
|
| 61 |
|
| 62 |
if (memory_pool_check_node(p_pool, p_nodes[i]) != 0)
|
| 63 |
{
|
| 64 |
printf("Error of node %d address\n", i);
|
| 65 |
}
|
| 66 |
}
|
| 67 |
|
| 68 |
printf("Allocate completed, total=%d, allocated=%d, free=%d\n", p_pool->node_count_total, p_pool->node_count_allocated, p_pool->node_count_free);
|
| 69 |
|
| 70 |
if (memory_pool_alloc(p_pool) != NULL)
|
| 71 |
{
|
| 72 |
printf("Allocate node error, expected is NULL\n");
|
| 73 |
}
|
| 74 |
|
| 75 |
for (i = 0; i < NODE_PER_CHUNK * CHUNK_COUNT_LIMIT; i++)
|
| 76 |
{
|
| 77 |
if (memory_pool_check_node(p_pool, p_nodes[i]) != 0)
|
| 78 |
{
|
| 79 |
printf("Error of node %d address\n", i);
|
| 80 |
}
|
| 81 |
|
| 82 |
if (j > 0)
|
| 83 |
{
|
| 84 |
for (k = (int)sizeof(void *); k < NODE_SIZE; k++)
|
| 85 |
{
|
| 86 |
if ((*((char *)p_nodes[i] + k)) != 'A' + j - 1)
|
| 87 |
{
|
| 88 |
printf("Value of node[%d] at offset %d not equal to value set %c\n",
|
| 89 |
i, k, 'A' + j - 1);
|
| 90 |
}
|
| 91 |
}
|
| 92 |
}
|
| 93 |
}
|
| 94 |
|
| 95 |
for (i = 0; i < NODE_PER_CHUNK * CHUNK_COUNT_LIMIT; i++)
|
| 96 |
{
|
| 97 |
if (j == 0 && i % NODE_PER_CHUNK == 0)
|
| 98 |
{
|
| 99 |
printf("Free nodes, total=%d, allocated=%d, free=%d\n", p_pool->node_count_total, p_pool->node_count_allocated, p_pool->node_count_free);
|
| 100 |
}
|
| 101 |
|
| 102 |
memset(p_nodes[i], 'A' + j, NODE_SIZE);
|
| 103 |
|
| 104 |
memory_pool_free(p_pool, p_nodes[i]);
|
| 105 |
}
|
| 106 |
|
| 107 |
printf("Free completed, total=%d, allocated=%d, free=%d\n", p_pool->node_count_total, p_pool->node_count_allocated, p_pool->node_count_free);
|
| 108 |
}
|
| 109 |
|
| 110 |
memory_pool_cleanup(p_pool);
|
| 111 |
|
| 112 |
log_end();
|
| 113 |
|
| 114 |
return 0;
|
| 115 |
}
|