Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/pool.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pj/pool.h>
20 #include <pj/pool_buf.h>
21 #include <pj/rand.h>
22 #include <pj/log.h>
23 #include <pj/except.h>
24 #include <pj/unittest.h>
25 #include "test.h"
26
27 /**
28 * \page page_pjlib_pool_test Test: Pool
29 *
30 * This file provides implementation of \b pool_test(). It tests the
31 * functionality of the memory pool.
32 *
33 *
34 * This file is <b>pjlib-test/pool.c</b>
35 *
36 * \include pjlib-test/pool.c
37 */
38
39
40 #if INCLUDE_POOL_TEST
41
42 #define THIS_FILE "pool.c"
43 #define SIZE 4096
44
45 #ifndef PJ_IS_ALIGNED
46 # define PJ_IS_ALIGNED(PTR, ALIGNMENT) (!((pj_ssize_t)(PTR) & ((ALIGNMENT)-1)))
47 #endif
48
49 /* Normally we should throw exception when memory alloc fails.
50 * Here we do nothing so that the flow will go back to original caller,
51 * which will test the result using NULL comparison. Normally caller will
52 * catch the exception instead of checking for NULLs.
53 */
54 static void null_callback(pj_pool_t *pool, pj_size_t size)
55 {
56 PJ_UNUSED_ARG(pool);
57 PJ_UNUSED_ARG(size);
58 }
59
60 #define GET_FREE(p) (pj_pool_get_capacity(p)-pj_pool_get_used_size(p))
61
62 /* Test that the capacity and used size reported by the pool is correct.
63 */
64 static int capacity_test(void)
65 {
66 pj_pool_t *pool = pj_pool_create(mem, NULL, SIZE, 0, &null_callback);
67 pj_size_t freesize;
68
69 PJ_LOG(3,("test", "...capacity_test()"));
70
71 PJ_TEST_NOT_NULL(pool, NULL, return -200);
72
73 freesize = GET_FREE(pool);
74 PJ_TEST_NOT_NULL(pj_pool_alloc(pool, freesize), "wrong freesize reported",
75 { pj_pool_release(pool); return -210; });
76
77 pj_pool_release(pool);
78
79 return 0;
80 }
81
82 /* Test that the alignment works. */
83 static int pool_alignment_test(void)
84 {
85 pj_pool_t *pool = NULL, *pool2 = NULL;
86 unsigned char *ptr;
87 enum { MEMSIZE = 64, LOOP = 100, POOL_ALIGNMENT_TEST = 4*PJ_POOL_ALIGNMENT };
88 unsigned i;
89 int rc = 0;
90 char msg[1024];
91 pj_size_t capacity;
92 pj_ssize_t alignment;
93
94 PJ_LOG(3,("test", "...alignment test"));
95
96 /* Test pj_pool_allocate_find() when alignment is so big */
97 pool = pj_pool_aligned_create(mem, NULL,
98 50 + sizeof(pj_pool_t) + sizeof(pj_pool_block),
99 50, 4,
100 &null_callback);
101 ptr = pj_pool_aligned_alloc(pool, 256, 1);
102 pj_pool_release(pool);
103 PJ_TEST_NOT_NULL(ptr, NULL, return -300);
104
105 /* Test to reproduce the situation when PJ_POOL_ALIGN_PTR(block->cur, alignment) > block->end */
106 /* Create a non-expandable pool of small capacity */
107 pool = pj_pool_aligned_create(mem, NULL,
108 50 + sizeof(pj_pool_t) + sizeof(pj_pool_block),
109 0, 4,
110 &null_callback);
111 PJ_TEST_NOT_NULL(pool, NULL, return -301);
112
113 ptr = pj_pool_alloc(pool, 0); /* ptr == block->cur */
114 PJ_TEST_NOT_NULL(ptr, NULL, { rc=-302; goto on_return; });
115
116 /* To guarantee PJ_POOL_ALIGN_PTR(block->cur, alignment) > block->end,
117 * our alignment must be large enough that block does not contain
118 * an aligned pointer. We find the alignment to be large enough that
119 * the largest number divisible by the alignment and smaller
120 * than block->end is also smaller than block->cur.
121 */
122 capacity = pj_pool_get_capacity(pool); /* ptr + capacity >= block->end */
123 alignment = (pj_ssize_t)pool->alignment;
124 while (alignment > 0 && (((pj_size_t)(ptr+capacity)) & ~(alignment-1)) >= (pj_size_t)ptr)
125 alignment <<= 1;
126 PJ_TEST_GT(alignment, 0, NULL, { rc=-303; goto on_return; });
127
128 /* Now PJ_POOL_ALIGN_PTR(block->cur, alignment) should be > block->end.
129 * We should not be able to allocate anything with this alignment.
130 */
131 ptr = pj_pool_aligned_alloc(pool, alignment, 0);
132 pj_ansi_snprintf(msg, sizeof(msg),
133 "alignment=%ld, capacity=%lu, block->buf=%p, ptr==block->cur=%p, block->end=%p",
134 (long)alignment, (unsigned long)capacity, pool->block_list.next->buf,
135 pool->block_list.next->cur,pool->block_list.next->end);
136 PJ_TEST_EQ(ptr, NULL, msg, { rc=-304; goto on_return; });
137
138 pj_pool_release(pool);
139
140 pool = pj_pool_create(mem, NULL, PJ_POOL_SIZE+MEMSIZE, MEMSIZE, NULL);
141 PJ_TEST_NOT_NULL(pool, NULL, return -305);
142
143 pool2 = pj_pool_aligned_create(mem, NULL, PJ_POOL_SIZE + MEMSIZE, MEMSIZE,
144 POOL_ALIGNMENT_TEST, NULL);
145 PJ_TEST_NOT_NULL(pool2, NULL, { pj_pool_release(pool); return -307; });
146
147 #define IS_ALIGNED(p) ((((unsigned long)(pj_ssize_t)p) & \
148 (PJ_POOL_ALIGNMENT-1)) == 0)
149 #define IS_ALIGNED2(p) ((((unsigned long)(pj_ssize_t)p) & \
150 (POOL_ALIGNMENT_TEST-1)) == 0)
151
152 for (i=0; i<LOOP; ++i) {
153 /* Test first allocation */
154 if (i % 2)
155 {
156 /* alignment > pool->alignment */
157 ptr = pj_pool_aligned_alloc(pool, POOL_ALIGNMENT_TEST, 1);
158 PJ_TEST_TRUE(IS_ALIGNED2(ptr), NULL, { rc=-310; goto on_return; });
159
160 /* alignment < pool->alignment */
161 ptr = pj_pool_aligned_alloc(pool2, PJ_POOL_ALIGNMENT, 1);
162 PJ_TEST_TRUE(IS_ALIGNED(ptr), NULL, { rc=-315; goto on_return; });
163 }
164 else
165 {
166 /* alignment == pool->alignment */
167 ptr = pj_pool_alloc(pool, 1);
168 PJ_TEST_TRUE(IS_ALIGNED(ptr), NULL, { rc=-320; goto on_return; });
169
170 /* alignment == pool->alignment */
171 ptr = pj_pool_alloc(pool2, 1);
172 PJ_TEST_TRUE(IS_ALIGNED2(ptr), NULL, { rc=-325; goto on_return; });
173 }
174
175 /* Test subsequent allocation */
176 ptr = pj_pool_alloc(pool, 1);
177 PJ_TEST_TRUE(IS_ALIGNED(ptr), NULL, { rc=-330; goto on_return; });
178
179 /* alignment > pool->alignment */
180 ptr = pj_pool_aligned_alloc(pool, POOL_ALIGNMENT_TEST, 1);
181 PJ_TEST_TRUE(IS_ALIGNED2(ptr), NULL, { rc=-335; goto on_return; });
182
183 /* alignment == pool->alignment */
184 ptr = pj_pool_alloc(pool2, 1);
185 PJ_TEST_TRUE(IS_ALIGNED2(ptr), NULL, { rc=-340; goto on_return; });
186
187 /* alignment < pool->alignment */
188 ptr = pj_pool_aligned_alloc(pool2, PJ_POOL_ALIGNMENT, 1);
189 PJ_TEST_TRUE(IS_ALIGNED(ptr), NULL, { rc=-345; goto on_return; });
190
191
192 /* Test allocation after new block is created */
193 ptr = pj_pool_alloc(pool, MEMSIZE*2+1);
194 PJ_TEST_TRUE(IS_ALIGNED(ptr), NULL, { rc=-350; goto on_return; });
195
196 ptr = pj_pool_aligned_alloc(pool, POOL_ALIGNMENT_TEST, MEMSIZE*2+1);
197 PJ_TEST_TRUE(IS_ALIGNED2(ptr), NULL, { rc=-355; goto on_return; });
198
199 ptr = pj_pool_alloc(pool2, MEMSIZE*2+1);
200 PJ_TEST_TRUE(IS_ALIGNED2(ptr), NULL, { rc=-360; goto on_return; });
201
202 ptr = pj_pool_aligned_alloc(pool2, PJ_POOL_ALIGNMENT, MEMSIZE*2+1);
203 PJ_TEST_TRUE(IS_ALIGNED(ptr), NULL, { rc=-365; goto on_return; });
204
205 /* Reset the pool */
206 pj_pool_reset(pool);
207 pj_pool_reset(pool2);
208
209 }
210
211 /* Done */
212 on_return:
213 if (pool)
214 pj_pool_release(pool);
215 if (pool2)
216 pj_pool_release(pool2);
217
218 return rc;
219 }
220
221 /* Test that the alignment works for pool on buf. */
222 static int pool_buf_alignment_test(void)
223 {
224 pj_pool_t *pool;
225 char buf[512];
226 void *ptr;
227 enum { LOOP = 100, POOL_ALIGNMENT_TEST = 4*PJ_POOL_ALIGNMENT};
228 unsigned i;
229 int rc = 0;
230
231 PJ_LOG(3,("test", "...pool_buf alignment test"));
232
233 pool = pj_pool_create_on_buf(NULL, buf, sizeof(buf));
234 PJ_TEST_NOT_NULL(pool, NULL, return -400);
235
236 for (i=0; i<LOOP; ++i) {
237 /* Test first allocation */
238 if (i % 2) {
239 ptr = pj_pool_aligned_alloc(pool, POOL_ALIGNMENT_TEST, 1);
240 PJ_TEST_TRUE(IS_ALIGNED2(ptr), NULL, { rc = -410; goto on_return; });
241 } else {
242 ptr = pj_pool_alloc(pool, 1);
243 PJ_TEST_TRUE(IS_ALIGNED(ptr), NULL, { rc = -415; goto on_return; });
244 }
245
246 /* Test subsequent allocation */
247 ptr = pj_pool_alloc(pool, 1);
248 PJ_TEST_TRUE(IS_ALIGNED(ptr), NULL, { rc = -420; goto on_return; });
249
250 ptr = pj_pool_aligned_alloc(pool, POOL_ALIGNMENT_TEST, 1);
251 PJ_TEST_TRUE(IS_ALIGNED2(ptr), NULL, { rc = -425; goto on_return; });
252
253 /* Reset the pool */
254 pj_pool_reset(pool);
255 }
256
257 /* Done */
258 on_return:
259 pj_pool_release(pool);
260
261 return rc;
262 }
263
264 /* Test function to drain the pool's space.
265 */
266 static int drain_test(pj_size_t size, pj_size_t increment)
267 {
268 pj_pool_t *pool = pj_pool_create(mem, NULL, size, increment,
269 &null_callback);
270 pj_size_t freesize;
271 void *p;
272 int status = 0;
273
274 PJ_LOG(3,("test", "...drain_test(%lu,%lu)", (unsigned long)size,
275 (unsigned long)increment));
276 PJ_TEST_NOT_NULL(pool, NULL, return -10);
277
278
279 /* Get free size */
280 freesize = GET_FREE(pool);
281 PJ_TEST_GTE(freesize, 1, NULL, { status=-15; goto on_error;});
282
283 /* Drain the pool until there's nothing left. */
284 while (freesize > 0) {
285 int size2;
286
287 if (freesize > 255)
288 size2 = ((pj_rand() & 0x000000FF) + PJ_POOL_ALIGNMENT) &
289 ~(PJ_POOL_ALIGNMENT - 1);
290 else
291 size2 = (int)freesize;
292
293 p = pj_pool_alloc(pool, size2);
294 PJ_TEST_NOT_NULL(p, NULL, { status=-20; goto on_error;});
295
296 freesize -= size2;
297 }
298
299 /* Check that capacity is zero. */
300 PJ_TEST_EQ(GET_FREE(pool), 0, NULL, { status=-30; goto on_error;});
301
302 /* Try to allocate once more */
303 p = pj_pool_alloc(pool, 257);
304 PJ_TEST_NOT_NULL(p, NULL, { status=-40; goto on_error;});
305
306 /* Check that capacity is NOT zero. */
307 PJ_TEST_NEQ(GET_FREE(pool), 0, NULL, { status=-50; goto on_error;});
308
309
310 on_error:
311 pj_pool_release(pool);
312 return status;
313 }
314
315 /* Test the buffer based pool */
316 static int pool_buf_test(void)
317 {
318 enum { STATIC_BUF_SIZE = 40 };
319 /* 16 is the internal struct in pool_buf */
320 static char buf[ STATIC_BUF_SIZE + sizeof(pj_pool_t) +
321 sizeof(pj_pool_block) + 2 * PJ_POOL_ALIGNMENT];
322 pj_pool_t *pool;
323 void *p;
324 PJ_USE_EXCEPTION;
325
326 PJ_LOG(3,("test", "...pool_buf test"));
327
328 pool = pj_pool_create_on_buf("no name", buf, sizeof(buf));
329 PJ_TEST_NOT_NULL(pool, NULL, return -70);
330
331 /* Drain the pool */
332 PJ_TRY {
333 PJ_TEST_NOT_NULL(pj_pool_alloc(pool, STATIC_BUF_SIZE/2), NULL, return -75);
334 PJ_TEST_NOT_NULL(pj_pool_alloc(pool, STATIC_BUF_SIZE/2), NULL, return -76);
335 }
336 PJ_CATCH_ANY {
337 PJ_TEST_TRUE(PJ_FALSE, "Caught exception", return -77);
338 }
339 PJ_END;
340
341 /* On the next alloc, exception should be thrown */
342 PJ_TRY {
343 p = pj_pool_alloc(pool, STATIC_BUF_SIZE);
344 PJ_TEST_TRUE(p==NULL, NULL, return -78);
345 }
346 PJ_CATCH_ANY {
347 /* This is the expected result */
348 }
349 PJ_END;
350
351 /* Done */
352 return 0;
353 }
354
355
356 int pool_test(void)
357 {
358 enum { LOOP = 2 };
359 int rc;
360 int loop;
361
362 #if PJ_HAS_POOL_ALT_API == 0
363 rc = capacity_test();
364 if (rc) return rc;
365 #endif //PJ_HAS_POOL_ALT_API == 0
366
367 rc = pool_alignment_test();
368 if (rc) return rc;
369
370 rc = pool_buf_alignment_test();
371 if (rc) return rc;
372
373 #if PJ_HAS_POOL_ALT_API == 0
374 for (loop=0; loop<LOOP; ++loop) {
375 /* Test that the pool should grow automaticly. */
376 rc = drain_test(SIZE, SIZE);
377 if (rc != 0) return rc;
378
379 /* Test situation where pool is not allowed to grow.
380 * We expect the test to return correct error.
381 */
382 rc = drain_test(SIZE, 0);
383 if (rc != -40) return rc;
384 }
385
386 rc = pool_buf_test();
387 if (rc != 0)
388 return rc;
389 #endif //PJ_HAS_POOL_ALT_API == 0
390
391 PJ_UNUSED_ARG(loop);
392 return 0;
393 }
394
395 #else
396 /* To prevent warning about "translation unit is empty"
397 * when this test is disabled.
398 */
399 int dummy_pool_test;
400 #endif /* INCLUDE_POOL_TEST */
401