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