Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include "lwip/opt.h"
00057
00058 #if !MEM_LIBC_MALLOC
00059
00060 #include "lwip/def.h"
00061 #include "lwip/mem.h"
00062 #include "lwip/sys.h"
00063 #include "lwip/stats.h"
00064 #include "lwip/err.h"
00065
00066 #include <string.h>
00067
00068 #if MEM_USE_POOLS
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 void *
00079 mem_malloc(mem_size_t size)
00080 {
00081 void *ret;
00082 struct memp_malloc_helper *element;
00083 memp_t poolnr;
00084 mem_size_t required_size = size + LWIP_MEM_ALIGN_SIZE(sizeof(struct memp_malloc_helper));
00085
00086 for (poolnr = MEMP_POOL_FIRST; poolnr <= MEMP_POOL_LAST; poolnr = (memp_t)(poolnr + 1)) {
00087 #if MEM_USE_POOLS_TRY_BIGGER_POOL
00088 again:
00089 #endif
00090
00091
00092 if (required_size <= memp_sizes[poolnr]) {
00093 break;
00094 }
00095 }
00096 if (poolnr > MEMP_POOL_LAST) {
00097 LWIP_ASSERT("mem_malloc(): no pool is that big!", 0);
00098 return NULL;
00099 }
00100 element = (struct memp_malloc_helper*)memp_malloc(poolnr);
00101 if (element == NULL) {
00102
00103
00104 #if MEM_USE_POOLS_TRY_BIGGER_POOL
00105
00106 if (poolnr < MEMP_POOL_LAST) {
00107 poolnr++;
00108 goto again;
00109 }
00110 #endif
00111 return NULL;
00112 }
00113
00114
00115 element->poolnr = poolnr;
00116
00117 ret = (u8_t*)element + LWIP_MEM_ALIGN_SIZE(sizeof(struct memp_malloc_helper));
00118
00119 return ret;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129 void
00130 mem_free(void *rmem)
00131 {
00132 struct memp_malloc_helper *hmem;
00133
00134 LWIP_ASSERT("rmem != NULL", (rmem != NULL));
00135 LWIP_ASSERT("rmem == MEM_ALIGN(rmem)", (rmem == LWIP_MEM_ALIGN(rmem)));
00136
00137
00138 hmem = (struct memp_malloc_helper*)(void*)((u8_t*)rmem - LWIP_MEM_ALIGN_SIZE(sizeof(struct memp_malloc_helper)));
00139
00140 LWIP_ASSERT("hmem != NULL", (hmem != NULL));
00141 LWIP_ASSERT("hmem == MEM_ALIGN(hmem)", (hmem == LWIP_MEM_ALIGN(hmem)));
00142 LWIP_ASSERT("hmem->poolnr < MEMP_MAX", (hmem->poolnr < MEMP_MAX));
00143
00144
00145 memp_free(hmem->poolnr, hmem);
00146 }
00147
00148 #else
00149
00150
00151
00152
00153
00154
00155
00156 struct mem {
00157
00158 mem_size_t next;
00159
00160 mem_size_t prev;
00161
00162 u8_t used;
00163 };
00164
00165
00166
00167
00168 #ifndef MIN_SIZE
00169 #define MIN_SIZE 12
00170 #endif
00171
00172 #define MIN_SIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(MIN_SIZE)
00173 #define SIZEOF_STRUCT_MEM LWIP_MEM_ALIGN_SIZE(sizeof(struct mem))
00174 #define MEM_SIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEM_SIZE)
00175
00176
00177
00178
00179
00180 #ifndef LWIP_RAM_HEAP_POINTER
00181
00182 u8_t ram_heap[MEM_SIZE_ALIGNED + (2*SIZEOF_STRUCT_MEM) + MEM_ALIGNMENT];
00183 #define LWIP_RAM_HEAP_POINTER ram_heap
00184 #endif
00185
00186
00187 static u8_t *ram;
00188
00189 static struct mem *ram_end;
00190
00191 static struct mem *lfree;
00192
00193
00194 #if !NO_SYS
00195 static sys_mutex_t mem_mutex;
00196 #endif
00197
00198 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
00199
00200 static volatile u8_t mem_free_count;
00201
00202
00203 #define LWIP_MEM_FREE_DECL_PROTECT() SYS_ARCH_DECL_PROTECT(lev_free)
00204 #define LWIP_MEM_FREE_PROTECT() SYS_ARCH_PROTECT(lev_free)
00205 #define LWIP_MEM_FREE_UNPROTECT() SYS_ARCH_UNPROTECT(lev_free)
00206 #define LWIP_MEM_ALLOC_DECL_PROTECT() SYS_ARCH_DECL_PROTECT(lev_alloc)
00207 #define LWIP_MEM_ALLOC_PROTECT() SYS_ARCH_PROTECT(lev_alloc)
00208 #define LWIP_MEM_ALLOC_UNPROTECT() SYS_ARCH_UNPROTECT(lev_alloc)
00209
00210 #else
00211
00212
00213 #define LWIP_MEM_FREE_DECL_PROTECT()
00214 #define LWIP_MEM_FREE_PROTECT() sys_mutex_lock(&mem_mutex)
00215 #define LWIP_MEM_FREE_UNPROTECT() sys_mutex_unlock(&mem_mutex)
00216
00217 #define LWIP_MEM_ALLOC_DECL_PROTECT()
00218 #define LWIP_MEM_ALLOC_PROTECT()
00219 #define LWIP_MEM_ALLOC_UNPROTECT()
00220
00221 #endif
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 static void
00236 plug_holes(struct mem *mem)
00237 {
00238 struct mem *nmem;
00239 struct mem *pmem;
00240
00241 LWIP_ASSERT("plug_holes: mem >= ram", (u8_t *)mem >= ram);
00242 LWIP_ASSERT("plug_holes: mem < ram_end", (u8_t *)mem < (u8_t *)ram_end);
00243 LWIP_ASSERT("plug_holes: mem->used == 0", mem->used == 0);
00244
00245
00246 LWIP_ASSERT("plug_holes: mem->next <= MEM_SIZE_ALIGNED", mem->next <= MEM_SIZE_ALIGNED);
00247
00248 nmem = (struct mem *)(void *)&ram[mem->next];
00249 if (mem != nmem && nmem->used == 0 && (u8_t *)nmem != (u8_t *)ram_end) {
00250
00251 if (lfree == nmem) {
00252 lfree = mem;
00253 }
00254 mem->next = nmem->next;
00255 ((struct mem *)(void *)&ram[nmem->next])->prev = (mem_size_t)((u8_t *)mem - ram);
00256 }
00257
00258
00259 pmem = (struct mem *)(void *)&ram[mem->prev];
00260 if (pmem != mem && pmem->used == 0) {
00261
00262 if (lfree == mem) {
00263 lfree = pmem;
00264 }
00265 pmem->next = mem->next;
00266 ((struct mem *)(void *)&ram[mem->next])->prev = (mem_size_t)((u8_t *)pmem - ram);
00267 }
00268 }
00269
00270
00271
00272
00273 void
00274 mem_init(void)
00275 {
00276 struct mem *mem;
00277
00278 LWIP_ASSERT("Sanity check alignment",
00279 (SIZEOF_STRUCT_MEM & (MEM_ALIGNMENT-1)) == 0);
00280
00281
00282 ram = (u8_t *)LWIP_MEM_ALIGN(LWIP_RAM_HEAP_POINTER);
00283
00284 mem = (struct mem *)(void *)ram;
00285 mem->next = MEM_SIZE_ALIGNED;
00286 mem->prev = 0;
00287 mem->used = 0;
00288
00289 ram_end = (struct mem *)(void *)&ram[MEM_SIZE_ALIGNED];
00290 ram_end->used = 1;
00291 ram_end->next = MEM_SIZE_ALIGNED;
00292 ram_end->prev = MEM_SIZE_ALIGNED;
00293
00294
00295 lfree = (struct mem *)(void *)ram;
00296
00297 MEM_STATS_AVAIL(avail, MEM_SIZE_ALIGNED);
00298
00299 if(sys_mutex_new(&mem_mutex) != ERR_OK) {
00300 LWIP_ASSERT("failed to create mem_mutex", 0);
00301 }
00302 }
00303
00304
00305
00306
00307
00308
00309
00310 void
00311 mem_free(void *rmem)
00312 {
00313 struct mem *mem;
00314 LWIP_MEM_FREE_DECL_PROTECT();
00315
00316 if (rmem == NULL) {
00317 LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("mem_free(p == NULL) was called.\n"));
00318 return;
00319 }
00320 LWIP_ASSERT("mem_free: sanity check alignment", (((mem_ptr_t)rmem) & (MEM_ALIGNMENT-1)) == 0);
00321
00322 LWIP_ASSERT("mem_free: legal memory", (u8_t *)rmem >= (u8_t *)ram &&
00323 (u8_t *)rmem < (u8_t *)ram_end);
00324
00325 if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) {
00326 SYS_ARCH_DECL_PROTECT(lev);
00327 LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_free: illegal memory\n"));
00328
00329 SYS_ARCH_PROTECT(lev);
00330 MEM_STATS_INC(illegal);
00331 SYS_ARCH_UNPROTECT(lev);
00332 return;
00333 }
00334
00335 LWIP_MEM_FREE_PROTECT();
00336
00337 mem = (struct mem *)(void *)((u8_t *)rmem - SIZEOF_STRUCT_MEM);
00338
00339 LWIP_ASSERT("mem_free: mem->used", mem->used);
00340
00341 mem->used = 0;
00342
00343 if (mem < lfree) {
00344
00345 lfree = mem;
00346 }
00347
00348 MEM_STATS_DEC_USED(used, mem->next - (mem_size_t)(((u8_t *)mem - ram)));
00349
00350
00351 plug_holes(mem);
00352 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
00353 mem_free_count = 1;
00354 #endif
00355 LWIP_MEM_FREE_UNPROTECT();
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368 void *
00369 mem_trim(void *rmem, mem_size_t newsize)
00370 {
00371 mem_size_t size;
00372 mem_size_t ptr, ptr2;
00373 struct mem *mem, *mem2;
00374
00375 LWIP_MEM_FREE_DECL_PROTECT();
00376
00377
00378
00379 newsize = LWIP_MEM_ALIGN_SIZE(newsize);
00380
00381 if(newsize < MIN_SIZE_ALIGNED) {
00382
00383 newsize = MIN_SIZE_ALIGNED;
00384 }
00385
00386 if (newsize > MEM_SIZE_ALIGNED) {
00387 return NULL;
00388 }
00389
00390 LWIP_ASSERT("mem_trim: legal memory", (u8_t *)rmem >= (u8_t *)ram &&
00391 (u8_t *)rmem < (u8_t *)ram_end);
00392
00393 if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) {
00394 SYS_ARCH_DECL_PROTECT(lev);
00395 LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_trim: illegal memory\n"));
00396
00397 SYS_ARCH_PROTECT(lev);
00398 MEM_STATS_INC(illegal);
00399 SYS_ARCH_UNPROTECT(lev);
00400 return rmem;
00401 }
00402
00403 mem = (struct mem *)(void *)((u8_t *)rmem - SIZEOF_STRUCT_MEM);
00404
00405 ptr = (mem_size_t)((u8_t *)mem - ram);
00406
00407 size = mem->next - ptr - SIZEOF_STRUCT_MEM;
00408 LWIP_ASSERT("mem_trim can only shrink memory", newsize <= size);
00409 if (newsize > size) {
00410
00411 return NULL;
00412 }
00413 if (newsize == size) {
00414
00415 return rmem;
00416 }
00417
00418
00419 LWIP_MEM_FREE_PROTECT();
00420
00421 mem2 = (struct mem *)(void *)&ram[mem->next];
00422 if(mem2->used == 0) {
00423
00424 mem_size_t next;
00425
00426 next = mem2->next;
00427
00428 ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
00429 if (lfree == mem2) {
00430 lfree = (struct mem *)(void *)&ram[ptr2];
00431 }
00432 mem2 = (struct mem *)(void *)&ram[ptr2];
00433 mem2->used = 0;
00434
00435 mem2->next = next;
00436
00437 mem2->prev = ptr;
00438
00439 mem->next = ptr2;
00440
00441
00442
00443 if (mem2->next != MEM_SIZE_ALIGNED) {
00444 ((struct mem *)(void *)&ram[mem2->next])->prev = ptr2;
00445 }
00446 MEM_STATS_DEC_USED(used, (size - newsize));
00447
00448 } else if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED <= size) {
00449
00450
00451
00452
00453
00454
00455
00456 ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
00457 mem2 = (struct mem *)(void *)&ram[ptr2];
00458 if (mem2 < lfree) {
00459 lfree = mem2;
00460 }
00461 mem2->used = 0;
00462 mem2->next = mem->next;
00463 mem2->prev = ptr;
00464 mem->next = ptr2;
00465 if (mem2->next != MEM_SIZE_ALIGNED) {
00466 ((struct mem *)(void *)&ram[mem2->next])->prev = ptr2;
00467 }
00468 MEM_STATS_DEC_USED(used, (size - newsize));
00469
00470 }
00471
00472
00473
00474
00475
00476
00477 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
00478 mem_free_count = 1;
00479 #endif
00480 LWIP_MEM_FREE_UNPROTECT();
00481 return rmem;
00482 }
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493 void *
00494 mem_malloc(mem_size_t size)
00495 {
00496 mem_size_t ptr, ptr2;
00497 struct mem *mem, *mem2;
00498 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
00499 u8_t local_mem_free_count = 0;
00500 #endif
00501 LWIP_MEM_ALLOC_DECL_PROTECT();
00502
00503 if (size == 0) {
00504 return NULL;
00505 }
00506
00507
00508
00509 size = LWIP_MEM_ALIGN_SIZE(size);
00510
00511 if(size < MIN_SIZE_ALIGNED) {
00512
00513 size = MIN_SIZE_ALIGNED;
00514 }
00515
00516 if (size > MEM_SIZE_ALIGNED) {
00517 return NULL;
00518 }
00519
00520
00521 sys_mutex_lock(&mem_mutex);
00522 LWIP_MEM_ALLOC_PROTECT();
00523 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
00524
00525 do {
00526 local_mem_free_count = 0;
00527 #endif
00528
00529
00530
00531
00532 for (ptr = (mem_size_t)((u8_t *)lfree - ram); ptr < MEM_SIZE_ALIGNED - size;
00533 ptr = ((struct mem *)(void *)&ram[ptr])->next) {
00534 mem = (struct mem *)(void *)&ram[ptr];
00535 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
00536 mem_free_count = 0;
00537 LWIP_MEM_ALLOC_UNPROTECT();
00538
00539 LWIP_MEM_ALLOC_PROTECT();
00540 if (mem_free_count != 0) {
00541
00542
00543 local_mem_free_count = 1;
00544 break;
00545 }
00546 #endif
00547
00548 if ((!mem->used) &&
00549 (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size) {
00550
00551
00552
00553 if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >= (size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED)) {
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564 ptr2 = ptr + SIZEOF_STRUCT_MEM + size;
00565
00566 mem2 = (struct mem *)(void *)&ram[ptr2];
00567 mem2->used = 0;
00568 mem2->next = mem->next;
00569 mem2->prev = ptr;
00570
00571 mem->next = ptr2;
00572 mem->used = 1;
00573
00574 if (mem2->next != MEM_SIZE_ALIGNED) {
00575 ((struct mem *)(void *)&ram[mem2->next])->prev = ptr2;
00576 }
00577 MEM_STATS_INC_USED(used, (size + SIZEOF_STRUCT_MEM));
00578 } else {
00579
00580
00581
00582
00583
00584
00585
00586 mem->used = 1;
00587 MEM_STATS_INC_USED(used, mem->next - (mem_size_t)((u8_t *)mem - ram));
00588 }
00589 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
00590 mem_malloc_adjust_lfree:
00591 #endif
00592 if (mem == lfree) {
00593 struct mem *cur = lfree;
00594
00595 while (cur->used && cur != ram_end) {
00596 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
00597 mem_free_count = 0;
00598 LWIP_MEM_ALLOC_UNPROTECT();
00599
00600 LWIP_MEM_ALLOC_PROTECT();
00601 if (mem_free_count != 0) {
00602
00603
00604 goto mem_malloc_adjust_lfree;
00605 }
00606 #endif
00607 cur = (struct mem *)(void *)&ram[cur->next];
00608 }
00609 lfree = cur;
00610 LWIP_ASSERT("mem_malloc: !lfree->used", ((lfree == ram_end) || (!lfree->used)));
00611 }
00612 LWIP_MEM_ALLOC_UNPROTECT();
00613 sys_mutex_unlock(&mem_mutex);
00614 LWIP_ASSERT("mem_malloc: allocated memory not above ram_end.",
00615 (mem_ptr_t)mem + SIZEOF_STRUCT_MEM + size <= (mem_ptr_t)ram_end);
00616 LWIP_ASSERT("mem_malloc: allocated memory properly aligned.",
00617 ((mem_ptr_t)mem + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT == 0);
00618 LWIP_ASSERT("mem_malloc: sanity check alignment",
00619 (((mem_ptr_t)mem) & (MEM_ALIGNMENT-1)) == 0);
00620
00621 return (u8_t *)mem + SIZEOF_STRUCT_MEM;
00622 }
00623 }
00624 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
00625
00626 } while(local_mem_free_count != 0);
00627 #endif
00628 LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("mem_malloc: could not allocate %"S16_F" bytes\n", (s16_t)size));
00629 MEM_STATS_INC(err);
00630 LWIP_MEM_ALLOC_UNPROTECT();
00631 sys_mutex_unlock(&mem_mutex);
00632 return NULL;
00633 }
00634
00635 #endif
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646 void *mem_calloc(mem_size_t count, mem_size_t size)
00647 {
00648 void *p;
00649
00650
00651 p = mem_malloc(count * size);
00652 if (p) {
00653
00654 memset(p, 0, count * size);
00655 }
00656 return p;
00657 }
00658
00659 #endif