1 |
606 |
jeremybenn |
/**
|
2 |
|
|
* @file
|
3 |
|
|
* Dynamic pool memory manager
|
4 |
|
|
*
|
5 |
|
|
* lwIP has dedicated pools for many structures (netconn, protocol control blocks,
|
6 |
|
|
* packet buffers, ...). All these pools are managed here.
|
7 |
|
|
*/
|
8 |
|
|
|
9 |
|
|
/*
|
10 |
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
11 |
|
|
* All rights reserved.
|
12 |
|
|
*
|
13 |
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
14 |
|
|
* are permitted provided that the following conditions are met:
|
15 |
|
|
*
|
16 |
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
17 |
|
|
* this list of conditions and the following disclaimer.
|
18 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
19 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
20 |
|
|
* and/or other materials provided with the distribution.
|
21 |
|
|
* 3. The name of the author may not be used to endorse or promote products
|
22 |
|
|
* derived from this software without specific prior written permission.
|
23 |
|
|
*
|
24 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
25 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
26 |
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
27 |
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
28 |
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
29 |
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
30 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
31 |
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
32 |
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
33 |
|
|
* OF SUCH DAMAGE.
|
34 |
|
|
*
|
35 |
|
|
* This file is part of the lwIP TCP/IP stack.
|
36 |
|
|
*
|
37 |
|
|
* Author: Adam Dunkels <adam@sics.se>
|
38 |
|
|
*
|
39 |
|
|
*/
|
40 |
|
|
|
41 |
|
|
#include "lwip/opt.h"
|
42 |
|
|
|
43 |
|
|
#include "lwip/memp.h"
|
44 |
|
|
#include "lwip/pbuf.h"
|
45 |
|
|
#include "lwip/udp.h"
|
46 |
|
|
#include "lwip/raw.h"
|
47 |
|
|
#include "lwip/tcp.h"
|
48 |
|
|
#include "lwip/igmp.h"
|
49 |
|
|
#include "lwip/api.h"
|
50 |
|
|
#include "lwip/api_msg.h"
|
51 |
|
|
#include "lwip/tcpip.h"
|
52 |
|
|
#include "lwip/sys.h"
|
53 |
|
|
#include "lwip/stats.h"
|
54 |
|
|
#include "netif/etharp.h"
|
55 |
|
|
#include "lwip/ip_frag.h"
|
56 |
|
|
|
57 |
|
|
#include <string.h>
|
58 |
|
|
|
59 |
|
|
#if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
|
60 |
|
|
|
61 |
|
|
struct memp {
|
62 |
|
|
struct memp *next;
|
63 |
|
|
#if MEMP_OVERFLOW_CHECK
|
64 |
|
|
const char *file;
|
65 |
|
|
int line;
|
66 |
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
67 |
|
|
};
|
68 |
|
|
|
69 |
|
|
#if MEMP_OVERFLOW_CHECK
|
70 |
|
|
/* if MEMP_OVERFLOW_CHECK is turned on, we reserve some bytes at the beginning
|
71 |
|
|
* and at the end of each element, initialize them as 0xcd and check
|
72 |
|
|
* them later. */
|
73 |
|
|
/* If MEMP_OVERFLOW_CHECK is >= 2, on every call to memp_malloc or memp_free,
|
74 |
|
|
* every single element in each pool is checked!
|
75 |
|
|
* This is VERY SLOW but also very helpful. */
|
76 |
|
|
/* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in
|
77 |
|
|
* lwipopts.h to change the amount reserved for checking. */
|
78 |
|
|
#ifndef MEMP_SANITY_REGION_BEFORE
|
79 |
|
|
#define MEMP_SANITY_REGION_BEFORE 16
|
80 |
|
|
#endif /* MEMP_SANITY_REGION_BEFORE*/
|
81 |
|
|
#if MEMP_SANITY_REGION_BEFORE > 0
|
82 |
|
|
#define MEMP_SANITY_REGION_BEFORE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE)
|
83 |
|
|
#else
|
84 |
|
|
#define MEMP_SANITY_REGION_BEFORE_ALIGNED 0
|
85 |
|
|
#endif /* MEMP_SANITY_REGION_BEFORE*/
|
86 |
|
|
#ifndef MEMP_SANITY_REGION_AFTER
|
87 |
|
|
#define MEMP_SANITY_REGION_AFTER 16
|
88 |
|
|
#endif /* MEMP_SANITY_REGION_AFTER*/
|
89 |
|
|
#if MEMP_SANITY_REGION_AFTER > 0
|
90 |
|
|
#define MEMP_SANITY_REGION_AFTER_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER)
|
91 |
|
|
#else
|
92 |
|
|
#define MEMP_SANITY_REGION_AFTER_ALIGNED 0
|
93 |
|
|
#endif /* MEMP_SANITY_REGION_AFTER*/
|
94 |
|
|
|
95 |
|
|
/* MEMP_SIZE: save space for struct memp and for sanity check */
|
96 |
|
|
#define MEMP_SIZE (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED)
|
97 |
|
|
#define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED)
|
98 |
|
|
|
99 |
|
|
#else /* MEMP_OVERFLOW_CHECK */
|
100 |
|
|
|
101 |
|
|
/* No sanity checks
|
102 |
|
|
* We don't need to preserve the struct memp while not allocated, so we
|
103 |
|
|
* can save a little space and set MEMP_SIZE to 0.
|
104 |
|
|
*/
|
105 |
|
|
#define MEMP_SIZE 0
|
106 |
|
|
#define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
|
107 |
|
|
|
108 |
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
109 |
|
|
|
110 |
|
|
/** This array holds the first free element of each pool.
|
111 |
|
|
* Elements form a linked list. */
|
112 |
|
|
static struct memp *memp_tab[MEMP_MAX];
|
113 |
|
|
|
114 |
|
|
#else /* MEMP_MEM_MALLOC */
|
115 |
|
|
|
116 |
|
|
#define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
|
117 |
|
|
|
118 |
|
|
#endif /* MEMP_MEM_MALLOC */
|
119 |
|
|
|
120 |
|
|
/** This array holds the element sizes of each pool. */
|
121 |
|
|
#if !MEM_USE_POOLS && !MEMP_MEM_MALLOC
|
122 |
|
|
static
|
123 |
|
|
#endif
|
124 |
|
|
const u16_t memp_sizes[MEMP_MAX] = {
|
125 |
|
|
#define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEM_ALIGN_SIZE(size),
|
126 |
|
|
#include "lwip/memp_std.h"
|
127 |
|
|
};
|
128 |
|
|
|
129 |
|
|
#if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
|
130 |
|
|
|
131 |
|
|
/** This array holds the number of elements in each pool. */
|
132 |
|
|
static const u16_t memp_num[MEMP_MAX] = {
|
133 |
|
|
#define LWIP_MEMPOOL(name,num,size,desc) (num),
|
134 |
|
|
#include "lwip/memp_std.h"
|
135 |
|
|
};
|
136 |
|
|
|
137 |
|
|
/** This array holds a textual description of each pool. */
|
138 |
|
|
#ifdef LWIP_DEBUG
|
139 |
|
|
static const char *memp_desc[MEMP_MAX] = {
|
140 |
|
|
#define LWIP_MEMPOOL(name,num,size,desc) (desc),
|
141 |
|
|
#include "lwip/memp_std.h"
|
142 |
|
|
};
|
143 |
|
|
#endif /* LWIP_DEBUG */
|
144 |
|
|
|
145 |
|
|
/** This is the actual memory used by the pools. */
|
146 |
|
|
static u8_t memp_memory[MEM_ALIGNMENT - 1
|
147 |
|
|
#define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )
|
148 |
|
|
#include "lwip/memp_std.h"
|
149 |
|
|
];
|
150 |
|
|
|
151 |
|
|
#if MEMP_SANITY_CHECK
|
152 |
|
|
/**
|
153 |
|
|
* Check that memp-lists don't form a circle
|
154 |
|
|
*/
|
155 |
|
|
static int
|
156 |
|
|
memp_sanity(void)
|
157 |
|
|
{
|
158 |
|
|
s16_t i, c;
|
159 |
|
|
struct memp *m, *n;
|
160 |
|
|
|
161 |
|
|
for (i = 0; i < MEMP_MAX; i++) {
|
162 |
|
|
for (m = memp_tab[i]; m != NULL; m = m->next) {
|
163 |
|
|
c = 1;
|
164 |
|
|
for (n = memp_tab[i]; n != NULL; n = n->next) {
|
165 |
|
|
if (n == m && --c < 0) {
|
166 |
|
|
return 0;
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
}
|
171 |
|
|
return 1;
|
172 |
|
|
}
|
173 |
|
|
#endif /* MEMP_SANITY_CHECK*/
|
174 |
|
|
#if MEMP_OVERFLOW_CHECK
|
175 |
|
|
/**
|
176 |
|
|
* Check if a memp element was victim of an overflow
|
177 |
|
|
* (e.g. the restricted area after it has been altered)
|
178 |
|
|
*
|
179 |
|
|
* @param p the memp element to check
|
180 |
|
|
* @param memp_size the element size of the pool p comes from
|
181 |
|
|
*/
|
182 |
|
|
static void
|
183 |
|
|
memp_overflow_check_element(struct memp *p, u16_t memp_size)
|
184 |
|
|
{
|
185 |
|
|
u16_t k;
|
186 |
|
|
u8_t *m;
|
187 |
|
|
#if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
|
188 |
|
|
m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
|
189 |
|
|
for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
|
190 |
|
|
if (m[k] != 0xcd) {
|
191 |
|
|
LWIP_ASSERT("detected memp underflow!", 0);
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
#endif
|
195 |
|
|
#if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
|
196 |
|
|
m = (u8_t*)p + MEMP_SIZE + memp_size;
|
197 |
|
|
for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
|
198 |
|
|
if (m[k] != 0xcd) {
|
199 |
|
|
LWIP_ASSERT("detected memp overflow!", 0);
|
200 |
|
|
}
|
201 |
|
|
}
|
202 |
|
|
#endif
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
/**
|
206 |
|
|
* Do an overflow check for all elements in every pool.
|
207 |
|
|
*
|
208 |
|
|
* @see memp_overflow_check_element for a description of the check
|
209 |
|
|
*/
|
210 |
|
|
static void
|
211 |
|
|
memp_overflow_check_all(void)
|
212 |
|
|
{
|
213 |
|
|
u16_t i, j;
|
214 |
|
|
struct memp *p;
|
215 |
|
|
|
216 |
|
|
p = LWIP_MEM_ALIGN(memp_memory);
|
217 |
|
|
for (i = 0; i < MEMP_MAX; ++i) {
|
218 |
|
|
p = p;
|
219 |
|
|
for (j = 0; j < memp_num[i]; ++j) {
|
220 |
|
|
memp_overflow_check_element(p, memp_sizes[i]);
|
221 |
|
|
p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
|
222 |
|
|
}
|
223 |
|
|
}
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
/**
|
227 |
|
|
* Initialize the restricted areas of all memp elements in every pool.
|
228 |
|
|
*/
|
229 |
|
|
static void
|
230 |
|
|
memp_overflow_init(void)
|
231 |
|
|
{
|
232 |
|
|
u16_t i, j;
|
233 |
|
|
struct memp *p;
|
234 |
|
|
u8_t *m;
|
235 |
|
|
|
236 |
|
|
p = LWIP_MEM_ALIGN(memp_memory);
|
237 |
|
|
for (i = 0; i < MEMP_MAX; ++i) {
|
238 |
|
|
p = p;
|
239 |
|
|
for (j = 0; j < memp_num[i]; ++j) {
|
240 |
|
|
#if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
|
241 |
|
|
m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
|
242 |
|
|
memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
|
243 |
|
|
#endif
|
244 |
|
|
#if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
|
245 |
|
|
m = (u8_t*)p + MEMP_SIZE + memp_sizes[i];
|
246 |
|
|
memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
|
247 |
|
|
#endif
|
248 |
|
|
p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
|
249 |
|
|
}
|
250 |
|
|
}
|
251 |
|
|
}
|
252 |
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
253 |
|
|
|
254 |
|
|
/**
|
255 |
|
|
* Initialize this module.
|
256 |
|
|
*
|
257 |
|
|
* Carves out memp_memory into linked lists for each pool-type.
|
258 |
|
|
*/
|
259 |
|
|
void
|
260 |
|
|
memp_init(void)
|
261 |
|
|
{
|
262 |
|
|
struct memp *memp;
|
263 |
|
|
u16_t i, j;
|
264 |
|
|
|
265 |
|
|
for (i = 0; i < MEMP_MAX; ++i) {
|
266 |
|
|
MEMP_STATS_AVAIL(used, i, 0);
|
267 |
|
|
MEMP_STATS_AVAIL(max, i, 0);
|
268 |
|
|
MEMP_STATS_AVAIL(err, i, 0);
|
269 |
|
|
MEMP_STATS_AVAIL(avail, i, memp_num[i]);
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
memp = LWIP_MEM_ALIGN(memp_memory);
|
273 |
|
|
/* for every pool: */
|
274 |
|
|
for (i = 0; i < MEMP_MAX; ++i) {
|
275 |
|
|
memp_tab[i] = NULL;
|
276 |
|
|
/* create a linked list of memp elements */
|
277 |
|
|
for (j = 0; j < memp_num[i]; ++j) {
|
278 |
|
|
memp->next = memp_tab[i];
|
279 |
|
|
memp_tab[i] = memp;
|
280 |
|
|
memp = (struct memp *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]
|
281 |
|
|
#if MEMP_OVERFLOW_CHECK
|
282 |
|
|
+ MEMP_SANITY_REGION_AFTER_ALIGNED
|
283 |
|
|
#endif
|
284 |
|
|
);
|
285 |
|
|
}
|
286 |
|
|
}
|
287 |
|
|
#if MEMP_OVERFLOW_CHECK
|
288 |
|
|
memp_overflow_init();
|
289 |
|
|
/* check everything a first time to see if it worked */
|
290 |
|
|
memp_overflow_check_all();
|
291 |
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
292 |
|
|
}
|
293 |
|
|
|
294 |
|
|
/**
|
295 |
|
|
* Get an element from a specific pool.
|
296 |
|
|
*
|
297 |
|
|
* @param type the pool to get an element from
|
298 |
|
|
*
|
299 |
|
|
* the debug version has two more parameters:
|
300 |
|
|
* @param file file name calling this function
|
301 |
|
|
* @param line number of line where this function is called
|
302 |
|
|
*
|
303 |
|
|
* @return a pointer to the allocated memory or a NULL pointer on error
|
304 |
|
|
*/
|
305 |
|
|
void *
|
306 |
|
|
#if !MEMP_OVERFLOW_CHECK
|
307 |
|
|
memp_malloc(memp_t type)
|
308 |
|
|
#else
|
309 |
|
|
memp_malloc_fn(memp_t type, const char* file, const int line)
|
310 |
|
|
#endif
|
311 |
|
|
{
|
312 |
|
|
struct memp *memp;
|
313 |
|
|
SYS_ARCH_DECL_PROTECT(old_level);
|
314 |
|
|
|
315 |
|
|
LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
|
316 |
|
|
|
317 |
|
|
SYS_ARCH_PROTECT(old_level);
|
318 |
|
|
#if MEMP_OVERFLOW_CHECK >= 2
|
319 |
|
|
memp_overflow_check_all();
|
320 |
|
|
#endif /* MEMP_OVERFLOW_CHECK >= 2 */
|
321 |
|
|
|
322 |
|
|
memp = memp_tab[type];
|
323 |
|
|
|
324 |
|
|
if (memp != NULL) {
|
325 |
|
|
memp_tab[type] = memp->next;
|
326 |
|
|
#if MEMP_OVERFLOW_CHECK
|
327 |
|
|
memp->next = NULL;
|
328 |
|
|
memp->file = file;
|
329 |
|
|
memp->line = line;
|
330 |
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
331 |
|
|
MEMP_STATS_INC_USED(used, type);
|
332 |
|
|
LWIP_ASSERT("memp_malloc: memp properly aligned",
|
333 |
|
|
((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
|
334 |
|
|
memp = (struct memp*)((u8_t*)memp + MEMP_SIZE);
|
335 |
|
|
} else {
|
336 |
|
|
LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", memp_desc[type]));
|
337 |
|
|
MEMP_STATS_INC(err, type);
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
SYS_ARCH_UNPROTECT(old_level);
|
341 |
|
|
|
342 |
|
|
return memp;
|
343 |
|
|
}
|
344 |
|
|
|
345 |
|
|
/**
|
346 |
|
|
* Put an element back into its pool.
|
347 |
|
|
*
|
348 |
|
|
* @param type the pool where to put mem
|
349 |
|
|
* @param mem the memp element to free
|
350 |
|
|
*/
|
351 |
|
|
void
|
352 |
|
|
memp_free(memp_t type, void *mem)
|
353 |
|
|
{
|
354 |
|
|
struct memp *memp;
|
355 |
|
|
SYS_ARCH_DECL_PROTECT(old_level);
|
356 |
|
|
|
357 |
|
|
if (mem == NULL) {
|
358 |
|
|
return;
|
359 |
|
|
}
|
360 |
|
|
LWIP_ASSERT("memp_free: mem properly aligned",
|
361 |
|
|
((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
|
362 |
|
|
|
363 |
|
|
memp = (struct memp *)((u8_t*)mem - MEMP_SIZE);
|
364 |
|
|
|
365 |
|
|
SYS_ARCH_PROTECT(old_level);
|
366 |
|
|
#if MEMP_OVERFLOW_CHECK
|
367 |
|
|
#if MEMP_OVERFLOW_CHECK >= 2
|
368 |
|
|
memp_overflow_check_all();
|
369 |
|
|
#else
|
370 |
|
|
memp_overflow_check_element(memp, memp_sizes[type]);
|
371 |
|
|
#endif /* MEMP_OVERFLOW_CHECK >= 2 */
|
372 |
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
373 |
|
|
|
374 |
|
|
MEMP_STATS_DEC(used, type);
|
375 |
|
|
|
376 |
|
|
memp->next = memp_tab[type];
|
377 |
|
|
memp_tab[type] = memp;
|
378 |
|
|
|
379 |
|
|
#if MEMP_SANITY_CHECK
|
380 |
|
|
LWIP_ASSERT("memp sanity", memp_sanity());
|
381 |
|
|
#endif /* MEMP_SANITY_CHECK */
|
382 |
|
|
|
383 |
|
|
SYS_ARCH_UNPROTECT(old_level);
|
384 |
|
|
}
|
385 |
|
|
|
386 |
|
|
#endif /* MEMP_MEM_MALLOC */
|