/[LeafOK_CVS]/lbbs/src/test_memory_pool.c
ViewVC logotype

Contents of /lbbs/src/test_memory_pool.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations)
Wed Nov 5 06:14:01 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.5: +6 -3 lines
Content type: text/x-csrc
Use enum / const int instead of macro define constant integers
Use const char * instead of macro define for constant strings

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

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1