]> git.sur5r.net Git - freertos/blob - Demo/lwIP_Demo_Rowley_ARM7/lwip-1.1.0/src/core/memp.c
First version under SVN is V4.0.1
[freertos] / Demo / lwIP_Demo_Rowley_ARM7 / lwip-1.1.0 / src / core / memp.c
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved. 
4  * 
5  * Redistribution and use in source and binary forms, with or without modification, 
6  * are permitted provided that the following conditions are met:
7  *
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. 
15  *
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 
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  * 
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32
33 #include "lwip/opt.h"
34
35 #include "lwip/memp.h"
36
37 #include "lwip/pbuf.h"
38 #include "lwip/udp.h"
39 #include "lwip/raw.h"
40 #include "lwip/tcp.h"
41 #include "lwip/api.h"
42 #include "lwip/api_msg.h"
43 #include "lwip/tcpip.h"
44
45 #include "lwip/sys.h"
46 #include "lwip/stats.h"
47
48 struct memp {
49   struct memp *next;
50 };
51
52
53
54 static struct memp *memp_tab[MEMP_MAX];
55
56 static const u16_t memp_sizes[MEMP_MAX] = {
57   sizeof(struct pbuf),
58   sizeof(struct raw_pcb),
59   sizeof(struct udp_pcb),
60   sizeof(struct tcp_pcb),
61   sizeof(struct tcp_pcb_listen),
62   sizeof(struct tcp_seg),
63   sizeof(struct netbuf),
64   sizeof(struct netconn),
65   sizeof(struct api_msg),
66   sizeof(struct tcpip_msg),
67   sizeof(struct sys_timeout)
68 };
69
70 static const u16_t memp_num[MEMP_MAX] = {
71   MEMP_NUM_PBUF,
72   MEMP_NUM_RAW_PCB,
73   MEMP_NUM_UDP_PCB,
74   MEMP_NUM_TCP_PCB,
75   MEMP_NUM_TCP_PCB_LISTEN,
76   MEMP_NUM_TCP_SEG,
77   MEMP_NUM_NETBUF,
78   MEMP_NUM_NETCONN,
79   MEMP_NUM_API_MSG,
80   MEMP_NUM_TCPIP_MSG,
81   MEMP_NUM_SYS_TIMEOUT
82 };
83
84 static u8_t memp_memory[(MEMP_NUM_PBUF *
85        MEM_ALIGN_SIZE(sizeof(struct pbuf) +
86           sizeof(struct memp)) +
87       MEMP_NUM_RAW_PCB *
88        MEM_ALIGN_SIZE(sizeof(struct raw_pcb) +
89           sizeof(struct memp)) +
90       MEMP_NUM_UDP_PCB *
91        MEM_ALIGN_SIZE(sizeof(struct udp_pcb) +
92           sizeof(struct memp)) +
93       MEMP_NUM_TCP_PCB *
94        MEM_ALIGN_SIZE(sizeof(struct tcp_pcb) +
95           sizeof(struct memp)) +
96       MEMP_NUM_TCP_PCB_LISTEN *
97        MEM_ALIGN_SIZE(sizeof(struct tcp_pcb_listen) +
98           sizeof(struct memp)) +
99       MEMP_NUM_TCP_SEG *
100        MEM_ALIGN_SIZE(sizeof(struct tcp_seg) +
101           sizeof(struct memp)) +
102       MEMP_NUM_NETBUF *
103        MEM_ALIGN_SIZE(sizeof(struct netbuf) +
104           sizeof(struct memp)) +
105       MEMP_NUM_NETCONN *
106        MEM_ALIGN_SIZE(sizeof(struct netconn) +
107           sizeof(struct memp)) +
108       MEMP_NUM_API_MSG *
109        MEM_ALIGN_SIZE(sizeof(struct api_msg) +
110           sizeof(struct memp)) +
111       MEMP_NUM_TCPIP_MSG *
112        MEM_ALIGN_SIZE(sizeof(struct tcpip_msg) +
113           sizeof(struct memp)) +
114       MEMP_NUM_SYS_TIMEOUT *
115        MEM_ALIGN_SIZE(sizeof(struct sys_timeout) +
116           sizeof(struct memp)))];
117
118
119 #if !SYS_LIGHTWEIGHT_PROT
120 static sys_sem_t mutex;
121 #endif
122
123 #if MEMP_SANITY_CHECK
124 static int
125 memp_sanity(void)
126 {
127   int i, c;
128   struct memp *m, *n;
129
130   for(i = 0; i < MEMP_MAX; i++) {
131     for(m = memp_tab[i]; m != NULL; m = m->next) {
132       c = 1;
133       for(n = memp_tab[i]; n != NULL; n = n->next) {
134          if (n == m) {
135           --c;
136         }
137         if (c < 0) return 0; /* LW was: abort(); */
138       }
139     }
140   }
141   return 1;
142 }
143 #endif /* MEMP_SANITY_CHECK*/
144
145 void
146 memp_init(void)
147 {
148   struct memp *m, *memp;
149   u16_t i, j;
150   u16_t size;
151       
152 #if MEMP_STATS
153   for(i = 0; i < MEMP_MAX; ++i) {
154     lwip_stats.memp[i].used = lwip_stats.memp[i].max =
155       lwip_stats.memp[i].err = 0;
156     lwip_stats.memp[i].avail = memp_num[i];
157   }
158 #endif /* MEMP_STATS */
159
160   memp = (struct memp *)&memp_memory[0];
161   for(i = 0; i < MEMP_MAX; ++i) {
162     size = MEM_ALIGN_SIZE(memp_sizes[i] + sizeof(struct memp));
163     if (memp_num[i] > 0) {
164       memp_tab[i] = memp;
165       m = memp;
166       
167       for(j = 0; j < memp_num[i]; ++j) {
168   m->next = (struct memp *)MEM_ALIGN((u8_t *)m + size);
169   memp = m;
170   m = m->next;
171       }
172       memp->next = NULL;
173       memp = m;
174     } else {
175       memp_tab[i] = NULL;
176     }
177   }
178
179 #if !SYS_LIGHTWEIGHT_PROT
180   mutex = sys_sem_new(1);
181 #endif
182
183   
184 }
185
186 void *
187 memp_malloc(memp_t type)
188 {
189   struct memp *memp;
190   void *mem;
191 #if SYS_LIGHTWEIGHT_PROT
192   SYS_ARCH_DECL_PROTECT(old_level);
193 #endif
194  
195   LWIP_ASSERT("memp_malloc: type < MEMP_MAX", type < MEMP_MAX);
196
197 #if SYS_LIGHTWEIGHT_PROT
198   SYS_ARCH_PROTECT(old_level);
199 #else /* SYS_LIGHTWEIGHT_PROT */  
200   sys_sem_wait(mutex);
201 #endif /* SYS_LIGHTWEIGHT_PROT */  
202
203   memp = memp_tab[type];
204   
205   if (memp != NULL) {    
206     memp_tab[type] = memp->next;    
207     memp->next = NULL;
208 #if MEMP_STATS
209     ++lwip_stats.memp[type].used;
210     if (lwip_stats.memp[type].used > lwip_stats.memp[type].max) {
211       lwip_stats.memp[type].max = lwip_stats.memp[type].used;
212     }
213 #endif /* MEMP_STATS */
214 #if SYS_LIGHTWEIGHT_PROT
215     SYS_ARCH_UNPROTECT(old_level);
216 #else /* SYS_LIGHTWEIGHT_PROT */
217     sys_sem_signal(mutex);
218 #endif /* SYS_LIGHTWEIGHT_PROT */  
219     LWIP_ASSERT("memp_malloc: memp properly aligned",
220      ((mem_ptr_t)MEM_ALIGN((u8_t *)memp + sizeof(struct memp)) % MEM_ALIGNMENT) == 0);
221
222     mem = MEM_ALIGN((u8_t *)memp + sizeof(struct memp));
223     return mem;
224   } else {
225     LWIP_DEBUGF(MEMP_DEBUG | 2, ("memp_malloc: out of memory in pool %d\n", type));
226 #if MEMP_STATS
227     ++lwip_stats.memp[type].err;
228 #endif /* MEMP_STATS */
229 #if SYS_LIGHTWEIGHT_PROT
230   SYS_ARCH_UNPROTECT(old_level);
231 #else /* SYS_LIGHTWEIGHT_PROT */
232   sys_sem_signal(mutex);
233 #endif /* SYS_LIGHTWEIGHT_PROT */  
234     return NULL;
235   }
236 }
237
238 void
239 memp_free(memp_t type, void *mem)
240 {
241   struct memp *memp;
242 #if SYS_LIGHTWEIGHT_PROT
243   SYS_ARCH_DECL_PROTECT(old_level);
244 #endif /* SYS_LIGHTWEIGHT_PROT */  
245
246   if (mem == NULL) {
247     return;
248   }
249   memp = (struct memp *)((u8_t *)mem - sizeof(struct memp));
250
251 #if SYS_LIGHTWEIGHT_PROT
252     SYS_ARCH_PROTECT(old_level);
253 #else /* SYS_LIGHTWEIGHT_PROT */  
254   sys_sem_wait(mutex);
255 #endif /* SYS_LIGHTWEIGHT_PROT */  
256
257 #if MEMP_STATS
258   lwip_stats.memp[type].used--; 
259 #endif /* MEMP_STATS */
260   
261   memp->next = memp_tab[type]; 
262   memp_tab[type] = memp;
263
264 #if MEMP_SANITY_CHECK
265   LWIP_ASSERT("memp sanity", memp_sanity());
266 #endif  
267
268 #if SYS_LIGHTWEIGHT_PROT
269   SYS_ARCH_UNPROTECT(old_level);
270 #else /* SYS_LIGHTWEIGHT_PROT */
271   sys_sem_signal(mutex);
272 #endif /* SYS_LIGHTWEIGHT_PROT */  
273 }
274