Giter VIP home page Giter VIP logo

skyeng1neering / dalloc Goto Github PK

View Code? Open in Web Editor NEW
16.0 1.0 2.0 13 KB

This is the custom implementation of function malloc for embedded systems, that defragmentate memory after using it. Good solution when you need to allocate memory dynamically, but memory fragmentation is the problem.

License: Apache License 2.0

Makefile 1.57% C 98.43%
microcontroller malloc embedded memory embedded-systems stm32 avr allocator alloc custom-allocator

dalloc's Introduction

About the project

This is the custom implementation of function malloc for embedded systems, that defragmentate memory after using it. Good solution when you need to allocate memory dynamically, but memory fragmentation is the problem. On this allocator based such project as uvector, ustring and usmartpointer.

What is the problem that dalloc solves?

Here is an illustration of memory fragmentation problem, while using default malloc function:

this On the last step there are 7 kB of free memory but not in solid region, so allocating of 4 kB fails

And how dalloc works:

this

How it works

When you call allocate function, it saves the pointer to the pointer of allocated memory area and when corresponding memory region is freed - the defragmentation procedure executes, an the content of pointer to the memory area is replaced by new actual value. So using of dalloc allocator is not the same as default malloc.

Usage

Simple examples of using dalloc

In your project you can use only one heap area, and several heap areas as well. If you want to use only one heap memory area in your project, you can do it like this:

/* File dalloc_conf.h */
#define USE_SINGLE_HEAP_MEMORY
#define SINGLE_HEAP_SIZE				4096UL //define heap size that you want to have
#include "dalloc.h"

int main(){
  /* Allocate memory region in heap memory */
  uint8_t *data_ptr = NULL;
  uint32_t allocation_size = 8;
  def_dalloc(allocation_size, (void**)&data_ptr);

  /* Check if allocation is successful */
  if(data_ptr == NULL){
    printf("Can't allocate %u bytes\n", allocation_size);
    return 0;
  }
 
  /* Now you can use allocated memory */
  for(uint32_t i = 0; i < allocation_size; i++){
    data_ptr[i] = 0xDE;
  }
 
  /* Free allocated memory */
  def_dfree((void**)&data_ptr);
 
  return 0;
}  
  

If you want to use several different heap areas, you can define it explicitly:

/* File dalloc_conf.h */
//#define USE_SINGLE_HEAP_MEMORY
#include "dalloc.h"

#define HEAP_SIZE			32

/* Declare an array that will be used for dynamic memory allocations */
uint8_t heap_array[HEAP_SIZE];

/* Declare an dalloc heap structure, it contains all allocations info */
heap_t heap;

int main(){
  /* Init heap memory */
  heap_init(&heap, (void*)heap_array, HEAP_SIZE);

  /* Allocate memory region in heap memory */
  uint8_t *data_ptr = NULL;
  uint32_t allocation_size = 8;
  dalloc(&heap, allocation_size, (void**)&data_ptr);

  /* Check if allocation is successful */
  if(data_ptr == NULL){
    printf("Can't allocate %u bytes\n", allocation_size);
    return 0;
  }
 
  /* Now you can use allocated memory */
  for(uint32_t i = 0; i < allocation_size; i++){
    data_ptr[i] = 0xDE;
  }
 
  /* Free allocated memory */
  dfree(&heap, (void**)&data_ptr, USING_PTR_ADDRESS);
 
  return 0;
}  
  

Limitations

As the address of pointer variable being saved and the value of the pointer variable is being updated when defragmentation is running - the pointer that was passed to dalloc function must exist before dfree function call, so you can't do something like this:

uint8_t* func_that_use_dalloc(heap_t *heap_ptr, uint32_t some_data_len){
  uint8_t* data_ptr = NULL;
  dalloc(heap, some_data_len, (void**)&data_ptr);
  return data_ptr;
}

Because when you call this function - the memory is allocated, but after this the pointer variable data_ptr which address that saves in heap_t structure isn't exist. So when you call dfree function, when defragmentation start you can get hard fault exception.

So the pointer which contains allocated memory address should exist in memory after allocating procedure. You can do this for example:

uint8_t* data_ptr = NULL;//data_ptr is just global variable

bool func_that_use_dalloc(heap_t *heap_ptr, uint32_t some_data_len){
  dalloc(heap, some_data_len, (void**)&data_ptr);
  
  /* Check if allocation is successful */
  if(data_ptr == NULL){
    return false;
  }
  return true;
}

Or this:

uint8_t* data_ptr = NULL;//data_ptr is the variable somewhere in your code

bool func_that_use_dalloc(heap_t *heap_ptr, uint8_t** data_ptr_address, uint32_t some_data_len){
  dalloc(heap, some_data_len, (void**)data_ptr_address);
  
  /* Check if allocation is successful */
  if(*data_ptr_address == NULL){
    return false;
  }
  return true;
}

func_that_use_dalloc(&heap, &data_ptr, some_data_len);

If you need to allocate memory in some function, and return pointer to allocated memory from it - just use usmartpointer, this is an c++ wrapper for dalloc, and you can use it more comfortable:

SmartPointer<uint8_t> func_that_use_dalloc(heap_t *heap_ptr, uint32_t some_data_len){
  SmartPointer<uint8_t> ptr;
  ptr.assignAllocMemPointer(heap_ptr);
  ptr.allocate(some_data_len);
  
  /* Maybe some logic */
  
  return ptr;
}

All memory moments here is managed automatically

dalloc's People

Contributors

skyeng1neering avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.