Skip to content

TLSF Allocator

lbuild module: modm:tlsf

Two-Level Segregated Fit memory allocator implementation. Written by Matthew Conte (matt@baisoku.org). Released under the BSD license. https://github.com/mattconte/tlsf

Features

  • O(1) cost for malloc, free, realloc, memalign
  • Extremely low overhead per allocation (4 bytes)
  • Low overhead per TLSF management of pools (~3kB)
  • Low fragmentation
  • Compiles to only a few kB of code and data
  • Support for adding and removing memory pool regions on the fly

Caveats

  • Currently, assumes architecture can make 4-byte aligned accesses
  • Not designed to be thread safe; the user must provide this

Notes

This code was based on the TLSF 1.4 spec and documentation found at:

http://rtportal.upv.es/rtmalloc/allocators/tlsf/index.shtml

It also leverages the TLSF 2.0 improvement to shrink the per-block overhead from 8 to 4 bytes.

History

2016/04/10 - v3.1 * Code moved to github * tlsfbits.h rolled into tlsf.c * License changed to BSD

2014/02/08 - v3.0 * This version is based on improvements from 3DInteractive GmbH * Interface changed to allow more than one memory pool * Separated pool handling from control structure (adding, removing, debugging) * Control structure and pools can still be constructed in the same memory block * Memory blocks for control structure and pools are checked for alignment * Added functions to retrieve control structure size, alignment size, min and max block size, overhead of pool structure, and overhead of a single allocation * Minimal Pool size is tlsf_block_size_min() + tlsf_pool_overhead() * Pool must be empty when it is removed, in order to allow O(1) removal

2011/10/20 - v2.0 * 64-bit support * More compiler intrinsics for ffs/fls * ffs/fls verification during TLSF creation in debug builds

2008/04/04 - v1.9 * Add tlsf_heap_check, a heap integrity check * Support a predefined tlsf_assert macro * Fix realloc case where block should shrink; if adjacent block is in use, execution would go down the slow path

2007/02/08 - v1.8 * Fix for unnecessary reallocation in tlsf_realloc

2007/02/03 - v1.7 * tlsf_heap_walk takes a callback * tlsf_realloc now returns NULL on failure * tlsf_memalign optimization for 4-byte alignment * Usage of size_t where appropriate

2006/11/21 - v1.6 * ffs/fls broken out into tlsfbits.h * tlsf_overhead queries per-pool overhead

2006/11/07 - v1.5 * Smart realloc implementation * Smart memalign implementation

2006/10/11 - v1.4 * Add some ffs/fls implementations * Minor code footprint reduction

2006/09/14 - v1.3 * Profiling indicates heavy use of blocks of size 1-128, so implement small block handling * Reduce pool overhead by about 1kb * Reduce minimum block size from 32 to 12 bytes * Realloc bug fix

2006/09/09 - v1.2 * Add tlsf_block_size * Static assertion mechanism for invariants * Minor bugfixes

2006/09/01 - v1.1 * Add tlsf_realloc * Add tlsf_walk_heap

2006/08/25 - v1.0 * First release

This module is only available for rp, sam, stm32.

Options

minimum_pool_size

Minimum pool size in byte

Default: 1024Ki (1048576) stm32h7
Default: 10Ki (10240) stm32{f1,l1}
Default: 128Ki (131072) samd5x/e5x, stm32{f2,f4,l4}
Default: 12Ki (12288) stm32f3
Default: 144Ki (147456) stm32g0
Default: 160Ki (163840) samg5x, stm32l4
Default: 16Ki (16384) samd1x/d2x/dax, stm32{f0,f3,l1}
Default: 18Ki (18432) stm32g0
Default: 192Ki (196608) samd5x/e5x, stm32f4
Default: 20Ki (20480) stm32{f1,l0}
Default: 22Ki (22528) stm32g4
Default: 24Ki (24576) stm32f100zct6
Default: 256Ki (262144) sam{d5x/e5x,e7x/s7x/v7x}, stm32{f4,f7,l5,u5}
Default: 264Ki (270336) rp2040
Default: 2Ki (2048) stm32l0
Default: 3008Ki (3080192) stm32u5
Default: 320Ki (327680) stm32{f4,h7,l4}
Default: 32Ki (32768) samd1x/d2x/dax, stm32{f0,f1,f3,f4,l1}
Default: 36Ki (36864) stm32g0
Default: 384Ki (393216) same7x/s7x/v7x, stm32{f7,h7}
Default: 40Ki (40960) stm32{f3,l4}
Default: 48Ki (49152) stm32{f1,l1}
Default: 4Ki (4096) samd1x/d2x/dax, stm32{f0,f1,l1}
Default: 512Ki (524288) stm32{f7,h7}
Default: 640Ki (655360) stm32l4
Default: 64Ki (65536) stm32{f1,f2,f3,f4,l4}
Default: 6Ki (6144) stm32{f0,f1}
Default: 768Ki (786432) stm32u5
Default: 80Ki (81920) stm32l1
Default: 8Ki (8192) samd1x/d2x/dax, stm32{f0,f1,g0,l0,l1}
Default: 96Ki (98304) stm32{f2,f4,g4}
Inputs: [4Ki .. 1024Ki .. 512Mi] stm32h7
Inputs: [4Ki .. 10Ki .. 512Mi] stm32{f1,l1}
Inputs: [4Ki .. 128Ki .. 512Mi] samd5x/e5x, stm32{f2,f4,l4}
Inputs: [4Ki .. 12Ki .. 512Mi] stm32f3
Inputs: [4Ki .. 144Ki .. 512Mi] stm32g0
Inputs: [4Ki .. 160Ki .. 512Mi] samg5x, stm32l4
Inputs: [4Ki .. 16Ki .. 512Mi] samd1x/d2x/dax, stm32{f0,f3,l1}
Inputs: [4Ki .. 18Ki .. 512Mi] stm32g0
Inputs: [4Ki .. 192Ki .. 512Mi] samd5x/e5x, stm32f4
Inputs: [4Ki .. 20Ki .. 512Mi] stm32{f1,l0}
Inputs: [4Ki .. 22Ki .. 512Mi] stm32g4
Inputs: [4Ki .. 24Ki .. 512Mi] stm32f100zct6
Inputs: [4Ki .. 256Ki .. 512Mi] sam{d5x/e5x,e7x/s7x/v7x}, stm32{f4,f7,l5,u5}
Inputs: [4Ki .. 264Ki .. 512Mi] rp2040
Inputs: [4Ki .. 2Ki .. 512Mi] stm32l0
Inputs: [4Ki .. 3008Ki .. 512Mi] stm32u5
Inputs: [4Ki .. 320Ki .. 512Mi] stm32{f4,h7,l4}
Inputs: [4Ki .. 32Ki .. 512Mi] samd1x/d2x/dax, stm32{f0,f1,f3,f4,l1}
Inputs: [4Ki .. 36Ki .. 512Mi] stm32g0
Inputs: [4Ki .. 384Ki .. 512Mi] same7x/s7x/v7x, stm32{f7,h7}
Inputs: [4Ki .. 40Ki .. 512Mi] stm32{f3,l4}
Inputs: [4Ki .. 48Ki .. 512Mi] stm32{f1,l1}
Inputs: [4Ki .. 512Ki .. 512Mi] stm32{f7,h7}
Inputs: [4Ki .. 640Ki .. 512Mi] stm32l4
Inputs: [4Ki .. 64Ki .. 512Mi] stm32{f1,f2,f3,f4,l4}
Inputs: [4Ki .. 6Ki .. 512Mi] stm32{f0,f1}
Inputs: [4Ki .. 768Ki .. 512Mi] stm32u5
Inputs: [4Ki .. 80Ki .. 512Mi] stm32l1
Inputs: [4Ki .. 8Ki .. 512Mi] samd1x/d2x/dax, stm32{f0,f1,g0,l0,l1}
Inputs: [4Ki .. 96Ki .. 512Mi] stm32{f2,f4,g4}
Inputs: [4Ki ... 512Mi] samd1x/d2x/dax, stm32{f0,f1,l1}

subdivisions

Number of subdivisions per memory pool

Default: 16
Inputs: [16, 32]

Limited availability: Check with 'lbuild discover' if this module is available for your target!