GithubHelp home page GithubHelp logo

alphaville / static_malloc Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 5 KB

C: malloc for static allocation! :eyes: :tractor:

CMake 13.47% Shell 0.31% C 86.23%
c cprogramming memory-management embedded embedded-systems systems-programming

static_malloc's Introduction

Static Malloc

In embedded programming it is often required to be 100% sure that dynamic memory allocation will work.

This is why, it should be used very carefully to guarantee deterministic behaviour.

If possible, memory should be allocated when the application starts and no further allocation should take place afterwards.

This is a simple framework for safe memory allocation in C.

This framework should be used provided that:

  • the exact amount of memory to be allocated is known before the start of the application
  • it is not necessary to free memory while the application runs

This is a header library, meaning, you just need to include static_malloc.h in your C file.

Using static_malloc

Step 1. Configure static_malloc_sizes.h and define the amount of memory to be allocated.

For example:

#define static_malloc_len_double 123456
#define static_malloc_len_int 1000
#define static_malloc_len_char 50
#define static_malloc_len_uint_t 5000

You may define any datatype to be allocated.

For example, if you have some datatype called my_datatype_t, and you need to pre-allocate memory of size 123000, you only need to define

#define static_malloc_len_my_datatype_t 123000

Step 2. You may now use static_malloc as follows

#include "../include/static_malloc.h"
#include <stdio.h>

int main(void){
    /* Allocate an array of double of length 20 */
    double * x = STATIC_MALLOC(double)(20);
    
     /* Allocate an array of double of length 500 */
    double * y = STATIC_MALLOC(double)(500);
    
    /* You don't need to (and you shouldn't) free the memory */
    return 0;
}

For the datatypes double, int and float, you may use STATIC_MALLOC as above.

For other less frequently used datatypes, e.g., char, short etc, you need to define STATIC_MALLOC(...) as in the following example:

#if STALLOC_ACTIVE_TYPE(char)
STATIC_MALLOC_DFN(char)
#endif

Here is a complete example

#include "include/static_malloc.h"

#if STALLOC_ACTIVE_TYPE(char)  // if static_malloc_len_char is defined
STATIC_MALLOC_DFN(char)        // define STATIC_MALLOC(char)
#endif

int main(void)
{
    /* Allocate 5 chars */
    char *x = STATIC_MALLOC(char)(5);

    return 0;
}

Pros and Cons

Pros:

  • Safe usage of memory
  • No need to free the memory

Cons:

  • Need to know how much memory the program needs, before it runs
  • Freeing memory is not possible
  • Reusing memory is

Installation

No installation is necessary!

For example, to compile main.c, use

gcc main.c

You only need to include the following line in your file:

#include "include/static_malloc.h"

Unit tests

To run the tests, you first need to run config.sh (requires cmake) and then enter build and run make all test.

Run the following:

./config.sh
cd build
make all test

static_malloc's People

Contributors

alphaville avatar

Stargazers

 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.