2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without modification,
\r
6 * are permitted provided that the following conditions are met:
\r
8 * 1. Redistributions of source code must retain the above copyright notice,
\r
9 * this list of conditions and the following disclaimer.
\r
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
11 * this list of conditions and the following disclaimer in the documentation
\r
12 * and/or other materials provided with the distribution.
\r
13 * 3. The name of the author may not be used to endorse or promote products
\r
14 * derived from this software without specific prior written permission.
\r
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
\r
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
\r
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
\r
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
\r
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
\r
27 * This file is part of the lwIP TCP/IP stack.
\r
29 * Author: Adam Dunkels <adam@sics.se>
\r
33 #include "lwip/opt.h"
\r
35 #include "lwip/memp.h"
\r
37 #include "lwip/pbuf.h"
\r
38 #include "lwip/udp.h"
\r
39 #include "lwip/raw.h"
\r
40 #include "lwip/tcp.h"
\r
41 #include "lwip/api.h"
\r
42 #include "lwip/api_msg.h"
\r
43 #include "lwip/tcpip.h"
\r
45 #include "lwip/sys.h"
\r
46 #include "lwip/stats.h"
\r
52 #define MEMP_SIZE MEM_ALIGN_SIZE(sizeof(struct memp))
\r
54 static struct memp *memp_tab[MEMP_MAX];
\r
56 static const u16_t memp_sizes[MEMP_MAX] = {
\r
57 MEM_ALIGN_SIZE(sizeof(struct pbuf)),
\r
58 MEM_ALIGN_SIZE(sizeof(struct raw_pcb)),
\r
59 MEM_ALIGN_SIZE(sizeof(struct udp_pcb)),
\r
60 MEM_ALIGN_SIZE(sizeof(struct tcp_pcb)),
\r
61 MEM_ALIGN_SIZE(sizeof(struct tcp_pcb_listen)),
\r
62 MEM_ALIGN_SIZE(sizeof(struct tcp_seg)),
\r
63 MEM_ALIGN_SIZE(sizeof(struct netbuf)),
\r
64 MEM_ALIGN_SIZE(sizeof(struct netconn)),
\r
65 MEM_ALIGN_SIZE(sizeof(struct api_msg)),
\r
66 MEM_ALIGN_SIZE(sizeof(struct tcpip_msg)),
\r
67 MEM_ALIGN_SIZE(sizeof(struct sys_timeo))
\r
70 static const u16_t memp_num[MEMP_MAX] = {
\r
75 MEMP_NUM_TCP_PCB_LISTEN,
\r
81 MEMP_NUM_SYS_TIMEOUT
\r
84 #define MEMP_TYPE_SIZE(qty, type) \
\r
85 ((qty) * (MEMP_SIZE + MEM_ALIGN_SIZE(sizeof(type))))
\r
87 static u8_t memp_memory[MEM_ALIGNMENT - 1 +
\r
88 MEMP_TYPE_SIZE(MEMP_NUM_PBUF, struct pbuf) +
\r
89 MEMP_TYPE_SIZE(MEMP_NUM_RAW_PCB, struct raw_pcb) +
\r
90 MEMP_TYPE_SIZE(MEMP_NUM_UDP_PCB, struct udp_pcb) +
\r
91 MEMP_TYPE_SIZE(MEMP_NUM_TCP_PCB, struct tcp_pcb) +
\r
92 MEMP_TYPE_SIZE(MEMP_NUM_TCP_PCB_LISTEN, struct tcp_pcb_listen) +
\r
93 MEMP_TYPE_SIZE(MEMP_NUM_TCP_SEG, struct tcp_seg) +
\r
94 MEMP_TYPE_SIZE(MEMP_NUM_NETBUF, struct netbuf) +
\r
95 MEMP_TYPE_SIZE(MEMP_NUM_NETCONN, struct netconn) +
\r
96 MEMP_TYPE_SIZE(MEMP_NUM_API_MSG, struct api_msg) +
\r
97 MEMP_TYPE_SIZE(MEMP_NUM_TCPIP_MSG, struct tcpip_msg) +
\r
98 MEMP_TYPE_SIZE(MEMP_NUM_SYS_TIMEOUT, struct sys_timeo)];
\r
100 #if !SYS_LIGHTWEIGHT_PROT
\r
101 static sys_sem_t mutex;
\r
104 #if MEMP_SANITY_CHECK
\r
109 struct memp *m, *n;
\r
111 for (i = 0; i < MEMP_MAX; i++) {
\r
112 for (m = memp_tab[i]; m != NULL; m = m->next) {
\r
114 for (n = memp_tab[i]; n != NULL; n = n->next) {
\r
115 if (n == m && --c < 0) {
\r
116 return 0; /* LW was: abort(); */
\r
123 #endif /* MEMP_SANITY_CHECK*/
\r
132 for (i = 0; i < MEMP_MAX; ++i) {
\r
133 lwip_stats.memp[i].used = lwip_stats.memp[i].max =
\r
134 lwip_stats.memp[i].err = 0;
\r
135 lwip_stats.memp[i].avail = memp_num[i];
\r
137 #endif /* MEMP_STATS */
\r
139 memp = MEM_ALIGN(memp_memory);
\r
140 for (i = 0; i < MEMP_MAX; ++i) {
\r
141 memp_tab[i] = NULL;
\r
142 for (j = 0; j < memp_num[i]; ++j) {
\r
143 memp->next = memp_tab[i];
\r
144 memp_tab[i] = memp;
\r
145 memp = (struct memp *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]);
\r
149 #if !SYS_LIGHTWEIGHT_PROT
\r
150 mutex = sys_sem_new(1);
\r
155 memp_malloc(memp_t type)
\r
159 #if SYS_LIGHTWEIGHT_PROT
\r
160 SYS_ARCH_DECL_PROTECT(old_level);
\r
163 LWIP_ASSERT("memp_malloc: type < MEMP_MAX", type < MEMP_MAX);
\r
165 #if SYS_LIGHTWEIGHT_PROT
\r
166 SYS_ARCH_PROTECT(old_level);
\r
167 #else /* SYS_LIGHTWEIGHT_PROT */
\r
168 sys_sem_wait(mutex);
\r
169 #endif /* SYS_LIGHTWEIGHT_PROT */
\r
171 memp = memp_tab[type];
\r
173 if (memp != NULL) {
\r
174 memp_tab[type] = memp->next;
\r
177 ++lwip_stats.memp[type].used;
\r
178 if (lwip_stats.memp[type].used > lwip_stats.memp[type].max) {
\r
179 lwip_stats.memp[type].max = lwip_stats.memp[type].used;
\r
181 #endif /* MEMP_STATS */
\r
182 mem = (u8_t *)memp + MEMP_SIZE;
\r
183 LWIP_ASSERT("memp_malloc: memp properly aligned",
\r
184 ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
\r
186 LWIP_DEBUGF(MEMP_DEBUG | 2, ("memp_malloc: out of memory in pool %"S16_F"\n", type));
\r
188 ++lwip_stats.memp[type].err;
\r
189 #endif /* MEMP_STATS */
\r
193 #if SYS_LIGHTWEIGHT_PROT
\r
194 SYS_ARCH_UNPROTECT(old_level);
\r
195 #else /* SYS_LIGHTWEIGHT_PROT */
\r
196 sys_sem_signal(mutex);
\r
197 #endif /* SYS_LIGHTWEIGHT_PROT */
\r
203 memp_free(memp_t type, void *mem)
\r
206 #if SYS_LIGHTWEIGHT_PROT
\r
207 SYS_ARCH_DECL_PROTECT(old_level);
\r
208 #endif /* SYS_LIGHTWEIGHT_PROT */
\r
214 memp = (struct memp *)((u8_t *)mem - MEMP_SIZE);
\r
216 #if SYS_LIGHTWEIGHT_PROT
\r
217 SYS_ARCH_PROTECT(old_level);
\r
218 #else /* SYS_LIGHTWEIGHT_PROT */
\r
219 sys_sem_wait(mutex);
\r
220 #endif /* SYS_LIGHTWEIGHT_PROT */
\r
223 lwip_stats.memp[type].used--;
\r
224 #endif /* MEMP_STATS */
\r
226 memp->next = memp_tab[type];
\r
227 memp_tab[type] = memp;
\r
229 #if MEMP_SANITY_CHECK
\r
230 LWIP_ASSERT("memp sanity", memp_sanity());
\r
233 #if SYS_LIGHTWEIGHT_PROT
\r
234 SYS_ARCH_UNPROTECT(old_level);
\r
235 #else /* SYS_LIGHTWEIGHT_PROT */
\r
236 sys_sem_signal(mutex);
\r
237 #endif /* SYS_LIGHTWEIGHT_PROT */
\r