2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
35 #include "lwip/memp.h"
37 #include "lwip/pbuf.h"
41 #include "lwip/api_msg.h"
42 #include "lwip/tcpip.h"
45 #include "lwip/stats.h"
53 static struct memp *memp_tab[MEMP_MAX];
55 static const u16_t memp_sizes[MEMP_MAX] = {
57 sizeof(struct udp_pcb),
58 sizeof(struct tcp_pcb),
59 sizeof(struct tcp_pcb_listen),
60 sizeof(struct tcp_seg),
61 sizeof(struct netbuf),
62 sizeof(struct netconn),
63 sizeof(struct api_msg),
64 sizeof(struct tcpip_msg),
65 sizeof(struct sys_timeout)
68 static const u16_t memp_num[MEMP_MAX] = {
72 MEMP_NUM_TCP_PCB_LISTEN,
81 static u8_t memp_memory[(MEMP_NUM_PBUF *
82 MEM_ALIGN_SIZE(sizeof(struct pbuf) +
83 sizeof(struct memp)) +
85 MEM_ALIGN_SIZE(sizeof(struct udp_pcb) +
86 sizeof(struct memp)) +
88 MEM_ALIGN_SIZE(sizeof(struct tcp_pcb) +
89 sizeof(struct memp)) +
90 MEMP_NUM_TCP_PCB_LISTEN *
91 MEM_ALIGN_SIZE(sizeof(struct tcp_pcb_listen) +
92 sizeof(struct memp)) +
94 MEM_ALIGN_SIZE(sizeof(struct tcp_seg) +
95 sizeof(struct memp)) +
97 MEM_ALIGN_SIZE(sizeof(struct netbuf) +
98 sizeof(struct memp)) +
100 MEM_ALIGN_SIZE(sizeof(struct netconn) +
101 sizeof(struct memp)) +
103 MEM_ALIGN_SIZE(sizeof(struct api_msg) +
104 sizeof(struct memp)) +
106 MEM_ALIGN_SIZE(sizeof(struct tcpip_msg) +
107 sizeof(struct memp)) +
108 MEMP_NUM_SYS_TIMEOUT *
109 MEM_ALIGN_SIZE(sizeof(struct sys_timeout) +
110 sizeof(struct memp)))];
112 /*-----------------------------------------------------------------------------------*/
113 #ifndef SYS_LIGHTWEIGHT_PROT
114 static sys_sem_t mutex;
116 /*-----------------------------------------------------------------------------------*/
124 for(i = 0; i < MEMP_MAX; i++) {
125 for(m = memp_tab[i]; m != NULL; m = m->next) {
127 for(n = memp_tab[i]; n != NULL; n = n->next) {
131 if(c < 0) return 0; /* LW was: abort(); */
137 #endif /* LWIP_DEBUG */
138 /*-----------------------------------------------------------------------------------*/
142 struct memp *m, *memp;
147 for(i = 0; i < MEMP_MAX; ++i) {
148 lwip_stats.memp[i].used = lwip_stats.memp[i].max =
149 lwip_stats.memp[i].err = 0;
150 lwip_stats.memp[i].avail = memp_num[i];
152 #endif /* MEMP_STATS */
154 memp = (struct memp *)&memp_memory[0];
155 for(i = 0; i < MEMP_MAX; ++i) {
156 size = MEM_ALIGN_SIZE(memp_sizes[i] + sizeof(struct memp));
157 if(memp_num[i] > 0) {
161 for(j = 0; j < memp_num[i]; ++j) {
162 m->next = (struct memp *)MEM_ALIGN((u8_t *)m + size);
173 #ifndef SYS_LIGHTWEIGHT_PROT
174 mutex = sys_sem_new(1);
179 /*-----------------------------------------------------------------------------------*/
181 memp_malloc(memp_t type)
186 LWIP_ASSERT("memp_malloc: type < MEMP_MAX", type < MEMP_MAX);
188 memp = memp_tab[type];
191 memp_tab[type] = memp->next;
194 ++lwip_stats.memp[type].used;
195 if(lwip_stats.memp[type].used > lwip_stats.memp[type].max) {
196 lwip_stats.memp[type].max = lwip_stats.memp[type].used;
198 #endif /* MEMP_STATS */
199 LWIP_ASSERT("memp_malloc: memp properly aligned",
200 ((u32_t)MEM_ALIGN((u8_t *)memp + sizeof(struct memp)) % MEM_ALIGNMENT) == 0);
202 mem = MEM_ALIGN((u8_t *)memp + sizeof(struct memp));
203 /* initialize memp memory with zeroes */
204 memset(mem, 0, memp_sizes[type]);
207 DEBUGF(MEMP_DEBUG, ("memp_malloc: out of memory in pool %d\n", type));
209 ++lwip_stats.memp[type].err;
210 #endif /* MEMP_STATS */
214 /*-----------------------------------------------------------------------------------*/
216 memp_mallocp(memp_t type)
219 #ifdef SYS_LIGHTWEIGHT_PROT
220 SYS_ARCH_DECL_PROTECT(old_level);
221 SYS_ARCH_PROTECT(old_level);
222 #else /* SYS_LIGHTWEIGHT_PROT */
224 #endif /* SYS_LIGHTWEIGHT_PROT */
226 mem = memp_malloc(type);
228 #ifdef SYS_LIGHTWEIGHT_PROT
229 SYS_ARCH_UNPROTECT(old_level);
230 #else /* SYS_LIGHTWEIGHT_PROT */
231 sys_sem_signal(mutex);
232 #endif /* SYS_LIGHTWEIGHT_PROT */
235 /*-----------------------------------------------------------------------------------*/
237 memp_free(memp_t type, void *mem)
244 memp = (struct memp *)((u8_t *)mem - sizeof(struct memp));
247 lwip_stats.memp[type].used--;
248 #endif /* MEMP_STATS */
250 memp->next = memp_tab[type];
251 memp_tab[type] = memp;
253 LWIP_ASSERT("memp sanity", memp_sanity());
257 /*-----------------------------------------------------------------------------------*/
259 memp_freep(memp_t type, void *mem)
261 #ifdef SYS_LIGHTWEIGHT_PROT
262 SYS_ARCH_DECL_PROTECT(old_level);
263 SYS_ARCH_PROTECT(old_level);
264 #else /* SYS_LIGHTWEIGHT_PROT */
266 #endif /* SYS_LIGHTWEIGHT_PROT */
268 memp_free(type, mem);
270 #ifdef SYS_LIGHTWEIGHT_PROT
271 SYS_ARCH_UNPROTECT(old_level);
272 #else /* SYS_LIGHTWEIGHT_PROT */
273 sys_sem_signal(mutex);
274 #endif /* SYS_LIGHTWEIGHT_PROT */
277 /*-----------------------------------------------------------------------------------*/