Freeing strategy causes fragmentation over time
Closed this issue · 1 comments
dkhusted commented
Great libray to start off with!
I am using in a STM32F429 application where its used during runtime for dynamically manging allocating and freeing buffers for incoming data. When stress testing the library, it seems that the total amount of available data starts to decrease, possibly cause by fragmentation. The below code was used for the test:
static lwmem_region_t regions[] = {
{(uint8_t *) SDRAM_LWMEM_BASE, 32768},
{(uint8_t *) (SDRAM_LWMEM_BASE + 0x8000), 32768},
{NULL, 0}
};
lwmem_assignmem(regions);
int * ptrArr[10];
for(int cnt = 0; cnt < 100; cnt++){
if(((cnt%10) == 0) && cnt != 0){
for(int idx = 0; idx < 5; idx++){
lwmem_free(&(ptrArr[idx]));
}
lwmem_free(&(ptrArr[8]));
lwmem_free(&(ptrArr[9]));
for(int idx = 0; idx < 5; idx++){
ptrArr[idx] = lwmem_malloc(cnt*2+idx*2);
}
ptrArr[8] = lwmem_malloc(cnt*3);
ptrArr[9] = lwmem_malloc(cnt);
for(int idx = 0; idx < 10; idx++){
lwmem_free(&(ptrArr[idx]));
}
}
ptrArr[cnt % 10] = lwmem_malloc(cnt*2);
}
MaJerle commented
I think you are misusing the library, no? When I compile your example, indeed I get a warning for void *
conversion to int **
.
lwmem_free(&(ptrArr[idx]));
should be:
lwmem_free(ptrArr[idx]);
//Or, with safe option:
lwmem_free_s((void**)&(ptrArr[idx]));
Then, your code seems a bit strange in the usage itself. You are freeing invalid pointer before you are allocating it.