From 08c7d0e38e594b829405fdcf2f5d212ee5d4d05c Mon Sep 17 00:00:00 2001 From: Yiheng Wu <36156959+jingkaimori@users.noreply.github.com> Date: Sun, 3 Dec 2023 21:50:18 +0800 Subject: [PATCH] [15_1] test of memory leakage in certain case --- System/Memory/fast_alloc.cpp | 4 +++ tests/System/Memory/fast_alloc_test.cpp | 40 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/System/Memory/fast_alloc.cpp b/System/Memory/fast_alloc.cpp index 1d5de7603..bd875c896 100644 --- a/System/Memory/fast_alloc.cpp +++ b/System/Memory/fast_alloc.cpp @@ -46,6 +46,10 @@ safe_malloc (size_t sz) { void* enlarge_malloc (size_t sz) { if (alloc_remains < sz) { + if (alloc_remains > 0) { + ind (alloc_mem) = alloc_ptr (alloc_remains); + alloc_ptr (alloc_remains)= alloc_mem; + } alloc_mem= (char*) safe_malloc (BLOCK_SIZE); #ifdef DEBUG_ON alloc_mem_top= alloc_mem_top >= alloc_mem + BLOCK_SIZE diff --git a/tests/System/Memory/fast_alloc_test.cpp b/tests/System/Memory/fast_alloc_test.cpp index dae74d46e..8503ce540 100644 --- a/tests/System/Memory/fast_alloc_test.cpp +++ b/tests/System/Memory/fast_alloc_test.cpp @@ -5,6 +5,7 @@ struct Complex { public: double re, im; Complex (double re_, double im_) : re (re_), im (im_) {} + Complex () : re (0.5), im (1.0) {} ~Complex () {} }; @@ -74,4 +75,43 @@ TEST_CASE ("test class") { tm_delete (p_complex); } +TEST_CASE ("test tm_*_array") { + uint8_t* p_complex= tm_new_array (100); + tm_delete_array (p_complex); +#ifdef OS_WASM + const size_t size_prim= 200, size_complex= 100; +#else + const size_t size_prim= 20000000, size_complex= 5000000; +#endif + p_complex= tm_new_array (size_prim); + tm_delete_array (p_complex); + Complex* p_wide= tm_new_array (size_complex); + tm_delete_array (p_wide); +} + +#ifndef OS_WASM +TEST_CASE ("test large bunch of tm_*") { + const int bnum= 100000; + int* volume[bnum]; + for (int i= 0; i < bnum; i++) { + volume[i]= tm_new (35); + } + for (int i= 0; i < bnum; i++) { + tm_delete (volume[i]); + } +} +TEST_MEMORY_LEAK_ALL +TEST_MEMORY_LEAK_RESET +#endif + +TEST_CASE ("test large bunch of tm_*_array with class") { + Complex* volume[NUM]; + for (int i= 0; i < NUM; i++) { + volume[i]= tm_new_array (9); + } + for (int i= 0; i < NUM; i++) { + tm_delete_array (volume[i]); + } +} + TEST_MEMORY_LEAK_ALL