mem_tc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-10-14 tyx the first version
  9. */
  10. #include <rtthread.h>
  11. #include <stdlib.h>
  12. #include "utest.h"
  13. struct rt_small_mem_item
  14. {
  15. rt_ubase_t pool_ptr; /**< small memory object addr */
  16. rt_size_t next; /**< next free item */
  17. rt_size_t prev; /**< prev free item */
  18. #ifdef RT_USING_MEMTRACE
  19. #ifdef ARCH_CPU_64BIT
  20. rt_uint8_t thread[8]; /**< thread name */
  21. #else
  22. rt_uint8_t thread[4]; /**< thread name */
  23. #endif /* ARCH_CPU_64BIT */
  24. #endif /* RT_USING_MEMTRACE */
  25. };
  26. struct rt_small_mem
  27. {
  28. struct rt_memory parent; /**< inherit from rt_memory */
  29. rt_uint8_t *heap_ptr; /**< pointer to the heap */
  30. struct rt_small_mem_item *heap_end;
  31. struct rt_small_mem_item *lfree;
  32. rt_size_t mem_size_aligned; /**< aligned memory size */
  33. };
  34. #define MEM_SIZE(_heap, _mem) \
  35. (((struct rt_small_mem_item *)(_mem))->next - ((rt_ubase_t)(_mem) - \
  36. (rt_ubase_t)((_heap)->heap_ptr)) - RT_ALIGN(sizeof(struct rt_small_mem_item), RT_ALIGN_SIZE))
  37. #define TEST_MEM_SIZE 1024
  38. static rt_size_t max_block(struct rt_small_mem *heap)
  39. {
  40. struct rt_small_mem_item *mem;
  41. rt_size_t max = 0, size;
  42. for (mem = (struct rt_small_mem_item *)heap->heap_ptr;
  43. mem != heap->heap_end;
  44. mem = (struct rt_small_mem_item *)&heap->heap_ptr[mem->next])
  45. {
  46. if (((rt_ubase_t)mem->pool_ptr & 0x1) == 0)
  47. {
  48. size = MEM_SIZE(heap, mem);
  49. if (size > max)
  50. {
  51. max = size;
  52. }
  53. }
  54. }
  55. return max;
  56. }
  57. static int _mem_cmp(void *ptr, rt_uint8_t v, rt_size_t size)
  58. {
  59. while (size-- != 0)
  60. {
  61. if (*(rt_uint8_t *)ptr != v)
  62. return *(rt_uint8_t *)ptr - v;
  63. }
  64. return 0;
  65. }
  66. struct mem_test_context
  67. {
  68. void *ptr;
  69. rt_size_t size;
  70. rt_uint8_t magic;
  71. };
  72. static void mem_functional_test(void)
  73. {
  74. rt_size_t total_size;
  75. rt_uint8_t *buf;
  76. struct rt_small_mem *heap;
  77. rt_uint8_t magic = __LINE__;
  78. /* Prepare test memory */
  79. buf = rt_malloc(TEST_MEM_SIZE);
  80. uassert_not_null(buf);
  81. uassert_int_equal(RT_ALIGN((rt_ubase_t)buf, RT_ALIGN_SIZE), (rt_ubase_t)buf);
  82. rt_memset(buf, 0xAA, TEST_MEM_SIZE);
  83. /* small heap init */
  84. heap = (struct rt_small_mem *)rt_smem_init("mem_tc", buf, TEST_MEM_SIZE);
  85. /* get total size */
  86. total_size = max_block(heap);
  87. uassert_int_not_equal(total_size, 0);
  88. /*
  89. * Allocate all memory at a time and test whether
  90. * the memory allocation release function is effective
  91. */
  92. {
  93. struct mem_test_context ctx;
  94. ctx.magic = magic++;
  95. ctx.size = max_block(heap);
  96. ctx.ptr = rt_smem_alloc(&heap->parent, ctx.size);
  97. uassert_not_null(ctx.ptr);
  98. rt_memset(ctx.ptr, ctx.magic, ctx.size);
  99. uassert_int_equal(_mem_cmp(ctx.ptr, ctx.magic, ctx.size), 0);
  100. rt_smem_free(ctx.ptr);
  101. uassert_int_equal(max_block(heap), total_size);
  102. }
  103. /*
  104. * Apply for memory release sequentially and
  105. * test whether memory block merging is effective
  106. */
  107. {
  108. rt_size_t i, max_free = 0;
  109. struct mem_test_context ctx[3];
  110. /* alloc mem */
  111. for (i = 0; i < sizeof(ctx) / sizeof(ctx[0]); i++)
  112. {
  113. ctx[i].magic = magic++;
  114. ctx[i].size = max_block(heap) / (sizeof(ctx) / sizeof(ctx[0]) - i);
  115. ctx[i].ptr = rt_smem_alloc(&heap->parent, ctx[i].size);
  116. uassert_not_null(ctx[i].ptr);
  117. rt_memset(ctx[i].ptr, ctx[i].magic, ctx[i].size);
  118. }
  119. /* All memory has been applied. The remaining memory should be 0 */
  120. uassert_int_equal(max_block(heap), 0);
  121. /* Verify that the memory data is correct */
  122. for (i = 0; i < sizeof(ctx) / sizeof(ctx[0]); i++)
  123. {
  124. uassert_int_equal(_mem_cmp(ctx[i].ptr, ctx[i].magic, ctx[i].size), 0);
  125. }
  126. /* Sequential memory release */
  127. for (i = 0; i < sizeof(ctx) / sizeof(ctx[0]); i++)
  128. {
  129. uassert_int_equal(_mem_cmp(ctx[i].ptr, ctx[i].magic, ctx[i].size), 0);
  130. rt_smem_free(ctx[i].ptr);
  131. max_free += ctx[i].size;
  132. uassert_true(max_block(heap) >= max_free);
  133. }
  134. /* Check whether the memory is fully merged */
  135. uassert_int_equal(max_block(heap), total_size);
  136. }
  137. /*
  138. * Apply for memory release at an interval to
  139. * test whether memory block merging is effective
  140. */
  141. {
  142. rt_size_t i, max_free = 0;
  143. struct mem_test_context ctx[3];
  144. /* alloc mem */
  145. for (i = 0; i < sizeof(ctx) / sizeof(ctx[0]); i++)
  146. {
  147. ctx[i].magic = magic++;
  148. ctx[i].size = max_block(heap) / (sizeof(ctx) / sizeof(ctx[0]) - i);
  149. ctx[i].ptr = rt_smem_alloc(&heap->parent, ctx[i].size);
  150. uassert_not_null(ctx[i].ptr);
  151. rt_memset(ctx[i].ptr, ctx[i].magic, ctx[i].size);
  152. }
  153. /* All memory has been applied. The remaining memory should be 0 */
  154. uassert_int_equal(max_block(heap), 0);
  155. /* Verify that the memory data is correct */
  156. for (i = 0; i < sizeof(ctx) / sizeof(ctx[0]); i++)
  157. {
  158. uassert_int_equal(_mem_cmp(ctx[i].ptr, ctx[i].magic, ctx[i].size), 0);
  159. }
  160. /* Release even address */
  161. for (i = 0; i < sizeof(ctx) / sizeof(ctx[0]); i++)
  162. {
  163. if (i % 2 == 0)
  164. {
  165. uassert_int_equal(_mem_cmp(ctx[i].ptr, ctx[i].magic, ctx[i].size), 0);
  166. rt_smem_free(ctx[i].ptr);
  167. uassert_true(max_block(heap) >= ctx[0].size);
  168. }
  169. }
  170. /* Release odd addresses and merge memory blocks */
  171. for (i = 0; i < sizeof(ctx) / sizeof(ctx[0]); i++)
  172. {
  173. if (i % 2 != 0)
  174. {
  175. uassert_int_equal(_mem_cmp(ctx[i].ptr, ctx[i].magic, ctx[i].size), 0);
  176. rt_smem_free(ctx[i].ptr);
  177. max_free += ctx[i - 1].size + ctx[i + 1].size;
  178. uassert_true(max_block(heap) >= max_free);
  179. }
  180. }
  181. /* Check whether the memory is fully merged */
  182. uassert_int_equal(max_block(heap), total_size);
  183. }
  184. /* mem realloc test,Small - > Large */
  185. {
  186. /* Request a piece of memory for subsequent reallocation operations */
  187. struct mem_test_context ctx[3];
  188. ctx[0].magic = magic++;
  189. ctx[0].size = max_block(heap) / 3;
  190. ctx[0].ptr = rt_smem_alloc(&heap->parent, ctx[0].size);
  191. uassert_not_null(ctx[0].ptr);
  192. rt_memset(ctx[0].ptr, ctx[0].magic, ctx[0].size);
  193. /* Apply for a small piece of memory and split the continuous memory */
  194. ctx[1].magic = magic++;
  195. ctx[1].size = RT_ALIGN_SIZE;
  196. ctx[1].ptr = rt_smem_alloc(&heap->parent, ctx[1].size);
  197. uassert_not_null(ctx[1].ptr);
  198. rt_memset(ctx[1].ptr, ctx[1].magic, ctx[1].size);
  199. /* Check whether the maximum memory block is larger than the first piece of memory */
  200. uassert_true(max_block(heap) > ctx[0].size);
  201. /* Reallocate the first piece of memory */
  202. ctx[2].magic = magic++;
  203. ctx[2].size = max_block(heap);
  204. ctx[2].ptr = rt_smem_realloc(&heap->parent, ctx[0].ptr, ctx[2].size);
  205. uassert_not_null(ctx[2].ptr);
  206. uassert_int_not_equal(ctx[0].ptr, ctx[2].ptr);
  207. uassert_int_equal(_mem_cmp(ctx[2].ptr, ctx[0].magic, ctx[0].size), 0);
  208. rt_memset(ctx[2].ptr, ctx[2].magic, ctx[2].size);
  209. /* Free the second piece of memory */
  210. uassert_int_equal(_mem_cmp(ctx[1].ptr, ctx[1].magic, ctx[1].size), 0);
  211. rt_smem_free(ctx[1].ptr);
  212. /* Free reallocated memory */
  213. uassert_int_equal(_mem_cmp(ctx[2].ptr, ctx[2].magic, ctx[2].size), 0);
  214. rt_smem_free(ctx[2].ptr);
  215. /* Check memory integrity */
  216. uassert_int_equal(max_block(heap), total_size);
  217. }
  218. /* mem realloc test,Large - > Small */
  219. {
  220. rt_size_t max_free;
  221. struct mem_test_context ctx;
  222. /* alloc a piece of memory */
  223. ctx.magic = magic++;
  224. ctx.size = max_block(heap) / 2;
  225. ctx.ptr = rt_smem_alloc(&heap->parent, ctx.size);
  226. uassert_not_null(ctx.ptr);
  227. rt_memset(ctx.ptr, ctx.magic, ctx.size);
  228. uassert_int_equal(_mem_cmp(ctx.ptr, ctx.magic, ctx.size), 0);
  229. /* Get remaining memory */
  230. max_free = max_block(heap);
  231. /* Change memory size */
  232. ctx.size = ctx.size / 2;
  233. uassert_int_equal((rt_ubase_t)rt_smem_realloc(&heap->parent, ctx.ptr, ctx.size), (rt_ubase_t)ctx.ptr);
  234. /* Get remaining size */
  235. uassert_true(max_block(heap) > max_free);
  236. /* Free memory */
  237. uassert_int_equal(_mem_cmp(ctx.ptr, ctx.magic, ctx.size), 0);
  238. rt_smem_free(ctx.ptr);
  239. /* Check memory integrity */
  240. uassert_int_equal(max_block(heap), total_size);
  241. }
  242. /* mem realloc test,equal */
  243. {
  244. rt_size_t max_free;
  245. struct mem_test_context ctx;
  246. /* alloc a piece of memory */
  247. ctx.magic = magic++;
  248. ctx.size = max_block(heap) / 2;
  249. ctx.ptr = rt_smem_alloc(&heap->parent, ctx.size);
  250. uassert_not_null(ctx.ptr);
  251. rt_memset(ctx.ptr, ctx.magic, ctx.size);
  252. uassert_int_equal(_mem_cmp(ctx.ptr, ctx.magic, ctx.size), 0);
  253. /* Get remaining memory */
  254. max_free = max_block(heap);
  255. /* Do not change memory size */
  256. uassert_int_equal((rt_ubase_t)rt_smem_realloc(&heap->parent, ctx.ptr, ctx.size), (rt_ubase_t)ctx.ptr);
  257. /* Get remaining size */
  258. uassert_true(max_block(heap) == max_free);
  259. /* Free memory */
  260. uassert_int_equal(_mem_cmp(ctx.ptr, ctx.magic, ctx.size), 0);
  261. rt_smem_free(ctx.ptr);
  262. /* Check memory integrity */
  263. uassert_int_equal(max_block(heap), total_size);
  264. }
  265. /* small heap deinit */
  266. rt_smem_detach(&heap->parent);
  267. /* release test resources */
  268. rt_free(buf);
  269. }
  270. struct mem_alloc_context
  271. {
  272. rt_list_t node;
  273. rt_size_t size;
  274. rt_uint8_t magic;
  275. };
  276. struct mem_alloc_head
  277. {
  278. rt_list_t list;
  279. rt_size_t count;
  280. rt_tick_t start;
  281. rt_tick_t end;
  282. rt_tick_t interval;
  283. };
  284. #define MEM_RANG_ALLOC_BLK_MIN 2
  285. #define MEM_RANG_ALLOC_BLK_MAX 5
  286. #define MEM_RANG_ALLOC_TEST_TIME 5
  287. static void mem_alloc_test(void)
  288. {
  289. struct mem_alloc_head head;
  290. rt_uint8_t *buf;
  291. struct rt_small_mem *heap;
  292. rt_size_t total_size, size;
  293. struct mem_alloc_context *ctx;
  294. /* init */
  295. rt_list_init(&head.list);
  296. head.count = 0;
  297. head.start = rt_tick_get();
  298. head.end = rt_tick_get() + rt_tick_from_millisecond(MEM_RANG_ALLOC_TEST_TIME * 1000);
  299. head.interval = (head.end - head.start) / 20;
  300. buf = rt_malloc(TEST_MEM_SIZE);
  301. uassert_not_null(buf);
  302. uassert_int_equal(RT_ALIGN((rt_ubase_t)buf, RT_ALIGN_SIZE), (rt_ubase_t)buf);
  303. rt_memset(buf, 0xAA, TEST_MEM_SIZE);
  304. heap = (struct rt_small_mem *)rt_smem_init("mem_tc", buf, TEST_MEM_SIZE);
  305. total_size = max_block(heap);
  306. uassert_int_not_equal(total_size, 0);
  307. /* test run */
  308. while (head.end - head.start < RT_TICK_MAX / 2)
  309. {
  310. if (rt_tick_get() - head.start >= head.interval)
  311. {
  312. head.start = rt_tick_get();
  313. rt_kprintf("#");
  314. }
  315. /* %60 probability to perform alloc operation */
  316. if (rand() % 10 >= 4)
  317. {
  318. size = rand() % MEM_RANG_ALLOC_BLK_MAX + MEM_RANG_ALLOC_BLK_MIN;
  319. size *= sizeof(struct mem_alloc_context);
  320. ctx = rt_smem_alloc(&heap->parent, size);
  321. if (ctx == RT_NULL)
  322. {
  323. if (head.count == 0)
  324. {
  325. break;
  326. }
  327. size = head.count / 2;
  328. while (size != head.count)
  329. {
  330. ctx = rt_list_first_entry(&head.list, struct mem_alloc_context, node);
  331. rt_list_remove(&ctx->node);
  332. if (ctx->size > sizeof(*ctx))
  333. {
  334. if (_mem_cmp(&ctx[1], ctx->magic, ctx->size - sizeof(*ctx)) != 0)
  335. {
  336. uassert_true(0);
  337. }
  338. }
  339. rt_memset(ctx, 0xAA, ctx->size);
  340. rt_smem_free(ctx);
  341. head.count --;
  342. }
  343. continue;
  344. }
  345. if (RT_ALIGN((rt_ubase_t)ctx, RT_ALIGN_SIZE) != (rt_ubase_t)ctx)
  346. {
  347. uassert_int_equal(RT_ALIGN((rt_ubase_t)ctx, RT_ALIGN_SIZE), (rt_ubase_t)ctx);
  348. }
  349. rt_memset(ctx, 0, size);
  350. rt_list_init(&ctx->node);
  351. ctx->size = size;
  352. ctx->magic = rand() & 0xff;
  353. if (ctx->size > sizeof(*ctx))
  354. {
  355. rt_memset(&ctx[1], ctx->magic, ctx->size - sizeof(*ctx));
  356. }
  357. rt_list_insert_after(&head.list, &ctx->node);
  358. head.count += 1;
  359. }
  360. else
  361. {
  362. if (!rt_list_isempty(&head.list))
  363. {
  364. ctx = rt_list_first_entry(&head.list, struct mem_alloc_context, node);
  365. rt_list_remove(&ctx->node);
  366. if (ctx->size > sizeof(*ctx))
  367. {
  368. if (_mem_cmp(&ctx[1], ctx->magic, ctx->size - sizeof(*ctx)) != 0)
  369. {
  370. uassert_true(0);
  371. }
  372. }
  373. rt_memset(ctx, 0xAA, ctx->size);
  374. rt_smem_free(ctx);
  375. head.count --;
  376. }
  377. }
  378. }
  379. while (!rt_list_isempty(&head.list))
  380. {
  381. ctx = rt_list_first_entry(&head.list, struct mem_alloc_context, node);
  382. rt_list_remove(&ctx->node);
  383. if (ctx->size > sizeof(*ctx))
  384. {
  385. if (_mem_cmp(&ctx[1], ctx->magic, ctx->size - sizeof(*ctx)) != 0)
  386. {
  387. uassert_true(0);
  388. }
  389. }
  390. rt_memset(ctx, 0xAA, ctx->size);
  391. rt_smem_free(ctx);
  392. head.count --;
  393. }
  394. uassert_int_equal(head.count, 0);
  395. uassert_int_equal(max_block(heap), total_size);
  396. /* small heap deinit */
  397. rt_smem_detach(&heap->parent);
  398. /* release test resources */
  399. rt_free(buf);
  400. }
  401. #define MEM_RANG_REALLOC_BLK_MIN 0
  402. #define MEM_RANG_REALLOC_BLK_MAX 5
  403. #define MEM_RANG_REALLOC_TEST_TIME 5
  404. struct mem_realloc_context
  405. {
  406. rt_size_t size;
  407. rt_uint8_t magic;
  408. };
  409. struct mem_realloc_head
  410. {
  411. struct mem_realloc_context **ctx_tab;
  412. rt_size_t count;
  413. rt_tick_t start;
  414. rt_tick_t end;
  415. rt_tick_t interval;
  416. };
  417. static void mem_realloc_test(void)
  418. {
  419. struct mem_realloc_head head;
  420. rt_uint8_t *buf;
  421. struct rt_small_mem *heap;
  422. rt_size_t total_size, size, idx;
  423. struct mem_realloc_context *ctx;
  424. int res;
  425. size = RT_ALIGN(sizeof(struct mem_realloc_context), RT_ALIGN_SIZE) + RT_ALIGN_SIZE;
  426. size = TEST_MEM_SIZE / size;
  427. /* init */
  428. head.ctx_tab = RT_NULL;
  429. head.count = size;
  430. head.start = rt_tick_get();
  431. head.end = rt_tick_get() + rt_tick_from_millisecond(MEM_RANG_ALLOC_TEST_TIME * 1000);
  432. head.interval = (head.end - head.start) / 20;
  433. buf = rt_malloc(TEST_MEM_SIZE);
  434. uassert_not_null(buf);
  435. uassert_int_equal(RT_ALIGN((rt_ubase_t)buf, RT_ALIGN_SIZE), (rt_ubase_t)buf);
  436. rt_memset(buf, 0xAA, TEST_MEM_SIZE);
  437. heap = (struct rt_small_mem *)rt_smem_init("mem_tc", buf, TEST_MEM_SIZE);
  438. total_size = max_block(heap);
  439. uassert_int_not_equal(total_size, 0);
  440. /* init ctx tab */
  441. size = head.count * sizeof(struct mem_realloc_context *);
  442. head.ctx_tab = rt_smem_alloc(&heap->parent, size);
  443. uassert_not_null(head.ctx_tab);
  444. rt_memset(head.ctx_tab, 0, size);
  445. /* test run */
  446. while (head.end - head.start < RT_TICK_MAX / 2)
  447. {
  448. if (rt_tick_get() - head.start >= head.interval)
  449. {
  450. head.start = rt_tick_get();
  451. rt_kprintf("#");
  452. }
  453. size = rand() % MEM_RANG_ALLOC_BLK_MAX + MEM_RANG_ALLOC_BLK_MIN;
  454. size *= sizeof(struct mem_realloc_context);
  455. idx = rand() % head.count;
  456. ctx = rt_smem_realloc(&heap->parent, head.ctx_tab[idx], size);
  457. if (ctx == RT_NULL)
  458. {
  459. if (size == 0)
  460. {
  461. if (head.ctx_tab[idx])
  462. {
  463. head.ctx_tab[idx] = RT_NULL;
  464. }
  465. }
  466. else
  467. {
  468. for (idx = 0; idx < head.count; idx++)
  469. {
  470. ctx = head.ctx_tab[idx];
  471. if (rand() % 2 && ctx)
  472. {
  473. if (ctx->size > sizeof(*ctx))
  474. {
  475. res = _mem_cmp(&ctx[1], ctx->magic, ctx->size - sizeof(*ctx));
  476. if (res != 0)
  477. {
  478. uassert_int_equal(res, 0);
  479. }
  480. }
  481. rt_memset(ctx, 0xAA, ctx->size);
  482. rt_smem_realloc(&heap->parent, ctx, 0);
  483. head.ctx_tab[idx] = RT_NULL;
  484. }
  485. }
  486. }
  487. continue;
  488. }
  489. /* check mem */
  490. if (head.ctx_tab[idx] != RT_NULL)
  491. {
  492. res = 0;
  493. if (ctx->size < size)
  494. {
  495. if (ctx->size > sizeof(*ctx))
  496. {
  497. res = _mem_cmp(&ctx[1], ctx->magic, ctx->size - sizeof(*ctx));
  498. }
  499. }
  500. else
  501. {
  502. if (size > sizeof(*ctx))
  503. {
  504. res = _mem_cmp(&ctx[1], ctx->magic, size - sizeof(*ctx));
  505. }
  506. }
  507. if (res != 0)
  508. {
  509. uassert_int_equal(res, 0);
  510. }
  511. }
  512. /* init mem */
  513. ctx->magic = rand() & 0xff;
  514. ctx->size = size;
  515. if (ctx->size > sizeof(*ctx))
  516. {
  517. rt_memset(&ctx[1], ctx->magic, ctx->size - sizeof(*ctx));
  518. }
  519. head.ctx_tab[idx] = ctx;
  520. }
  521. /* free all mem */
  522. for (idx = 0; idx < head.count; idx++)
  523. {
  524. ctx = head.ctx_tab[idx];
  525. if (ctx == RT_NULL)
  526. {
  527. continue;
  528. }
  529. if (ctx->size > sizeof(*ctx))
  530. {
  531. res = _mem_cmp(&ctx[1], ctx->magic, ctx->size - sizeof(*ctx));
  532. if (res != 0)
  533. {
  534. uassert_int_equal(res, 0);
  535. }
  536. }
  537. rt_memset(ctx, 0xAA, ctx->size);
  538. rt_smem_realloc(&heap->parent, ctx, 0);
  539. head.ctx_tab[idx] = RT_NULL;
  540. }
  541. uassert_int_not_equal(max_block(heap), total_size);
  542. /* small heap deinit */
  543. rt_smem_detach(&heap->parent);
  544. /* release test resources */
  545. rt_free(buf);
  546. }
  547. static rt_err_t utest_tc_init(void)
  548. {
  549. return RT_EOK;
  550. }
  551. static rt_err_t utest_tc_cleanup(void)
  552. {
  553. return RT_EOK;
  554. }
  555. static void testcase(void)
  556. {
  557. UTEST_UNIT_RUN(mem_functional_test);
  558. UTEST_UNIT_RUN(mem_alloc_test);
  559. UTEST_UNIT_RUN(mem_realloc_test);
  560. }
  561. UTEST_TC_EXPORT(testcase, "testcases.kernel.mem_tc", utest_tc_init, utest_tc_cleanup, 20);