Maxim nexpaq / nexpaq_dev
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_memp.c Source File

lwip_memp.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Dynamic pool memory manager
00004  *
00005  * lwIP has dedicated pools for many structures (netconn, protocol control blocks,
00006  * packet buffers, ...). All these pools are managed here.
00007  */
00008 
00009 /*
00010  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  * 2. Redistributions in binary form must reproduce the above copyright notice,
00019  *    this list of conditions and the following disclaimer in the documentation
00020  *    and/or other materials provided with the distribution.
00021  * 3. The name of the author may not be used to endorse or promote products
00022  *    derived from this software without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00025  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00026  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00027  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00028  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00029  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00032  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00033  * OF SUCH DAMAGE.
00034  *
00035  * This file is part of the lwIP TCP/IP stack.
00036  *
00037  * Author: Adam Dunkels <adam@sics.se>
00038  *
00039  */
00040 
00041 #include "lwip/opt.h"
00042 
00043 #include "lwip/memp.h"
00044 #include "lwip/pbuf.h"
00045 #include "lwip/udp.h"
00046 #include "lwip/raw.h"
00047 #include "lwip/tcp_impl.h"
00048 #include "lwip/igmp.h"
00049 #include "lwip/api.h"
00050 #include "lwip/api_msg.h"
00051 #include "lwip/tcpip.h"
00052 #include "lwip/sys.h"
00053 #include "lwip/timers.h"
00054 #include "lwip/stats.h"
00055 #include "netif/etharp.h"
00056 #include "lwip/ip_frag.h"
00057 #include "lwip/snmp_structs.h"
00058 #include "lwip/snmp_msg.h"
00059 #include "lwip/dns.h"
00060 #include "netif/ppp_oe.h"
00061 
00062 #include <string.h>
00063 
00064 #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
00065 
00066 struct memp {
00067   struct memp *next;
00068 #if MEMP_OVERFLOW_CHECK
00069   const char *file;
00070   int line;
00071 #endif /* MEMP_OVERFLOW_CHECK */
00072 };
00073 
00074 #if MEMP_OVERFLOW_CHECK
00075 /* if MEMP_OVERFLOW_CHECK is turned on, we reserve some bytes at the beginning
00076  * and at the end of each element, initialize them as 0xcd and check
00077  * them later. */
00078 /* If MEMP_OVERFLOW_CHECK is >= 2, on every call to memp_malloc or memp_free,
00079  * every single element in each pool is checked!
00080  * This is VERY SLOW but also very helpful. */
00081 /* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in
00082  * lwipopts.h to change the amount reserved for checking. */
00083 #ifndef MEMP_SANITY_REGION_BEFORE
00084 #define MEMP_SANITY_REGION_BEFORE  16
00085 #endif /* MEMP_SANITY_REGION_BEFORE*/
00086 #if MEMP_SANITY_REGION_BEFORE > 0
00087 #define MEMP_SANITY_REGION_BEFORE_ALIGNED    LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE)
00088 #else
00089 #define MEMP_SANITY_REGION_BEFORE_ALIGNED    0
00090 #endif /* MEMP_SANITY_REGION_BEFORE*/
00091 #ifndef MEMP_SANITY_REGION_AFTER
00092 #define MEMP_SANITY_REGION_AFTER   16
00093 #endif /* MEMP_SANITY_REGION_AFTER*/
00094 #if MEMP_SANITY_REGION_AFTER > 0
00095 #define MEMP_SANITY_REGION_AFTER_ALIGNED     LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER)
00096 #else
00097 #define MEMP_SANITY_REGION_AFTER_ALIGNED     0
00098 #endif /* MEMP_SANITY_REGION_AFTER*/
00099 
00100 /* MEMP_SIZE: save space for struct memp and for sanity check */
00101 #define MEMP_SIZE          (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED)
00102 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED)
00103 
00104 #else /* MEMP_OVERFLOW_CHECK */
00105 
00106 /* No sanity checks
00107  * We don't need to preserve the struct memp while not allocated, so we
00108  * can save a little space and set MEMP_SIZE to 0.
00109  */
00110 #define MEMP_SIZE           0
00111 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
00112 
00113 #endif /* MEMP_OVERFLOW_CHECK */
00114 
00115 /** This array holds the first free element of each pool.
00116  *  Elements form a linked list. */
00117 static struct memp *memp_tab[MEMP_MAX];
00118 
00119 #else /* MEMP_MEM_MALLOC */
00120 
00121 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
00122 
00123 #endif /* MEMP_MEM_MALLOC */
00124 
00125 /** This array holds the element sizes of each pool. */
00126 #if !MEM_USE_POOLS && !MEMP_MEM_MALLOC
00127 static
00128 #endif
00129 const u16_t memp_sizes[MEMP_MAX] = {
00130 #define LWIP_MEMPOOL(name,num,size,desc)  LWIP_MEM_ALIGN_SIZE(size),
00131 #include "lwip/memp_std.h"
00132 };
00133 
00134 #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
00135 
00136 /** This array holds the number of elements in each pool. */
00137 static const u16_t memp_num[MEMP_MAX] = {
00138 #define LWIP_MEMPOOL(name,num,size,desc)  (num),
00139 #include "lwip/memp_std.h"
00140 };
00141 
00142 /** This array holds a textual description of each pool. */
00143 #ifdef LWIP_DEBUG
00144 static const char *memp_desc[MEMP_MAX] = {
00145 #define LWIP_MEMPOOL(name,num,size,desc)  (desc),
00146 #include "lwip/memp_std.h"
00147 };
00148 #endif /* LWIP_DEBUG */
00149 
00150 #if MEMP_SEPARATE_POOLS
00151 
00152 /** This creates each memory pool. These are named memp_memory_XXX_base (where
00153  * XXX is the name of the pool defined in memp_std.h).
00154  * To relocate a pool, declare it as extern in cc.h. Example for GCC:
00155  *   extern u8_t __attribute__((section(".onchip_mem"))) memp_memory_UDP_PCB_base[];
00156  */
00157 #define LWIP_MEMPOOL(name,num,size,desc) u8_t memp_memory_ ## name ## _base \
00158   [((num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size)))];
00159 #include "lwip/memp_std.h"
00160 
00161 /** This array holds the base of each memory pool. */
00162 static u8_t *const memp_bases[] = {
00163 #define LWIP_MEMPOOL(name,num,size,desc) memp_memory_ ## name ## _base,
00164 #include "lwip/memp_std.h"
00165 };
00166 
00167 #else /* MEMP_SEPARATE_POOLS */
00168 
00169 #if defined(TARGET_LPC1768)
00170 #  if defined (__ICCARM__)
00171 #     define ETHMEM_SECTION
00172 #  elif defined(TOOLCHAIN_GCC_CR)
00173 #     define ETHMEM_SECTION __attribute__((section(".data.$RamPeriph32")))
00174 #  else
00175 #     define ETHMEM_SECTION __attribute__((section("AHBSRAM1"),aligned))
00176 #  endif
00177 #else
00178 #define ETHMEM_SECTION
00179 #endif
00180 
00181 /** This is the actual memory used by the pools (all pools in one big block). */
00182 #if defined (__ICCARM__)
00183 #pragma location = ".ethusbram"
00184 #endif
00185 static u8_t memp_memory[MEM_ALIGNMENT - 1
00186 #define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )
00187 #include "lwip/memp_std.h"
00188 ] ETHMEM_SECTION;
00189 
00190 #endif /* MEMP_SEPARATE_POOLS */
00191 
00192 #if MEMP_SANITY_CHECK
00193 /**
00194  * Check that memp-lists don't form a circle
00195  */
00196 static int
00197 memp_sanity(void)
00198 {
00199   s16_t i, c;
00200   struct memp *m, *n;
00201 
00202   for (i = 0; i < MEMP_MAX; i++) {
00203     for (m = memp_tab[i]; m != NULL; m = m->next) {
00204       c = 1;
00205       for (n = memp_tab[i]; n != NULL; n = n->next) {
00206         if (n == m && --c < 0) {
00207           return 0;
00208         }
00209       }
00210     }
00211   }
00212   return 1;
00213 }
00214 #endif /* MEMP_SANITY_CHECK*/
00215 #if MEMP_OVERFLOW_CHECK
00216 #if defined(LWIP_DEBUG) && MEMP_STATS
00217 static const char * memp_overflow_names[] = {
00218 #define LWIP_MEMPOOL(name,num,size,desc) "/"desc,
00219 #include "lwip/memp_std.h"
00220   };
00221 #endif
00222 
00223 /**
00224  * Check if a memp element was victim of an overflow
00225  * (e.g. the restricted area after it has been altered)
00226  *
00227  * @param p the memp element to check
00228  * @param memp_type the pool p comes from
00229  */
00230 static void
00231 memp_overflow_check_element_overflow(struct memp *p, u16_t memp_type)
00232 {
00233   u16_t k;
00234   u8_t *m;
00235 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
00236   m = (u8_t*)p + MEMP_SIZE + memp_sizes[memp_type];
00237   for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
00238     if (m[k] != 0xcd) {
00239       char errstr[128] = "detected memp overflow in pool ";
00240       char digit[] = "0";
00241       if(memp_type >= 10) {
00242         digit[0] = '0' + (memp_type/10);
00243         strcat(errstr, digit);
00244       }
00245       digit[0] = '0' + (memp_type%10);
00246       strcat(errstr, digit);
00247 #if defined(LWIP_DEBUG) && MEMP_STATS
00248       strcat(errstr, memp_overflow_names[memp_type]);
00249 #endif
00250       LWIP_ASSERT(errstr, 0);
00251     }
00252   }
00253 #endif
00254 }
00255 
00256 /**
00257  * Check if a memp element was victim of an underflow
00258  * (e.g. the restricted area before it has been altered)
00259  *
00260  * @param p the memp element to check
00261  * @param memp_type the pool p comes from
00262  */
00263 static void
00264 memp_overflow_check_element_underflow(struct memp *p, u16_t memp_type)
00265 {
00266   u16_t k;
00267   u8_t *m;
00268 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
00269   m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
00270   for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
00271     if (m[k] != 0xcd) {
00272       char errstr[128] = "detected memp underflow in pool ";
00273       char digit[] = "0";
00274       if(memp_type >= 10) {
00275         digit[0] = '0' + (memp_type/10);
00276         strcat(errstr, digit);
00277       }
00278       digit[0] = '0' + (memp_type%10);
00279       strcat(errstr, digit);
00280 #if defined(LWIP_DEBUG) && MEMP_STATS
00281       strcat(errstr, memp_overflow_names[memp_type]);
00282 #endif
00283       LWIP_ASSERT(errstr, 0);
00284     }
00285   }
00286 #endif
00287 }
00288 
00289 /**
00290  * Do an overflow check for all elements in every pool.
00291  *
00292  * @see memp_overflow_check_element for a description of the check
00293  */
00294 static void
00295 memp_overflow_check_all(void)
00296 {
00297   u16_t i, j;
00298   struct memp *p;
00299 
00300   p = (struct memp *)LWIP_MEM_ALIGN(memp_memory);
00301   for (i = 0; i < MEMP_MAX; ++i) {
00302     p = p;
00303     for (j = 0; j < memp_num[i]; ++j) {
00304       memp_overflow_check_element_overflow(p, i);
00305       p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
00306     }
00307   }
00308   p = (struct memp *)LWIP_MEM_ALIGN(memp_memory);
00309   for (i = 0; i < MEMP_MAX; ++i) {
00310     p = p;
00311     for (j = 0; j < memp_num[i]; ++j) {
00312       memp_overflow_check_element_underflow(p, i);
00313       p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
00314     }
00315   }
00316 }
00317 
00318 /**
00319  * Initialize the restricted areas of all memp elements in every pool.
00320  */
00321 static void
00322 memp_overflow_init(void)
00323 {
00324   u16_t i, j;
00325   struct memp *p;
00326   u8_t *m;
00327 
00328   p = (struct memp *)LWIP_MEM_ALIGN(memp_memory);
00329   for (i = 0; i < MEMP_MAX; ++i) {
00330     p = p;
00331     for (j = 0; j < memp_num[i]; ++j) {
00332 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
00333       m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
00334       memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
00335 #endif
00336 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
00337       m = (u8_t*)p + MEMP_SIZE + memp_sizes[i];
00338       memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
00339 #endif
00340       p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
00341     }
00342   }
00343 }
00344 #endif /* MEMP_OVERFLOW_CHECK */
00345 
00346 /**
00347  * Initialize this module.
00348  *
00349  * Carves out memp_memory into linked lists for each pool-type.
00350  */
00351 void
00352 memp_init(void)
00353 {
00354   struct memp *memp;
00355   u16_t i, j;
00356 
00357   for (i = 0; i < MEMP_MAX; ++i) {
00358     MEMP_STATS_AVAIL(used, i, 0);
00359     MEMP_STATS_AVAIL(max, i, 0);
00360     MEMP_STATS_AVAIL(err, i, 0);
00361     MEMP_STATS_AVAIL(avail, i, memp_num[i]);
00362   }
00363 
00364 #if !MEMP_SEPARATE_POOLS
00365   memp = (struct memp *)LWIP_MEM_ALIGN(memp_memory);
00366 #endif /* !MEMP_SEPARATE_POOLS */
00367   /* for every pool: */
00368   for (i = 0; i < MEMP_MAX; ++i) {
00369     memp_tab[i] = NULL;
00370 #if MEMP_SEPARATE_POOLS
00371     memp = (struct memp*)memp_bases[i];
00372 #endif /* MEMP_SEPARATE_POOLS */
00373     /* create a linked list of memp elements */
00374     for (j = 0; j < memp_num[i]; ++j) {
00375       memp->next = memp_tab[i];
00376       memp_tab[i] = memp;
00377       memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]
00378 #if MEMP_OVERFLOW_CHECK
00379         + MEMP_SANITY_REGION_AFTER_ALIGNED
00380 #endif
00381       );
00382     }
00383   }
00384 #if MEMP_OVERFLOW_CHECK
00385   memp_overflow_init();
00386   /* check everything a first time to see if it worked */
00387   memp_overflow_check_all();
00388 #endif /* MEMP_OVERFLOW_CHECK */
00389 }
00390 
00391 /**
00392  * Get an element from a specific pool.
00393  *
00394  * @param type the pool to get an element from
00395  *
00396  * the debug version has two more parameters:
00397  * @param file file name calling this function
00398  * @param line number of line where this function is called
00399  *
00400  * @return a pointer to the allocated memory or a NULL pointer on error
00401  */
00402 void *
00403 #if !MEMP_OVERFLOW_CHECK
00404 memp_malloc(memp_t type)
00405 #else
00406 memp_malloc_fn(memp_t type, const char* file, const int line)
00407 #endif
00408 {
00409   struct memp *memp;
00410   SYS_ARCH_DECL_PROTECT(old_level);
00411 
00412   LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
00413 
00414   SYS_ARCH_PROTECT(old_level);
00415 #if MEMP_OVERFLOW_CHECK >= 2
00416   memp_overflow_check_all();
00417 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
00418 
00419   memp = memp_tab[type];
00420 
00421   if (memp != NULL) {
00422     memp_tab[type] = memp->next;
00423 #if MEMP_OVERFLOW_CHECK
00424     memp->next = NULL;
00425     memp->file = file;
00426     memp->line = line;
00427 #endif /* MEMP_OVERFLOW_CHECK */
00428     MEMP_STATS_INC_USED(used, type);
00429     LWIP_ASSERT("memp_malloc: memp properly aligned",
00430                 ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
00431     memp = (struct memp*)(void *)((u8_t*)memp + MEMP_SIZE);
00432   } else {
00433     LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", memp_desc[type]));
00434     MEMP_STATS_INC(err, type);
00435   }
00436 
00437   SYS_ARCH_UNPROTECT(old_level);
00438 
00439   return memp;
00440 }
00441 
00442 /**
00443  * Put an element back into its pool.
00444  *
00445  * @param type the pool where to put mem
00446  * @param mem the memp element to free
00447  */
00448 void
00449 memp_free(memp_t type, void *mem)
00450 {
00451   struct memp *memp;
00452   SYS_ARCH_DECL_PROTECT(old_level);
00453 
00454   if (mem == NULL) {
00455     return;
00456   }
00457   LWIP_ASSERT("memp_free: mem properly aligned",
00458                 ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
00459 
00460   memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);
00461 
00462   SYS_ARCH_PROTECT(old_level);
00463 #if MEMP_OVERFLOW_CHECK
00464 #if MEMP_OVERFLOW_CHECK >= 2
00465   memp_overflow_check_all();
00466 #else
00467   memp_overflow_check_element_overflow(memp, type);
00468   memp_overflow_check_element_underflow(memp, type);
00469 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
00470 #endif /* MEMP_OVERFLOW_CHECK */
00471 
00472   MEMP_STATS_DEC(used, type);
00473 
00474   memp->next = memp_tab[type];
00475   memp_tab[type] = memp;
00476 
00477 #if MEMP_SANITY_CHECK
00478   LWIP_ASSERT("memp sanity", memp_sanity());
00479 #endif /* MEMP_SANITY_CHECK */
00480 
00481   SYS_ARCH_UNPROTECT(old_level);
00482 }
00483 
00484 #endif /* MEMP_MEM_MALLOC */