| 45 | #define SLAB_RANG_ALLOC_TEST_TIME 5 |
| 46 | |
| 47 | static void slab_alloc_test(void) |
| 48 | { |
| 49 | struct slab_alloc_head head; |
| 50 | rt_uint8_t *buf; |
| 51 | rt_slab_t heap; |
| 52 | rt_size_t size; |
| 53 | struct slab_alloc_context *ctx; |
| 54 | |
| 55 | /* init */ |
| 56 | rt_list_init(&head.list); |
| 57 | head.count = 0; |
| 58 | head.start = rt_tick_get(); |
| 59 | head.end = rt_tick_get() + rt_tick_from_millisecond(SLAB_RANG_ALLOC_TEST_TIME * 1000); |
| 60 | head.interval = (head.end - head.start) / 20; |
| 61 | buf = rt_malloc(TEST_SLAB_SIZE); |
| 62 | uassert_not_null(buf); |
| 63 | uassert_int_equal(RT_ALIGN((rt_ubase_t)buf, RT_ALIGN_SIZE), (rt_ubase_t)buf); |
| 64 | rt_memset(buf, 0xAA, TEST_SLAB_SIZE); |
| 65 | heap = rt_slab_init("slab_tc", buf, TEST_SLAB_SIZE); |
| 66 | // test run |
| 67 | while (head.end - head.start < RT_TICK_MAX / 2) |
| 68 | { |
| 69 | if (rt_tick_get() - head.start >= head.interval) |
| 70 | { |
| 71 | head.start = rt_tick_get(); |
| 72 | rt_kprintf("#"); |
| 73 | } |
| 74 | // %60 probability to perform alloc operation |
| 75 | if (rand() % 10 >= 4) |
| 76 | { |
| 77 | size = rand() % SLAB_RANG_ALLOC_BLK_MAX + SLAB_RANG_ALLOC_BLK_MIN; |
| 78 | size *= sizeof(struct slab_alloc_context); |
| 79 | ctx = rt_slab_alloc(heap, size); |
| 80 | if (ctx == RT_NULL) |
| 81 | { |
| 82 | if (head.count == 0) |
| 83 | { |
| 84 | break; |
| 85 | } |
| 86 | size = head.count / 2; |
| 87 | while (size != head.count) |
| 88 | { |
| 89 | ctx = rt_list_first_entry(&head.list, struct slab_alloc_context, node); |
| 90 | rt_list_remove(&ctx->node); |
| 91 | if (ctx->size > sizeof(*ctx)) |
| 92 | { |
| 93 | if (_mem_cmp(&ctx[1], ctx->magic, ctx->size - sizeof(*ctx)) != 0) |
| 94 | { |
| 95 | uassert_true(0); |
| 96 | } |
| 97 | } |
| 98 | rt_memset(ctx, 0xAA, ctx->size); |
| 99 | rt_slab_free(heap, ctx); |
| 100 | head.count --; |
| 101 | } |
| 102 | continue; |
| 103 | } |
| 104 | //if (RT_ALIGN((rt_ubase_t)ctx, RT_ALIGN_SIZE) != (rt_ubase_t)ctx) |
nothing calls this directly
no test coverage detected