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