Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/lwip/core/memp.c need to be fixed.

0001 /**
0002  * @file
0003  * Dynamic pool memory manager
0004  *
0005  * lwIP has dedicated pools for many structures (netconn, protocol control blocks,
0006  * packet buffers, ...). All these pools are managed here.
0007  */
0008 
0009 /*
0010  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
0011  * All rights reserved. 
0012  * 
0013  * Redistribution and use in source and binary forms, with or without modification, 
0014  * are permitted provided that the following conditions are met:
0015  *
0016  * 1. Redistributions of source code must retain the above copyright notice,
0017  *    this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright notice,
0019  *    this list of conditions and the following disclaimer in the documentation
0020  *    and/or other materials provided with the distribution.
0021  * 3. The name of the author may not be used to endorse or promote products
0022  *    derived from this software without specific prior written permission. 
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
0025  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
0026  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
0027  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
0028  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
0029  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
0032  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
0033  * OF SUCH DAMAGE.
0034  *
0035  * This file is part of the lwIP TCP/IP stack.
0036  * 
0037  * Author: Adam Dunkels <adam@sics.se>
0038  *
0039  */
0040 
0041 #include "lwip/opt.h"
0042 
0043 #include "lwip/memp.h"
0044 #include "lwip/pbuf.h"
0045 #include "lwip/udp.h"
0046 #include "lwip/raw.h"
0047 #include "lwip/tcp.h"
0048 #include "lwip/igmp.h"
0049 #include "lwip/api.h"
0050 #include "lwip/api_msg.h"
0051 #include "lwip/tcpip.h"
0052 #include "lwip/sys.h"
0053 #include "lwip/stats.h"
0054 #include "netif/etharp.h"
0055 #include "lwip/ip_frag.h"
0056 
0057 #include <string.h>
0058 
0059 #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
0060 
0061 struct memp {
0062   struct memp *next;
0063 #if MEMP_OVERFLOW_CHECK
0064   const char *file;
0065   int line;
0066 #endif /* MEMP_OVERFLOW_CHECK */
0067 };
0068 
0069 #if MEMP_OVERFLOW_CHECK
0070 /* if MEMP_OVERFLOW_CHECK is turned on, we reserve some bytes at the beginning
0071  * and at the end of each element, initialize them as 0xcd and check
0072  * them later. */
0073 /* If MEMP_OVERFLOW_CHECK is >= 2, on every call to memp_malloc or memp_free,
0074  * every single element in each pool is checked!
0075  * This is VERY SLOW but also very helpful. */
0076 /* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in
0077  * lwipopts.h to change the amount reserved for checking. */
0078 #ifndef MEMP_SANITY_REGION_BEFORE
0079 #define MEMP_SANITY_REGION_BEFORE  16
0080 #endif /* MEMP_SANITY_REGION_BEFORE*/
0081 #if MEMP_SANITY_REGION_BEFORE > 0
0082 #define MEMP_SANITY_REGION_BEFORE_ALIGNED    LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE)
0083 #else
0084 #define MEMP_SANITY_REGION_BEFORE_ALIGNED    0
0085 #endif /* MEMP_SANITY_REGION_BEFORE*/
0086 #ifndef MEMP_SANITY_REGION_AFTER
0087 #define MEMP_SANITY_REGION_AFTER   16
0088 #endif /* MEMP_SANITY_REGION_AFTER*/
0089 #if MEMP_SANITY_REGION_AFTER > 0
0090 #define MEMP_SANITY_REGION_AFTER_ALIGNED     LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER)
0091 #else
0092 #define MEMP_SANITY_REGION_AFTER_ALIGNED     0
0093 #endif /* MEMP_SANITY_REGION_AFTER*/
0094 
0095 /* MEMP_SIZE: save space for struct memp and for sanity check */
0096 #define MEMP_SIZE          (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED)
0097 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED)
0098 
0099 #else /* MEMP_OVERFLOW_CHECK */
0100 
0101 /* No sanity checks
0102  * We don't need to preserve the struct memp while not allocated, so we
0103  * can save a little space and set MEMP_SIZE to 0.
0104  */
0105 #define MEMP_SIZE           0
0106 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
0107 
0108 #endif /* MEMP_OVERFLOW_CHECK */
0109 
0110 /** This array holds the first free element of each pool.
0111  *  Elements form a linked list. */
0112 static struct memp *memp_tab[MEMP_MAX];
0113 
0114 #else /* MEMP_MEM_MALLOC */
0115 
0116 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
0117 
0118 #endif /* MEMP_MEM_MALLOC */
0119 
0120 /** This array holds the element sizes of each pool. */
0121 #if !MEM_USE_POOLS && !MEMP_MEM_MALLOC
0122 static
0123 #endif
0124 const u16_t memp_sizes[MEMP_MAX] = {
0125 #define LWIP_MEMPOOL(name,num,size,desc)  LWIP_MEM_ALIGN_SIZE(size),
0126 #include "lwip/memp_std.h"
0127 };
0128 
0129 #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
0130 
0131 /** This array holds the number of elements in each pool. */
0132 static const u16_t memp_num[MEMP_MAX] = {
0133 #define LWIP_MEMPOOL(name,num,size,desc)  (num),
0134 #include "lwip/memp_std.h"
0135 };
0136 
0137 /** This array holds a textual description of each pool. */
0138 #ifdef LWIP_DEBUG
0139 static const char *memp_desc[MEMP_MAX] = {
0140 #define LWIP_MEMPOOL(name,num,size,desc)  (desc),
0141 #include "lwip/memp_std.h"
0142 };
0143 #endif /* LWIP_DEBUG */
0144 
0145 /** This is the actual memory used by the pools. */
0146 static u8_t memp_memory[MEM_ALIGNMENT - 1 
0147 #define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )
0148 #include "lwip/memp_std.h"
0149 ];
0150 
0151 #if MEMP_SANITY_CHECK
0152 /**
0153  * Check that memp-lists don't form a circle
0154  */
0155 static int
0156 memp_sanity(void)
0157 {
0158   s16_t i, c;
0159   struct memp *m, *n;
0160 
0161   for (i = 0; i < MEMP_MAX; i++) {
0162     for (m = memp_tab[i]; m != NULL; m = m->next) {
0163       c = 1;
0164       for (n = memp_tab[i]; n != NULL; n = n->next) {
0165         if (n == m && --c < 0) {
0166           return 0;
0167         }
0168       }
0169     }
0170   }
0171   return 1;
0172 }
0173 #endif /* MEMP_SANITY_CHECK*/
0174 #if MEMP_OVERFLOW_CHECK
0175 /**
0176  * Check if a memp element was victim of an overflow
0177  * (e.g. the restricted area after it has been altered)
0178  *
0179  * @param p the memp element to check
0180  * @param memp_size the element size of the pool p comes from
0181  */
0182 static void
0183 memp_overflow_check_element(struct memp *p, u16_t memp_size)
0184 {
0185   u16_t k;
0186   u8_t *m;
0187 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
0188   m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
0189   for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
0190     if (m[k] != 0xcd) {
0191       LWIP_ASSERT("detected memp underflow!", 0);
0192     }
0193   }
0194 #endif
0195 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
0196   m = (u8_t*)p + MEMP_SIZE + memp_size;
0197   for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
0198     if (m[k] != 0xcd) {
0199       LWIP_ASSERT("detected memp overflow!", 0);
0200     }
0201   }
0202 #endif
0203 }
0204 
0205 /**
0206  * Do an overflow check for all elements in every pool.
0207  *
0208  * @see memp_overflow_check_element for a description of the check
0209  */
0210 static void
0211 memp_overflow_check_all(void)
0212 {
0213   u16_t i, j;
0214   struct memp *p;
0215 
0216   p = LWIP_MEM_ALIGN(memp_memory);
0217   for (i = 0; i < MEMP_MAX; ++i) {
0218     p = p;
0219     for (j = 0; j < memp_num[i]; ++j) {
0220       memp_overflow_check_element(p, memp_sizes[i]);
0221       p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
0222     }
0223   }
0224 }
0225 
0226 /**
0227  * Initialize the restricted areas of all memp elements in every pool.
0228  */
0229 static void
0230 memp_overflow_init(void)
0231 {
0232   u16_t i, j;
0233   struct memp *p;
0234   u8_t *m;
0235 
0236   p = LWIP_MEM_ALIGN(memp_memory);
0237   for (i = 0; i < MEMP_MAX; ++i) {
0238     p = p;
0239     for (j = 0; j < memp_num[i]; ++j) {
0240 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
0241       m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
0242       memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
0243 #endif
0244 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
0245       m = (u8_t*)p + MEMP_SIZE + memp_sizes[i];
0246       memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
0247 #endif
0248       p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
0249     }
0250   }
0251 }
0252 #endif /* MEMP_OVERFLOW_CHECK */
0253 
0254 /**
0255  * Initialize this module.
0256  * 
0257  * Carves out memp_memory into linked lists for each pool-type.
0258  */
0259 void
0260 memp_init(void)
0261 {
0262   struct memp *memp;
0263   u16_t i, j;
0264 
0265   for (i = 0; i < MEMP_MAX; ++i) {
0266     MEMP_STATS_AVAIL(used, i, 0);
0267     MEMP_STATS_AVAIL(max, i, 0);
0268     MEMP_STATS_AVAIL(err, i, 0);
0269     MEMP_STATS_AVAIL(avail, i, memp_num[i]);
0270   }
0271 
0272   memp = LWIP_MEM_ALIGN(memp_memory);
0273   /* for every pool: */
0274   for (i = 0; i < MEMP_MAX; ++i) {
0275     memp_tab[i] = NULL;
0276     /* create a linked list of memp elements */
0277     for (j = 0; j < memp_num[i]; ++j) {
0278       memp->next = memp_tab[i];
0279       memp_tab[i] = memp;
0280       memp = (struct memp *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]
0281 #if MEMP_OVERFLOW_CHECK
0282         + MEMP_SANITY_REGION_AFTER_ALIGNED
0283 #endif
0284       );
0285     }
0286   }
0287 #if MEMP_OVERFLOW_CHECK
0288   memp_overflow_init();
0289   /* check everything a first time to see if it worked */
0290   memp_overflow_check_all();
0291 #endif /* MEMP_OVERFLOW_CHECK */
0292 }
0293 
0294 /**
0295  * Get an element from a specific pool.
0296  *
0297  * @param type the pool to get an element from
0298  *
0299  * the debug version has two more parameters:
0300  * @param file file name calling this function
0301  * @param line number of line where this function is called
0302  *
0303  * @return a pointer to the allocated memory or a NULL pointer on error
0304  */
0305 void *
0306 #if !MEMP_OVERFLOW_CHECK
0307 memp_malloc(memp_t type)
0308 #else
0309 memp_malloc_fn(memp_t type, const char* file, const int line)
0310 #endif
0311 {
0312   struct memp *memp;
0313   SYS_ARCH_DECL_PROTECT(old_level);
0314  
0315   LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
0316 
0317   SYS_ARCH_PROTECT(old_level);
0318 #if MEMP_OVERFLOW_CHECK >= 2
0319   memp_overflow_check_all();
0320 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
0321 
0322   memp = memp_tab[type];
0323   
0324   if (memp != NULL) {
0325     memp_tab[type] = memp->next;
0326 #if MEMP_OVERFLOW_CHECK
0327     memp->next = NULL;
0328     memp->file = file;
0329     memp->line = line;
0330 #endif /* MEMP_OVERFLOW_CHECK */
0331     MEMP_STATS_INC_USED(used, type);
0332     LWIP_ASSERT("memp_malloc: memp properly aligned",
0333                 ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
0334     memp = (struct memp*)((u8_t*)memp + MEMP_SIZE);
0335   } else {
0336     LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", memp_desc[type]));
0337     MEMP_STATS_INC(err, type);
0338   }
0339 
0340   SYS_ARCH_UNPROTECT(old_level);
0341 
0342   return memp;
0343 }
0344 
0345 /**
0346  * Put an element back into its pool.
0347  *
0348  * @param type the pool where to put mem
0349  * @param mem the memp element to free
0350  */
0351 void
0352 memp_free(memp_t type, void *mem)
0353 {
0354   struct memp *memp;
0355   SYS_ARCH_DECL_PROTECT(old_level);
0356 
0357   if (mem == NULL) {
0358     return;
0359   }
0360   LWIP_ASSERT("memp_free: mem properly aligned",
0361                 ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
0362 
0363   memp = (struct memp *)((u8_t*)mem - MEMP_SIZE);
0364 
0365   SYS_ARCH_PROTECT(old_level);
0366 #if MEMP_OVERFLOW_CHECK
0367 #if MEMP_OVERFLOW_CHECK >= 2
0368   memp_overflow_check_all();
0369 #else
0370   memp_overflow_check_element(memp, memp_sizes[type]);
0371 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
0372 #endif /* MEMP_OVERFLOW_CHECK */
0373 
0374   MEMP_STATS_DEC(used, type); 
0375   
0376   memp->next = memp_tab[type]; 
0377   memp_tab[type] = memp;
0378 
0379 #if MEMP_SANITY_CHECK
0380   LWIP_ASSERT("memp sanity", memp_sanity());
0381 #endif /* MEMP_SANITY_CHECK */
0382 
0383   SYS_ARCH_UNPROTECT(old_level);
0384 }
0385 
0386 #endif /* MEMP_MEM_MALLOC */