3 Two Level Segregated Fit memory allocator implementation with O(1)
4 time complexity, distributed under the BSD License.
8 * O(1) cost for malloc, free, realloc, memalign
9 * Extremely low overhead per allocation (4 bytes)
10 * Low overhead per TLSF management of pools (~3kB)
12 * Compiles to only a few kB of code and data
13 * Support for adding and removing memory pool regions on the fly
17 You can essentially pass a memory pool for TLSF to manage.
19 enum { STATIC_POOL_SIZE = 1 << 20 };
20 static char s_pool[STATIC_POOL_SIZE];
22 tlsf_t instance = tlsf_create_with_pool(s_pool, STATIC_POOL_SIZE);
24 void *data = tlsf_malloc(instance, 64);
25 tlsf_free(instance, data);
30 * Currently, assumes architecture can make 4-byte aligned accesses
31 * Not designed to be thread safe; the user must provide this
34 This code was based on the TLSF 1.4 spec and documentation found at:
35 http://www.gii.upv.es/tlsf/main/docs
39 M. Masmano, I. Ripoll, A. Crespo, and J. Real.
40 TLSF: a new dynamic memory allocator for real-time systems.
41 In Proc. ECRTS (2004), IEEE Computer Society, pp. 79-86.
43 This implementation was written to the specification of the document,
44 therefore no GPL restrictions apply.
46 It also leverages the TLSF 2.0 improvement to shrink the per-block overhead
51 * Due to the internal block structure size and the implementation
52 details of `tlsf_memalign`, there is worst-case behavior when requesting
53 small (<16 byte) blocks aligned to 8-byte boundaries. Overuse of memalign
54 will generally increase fragmentation, but this particular case will leave
55 lots of unusable "holes" in the pool. The solution would be to internally
56 align all blocks to 8 bytes, but this will require significantl changes
57 to the implementation.
61 TLSF-BSD is freely redistributable under the two-clause BSD License.
62 Use of this source code is governed by a BSD-style license that can be found
63 in the `LICENSE` file.