Content originally posted in LPCWare by EricBI on Sat Mar 21 01:08:27 MST 2015
i'm using a cortex M3 lpc1768.
I Found out that only the realloc from redlib is not working, in newlib it works fine.
I have created a test project on github: epinxteren/memory-allocation-test
<code>
#define MEM_BLOCK 200
#define POINTERS 40000 / MEM_BLOCK
void test_dynamicAllocation() {
// Enough pointers to go over the 32k of memory.
void * p[POINTERS];
uint32_t used_memory = 0;
uint32_t i = 0;
/****************
** TEST Malloc.
****************/
for (i = 0; i <POINTERS; i++) {
p = malloc(MEM_BLOCK);
if (p) {
used_memory += MEM_BLOCK;
memset(p, 0xAA, MEM_BLOCK);
}
}
// Test free
for (i = 0; i <POINTERS; i++) {
if (p) {
free(p);
p = NULL;
}
}
used_memory = 0;
/****************
** TEST calloc.
****************/
for (i = 0; i <POINTERS; i++) {
p = calloc(1, MEM_BLOCK);
if (p) {
used_memory += MEM_BLOCK;
memset(p, 0xAA, MEM_BLOCK);
}
}
// Test free
for (i = 0; i <POINTERS; i++) {
if (p) {
free(p);
p = NULL;
}
}
used_memory = 0;
/****************
** TEST realloc with NULL pointer as first argument..
****************/
for (i = 0; i <POINTERS; i++) {
p = realloc(p, MEM_BLOCK);
if (p) {
used_memory += MEM_BLOCK;
memset(p, 0xAA, MEM_BLOCK);
}
}
// Test free
for (i = 0; i <POINTERS; i++) {
if (p) {
free(p);
p = NULL;
}
}
/****************
** TEST Test realloc, grow and shrink
****************/
void * pointer = NULL;
int grow = MEM_BLOCK;
used_memory = grow;
for (;;) {
void * temp = realloc(pointer, used_memory);
if (temp && used_memory != 0) {
pointer = temp;
memset(pointer, 0xAA, used_memory);
} else {
grow = grow * -1;
}
if (grow < 0 && used_memory <= MEM_BLOCK) {
used_memory = 0;
} else {
used_memory += grow;
}
}
}
</code>