xref: /minix3/minix/lib/liblwip/dist/src/include/lwip/priv/memp_priv.h (revision 5d5fbe79c1b60734f34c69330aec5496644e8651)
1*5d5fbe79SDavid van Moolenbroek /**
2*5d5fbe79SDavid van Moolenbroek  * @file
3*5d5fbe79SDavid van Moolenbroek  * memory pools lwIP internal implementations (do not use in application code)
4*5d5fbe79SDavid van Moolenbroek  */
5*5d5fbe79SDavid van Moolenbroek 
6*5d5fbe79SDavid van Moolenbroek /*
7*5d5fbe79SDavid van Moolenbroek  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8*5d5fbe79SDavid van Moolenbroek  * All rights reserved.
9*5d5fbe79SDavid van Moolenbroek  *
10*5d5fbe79SDavid van Moolenbroek  * Redistribution and use in source and binary forms, with or without modification,
11*5d5fbe79SDavid van Moolenbroek  * are permitted provided that the following conditions are met:
12*5d5fbe79SDavid van Moolenbroek  *
13*5d5fbe79SDavid van Moolenbroek  * 1. Redistributions of source code must retain the above copyright notice,
14*5d5fbe79SDavid van Moolenbroek  *    this list of conditions and the following disclaimer.
15*5d5fbe79SDavid van Moolenbroek  * 2. Redistributions in binary form must reproduce the above copyright notice,
16*5d5fbe79SDavid van Moolenbroek  *    this list of conditions and the following disclaimer in the documentation
17*5d5fbe79SDavid van Moolenbroek  *    and/or other materials provided with the distribution.
18*5d5fbe79SDavid van Moolenbroek  * 3. The name of the author may not be used to endorse or promote products
19*5d5fbe79SDavid van Moolenbroek  *    derived from this software without specific prior written permission.
20*5d5fbe79SDavid van Moolenbroek  *
21*5d5fbe79SDavid van Moolenbroek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22*5d5fbe79SDavid van Moolenbroek  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23*5d5fbe79SDavid van Moolenbroek  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24*5d5fbe79SDavid van Moolenbroek  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25*5d5fbe79SDavid van Moolenbroek  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26*5d5fbe79SDavid van Moolenbroek  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27*5d5fbe79SDavid van Moolenbroek  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28*5d5fbe79SDavid van Moolenbroek  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29*5d5fbe79SDavid van Moolenbroek  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30*5d5fbe79SDavid van Moolenbroek  * OF SUCH DAMAGE.
31*5d5fbe79SDavid van Moolenbroek  *
32*5d5fbe79SDavid van Moolenbroek  * This file is part of the lwIP TCP/IP stack.
33*5d5fbe79SDavid van Moolenbroek  *
34*5d5fbe79SDavid van Moolenbroek  * Author: Adam Dunkels <adam@sics.se>
35*5d5fbe79SDavid van Moolenbroek  *
36*5d5fbe79SDavid van Moolenbroek  */
37*5d5fbe79SDavid van Moolenbroek 
38*5d5fbe79SDavid van Moolenbroek #ifndef LWIP_HDR_MEMP_PRIV_H
39*5d5fbe79SDavid van Moolenbroek #define LWIP_HDR_MEMP_PRIV_H
40*5d5fbe79SDavid van Moolenbroek 
41*5d5fbe79SDavid van Moolenbroek #include "lwip/opt.h"
42*5d5fbe79SDavid van Moolenbroek 
43*5d5fbe79SDavid van Moolenbroek #ifdef __cplusplus
44*5d5fbe79SDavid van Moolenbroek extern "C" {
45*5d5fbe79SDavid van Moolenbroek #endif
46*5d5fbe79SDavid van Moolenbroek 
47*5d5fbe79SDavid van Moolenbroek #include "lwip/mem.h"
48*5d5fbe79SDavid van Moolenbroek 
49*5d5fbe79SDavid van Moolenbroek #if MEMP_OVERFLOW_CHECK
50*5d5fbe79SDavid van Moolenbroek /* if MEMP_OVERFLOW_CHECK is turned on, we reserve some bytes at the beginning
51*5d5fbe79SDavid van Moolenbroek  * and at the end of each element, initialize them as 0xcd and check
52*5d5fbe79SDavid van Moolenbroek  * them later. */
53*5d5fbe79SDavid van Moolenbroek /* If MEMP_OVERFLOW_CHECK is >= 2, on every call to memp_malloc or memp_free,
54*5d5fbe79SDavid van Moolenbroek  * every single element in each pool is checked!
55*5d5fbe79SDavid van Moolenbroek  * This is VERY SLOW but also very helpful. */
56*5d5fbe79SDavid van Moolenbroek /* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in
57*5d5fbe79SDavid van Moolenbroek  * lwipopts.h to change the amount reserved for checking. */
58*5d5fbe79SDavid van Moolenbroek #ifndef MEMP_SANITY_REGION_BEFORE
59*5d5fbe79SDavid van Moolenbroek #define MEMP_SANITY_REGION_BEFORE  16
60*5d5fbe79SDavid van Moolenbroek #endif /* MEMP_SANITY_REGION_BEFORE*/
61*5d5fbe79SDavid van Moolenbroek #if MEMP_SANITY_REGION_BEFORE > 0
62*5d5fbe79SDavid van Moolenbroek #define MEMP_SANITY_REGION_BEFORE_ALIGNED    LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE)
63*5d5fbe79SDavid van Moolenbroek #else
64*5d5fbe79SDavid van Moolenbroek #define MEMP_SANITY_REGION_BEFORE_ALIGNED    0
65*5d5fbe79SDavid van Moolenbroek #endif /* MEMP_SANITY_REGION_BEFORE*/
66*5d5fbe79SDavid van Moolenbroek #ifndef MEMP_SANITY_REGION_AFTER
67*5d5fbe79SDavid van Moolenbroek #define MEMP_SANITY_REGION_AFTER   16
68*5d5fbe79SDavid van Moolenbroek #endif /* MEMP_SANITY_REGION_AFTER*/
69*5d5fbe79SDavid van Moolenbroek #if MEMP_SANITY_REGION_AFTER > 0
70*5d5fbe79SDavid van Moolenbroek #define MEMP_SANITY_REGION_AFTER_ALIGNED     LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER)
71*5d5fbe79SDavid van Moolenbroek #else
72*5d5fbe79SDavid van Moolenbroek #define MEMP_SANITY_REGION_AFTER_ALIGNED     0
73*5d5fbe79SDavid van Moolenbroek #endif /* MEMP_SANITY_REGION_AFTER*/
74*5d5fbe79SDavid van Moolenbroek 
75*5d5fbe79SDavid van Moolenbroek /* MEMP_SIZE: save space for struct memp and for sanity check */
76*5d5fbe79SDavid van Moolenbroek #define MEMP_SIZE          (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED)
77*5d5fbe79SDavid van Moolenbroek #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED)
78*5d5fbe79SDavid van Moolenbroek 
79*5d5fbe79SDavid van Moolenbroek #else /* MEMP_OVERFLOW_CHECK */
80*5d5fbe79SDavid van Moolenbroek 
81*5d5fbe79SDavid van Moolenbroek /* No sanity checks
82*5d5fbe79SDavid van Moolenbroek  * We don't need to preserve the struct memp while not allocated, so we
83*5d5fbe79SDavid van Moolenbroek  * can save a little space and set MEMP_SIZE to 0.
84*5d5fbe79SDavid van Moolenbroek  */
85*5d5fbe79SDavid van Moolenbroek #define MEMP_SIZE           0
86*5d5fbe79SDavid van Moolenbroek #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
87*5d5fbe79SDavid van Moolenbroek 
88*5d5fbe79SDavid van Moolenbroek #endif /* MEMP_OVERFLOW_CHECK */
89*5d5fbe79SDavid van Moolenbroek 
90*5d5fbe79SDavid van Moolenbroek #if !MEMP_MEM_MALLOC || MEMP_OVERFLOW_CHECK
91*5d5fbe79SDavid van Moolenbroek struct memp {
92*5d5fbe79SDavid van Moolenbroek   struct memp *next;
93*5d5fbe79SDavid van Moolenbroek #if MEMP_OVERFLOW_CHECK
94*5d5fbe79SDavid van Moolenbroek   const char *file;
95*5d5fbe79SDavid van Moolenbroek   int line;
96*5d5fbe79SDavid van Moolenbroek #endif /* MEMP_OVERFLOW_CHECK */
97*5d5fbe79SDavid van Moolenbroek };
98*5d5fbe79SDavid van Moolenbroek #endif /* !MEMP_MEM_MALLOC || MEMP_OVERFLOW_CHECK */
99*5d5fbe79SDavid van Moolenbroek 
100*5d5fbe79SDavid van Moolenbroek #if MEM_USE_POOLS && MEMP_USE_CUSTOM_POOLS
101*5d5fbe79SDavid van Moolenbroek /* Use a helper type to get the start and end of the user "memory pools" for mem_malloc */
102*5d5fbe79SDavid van Moolenbroek typedef enum {
103*5d5fbe79SDavid van Moolenbroek     /* Get the first (via:
104*5d5fbe79SDavid van Moolenbroek        MEMP_POOL_HELPER_START = ((u8_t) 1*MEMP_POOL_A + 0*MEMP_POOL_B + 0*MEMP_POOL_C + 0)*/
105*5d5fbe79SDavid van Moolenbroek     MEMP_POOL_HELPER_FIRST = ((u8_t)
106*5d5fbe79SDavid van Moolenbroek #define LWIP_MEMPOOL(name,num,size,desc)
107*5d5fbe79SDavid van Moolenbroek #define LWIP_MALLOC_MEMPOOL_START 1
108*5d5fbe79SDavid van Moolenbroek #define LWIP_MALLOC_MEMPOOL(num, size) * MEMP_POOL_##size + 0
109*5d5fbe79SDavid van Moolenbroek #define LWIP_MALLOC_MEMPOOL_END
110*5d5fbe79SDavid van Moolenbroek #include "lwip/priv/memp_std.h"
111*5d5fbe79SDavid van Moolenbroek     ) ,
112*5d5fbe79SDavid van Moolenbroek     /* Get the last (via:
113*5d5fbe79SDavid van Moolenbroek        MEMP_POOL_HELPER_END = ((u8_t) 0 + MEMP_POOL_A*0 + MEMP_POOL_B*0 + MEMP_POOL_C*1) */
114*5d5fbe79SDavid van Moolenbroek     MEMP_POOL_HELPER_LAST = ((u8_t)
115*5d5fbe79SDavid van Moolenbroek #define LWIP_MEMPOOL(name,num,size,desc)
116*5d5fbe79SDavid van Moolenbroek #define LWIP_MALLOC_MEMPOOL_START
117*5d5fbe79SDavid van Moolenbroek #define LWIP_MALLOC_MEMPOOL(num, size) 0 + MEMP_POOL_##size *
118*5d5fbe79SDavid van Moolenbroek #define LWIP_MALLOC_MEMPOOL_END 1
119*5d5fbe79SDavid van Moolenbroek #include "lwip/priv/memp_std.h"
120*5d5fbe79SDavid van Moolenbroek     )
121*5d5fbe79SDavid van Moolenbroek } memp_pool_helper_t;
122*5d5fbe79SDavid van Moolenbroek 
123*5d5fbe79SDavid van Moolenbroek /* The actual start and stop values are here (cast them over)
124*5d5fbe79SDavid van Moolenbroek    We use this helper type and these defines so we can avoid using const memp_t values */
125*5d5fbe79SDavid van Moolenbroek #define MEMP_POOL_FIRST ((memp_t) MEMP_POOL_HELPER_FIRST)
126*5d5fbe79SDavid van Moolenbroek #define MEMP_POOL_LAST   ((memp_t) MEMP_POOL_HELPER_LAST)
127*5d5fbe79SDavid van Moolenbroek #endif /* MEM_USE_POOLS && MEMP_USE_CUSTOM_POOLS */
128*5d5fbe79SDavid van Moolenbroek 
129*5d5fbe79SDavid van Moolenbroek /** Memory pool descriptor */
130*5d5fbe79SDavid van Moolenbroek struct memp_desc {
131*5d5fbe79SDavid van Moolenbroek #if defined(LWIP_DEBUG) || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY
132*5d5fbe79SDavid van Moolenbroek   /** Textual description */
133*5d5fbe79SDavid van Moolenbroek   const char *desc;
134*5d5fbe79SDavid van Moolenbroek #endif /* LWIP_DEBUG || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY */
135*5d5fbe79SDavid van Moolenbroek #if MEMP_STATS
136*5d5fbe79SDavid van Moolenbroek   /** Statistics */
137*5d5fbe79SDavid van Moolenbroek   struct stats_mem *stats;
138*5d5fbe79SDavid van Moolenbroek #endif
139*5d5fbe79SDavid van Moolenbroek 
140*5d5fbe79SDavid van Moolenbroek   /** Element size */
141*5d5fbe79SDavid van Moolenbroek   u16_t size;
142*5d5fbe79SDavid van Moolenbroek 
143*5d5fbe79SDavid van Moolenbroek #if !MEMP_MEM_MALLOC
144*5d5fbe79SDavid van Moolenbroek   /** Number of elements */
145*5d5fbe79SDavid van Moolenbroek   u16_t num;
146*5d5fbe79SDavid van Moolenbroek 
147*5d5fbe79SDavid van Moolenbroek   /** Base address */
148*5d5fbe79SDavid van Moolenbroek   u8_t *base;
149*5d5fbe79SDavid van Moolenbroek 
150*5d5fbe79SDavid van Moolenbroek   /** First free element of each pool. Elements form a linked list. */
151*5d5fbe79SDavid van Moolenbroek   struct memp **tab;
152*5d5fbe79SDavid van Moolenbroek #endif /* MEMP_MEM_MALLOC */
153*5d5fbe79SDavid van Moolenbroek };
154*5d5fbe79SDavid van Moolenbroek 
155*5d5fbe79SDavid van Moolenbroek #if defined(LWIP_DEBUG) || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY
156*5d5fbe79SDavid van Moolenbroek #define DECLARE_LWIP_MEMPOOL_DESC(desc) (desc),
157*5d5fbe79SDavid van Moolenbroek #else
158*5d5fbe79SDavid van Moolenbroek #define DECLARE_LWIP_MEMPOOL_DESC(desc)
159*5d5fbe79SDavid van Moolenbroek #endif
160*5d5fbe79SDavid van Moolenbroek 
161*5d5fbe79SDavid van Moolenbroek #if MEMP_STATS
162*5d5fbe79SDavid van Moolenbroek #define LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(name) static struct stats_mem name;
163*5d5fbe79SDavid van Moolenbroek #define LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(name) &name,
164*5d5fbe79SDavid van Moolenbroek #else
165*5d5fbe79SDavid van Moolenbroek #define LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(name)
166*5d5fbe79SDavid van Moolenbroek #define LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(name)
167*5d5fbe79SDavid van Moolenbroek #endif
168*5d5fbe79SDavid van Moolenbroek 
169*5d5fbe79SDavid van Moolenbroek void memp_init_pool(const struct memp_desc *desc);
170*5d5fbe79SDavid van Moolenbroek 
171*5d5fbe79SDavid van Moolenbroek #if MEMP_OVERFLOW_CHECK
172*5d5fbe79SDavid van Moolenbroek void *memp_malloc_pool_fn(const struct memp_desc* desc, const char* file, const int line);
173*5d5fbe79SDavid van Moolenbroek #define memp_malloc_pool(d) memp_malloc_pool_fn((d), __FILE__, __LINE__)
174*5d5fbe79SDavid van Moolenbroek #else
175*5d5fbe79SDavid van Moolenbroek void *memp_malloc_pool(const struct memp_desc *desc);
176*5d5fbe79SDavid van Moolenbroek #endif
177*5d5fbe79SDavid van Moolenbroek void  memp_free_pool(const struct memp_desc* desc, void *mem);
178*5d5fbe79SDavid van Moolenbroek 
179*5d5fbe79SDavid van Moolenbroek #ifdef __cplusplus
180*5d5fbe79SDavid van Moolenbroek }
181*5d5fbe79SDavid van Moolenbroek #endif
182*5d5fbe79SDavid van Moolenbroek 
183*5d5fbe79SDavid van Moolenbroek #endif /* LWIP_HDR_MEMP_PRIV_H */
184