/[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.2 - (show annotations)
Sat Jun 21 02:15:18 2025 UTC (8 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.1: +2 -2 lines
Content type: text/x-csrc
Re-order included order files

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

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