Giter VIP home page Giter VIP logo

static_malloc's Introduction

static_malloc

This library is just an Arduino wrapper around Andrey Rys static memory allocator library smalloc (https://github.com/electrorys/smalloc). The full credit for the very useful code goes to Andrey of course. Here his man-page of smalloc

For the convenience of Arduino users I 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 which is required for the usual Arduino ino/cpp projects.

The main purpose of this library is to enable dynamic memory allocation on one or more predefined static buffers. This buffer can be placed wherever you want. Thus, the library can easily be used to dynamically access external memory chips like the optional 8MB PSRAM on the Teensy 4.1.

Here a quick example showing the usage:

#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); // use a memory pool on the external ram

    u1 = (uint32_t*)sm_malloc(sizeof(uint32_t));        // 1 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()
{
}

Off course, smalloc also provides sm_free and sm_calloc. Additionally, it can be used to handle more than one memory pool in parallel and provides an "out of memory" callback.

See the examples folder in the sources for more examples.

Here a quick video showing how sm_malloc allocs/frees memory. The Teensy code implements a 54000 byte static heap and smalloc randomly allocates and frees memory chunks on the heap. Each bar corresponds to one of the chunks. You find the corresponding firmware and (Win10) software in the extras folder.

Watch the video

static_malloc's People

Contributors

luni64 avatar lunoptics avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.