MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / mem_alloc_test

Function mem_alloc_test

src/utest/mem_tc.c:302–416  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

300#define MEM_RANG_ALLOC_TEST_TIME 5
301
302static 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;

Callers

nothing calls this directly

Calls 15

rt_list_initFunction · 0.85
rt_tick_getFunction · 0.85
rt_tick_from_millisecondFunction · 0.85
rt_mallocFunction · 0.85
rt_memsetFunction · 0.85
rt_smem_initFunction · 0.85
max_blockFunction · 0.85
rt_kprintfFunction · 0.85
rt_smem_allocFunction · 0.85
rt_list_removeFunction · 0.85
rt_smem_freeFunction · 0.85
rt_list_insert_afterFunction · 0.85

Tested by

no test coverage detected