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
- Written in C (C11), compatible with
stdint.hdata 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
#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.
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.
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.
- Copy the
lwmemfolder to your project - Add
lwmem/src/includeto the include path - Add the source files from
lwmem/src/lwmem/to the build, and a system port fromlwmem/src/system/whenLWMEM_CFG_OSis enabled - Create
lwmem_opts.hin a folder on the include path (see Configuration)
LwMEM is available in the PlatformIO Registry. Add it to your platformio.ini:
lib_deps =
majerle/LwMEMFresh contributions are always welcome. Simple instructions to proceed:
- Fork Github repository
- Follow C style & coding rules and use
clang-formatto format the code - Create a pull request to
developbranch with new features or bug fixes
Alternatively you may:
- Report a bug
- Ask for a feature request
LwMEM is released under the MIT license.