malloc free malloc is ok

This commit is contained in:
lyon 2021-11-15 21:56:46 +08:00
parent 4df52f7669
commit 2b25ab6812
2 changed files with 81 additions and 0 deletions

View File

@ -13,3 +13,71 @@ TEST(pool, init) {
pool_deinit(&p);
EXPECT_EQ((uint64_t)p.mem, NULL);
}
TEST(pool, m1) {
Pool p = pool_init(125, 4);
EXPECT_EQ(p.aline, 4);
EXPECT_EQ(p.size, 128);
uint64_t mem1_offset = (uint64_t)pool_malloc(&p, 4) - (uint64_t)p.mem;
pool_printBlocks(&p, 0, 128);
EXPECT_EQ(1, bitmap_get(p.bitmap, 0));
EXPECT_EQ(0, bitmap_get(p.bitmap, 1));
EXPECT_EQ(0, mem1_offset);
pool_deinit(&p);
}
TEST(pool, m2) {
Pool p = pool_init(125, 4);
EXPECT_EQ(p.aline, 4);
EXPECT_EQ(p.size, 128);
uint64_t mem1_offset = (uint64_t)pool_malloc(&p, 4) - (uint64_t)p.mem;
uint64_t mem2_offset = (uint64_t)pool_malloc(&p, 4) - (uint64_t)p.mem;
pool_printBlocks(&p, 0, 128);
EXPECT_EQ(1, bitmap_get(p.bitmap, 0));
EXPECT_EQ(1, bitmap_get(p.bitmap, 1));
EXPECT_EQ(0, mem1_offset);
EXPECT_EQ(4, mem2_offset);
pool_deinit(&p);
}
TEST(pool, mf1) {
Pool p = pool_init(125, 4);
EXPECT_EQ(p.aline, 4);
EXPECT_EQ(p.size, 128);
void* mem1 = pool_malloc(&p, 4);
uint64_t mem1_offset = (uint64_t)mem1 - (uint64_t)p.mem;
EXPECT_EQ(1, bitmap_get(p.bitmap, 0));
pool_printBlocks(&p, 0, 128);
pool_free(&p, mem1, 4);
pool_printBlocks(&p, 0, 128);
EXPECT_EQ(0, bitmap_get(p.bitmap, 0));
EXPECT_EQ(0, mem1_offset);
pool_deinit(&p);
}
TEST(pool, mf2) {
Pool p = pool_init(125, 16);
EXPECT_EQ(p.aline, 16);
EXPECT_EQ(p.size, 128);
void* mem1 = pool_malloc(&p, 4);
uint64_t mem1_offset = (uint64_t)mem1 - (uint64_t)p.mem;
EXPECT_EQ(1, bitmap_get(p.bitmap, 0));
pool_printBlocks(&p, 0, 128);
pool_free(&p, mem1, 4);
pool_printBlocks(&p, 0, 128);
EXPECT_EQ(0, bitmap_get(p.bitmap, 0));
EXPECT_EQ(0, mem1_offset);
pool_deinit(&p);
}
TEST(pool, mfm1) {
Pool p = pool_init(125, 4);
EXPECT_EQ(p.size, 128);
void* mem1 = pool_malloc(&p, 4);
pool_malloc(&p, 16);
pool_free(&p, mem1, 4);
pool_malloc(&p, 16);
pool_printBlocks(&p, 0, 128);
pool_deinit(&p);
}

View File

@ -84,16 +84,29 @@ void pool_printBlocks(Pool* pool, uint32_t size_min, uint32_t size_max) {
uint32_t block_index_min = pool_getBlockIndex_byMemSize(pool, size_min);
uint32_t block_index_max = pool_getBlockIndex_byMemSize(pool, size_max);
__platformPrintf("[bitmap]\r\n");
uint8_t is_end = 0;
for (uint32_t i = block_index_min; i < block_index_max; i += 16) {
if (is_end) {
break;
}
__platformPrintf("0x%x\t: ", i * pool->aline, (i + 15) * pool->aline);
for (uint32_t j = i; j < i + 16; j += 4) {
if (is_end) {
break;
}
for (uint32_t k = j; k < j + 4; k++) {
if (k >= block_index_max) {
is_end = 1;
break;
}
__platformPrintf("%d", bitmap_get(pool->bitmap, k));
}
__platformPrintf(" ");
}
__platformPrintf("\r\n");
}
exit:
__platformPrintf("\r\n");
}
void* pool_malloc(Pool* pool, uint32_t size) {