1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
TLSF Memory Storage allocator implementation.
Version 2.4 Feb 2008
Authors: Miguel Masmano, Ismael Ripoll & Alfons Crespo.
Copyright UPVLC, OCERA Consortium.
TLSF is released in the GPL/LGPL licence. The exact terms of the licence
are described in the COPYING file.
This component provides basic memory allocation functions:
malloc and free, as defined in the standard "C" library.
This allocator was designed to provide real-time performance, that is:
1.- Bounded time malloc and free.
2.- Fast response time.
3.- Efficient memory management, that is low fragmentation.
The worst response time for both malloc and free is O(1).
How to use it:
This code is prepared to be used as a stand-alone code that can be
linked with a regular application or it can be compiled to be a Linux
module (which required the BigPhysicalArea patch). Initially the
module was designed to work jointly with RTLinux-GPL, but it can be
used as a stand alone Linux module.
When compiled as a regular linux process the API is:
Initialisation and destruction functions
----------------------------------------
init_memory_pool may be called before any request or release call:
- size_t init_memory_pool(size_t, void *);
- void destroy_memory_pool(void *);
Request and release functions
-----------------------------
As can be seen, there are two functions for each traditional memory
allocation function (malloc, free, realloc, and calloc). One with the
prefix "tlsf_" and the other with the suffix "_ex".
The versions with the prefix "tlsf_" provides the expected behaviour,
that is, allocating/releasing memory from the default memory pool. The
default memory pool is the last pool initialised by the
init_memory_pool function.
On the other hand, the functions with the prefix "_ex" enable the use of several memory pools.
- void *tlsf_malloc(size_t);
- void *malloc_ex(size_t, void *);
- void tlsf_free(void *ptr);
- void free_ex(void *, void *);
- void *tlsf_realloc(void *ptr, size_t size);
- void *realloc_ex(void *, size_t, void *);
- void *tlsf_calloc(size_t nelem, size_t elem_size);
- void *calloc_ex(size_t, size_t, void *);
EXAMPLE OF USE:
char memory_pool[1024*1024];
{
...
init_memory_pool(1024*1024, memory_pool);
...
ptr1=malloc_ex(100, memory_pool);
ptr2=tlsf_malloc(100); // This function will use memory_pool
...
tlsf_free(ptr2);
free_ex(ptr1, memory_pool);
}
Growing the memory pool
-----------------------
Starting from the version 2.4, the function add_new_area adds an
memory area to an existing memory pool.
- size_t add_new_area(void *, size_t, void *);
This feature is pretty useful when an existing memory pool is running
low and we want to add more free memory to it.
EXAMPLE OF USE:
char memory_pool[1024*1024];
char memory_pool2[1024*1024];
{
...
init_memory_pool(1024*1024, memory_pool);
...
ptr[0]=malloc_ex(1024*256 memory_pool);
ptr[1]=malloc_ex(1024*512, memory_pool);
add_new_area(memory_pool2, 1024*1024, memory_pool);
// Now we have an extra free memory area of 1Mb
// The next malloc may not fail
ptr[2]=malloc_ex(1024*512, memory_pool);
...
}
SBRK and MMAP support
---------------------
The version 2.4 can use the functions SBRK and MMAP to _automatically_
growing the memory pool, before running out of memory.
So, when this feature is enabled, unless the operating system were out
of memory, a malloc operation would not fail due to an "out-of-memory"
error.
To enable this support, compile tlsf.c with the FLAGS -DUSE_MMAP=1 or
-DUSE_SBRK=1 depending on whether you want to use "mmap" or "sbrk" or both.
** By default (default Makefile) this feature is enabled.
EXAMPLE OF USE:
gcc -o tlsf.o -O2 -Wall -DUSE_MMAP=1 -DUSE_SBRK=1
---
If the sbrk/mmap support is enabled and we are _only_ going to use one
memory pool, it is not necessary to call init_memory_pool
EXAMPLE OF USE (with MMAP/SBRK support enabled):
{
...
ptr2=tlsf_malloc(100); // This function will use memory_pool
...
tlsf_free(ptr2);
}
This work has been supported by the followin projects:
EUROPEAN: IST-2001-35102(OCERA) http://www.ocera.org.
SPANISH: TIN2005-08665-C3-03
|