Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

Commit

Permalink
Add TLSF allocator and enable by default.
Browse files Browse the repository at this point in the history
These changes add the TLSF implementation from http://tlsf.baisoku.org
and enable it by default, completely replacing the newlib allocator.

Up to three arenas are constructed:
- SRAM: with `.heap1`, `.heap2` if available and `.heap3` if available
- CCM or DTCM: with `.heap0` if available
- ITCM: with `.heap4` if available

These three functions are provided:
- `malloc`: Uses SRAM by default, falls back to CCM if SRAM is full
- `realloc`: determines source arena by pointer address
- `free`: determines source arena by pointer address
  • Loading branch information
salkinium committed Apr 16, 2016
2 parents c17840c + ba243c9 commit ffa4e1b
Show file tree
Hide file tree
Showing 13 changed files with 1,641 additions and 15 deletions.
4 changes: 4 additions & 0 deletions examples/stm32f469_discovery/tlsf-allocator/SConstruct
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# path to the xpcc root directory
xpccpath = '../../..'
# execute the common SConstruct file
execfile(xpccpath + '/scons/SConstruct')
54 changes: 54 additions & 0 deletions examples/stm32f469_discovery/tlsf-allocator/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <xpcc/architecture/platform.hpp>
#include <tlsf.h>

extern tlsf_t tlsf_heap0;
extern uint32_t __heap1_start;

int main()
{
Board::initialize();
Board::Leds::setOutput();

// 200 pointers to allocated memories
void *d[200];
int free_ii = 0;

for (int ii=0; ii < 200; ii++)
{
d[ii] = nullptr;
uint32_t size = rand() % (1024*20);

if ((rand() % 100) >= 75) {
d[ii] = tlsf_malloc(tlsf_heap0, size);
// explicit allocation in CCM
XPCC_LOG_INFO << ".heap0 ";
} else {
d[ii] = malloc(size);
if (d[ii] && d[ii] < &__heap1_start) {
// allocation on SRAM has failed and fallback uses CCM!
XPCC_LOG_INFO << ".heap0FB";
}
else XPCC_LOG_INFO << ".heap123";
}

// print what size we requested and if it succeeded
XPCC_LOG_INFO.printf(" malloc(%2ukB) = ", size/1024);
if (d[ii]) XPCC_LOG_INFO << d[ii];
else XPCC_LOG_INFO << "NO MEMORY ";

if ((rand() % 100) >= 50) {
// only some memory is returned to the system
void* df = d[free_ii++];
XPCC_LOG_INFO << " ...freeing " << df;
free(df);
}
XPCC_LOG_INFO << xpcc::endl;
}

while (1)
{
Board::Leds::toggle();
xpcc::delayMilliseconds(500);
}
return 0;
}
6 changes: 6 additions & 0 deletions examples/stm32f469_discovery/tlsf-allocator/project.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build]
board = stm32f469_discovery
buildpath = ${xpccpath}/build/stm32f469_discovery/${name}

[parameters]
uart.stm32.3.tx_buffer = 2048
3 changes: 2 additions & 1 deletion ext/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ buildIncludes = []
globalIncludes = []

if env['ARCHITECTURE'].startswith('cortex-m'):
globalIncludes += ['cmsis/Include']
globalIncludes += ['cmsis/Include', 'tlsf']
sourcePath += ['tlsf']

# -----------------------------------------------------------------------------
# Add the STM32 device header files
Expand Down
92 changes: 92 additions & 0 deletions ext/tlsf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# [tlsf](https://github.com/mattconte/tlsf)
Two-Level Segregated Fit memory allocator implementation.
Written by Matthew Conte (matt@baisoku.org).
Released under the BSD license.

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
Loading

0 comments on commit ffa4e1b

Please sign in to comment.