| 300 | #define MEM_RANG_ALLOC_TEST_TIME 5 |
| 301 | |
| 302 | static void mem_alloc_test(void) |
| 303 | { |
| 304 | struct mem_alloc_head head; |
| 305 | rt_uint8_t *buf; |
| 306 | struct rt_small_mem *heap; |
| 307 | rt_size_t total_size, size; |
| 308 | struct mem_alloc_context *ctx; |
| 309 | |
| 310 | /* init */ |
| 311 | rt_list_init(&head.list); |
| 312 | head.count = 0; |
| 313 | head.start = rt_tick_get(); |
| 314 | head.end = rt_tick_get() + rt_tick_from_millisecond(MEM_RANG_ALLOC_TEST_TIME * 1000); |
| 315 | head.interval = (head.end - head.start) / 20; |
| 316 | buf = rt_malloc(TEST_MEM_SIZE); |
| 317 | uassert_not_null(buf); |
| 318 | uassert_int_equal(RT_ALIGN((rt_ubase_t)buf, RT_ALIGN_SIZE), (rt_ubase_t)buf); |
| 319 | rt_memset(buf, 0xAA, TEST_MEM_SIZE); |
| 320 | heap = (struct rt_small_mem *)rt_smem_init("mem_tc", buf, TEST_MEM_SIZE); |
| 321 | total_size = max_block(heap); |
| 322 | uassert_int_not_equal(total_size, 0); |
| 323 | /* test run */ |
| 324 | while (head.end - head.start < RT_TICK_MAX / 2) |
| 325 | { |
| 326 | if (rt_tick_get() - head.start >= head.interval) |
| 327 | { |
| 328 | head.start = rt_tick_get(); |
| 329 | rt_kprintf("#"); |
| 330 | } |
| 331 | /* %60 probability to perform alloc operation */ |
| 332 | if (rand() % 10 >= 4) |
| 333 | { |
| 334 | size = rand() % MEM_RANG_ALLOC_BLK_MAX + MEM_RANG_ALLOC_BLK_MIN; |
| 335 | size *= sizeof(struct mem_alloc_context); |
| 336 | ctx = rt_smem_alloc(&heap->parent, size); |
| 337 | if (ctx == RT_NULL) |
| 338 | { |
| 339 | if (head.count == 0) |
| 340 | { |
| 341 | break; |
| 342 | } |
| 343 | size = head.count / 2; |
| 344 | while (size != head.count) |
| 345 | { |
| 346 | ctx = rt_list_first_entry(&head.list, struct mem_alloc_context, node); |
| 347 | rt_list_remove(&ctx->node); |
| 348 | if (ctx->size > sizeof(*ctx)) |
| 349 | { |
| 350 | if (_mem_cmp(&ctx[1], ctx->magic, ctx->size - sizeof(*ctx)) != 0) |
| 351 | { |
| 352 | uassert_true(0); |
| 353 | } |
| 354 | } |
| 355 | rt_memset(ctx, 0xAA, ctx->size); |
| 356 | rt_smem_free(ctx); |
| 357 | head.count --; |
| 358 | } |
| 359 | continue; |
nothing calls this directly
no test coverage detected