• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * memory pools lwIP internal implementations (do not use in application code)
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_PRIV_H
39 #define LWIP_HDR_MEMP_PRIV_H
40 
41 #include "lwip/opt.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 #include "lwip/mem.h"
48 #include "lwip/priv/mem_priv.h"
49 
50 #if MEMP_OVERFLOW_CHECK
51 
52 
53 /* MEMP_SIZE: save space for struct memp and for sanity check */
54 #define MEMP_SIZE          (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEM_SANITY_REGION_BEFORE_ALIGNED)
55 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEM_SANITY_REGION_AFTER_ALIGNED)
56 
57 #else /* MEMP_OVERFLOW_CHECK */
58 
59 /* No sanity checks
60  * We don't need to preserve the struct memp while not allocated, so we
61  * can save a little space and set MEMP_SIZE to 0.
62  */
63 #define MEMP_SIZE           0
64 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
65 
66 #endif /* MEMP_OVERFLOW_CHECK */
67 
68 #if !MEMP_MEM_MALLOC || MEMP_OVERFLOW_CHECK
69 struct memp {
70   struct memp *next;
71 #if MEMP_OVERFLOW_CHECK
72   const char *file;
73   int line;
74 #endif /* MEMP_OVERFLOW_CHECK */
75 };
76 #endif /* !MEMP_MEM_MALLOC || MEMP_OVERFLOW_CHECK */
77 
78 #if MEM_USE_POOLS && MEMP_USE_CUSTOM_POOLS
79 /* Use a helper type to get the start and end of the user "memory pools" for mem_malloc */
80 typedef enum {
81     /* Get the first (via:
82        MEMP_POOL_HELPER_START = ((u8_t) 1*MEMP_POOL_A + 0*MEMP_POOL_B + 0*MEMP_POOL_C + 0)*/
83     MEMP_POOL_HELPER_FIRST = ((u8_t)
84 #define LWIP_MEMPOOL(name,num,size,desc)
85 #define LWIP_MALLOC_MEMPOOL_START 1
86 #define LWIP_MALLOC_MEMPOOL(num, size) * MEMP_POOL_##size + 0
87 #define LWIP_MALLOC_MEMPOOL_END
88 #include "lwip/priv/memp_std.h"
89     ) ,
90     /* Get the last (via:
91        MEMP_POOL_HELPER_END = ((u8_t) 0 + MEMP_POOL_A*0 + MEMP_POOL_B*0 + MEMP_POOL_C*1) */
92     MEMP_POOL_HELPER_LAST = ((u8_t)
93 #define LWIP_MEMPOOL(name,num,size,desc)
94 #define LWIP_MALLOC_MEMPOOL_START
95 #define LWIP_MALLOC_MEMPOOL(num, size) 0 + MEMP_POOL_##size *
96 #define LWIP_MALLOC_MEMPOOL_END 1
97 #include "lwip/priv/memp_std.h"
98     )
99 } memp_pool_helper_t;
100 
101 /* The actual start and stop values are here (cast them over)
102    We use this helper type and these defines so we can avoid using const memp_t values */
103 #define MEMP_POOL_FIRST ((memp_t) MEMP_POOL_HELPER_FIRST)
104 #define MEMP_POOL_LAST   ((memp_t) MEMP_POOL_HELPER_LAST)
105 #endif /* MEM_USE_POOLS && MEMP_USE_CUSTOM_POOLS */
106 
107 /** Memory pool descriptor */
108 struct memp_desc {
109 #if defined(LWIP_DEBUG) || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY
110   /** Textual description */
111   const char *desc;
112 #endif /* LWIP_DEBUG || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY */
113 #if MEMP_STATS
114   /** Statistics */
115   struct stats_mem *stats;
116 #endif
117 
118   /** Element size */
119   u16_t size;
120 
121 #if !MEMP_MEM_MALLOC
122   /** Number of elements */
123   u16_t num;
124 
125   /** Base address */
126   u8_t *base;
127 
128   /** First free element of each pool. Elements form a linked list. */
129   struct memp **tab;
130 #endif /* MEMP_MEM_MALLOC */
131 };
132 
133 #if defined(LWIP_DEBUG) || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY
134 #define DECLARE_LWIP_MEMPOOL_DESC(desc) (desc),
135 #else
136 #define DECLARE_LWIP_MEMPOOL_DESC(desc)
137 #endif
138 
139 #if MEMP_STATS
140 #define LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(name) static struct stats_mem name;
141 #define LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(name) &name,
142 #else
143 #define LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(name)
144 #define LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(name)
145 #endif
146 
147 void memp_init_pool(const struct memp_desc *desc);
148 
149 #if MEMP_OVERFLOW_CHECK
150 void *memp_malloc_pool_fn(const struct memp_desc* desc, const char* file, const int line);
151 #define memp_malloc_pool(d) memp_malloc_pool_fn((d), __FILE__, __LINE__)
152 #else
153 void *memp_malloc_pool(const struct memp_desc *desc);
154 #endif
155 void  memp_free_pool(const struct memp_desc* desc, void *mem);
156 
157 #ifdef __cplusplus
158 }
159 #endif
160 
161 #endif /* LWIP_HDR_MEMP_PRIV_H */
162