项目作者: alphaville

项目描述 :
C: malloc for static allocation! :eyes: :tractor:
高级语言: C
项目地址: git://github.com/alphaville/static_malloc.git
创建时间: 2018-11-19T23:55:36Z
项目社区:https://github.com/alphaville/static_malloc

开源协议:

下载


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:

  1. #define static_malloc_len_double 123456
  2. #define static_malloc_len_int 1000
  3. #define static_malloc_len_char 50
  4. #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

  1. #define static_malloc_len_my_datatype_t 123000

Step 2. You may now use static_malloc as follows

  1. #include "../include/static_malloc.h"
  2. #include <stdio.h>
  3. int main(void){
  4. /* Allocate an array of double of length 20 */
  5. double * x = STATIC_MALLOC(double)(20);
  6. /* Allocate an array of double of length 500 */
  7. double * y = STATIC_MALLOC(double)(500);
  8. /* You don't need to (and you shouldn't) free the memory */
  9. return 0;
  10. }

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:

  1. #if STALLOC_ACTIVE_TYPE(char)
  2. STATIC_MALLOC_DFN(char)
  3. #endif

Here is a complete example

  1. #include "include/static_malloc.h"
  2. #if STALLOC_ACTIVE_TYPE(char) // if static_malloc_len_char is defined
  3. STATIC_MALLOC_DFN(char) // define STATIC_MALLOC(char)
  4. #endif
  5. int main(void)
  6. {
  7. /* Allocate 5 chars */
  8. char *x = STATIC_MALLOC(char)(5);
  9. return 0;
  10. }

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

  1. gcc main.c

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

  1. #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:

  1. ./config.sh
  2. cd build
  3. make all test