Memory Pool
Create a pool of memory blocks
All Data Structures Files Functions Variables Typedefs Macros Pages
test-memory-pool.c
Go to the documentation of this file.
1 
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <memory.h>
13 #include <time.h>
14 #include "memory_pool.h"
15 
16 
17 
18 // magic value to check for data corruption
19 #define NODE_MAGIC 0xBAADA555
20 
21 
22 
23 
24 
25 void main ()
26 {
27  size_t count;
28  size_t block_size;
29  size_t available;
30  bool returned_bool;
31 
32 
34  // TEST 1
35  //
36  // basic create and destruct
38 
39  printf("\n\n\t****************** TEST 1 *****************\n\n\n");
40 
41  memory_pool_t * pool_1;
42  count = 5;
43  block_size = 1;
44 
45  printf("main: pool_1=%p, count=%zu, block_size=%zu\n",
46  pool_1, count, block_size);
47 
48  // initialze
49  pool_1 = memory_pool_init(count, block_size);
50 
51  // destruct
52  returned_bool = memory_pool_destroy(pool_1);
53 
54  printf("main: destroy returned: %s\n",
55  returned_bool ? "TRUE":"FALSE");
56 
57 
59  // TEST 2
60  //
61  // basic tests of aquisition and release
63 
64  printf("\n\n\t****************** TEST 2 *****************\n\n\n");
65 
66  memory_pool_t * pool_2;
67  count = 5;
68  block_size = 4;
69 
70  printf("main: pool_1=%p, count=%zu, block_size=%zu\n",
71  pool_2, count, block_size);
72 
73  // initialze
74  pool_2 = memory_pool_init(count, block_size);
75 
76  // check available size
77  available = memory_pool_available(pool_2);
78  printf("main: pool_2=%p, pool_2->available=%zu\n", pool_2, available);
79 
80  // acquire 1 data block
81  void * test_block;
82  test_block = memory_pool_acquire(pool_2);
83  printf("main: pointer aquired=%p\n", test_block);
84 
85  // try to pop (aquire) too many data blocks
86  int too_many = count + 2;
87  for( int n = 0; n < too_many; ++n ) {
88  memory_pool_acquire(pool_2);
89  }
90 
91  printf("main: tried to aquire %d more from pool_1\n", too_many);
92 
93  // dereference memory pointed to by test_block
94  unsigned int u_data = *(unsigned int*) test_block;
95 
96  printf("main: test_block=%p, dereferenced=%u\n", test_block, u_data);
97 
98  // release back the first acquired block to test destruction
99  memory_pool_release(pool_2, test_block);
100 
101  // destruct
102  returned_bool = memory_pool_destroy(pool_2);
103 
104  printf("main: destroy returned: %s\n",
105  returned_bool ? "TRUE":"FALSE"); // bool
106 
107 
109  // TEST 3
110  //
111  // 1. create random size memory pool
112  // 2. acquire all data blocks
113  // 3. fill all data blocks using magic value
114  // 4. relese all data blocks back into pool
115  // 5. dump all data before destruct
117 
118  printf("\n\n\t****************** TEST 3 *****************\n"
119  "\t* 1. create random size memory pool\n"
120  "\t* 2. acquire all data blocks\n"
121  "\t* 3. fill all data blocks using magic value\n"
122  "\t* 4. release all data blocks back into pool\n"
123  "\t* 5. dump all data before destruct\n"
124  "\t*******************************************\n\n\n");
125 
126  // seed random number generator
127  srand(time(0));
128 
129  // between 1 and 10 data blocks
130  count = (rand() % 9) +1;
131 
132  memory_pool_t * pool_3;
133  block_size = sizeof(NODE_MAGIC);
134 
135  printf("main: pool_1=%p, count=%zu, block_size=%zu\n",
136  pool_3, count, block_size);
137 
138  // initialze
139  pool_3 = memory_pool_init(count, block_size);
140 
141  // create an array of pointers to store all data blocks
142  void ** magic_array = (void **) calloc(count, sizeof(0xBAADA555));
143 
144  // aquire all blocks in pool
145  for( int n = 0; n < count; ++n ) {
146  magic_array[n] = memory_pool_acquire(pool_3);
147  }
148 
149  // print data in all aquired blocks (should all be zero)
150  for( int n = 0; n < count; ++n ) {
151  printf("main: reading: magic_array[%d]=%p, data=%X\n",
152  n, magic_array[n], *(unsigned int*) (magic_array[n]));
153  }
154 
155  // change value of each data block to magic value
156  for( int n = 0; n < count; ++n ) {
157  *(unsigned int*) magic_array[n] = 0xBAADA555;
158  printf("main: writing: magic_array[%d]=%p, data=%X\n",
159  n, magic_array[n], *(unsigned int*) (magic_array[n]));
160  }
161 
162  // release all blocks back into pool
163  for( int n = 0; n < count; ++n ) {
164  returned_bool = memory_pool_release(pool_3, magic_array[n]);
165  }
166 
167  // re-aquire all blocks in pool
168  for( int n = 0; n < count; ++n ) {
169  magic_array[n] = memory_pool_acquire(pool_3);
170  }
171 
172  // print data in all re-aquired blocks
173  for( int n = 0; n < count; ++n ) {
174  printf("main: reading: magic_array[%d]=%p, data=%X\n",
175  n, magic_array[n], *(unsigned int*) (magic_array[n]));
176  }
177 
178  // dump pool prior to destructing
179  memory_pool_dump(pool_3);
180 
181  // destruct
182  returned_bool = memory_pool_destroy(pool_3);
183 
184  printf("main: destroy returned: %s\n",
185  returned_bool ? "TRUE":"FALSE"); // bool
186 
187  return;
188 }
189 
size_t block_size
Definition: memory_pool.c:36
#define NODE_MAGIC
size_t memory_pool_available(memory_pool_t *mp)
memory pool availability function
Definition: memory_pool.c:293
bool memory_pool_release(memory_pool_t *mp, void *data)
memory pool push function
Definition: memory_pool.c:253
void main()
size_t count
Definition: memory_pool.c:35
memory_pool_t * memory_pool_init(size_t count, size_t block_size)
memory pool initializer
Definition: memory_pool.c:90
size_t available
Definition: memory_pool.c:37
void * memory_pool_acquire(memory_pool_t *mp)
memory pool pop function
Definition: memory_pool.c:214
void memory_pool_dump(memory_pool_t *mp)
memory pool dump function
Definition: memory_pool.c:310
bool memory_pool_destroy(memory_pool_t *mp)
Definition: memory_pool.c:154