luni
Well-known member
This morning I stumbled over Andrey Rys static memory allocator library smalloc (https://github.com/electrorys/smalloc). This library provides malloc, calloc, free and a couple of other memory allocation functions which work on a static/global buffer which can be passed into the library. Thus, it can easily be used for dynamic memory allocation on EXTMEM and DTCM without changing anything in the core files. I did a few tests which worked without obvious issues.
Off course smalloc also provides sm_free and sm_calloc. You can choose if you want your memory zeroed and you can attach an "out of memory" callback to handle memory overrun issues. It can also be used to handle more than one memory pool which might come in handy from time to time.
For the convenience of Arduino users I did a clone of Andreys unix? library, sorted the original files into the folders required by the Arduino library specification and added a few usage examples. I also added a header handling the extern "C" {}wrapper required for the usual Arduino ino/cpp projects. You find the clone here https://github.com/luni64/static_malloc
Here a quick example showing the usage:
Output:
Unfortunately, I forgot my T4.1 at another place and can currently only test it with a DTCM buffer. Would be great if someone could give the external RAM a try.
I added examples to the repo showing the use of the "out of memory" callback and how to use 'placement new' to construct c++ objects on the EXTMEM heap.
Off course smalloc also provides sm_free and sm_calloc. You can choose if you want your memory zeroed and you can attach an "out of memory" callback to handle memory overrun issues. It can also be used to handle more than one memory pool which might come in handy from time to time.
For the convenience of Arduino users I did a clone of Andreys unix? library, sorted the original files into the folders required by the Arduino library specification and added a few usage examples. I also added a header handling the extern "C" {}wrapper required for the usual Arduino ino/cpp projects. You find the clone here https://github.com/luni64/static_malloc
Here a quick example showing the usage:
Code:
#include "static_malloc.h"
constexpr size_t myHeapSize = 1024 * 100;
EXTMEM uint8_t myHeap[myHeapSize]; // 100kB memory pool on the external ram chip
//----------------------
float* floatArray;
uint32_t* u1;
char* text;
void setup()
{
sm_set_default_pool(myHeap, myHeapSize, false, nullptr); // init with the EXTMEM pool, do not zero the buffer, no out of memory callback
u1 = (uint32_t*)sm_malloc(sizeof(uint32_t)); // one uint32_t
floatArray = (float*)sm_malloc(10 * sizeof(float)); // array of 10 floats
text = (char*)sm_malloc(100); // c-string 100 bytes
*u1 = 100;
for (int i = 0; i < 10; i++) { floatArray[i] = i * M_PI; }
text = strcpy(text, "Hello World");
while (!Serial) {}
Serial.println(text);
Serial.println(*u1);
Serial.println(floatArray[4]);
}
void loop(){
}
Code:
Hello World
100
12.57
Unfortunately, I forgot my T4.1 at another place and can currently only test it with a DTCM buffer. Would be great if someone could give the external RAM a try.
I added examples to the repo showing the use of the "out of memory" callback and how to use 'placement new' to construct c++ objects on the EXTMEM heap.
Last edited: