Skip to content

Commit

Permalink
[15_1] test of memory leakage in certain case
Browse files Browse the repository at this point in the history
  • Loading branch information
jingkaimori authored Dec 3, 2023
1 parent 3c0124c commit 08c7d0e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions System/Memory/fast_alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions tests/System/Memory/fast_alloc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {}
};

Expand Down Expand Up @@ -74,4 +75,43 @@ TEST_CASE ("test class") {
tm_delete (p_complex);
}

TEST_CASE ("test tm_*_array") {
uint8_t* p_complex= tm_new_array<uint8_t> (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<uint8_t> (size_prim);
tm_delete_array (p_complex);
Complex* p_wide= tm_new_array<Complex> (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<int> (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<Complex> (9);
}
for (int i= 0; i < NUM; i++) {
tm_delete_array (volume[i]);
}
}

TEST_MEMORY_LEAK_ALL

0 comments on commit 08c7d0e

Please sign in to comment.