I am using a Teensy 4.1 for a project and need to dynamically allocate and reallocate memory throughout the life of the program. I have soldered on 16mb of psram and the memory test passes . . . but every time i attempt to use extmem_realloc and extmem_malloc it returns null each time and no memory is allocated. Please advise what I need to do. Happy to answer any questions to help get a better view of what may be going on but on my end it just seems those two function are just not working at all.
in case it helps anyone this is the code in question
in above both _free and _realloc is just a wrapper to directly call extmem_free and extmem_realloc since this was a part of a project on a different platform that i was bringing to the teensy
here is the result of the memtest code along with allocation to show u what i mean . . .
OUTPUT:
EXTMEM Memory Test, 16 Mbyte
CCM_CBCMR=B5AE8304 (88.0 MHz)
. . . many pattern outputs
test ran for 72.86 seconds
All memory tests passed
Total used: 0 bytes
Total user vars: 0 bytes
Free space: 15.9990 MB
Allocated blocks: 0
10mb allocation >> NULL
in case it helps anyone this is the code in question
C:
void* reallocate (GC* gc, void* pointer, size_t oldSize, size_t newSize)
{
gc->bytesAllocated += newSize - oldSize;
if (newSize > oldSize)
{
if (debugStressGC) collectGarbage(gc);
if (gc->bytesAllocated > gc->nextGC) collectGarbage(gc);
}
if (newSize == 0)
{
_free(pointer);
return NULL;
}
void* result = _realloc(pointer, newSize);
// GCOV_EXCL_START
if (result == NULL)
{
printf("realloc error (return NULL) >> %s [%i,%i]\n", (pointer == NULL ? "NULL ptr" : "active ptr"), oldSize, newSize);
exit(1);
};
// GCOV_EXCL_STOP
return result;
}
here is the result of the memtest code along with allocation to show u what i mean . . .
C++:
extern "C" uint8_t external_psram_size;
bool memory_ok = false;
uint32_t *memory_begin, *memory_end;
bool check_fixed_pattern(uint32_t pattern);
bool check_lfsr_pattern(uint32_t seed);
void setup()
{
while (!Serial) ; // wait
pinMode(13, OUTPUT);
uint8_t size = external_psram_size;
Serial.printf("EXTMEM Memory Test, %d Mbyte\n", size);
if (size == 0) return;
const float clocks[4] = {396.0f, 720.0f, 664.62f, 528.0f};
const float frequency = clocks[(CCM_CBCMR >> 8) & 3] / (float)(((CCM_CBCMR >> 29) & 7) + 1);
Serial.printf(" CCM_CBCMR=%08X (%.1f MHz)\n", CCM_CBCMR, frequency);
memory_begin = (uint32_t *)(0x70000000);
memory_end = (uint32_t *)(0x70000000 + size * 1048576);
elapsedMillis msec = 0;
if (!check_fixed_pattern(0x5A698421)) return;
. . . lfsr pattern tests
. . . fixed pattern tests
Serial.printf(" test ran for %.2f seconds\n", (float)msec / 1000.0f);
Serial.println("All memory tests passed :-)");
memory_ok = true;
size_t total, totalUser, free;
int blocks;
sm_malloc_stats_pool(&extmem_smalloc_pool, &total, &totalUser, &free, &blocks);
Serial.printf(
"Total used: %u bytes\n"
"Total user vars: %u bytes\n"
"Free space: %.4f MB\n"
"Allocated blocks: %u\n\n",
total, totalUser, free / 1024 / 1024.0f, blocks
);
void* mem;
mem = extmem_malloc(1024 * 1024 * 5);
Serial.printf("10mb allocation >> %s\n", mem == NULL ? "NULL" : "ALLOCATED");
}
. . . rest of memory test code
OUTPUT:
EXTMEM Memory Test, 16 Mbyte
CCM_CBCMR=B5AE8304 (88.0 MHz)
. . . many pattern outputs
test ran for 72.86 seconds
All memory tests passed
Total used: 0 bytes
Total user vars: 0 bytes
Free space: 15.9990 MB
Allocated blocks: 0
10mb allocation >> NULL
Last edited: