3 * memory pools lwIP internal implementations (do not use in application code)
7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
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.
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
32 * This file is part of the lwIP TCP/IP stack.
34 * Author: Adam Dunkels <adam@sics.se>
38 #ifndef LWIP_HDR_MEMP_PRIV_H
39 #define LWIP_HDR_MEMP_PRIV_H
49 #if MEMP_OVERFLOW_CHECK
50 /* if MEMP_OVERFLOW_CHECK is turned on, we reserve some bytes at the beginning
51 * and at the end of each element, initialize them as 0xcd and check
53 /* If MEMP_OVERFLOW_CHECK is >= 2, on every call to memp_malloc or memp_free,
54 * every single element in each pool is checked!
55 * This is VERY SLOW but also very helpful. */
56 /* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in
57 * lwipopts.h to change the amount reserved for checking. */
58 #ifndef MEMP_SANITY_REGION_BEFORE
59 #define MEMP_SANITY_REGION_BEFORE 16
60 #endif /* MEMP_SANITY_REGION_BEFORE*/
61 #if MEMP_SANITY_REGION_BEFORE > 0
62 #define MEMP_SANITY_REGION_BEFORE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE)
64 #define MEMP_SANITY_REGION_BEFORE_ALIGNED 0
65 #endif /* MEMP_SANITY_REGION_BEFORE*/
66 #ifndef MEMP_SANITY_REGION_AFTER
67 #define MEMP_SANITY_REGION_AFTER 16
68 #endif /* MEMP_SANITY_REGION_AFTER*/
69 #if MEMP_SANITY_REGION_AFTER > 0
70 #define MEMP_SANITY_REGION_AFTER_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER)
72 #define MEMP_SANITY_REGION_AFTER_ALIGNED 0
73 #endif /* MEMP_SANITY_REGION_AFTER*/
75 /* MEMP_SIZE: save space for struct memp and for sanity check */
76 #define MEMP_SIZE (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED)
77 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED)
79 #else /* MEMP_OVERFLOW_CHECK */
82 * We don't need to preserve the struct memp while not allocated, so we
83 * can save a little space and set MEMP_SIZE to 0.
86 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
88 #endif /* MEMP_OVERFLOW_CHECK */
90 #if !MEMP_MEM_MALLOC || MEMP_OVERFLOW_CHECK
93 #if MEMP_OVERFLOW_CHECK
96 #endif /* MEMP_OVERFLOW_CHECK */
98 #endif /* !MEMP_MEM_MALLOC || MEMP_OVERFLOW_CHECK */
100 #if MEM_USE_POOLS && MEMP_USE_CUSTOM_POOLS
101 /* Use a helper type to get the start and end of the user "memory pools" for mem_malloc */
103 /* Get the first (via:
104 MEMP_POOL_HELPER_START = ((u8_t) 1*MEMP_POOL_A + 0*MEMP_POOL_B + 0*MEMP_POOL_C + 0)*/
105 MEMP_POOL_HELPER_FIRST
= ((u8_t
)
106 #define LWIP_MEMPOOL(name,num,size,desc)
107 #define LWIP_MALLOC_MEMPOOL_START 1
108 #define LWIP_MALLOC_MEMPOOL(num, size) * MEMP_POOL_##size + 0
109 #define LWIP_MALLOC_MEMPOOL_END
110 #include "lwip/priv/memp_std.h"
112 /* Get the last (via:
113 MEMP_POOL_HELPER_END = ((u8_t) 0 + MEMP_POOL_A*0 + MEMP_POOL_B*0 + MEMP_POOL_C*1) */
114 MEMP_POOL_HELPER_LAST
= ((u8_t
)
115 #define LWIP_MEMPOOL(name,num,size,desc)
116 #define LWIP_MALLOC_MEMPOOL_START
117 #define LWIP_MALLOC_MEMPOOL(num, size) 0 + MEMP_POOL_##size *
118 #define LWIP_MALLOC_MEMPOOL_END 1
119 #include "lwip/priv/memp_std.h"
121 } memp_pool_helper_t
;
123 /* The actual start and stop values are here (cast them over)
124 We use this helper type and these defines so we can avoid using const memp_t values */
125 #define MEMP_POOL_FIRST ((memp_t) MEMP_POOL_HELPER_FIRST)
126 #define MEMP_POOL_LAST ((memp_t) MEMP_POOL_HELPER_LAST)
127 #endif /* MEM_USE_POOLS && MEMP_USE_CUSTOM_POOLS */
129 /** Memory pool descriptor */
131 #if defined(LWIP_DEBUG) || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY
132 /** Textual description */
134 #endif /* LWIP_DEBUG || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY */
137 struct stats_mem
*stats
;
144 /** Number of elements */
150 /** First free element of each pool. Elements form a linked list. */
152 #endif /* MEMP_MEM_MALLOC */
155 #if defined(LWIP_DEBUG) || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY
156 #define DECLARE_LWIP_MEMPOOL_DESC(desc) (desc),
158 #define DECLARE_LWIP_MEMPOOL_DESC(desc)
162 #define LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(name) static struct stats_mem name;
163 #define LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(name) &name,
165 #define LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(name)
166 #define LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(name)
169 void memp_init_pool(const struct memp_desc
*desc
);
171 #if MEMP_OVERFLOW_CHECK
172 void *memp_malloc_pool_fn(const struct memp_desc
* desc
, const char* file
, const int line
);
173 #define memp_malloc_pool(d) memp_malloc_pool_fn((d), __FILE__, __LINE__)
175 void *memp_malloc_pool(const struct memp_desc
*desc
);
177 void memp_free_pool(const struct memp_desc
* desc
, void *mem
);
183 #endif /* LWIP_HDR_MEMP_PRIV_H */