Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers memp.h Source File

memp.h

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Memory pool API
00004  */
00005 
00006 /*
00007  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without modification,
00011  * are permitted provided that the following conditions are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright notice,
00014  *    this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright notice,
00016  *    this list of conditions and the following disclaimer in the documentation
00017  *    and/or other materials provided with the distribution.
00018  * 3. The name of the author may not be used to endorse or promote products
00019  *    derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00023  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00024  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00026  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00029  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00030  * OF SUCH DAMAGE.
00031  *
00032  * This file is part of the lwIP TCP/IP stack.
00033  *
00034  * Author: Adam Dunkels <adam@sics.se>
00035  *
00036  */
00037 
00038 #ifndef LWIP_HDR_MEMP_H
00039 #define LWIP_HDR_MEMP_H
00040 
00041 #include "lwip/opt.h"
00042 
00043 #ifdef __cplusplus
00044 extern "C" {
00045 #endif
00046 
00047 /* run once with empty definition to handle all custom includes in lwippools.h */
00048 #define LWIP_MEMPOOL(name,num,size,desc)
00049 #include "lwip/priv/memp_std.h"
00050 
00051 /** Create the list of all memory pools managed by memp. MEMP_MAX represents a NULL pool at the end */
00052 typedef enum {
00053 #define LWIP_MEMPOOL(name,num,size,desc)  MEMP_##name,
00054 #include "lwip/priv/memp_std.h"
00055   MEMP_MAX
00056 } memp_t;
00057 
00058 #include "lwip/priv/memp_priv.h"
00059 #include "lwip/stats.h"
00060 
00061 extern const struct memp_desc* const memp_pools[MEMP_MAX];
00062 
00063 /**
00064  * @ingroup mempool
00065  * Declare prototype for private memory pool if it is used in multiple files
00066  */
00067 #define LWIP_MEMPOOL_PROTOTYPE(name) extern const struct memp_desc memp_ ## name
00068 
00069 #if MEMP_MEM_MALLOC
00070 
00071 #define LWIP_MEMPOOL_DECLARE(name,num,size,desc) \
00072   LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(memp_stats_ ## name) \
00073   const struct memp_desc memp_ ## name = { \
00074     DECLARE_LWIP_MEMPOOL_DESC(desc) \
00075     LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(memp_stats_ ## name) \
00076     LWIP_MEM_ALIGN_SIZE(size) \
00077   };
00078 
00079 #else /* MEMP_MEM_MALLOC */
00080 
00081 /**
00082  * @ingroup mempool
00083  * Declare a private memory pool
00084  * Private mempools example:
00085  * .h: only when pool is used in multiple .c files: LWIP_MEMPOOL_PROTOTYPE(my_private_pool);
00086  * .c:
00087  *   - in global variables section: LWIP_MEMPOOL_DECLARE(my_private_pool, 10, sizeof(foo), "Some description")
00088  *   - call ONCE before using pool (e.g. in some init() function): LWIP_MEMPOOL_INIT(my_private_pool);
00089  *   - allocate: void* my_new_mem = LWIP_MEMPOOL_ALLOC(my_private_pool);
00090  *   - free: LWIP_MEMPOOL_FREE(my_private_pool, my_new_mem);
00091  *
00092  * To relocate a pool, declare it as extern in cc.h. Example for GCC:
00093  *   extern u8_t \_\_attribute\_\_((section(".onchip_mem"))) memp_memory_my_private_pool_base[];
00094  */
00095 #define LWIP_MEMPOOL_DECLARE(name,num,size,desc) \
00096   LWIP_DECLARE_MEMORY_ALIGNED(memp_memory_ ## name ## _base, ((num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size)))); \
00097     \
00098   LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(memp_stats_ ## name) \
00099     \
00100   static struct memp *memp_tab_ ## name; \
00101     \
00102   const struct memp_desc memp_ ## name = { \
00103     DECLARE_LWIP_MEMPOOL_DESC(desc) \
00104     LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(memp_stats_ ## name) \
00105     LWIP_MEM_ALIGN_SIZE(size), \
00106     (num), \
00107     memp_memory_ ## name ## _base, \
00108     &memp_tab_ ## name \
00109   };
00110 
00111 #endif /* MEMP_MEM_MALLOC */
00112 
00113 /**
00114  * @ingroup mempool
00115  * Initialize a private memory pool
00116  */
00117 #define LWIP_MEMPOOL_INIT(name)    memp_init_pool(&memp_ ## name)
00118 /**
00119  * @ingroup mempool
00120  * Allocate from a private memory pool
00121  */
00122 #define LWIP_MEMPOOL_ALLOC(name)   memp_malloc_pool(&memp_ ## name)
00123 /**
00124  * @ingroup mempool
00125  * Free element from a private memory pool
00126  */
00127 #define LWIP_MEMPOOL_FREE(name, x) memp_free_pool(&memp_ ## name, (x))
00128 
00129 #if MEM_USE_POOLS
00130 /** This structure is used to save the pool one element came from.
00131  * This has to be defined here as it is required for pool size calculation. */
00132 struct memp_malloc_helper
00133 {
00134    memp_t poolnr;
00135 #if MEMP_OVERFLOW_CHECK || (LWIP_STATS && MEM_STATS)
00136    u16_t size;
00137 #endif /* MEMP_OVERFLOW_CHECK || (LWIP_STATS && MEM_STATS) */
00138 };
00139 #endif /* MEM_USE_POOLS */
00140 
00141 void  memp_init(void);
00142 
00143 #if MEMP_OVERFLOW_CHECK
00144 void *memp_malloc_fn(memp_t type, const char* file, const int line);
00145 #define memp_malloc(t) memp_malloc_fn((t), __FILE__, __LINE__)
00146 #else
00147 void *memp_malloc(memp_t type);
00148 #endif
00149 void  memp_free(memp_t type, void *mem);
00150 
00151 #ifdef __cplusplus
00152 }
00153 #endif
00154 
00155 #endif /* LWIP_HDR_MEMP_H */