Skip to content
MaJerlePublic

About

Lightweight dynamic memory manager library for embedded systems with memory constraints. It implements malloc, calloc, realloc and free functions

Topics

Resources

Stars

467 stars

Watchers

17 watching

Forks

Repository files navigation

LwMEM — Lightweight dynamic memory manager for embedded systems

Build & Test Release PlatformIO Registry License: MIT Documentation

LwMEM is a lightweight dynamic memory manager optimized for embedded systems, written in C11. It provides drop-in malloc, calloc, realloc and free replacements that work on one or more user-defined memory regions, for example internal SRAM and external SDRAM at the same time, with optional thread safety and runtime statistics.

📖 Documentation · Changelog · Releases

Features

  • Written in C (C11), compatible with stdint.h data types
  • Implements standard C library functions for memory allocation, malloc, calloc, realloc and free
  • Uses first-fit algorithm to search for free block
  • Supports multiple allocation instances to split between memories and/or CPU cores
  • Supports different memory regions, ideal for use with fragmented or embedded memories
  • Highly configurable for memory allocation and reallocation
  • Supports automotive applications
  • Supports advanced free/realloc algorithms to optimize memory usage
  • Supports light implementation with allocation only
  • Optional runtime allocation statistics: used/free/minimum-ever-free bytes and alloc/free counts
  • Safe free and realloc variants that null the caller's pointer after the operation
  • Optional memory wipe on free/realloc for security-sensitive applications
  • Query the usable size of an allocated block at runtime
  • Operating system ready, thread-safe API, with ready-made ports for CMSIS-OS, pthreads, ThreadX and Win32
  • C++ wrapper functions
  • User friendly MIT license

Quick start

#include "lwmem/lwmem.h"

/* Memory used by the manager */
static uint8_t heap_data[1024];

/* List of regions, terminated with { NULL, 0 } */
static lwmem_region_t regions[] = {
    {heap_data, sizeof(heap_data)},
    /* More regions can be added, for example external SDRAM: { (void*)0xC0000000, 0x00800000 }, */
    {NULL, 0},
};

int
main(void) {
    void* ptr;

    /* Assign regions to the manager, once at startup */
    lwmem_assignmem(regions);

    /* Allocate 8 bytes of memory */
    ptr = lwmem_malloc(8);
    if (ptr != NULL) {
        /* Allocation successful */
    }

    /* Free memory and set the pointer to NULL */
    lwmem_free_s(&ptr);
    return 0;
}

Note: When using multiple regions, they must be listed in increasing order of their start address and must not overlap. See How it works in the documentation.

More examples are in docs/examples_src, examples and in the documentation.

Configuration

Thread safety, statistics, alignment, memory wipe and the light (allocation-only) mode are configured in the lwmem_opts.h file. Copy lwmem/src/include/lwmem/lwmem_opts_template.h to your project, rename it to lwmem_opts.h, and override the defaults listed in lwmem_opt.h. See Getting started in the documentation for details.

When LWMEM_CFG_OS is enabled, the library needs a few system functions for mutex handling. Ready-made implementations for CMSIS-OS, pthreads, ThreadX and Win32 are in lwmem/src/system, see Thread safety in the documentation.

Add to your project

CMake

set(LWMEM_OPTS_FILE ${CMAKE_CURRENT_LIST_DIR}/lwmem_opts.h) # Optional, default options are used otherwise
set(LWMEM_SYS_PORT "cmsis_os")                              # Optional, one of: cmsis_os, pthread, threadx, win32
add_subdirectory(path/to/lwmem/lwmem)
target_link_libraries(${PROJECT_NAME} PRIVATE lwmem)

Optional compiler options and definitions can be set with LWMEM_COMPILE_OPTIONS and LWMEM_COMPILE_DEFINITIONS, see lwmem/library.cmake.

Any other build system

  1. Copy the lwmem folder to your project
  2. Add lwmem/src/include to the include path
  3. Add the source files from lwmem/src/lwmem/ to the build, and a system port from lwmem/src/system/ when LWMEM_CFG_OS is enabled
  4. Create lwmem_opts.h in a folder on the include path (see Configuration)

PlatformIO

LwMEM is available in the PlatformIO Registry. Add it to your platformio.ini:

lib_deps =
    majerle/LwMEM

Contribute

Fresh contributions are always welcome. Simple instructions to proceed:

  1. Fork Github repository
  2. Follow C style & coding rules and use clang-format to format the code
  3. Create a pull request to develop branch with new features or bug fixes

Alternatively you may:

  1. Report a bug
  2. Ask for a feature request

License

LwMEM is released under the MIT license.

About

Lightweight dynamic memory manager library for embedded systems with memory constraints. It implements malloc, calloc, realloc and free functions

Topics

Resources

Stars

467 stars

Watchers

17 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages