Skip to content

Commit 07ba488

Browse files
committed
Improve documentation
1 parent d92262d commit 07ba488

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,57 @@ therefore no GPL restrictions apply.
1717
* Not thread safe. API calls must be protected by a mutex in a multi-threaded environment.
1818
* Works in environments with only minimal libc, uses only `stddef.h`, `stdbool.h`, `stdint.h` and `string.h`.
1919

20+
## How it works
21+
22+
This package offers constant, O(1)-time memory block allocation and deallocation.
23+
24+
The structure consists of an array indexed by `log(2, request_size)`.
25+
In other words, requests are divided up according to the requsted size's most significant bit (MSB).
26+
A pointer to the second level of the structure is contained in each item of the array.
27+
At this level, the free blocks of each slab size are divided into x additional groups,
28+
where x is a configurable number.
29+
An array of size x that implements this partitioning is indexed by taking the value of the `log(2, x)` bits that follow the MSB.
30+
Each value denotes the start of a linked list of free blocks (or is `NULL`).
31+
32+
Finding a free block in the correctly sized class (or, if none are available, in a larger size class) in constant time requires using the bitmaps representing the availability of free blocks (of a certain size class).
33+
34+
When `tlsf_free()` is called, the block examines if it may coalesce with nearby free blocks before returning to the free list.
35+
```
36+
+--------------------------------------------------------------------+
37+
| Main structure: |
38+
| Segregated List(array of pointers to second levels) |
39+
| Bitmap showing which levels have free blocks |
40+
| |
41+
| Blocks fit into different size ranges: |
42+
| |
43+
| |2^31|...|2^9|2^8|2^7|2^6|2^5|2^4 |
44+
+--------------------------------------------------------------------+
45+
| |
46+
| | +-----------------------+
47+
| | | Level Two |
48+
+-----------------------+ | +-----> | Array of free lists |
49+
| Level Two |<--+ | Bitmap |
50+
| Array of free lists | +-----------------------+
51+
| bitmaps |
52+
| | | ... | | |
53+
+-----------------------+
54+
| \
55+
| \
56+
+-----------+ +------------+
57+
| Free_Block | | Free_Block |
58+
| | | |
59+
| | | |
60+
+------------+ +------------+
61+
|
62+
|
63+
|
64+
+------------+
65+
| Free_Block |
66+
| |
67+
| |
68+
+------------+
69+
```
70+
2071
## Reference
2172

2273
M. Masmano, I. Ripoll, A. Crespo, and J. Real.

tlsf.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,17 @@ typedef struct {
2626

2727
void *tlsf_resize(tlsf_t *, size_t);
2828
void *tlsf_aalloc(tlsf_t *, size_t, size_t);
29-
void *tlsf_malloc(tlsf_t *, size_t);
29+
30+
/**
31+
* Allocates the requested @size bytes of memory and returns a pointer to it.
32+
* On failure, returns NULL.
33+
*/
34+
void *tlsf_malloc(tlsf_t *, size_t size);
3035
void *tlsf_realloc(tlsf_t *, void *, size_t);
36+
37+
/**
38+
* Releases the previously allocated memory, given the pointer.
39+
*/
3140
void tlsf_free(tlsf_t *, void *);
3241

3342
#ifdef TLSF_ENABLE_CHECK

0 commit comments

Comments
 (0)