| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 106 |
|
| 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); |
| 114 |
PJ_TEST_NOT_NULL(ptr, NULL, { rc=-302; goto on_return; }); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
capacity = pj_pool_get_capacity(pool); |
| 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 |
|
| 129 |
|
| 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 |
|
| 154 |
if (i % 2) |
| 155 |
{ |
| 156 |
|
| 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 |
|
| 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 |
|
| 167 |
ptr = pj_pool_alloc(pool, 1); |
| 168 |
PJ_TEST_TRUE(IS_ALIGNED(ptr), NULL, { rc=-320; goto on_return; }); |
| 169 |
|
| 170 |
|
| 171 |
ptr = pj_pool_alloc(pool2, 1); |
| 172 |
PJ_TEST_TRUE(IS_ALIGNED2(ptr), NULL, { rc=-325; goto on_return; }); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
ptr = pj_pool_alloc(pool, 1); |
| 177 |
PJ_TEST_TRUE(IS_ALIGNED(ptr), NULL, { rc=-330; goto on_return; }); |
| 178 |
|
| 179 |
|
| 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 |
|
| 184 |
ptr = pj_pool_alloc(pool2, 1); |
| 185 |
PJ_TEST_TRUE(IS_ALIGNED2(ptr), NULL, { rc=-340; goto on_return; }); |
| 186 |
|
| 187 |
|
| 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 |
|
| 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 |
|
| 206 |
pj_pool_reset(pool); |
| 207 |
pj_pool_reset(pool2); |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 254 |
pj_pool_reset(pool); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
on_return: |
| 259 |
pj_pool_release(pool); |
| 260 |
|
| 261 |
return rc; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 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 |
|
| 280 |
freesize = GET_FREE(pool); |
| 281 |
PJ_TEST_GTE(freesize, 1, NULL, { status=-15; goto on_error;}); |
| 282 |
|
| 283 |
|
| 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 |
|
| 300 |
PJ_TEST_EQ(GET_FREE(pool), 0, NULL, { status=-30; goto on_error;}); |
| 301 |
|
| 302 |
|
| 303 |
p = pj_pool_alloc(pool, 257); |
| 304 |
PJ_TEST_NOT_NULL(p, NULL, { status=-40; goto on_error;}); |
| 305 |
|
| 306 |
|
| 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 |
|
| 316 |
static int pool_buf_test(void) |
| 317 |
{ |
| 318 |
enum { STATIC_BUF_SIZE = 40 }; |
| 319 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 348 |
} |
| 349 |
PJ_END; |
| 350 |
|
| 351 |
|
| 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 |
| 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 |
|
| 376 |
rc = drain_test(SIZE, SIZE); |
| 377 |
if (rc != 0) return rc; |
| 378 |
|
| 379 |
|
| 380 |
|
| 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 |
| 390 |
|
| 391 |
PJ_UNUSED_ARG(loop); |
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
#else |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
int dummy_pool_test; |
| 400 |
#endif |
| 401 |
|