1 /** 2 * @file 3 * Memory pool API 4 */ 5 6 /* 7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without modification, 11 * are permitted provided that the following conditions are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright notice, 14 * this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 * OF SUCH DAMAGE. 31 * 32 * This file is part of the lwIP TCP/IP stack. 33 * 34 * Author: Adam Dunkels <adam@sics.se> 35 * 36 */ 37 38 #ifndef LWIP_HDR_MEMP_H 39 #define LWIP_HDR_MEMP_H 40 41 #include "lwip/opt.h" 42 43 #if defined (__cplusplus) && __cplusplus 44 extern "C" { 45 #endif 46 47 /* run once with empty definition to handle all custom includes in lwippools.h */ 48 #define LWIP_MEMPOOL(name, num, size, desc) 49 #include "lwip/priv/memp_std.h" 50 51 /** Create the list of all memory pools managed by memp. MEMP_MAX represents a NULL pool at the end */ 52 typedef enum { 53 #define LWIP_MEMPOOL(name, num, size, desc) MEMP_##name, 54 #include "lwip/priv/memp_std.h" 55 MEMP_MAX 56 } memp_t; 57 58 #include "lwip/priv/memp_priv.h" 59 #include "lwip/stats.h" 60 61 #if LWIP_ALLOW_SOCKET_CONFIG 62 extern struct memp_desc *memp_pools[MEMP_MAX]; 63 #else 64 extern const struct memp_desc *const memp_pools[MEMP_MAX]; 65 #endif 66 67 /** 68 * @ingroup mempool 69 * Declare prototype for private memory pool if it is used in multiple files 70 */ 71 #define LWIP_MEMPOOL_PROTOTYPE(name) extern const struct memp_desc memp_ ## name 72 73 #if MEMP_MEM_MALLOC 74 75 #define LWIP_MEMPOOL_DECLARE(name, num, size, desc) \ 76 LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(memp_stats_ ## name) \ 77 const struct memp_desc memp_ ## name = { \ 78 DECLARE_LWIP_MEMPOOL_DESC(desc) \ 79 LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(memp_stats_ ## name) \ 80 LWIP_MEM_ALIGN_SIZE(size) \ 81 }; 82 83 #else /* MEMP_MEM_MALLOC */ 84 85 /** 86 * @ingroup mempool 87 * Declare a private memory pool 88 * Private mempools example: 89 * .h: only when pool is used in multiple .c files: LWIP_MEMPOOL_PROTOTYPE(my_private_pool); 90 * .c: 91 * - in global variables section: LWIP_MEMPOOL_DECLARE(my_private_pool, 10, sizeof(foo), "Some description") 92 * - call ONCE before using pool (e.g. in some init() function): LWIP_MEMPOOL_INIT(my_private_pool); 93 * - allocate: void* my_new_mem = LWIP_MEMPOOL_ALLOC(my_private_pool); 94 * - free: LWIP_MEMPOOL_FREE(my_private_pool, my_new_mem); 95 * 96 * To relocate a pool, declare it as extern in cc.h. Example for GCC: 97 * extern u8_t __attribute__((section(".onchip_mem"))) memp_memory_my_private_pool[]; 98 */ 99 #define LWIP_MEMPOOL_DECLARE(name, num, size, desc) \ 100 LWIP_DECLARE_MEMORY_ALIGNED(memp_memory_ ## name ## _base, ((num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size)))); \ 101 \ 102 LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(memp_stats_ ## name) \ 103 \ 104 static struct memp *memp_tab_ ## name; \ 105 \ 106 const struct memp_desc memp_ ## name = { \ 107 DECLARE_LWIP_MEMPOOL_DESC(desc) \ 108 LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(memp_stats_ ## name) \ 109 LWIP_MEM_ALIGN_SIZE(size), \ 110 (num), \ 111 memp_memory_ ## name ## _base, \ 112 &memp_tab_ ## name \ 113 }; 114 115 #endif /* MEMP_MEM_MALLOC */ 116 117 /** 118 * @ingroup mempool 119 * Initialize a private memory pool 120 */ 121 #define LWIP_MEMPOOL_INIT(name) memp_init_pool(&memp_ ## name) 122 /** 123 * @ingroup mempool 124 * Allocate from a private memory pool 125 */ 126 #define LWIP_MEMPOOL_ALLOC(name) memp_malloc_pool(&memp_ ## name) 127 /** 128 * @ingroup mempool 129 * Free element from a private memory pool 130 */ 131 #define LWIP_MEMPOOL_FREE(name, x) memp_free_pool(&memp_ ## name, (x)) 132 133 #if MEM_USE_POOLS 134 /** This structure is used to save the pool one element came from. 135 * This has to be defined here as it is required for pool size calculation. */ 136 struct memp_malloc_helper { 137 memp_t poolnr; 138 #if MEMP_OVERFLOW_CHECK || (LWIP_STATS && MEM_STATS) 139 u16_t size; 140 #endif /* MEMP_OVERFLOW_CHECK || (LWIP_STATS && MEM_STATS) */ 141 }; 142 #endif /* MEM_USE_POOLS */ 143 144 void memp_init(void); 145 146 #if MEMP_OVERFLOW_CHECK 147 void *memp_malloc_fn(memp_t type, const char *file, const int line); 148 #define memp_malloc(t) memp_malloc_fn((t), __FILE__, __LINE__) 149 #else 150 void *memp_malloc(memp_t type); 151 #endif 152 void memp_free(memp_t type, void *mem); 153 154 u32_t memp_check(memp_t type, void *mem); 155 156 #if defined (__cplusplus) && __cplusplus 157 } 158 #endif 159 160 #endif /* LWIP_HDR_MEMP_H */ 161