jmininger / TLSF_memory_allocator

A dynamic memory allocator for real-time systems

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Two Level Segregated Fit(TLSF) Dynamic Memory Allocator For Real-Time Systems

NOTE: THIS IS A TOY PROJECT. DO NOT USE FOR ANYTHING IMPORTANT

Based on the whitepaper from Miguel Masmano, Ismael Ripoll & Alfons Crespo.
(See paper here: http://www.gii.upv.es/tlsf/files/ecrts04_tlsf.pdf)
Even more information can be found here: http://www.gii.upv.es/tlsf/

Use:
void* tlsf_init(void* pool_start, size_t pool_size);
void* tlsf_malloc(void* tlsf, size_t size);
void tlsf_free(void *tlsf, void* ptr);

Since the allocator is built for RT systems, no sbrk() syscall is used, and the 
memory pool should be initialized before use. 

How it works:
Provides allocation/deallocation of memory blocks in constant, O(1), time

The structure contains an array indexed by log(2, reqSize). In other words, requests are split up based on the most significant bit of the requested size. Each entry in the array contains a pointer to the second level of the structure, which further partions the free blocks of each slab size into x more groups, where x is a configurable constant (in this particular implementation, x=16). This partioning is implemented by an array of size = x, and is indexed by taking the value of the log(2,x) bits that come after the most significant one. Each value points to the first element of a linked list of
free blocks (or is NULL).

To fulfill a tlsf_malloc(), the bitmaps showing the availibility of free blocks (of a given size
class) are used to find a free block in the matching size class (or if there are none, in a larger
size class) in constant time

When tlsf_free() is called, the block checks for the possibility of coalescing with adjacent
free blocks and then returns the block to the free list 

-----------------------------------------------------------------------------
|Main structure: Contains...                                                |
| Segregated List(array of pointers to second levels)                       |
| Bitmap showing which levels have free blocks                              |
|                                                                           |
|   Blocks fit into different size ranges:                                  |
|                                                                           |
|       |2^31|...|2^9|2^8|2^7|2^6|2^5|2^4                                   |
-----------------------------------------------------------------------------
                            |       |
                            |       |       -------------------------
                            |       |       |Level Two              |
-------------------------   |       |_____> |   Array of free lists |
|Level Two              |<__|               |   Bitmap              |
|   Array of free lists |                   -------------------------
|   bitmaps             |
|   |   |   ... |   |   |
-------------------------
    |       |
    |       |
------------  ------------
|Free_Block | |Free_Block |
|           | |           |
|           | |           |
------------- -------------
      |
      |
      | 
------------
|Free_Block |
|           |
|           |
-------------

About

A dynamic memory allocator for real-time systems


Languages

Language:C 100.0%