OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [ethernet/] [lwIP/] [core/] [memp.c] - Blame information for rev 606

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 606 jeremybenn
/*
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
#define MEMP_SIZE MEM_ALIGN_SIZE(sizeof(struct memp))
53
 
54
static struct memp *memp_tab[MEMP_MAX];
55
 
56
static const u16_t memp_sizes[MEMP_MAX] = {
57
  MEM_ALIGN_SIZE(sizeof(struct pbuf)),
58
  MEM_ALIGN_SIZE(sizeof(struct raw_pcb)),
59
  MEM_ALIGN_SIZE(sizeof(struct udp_pcb)),
60
  MEM_ALIGN_SIZE(sizeof(struct tcp_pcb)),
61
  MEM_ALIGN_SIZE(sizeof(struct tcp_pcb_listen)),
62
  MEM_ALIGN_SIZE(sizeof(struct tcp_seg)),
63
  MEM_ALIGN_SIZE(sizeof(struct netbuf)),
64
  MEM_ALIGN_SIZE(sizeof(struct netconn)),
65
  MEM_ALIGN_SIZE(sizeof(struct api_msg)),
66
  MEM_ALIGN_SIZE(sizeof(struct tcpip_msg)),
67
  MEM_ALIGN_SIZE(sizeof(struct sys_timeo))
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
#define MEMP_TYPE_SIZE(qty, type) \
85
  ((qty) * (MEMP_SIZE + MEM_ALIGN_SIZE(sizeof(type))))
86
 
87
static u8_t memp_memory[MEM_ALIGNMENT - 1 +
88
  MEMP_TYPE_SIZE(MEMP_NUM_PBUF, struct pbuf) +
89
  MEMP_TYPE_SIZE(MEMP_NUM_RAW_PCB, struct raw_pcb) +
90
  MEMP_TYPE_SIZE(MEMP_NUM_UDP_PCB, struct udp_pcb) +
91
  MEMP_TYPE_SIZE(MEMP_NUM_TCP_PCB, struct tcp_pcb) +
92
  MEMP_TYPE_SIZE(MEMP_NUM_TCP_PCB_LISTEN, struct tcp_pcb_listen) +
93
  MEMP_TYPE_SIZE(MEMP_NUM_TCP_SEG, struct tcp_seg) +
94
  MEMP_TYPE_SIZE(MEMP_NUM_NETBUF, struct netbuf) +
95
  MEMP_TYPE_SIZE(MEMP_NUM_NETCONN, struct netconn) +
96
  MEMP_TYPE_SIZE(MEMP_NUM_API_MSG, struct api_msg) +
97
  MEMP_TYPE_SIZE(MEMP_NUM_TCPIP_MSG, struct tcpip_msg) +
98
  MEMP_TYPE_SIZE(MEMP_NUM_SYS_TIMEOUT, struct sys_timeo)];
99
 
100
#if !SYS_LIGHTWEIGHT_PROT
101
static sys_sem_t mutex;
102
#endif
103
 
104
#if MEMP_SANITY_CHECK
105
static int
106
memp_sanity(void)
107
{
108
  s16_t i, c;
109
  struct memp *m, *n;
110
 
111
  for (i = 0; i < MEMP_MAX; i++) {
112
    for (m = memp_tab[i]; m != NULL; m = m->next) {
113
      c = 1;
114
      for (n = memp_tab[i]; n != NULL; n = n->next) {
115
        if (n == m && --c < 0) {
116
          return 0; /* LW was: abort(); */
117
        }
118
      }
119
    }
120
  }
121
  return 1;
122
}
123
#endif /* MEMP_SANITY_CHECK*/
124
 
125
void
126
memp_init(void)
127
{
128
  struct memp *memp;
129
  u16_t i, j;
130
 
131
#if MEMP_STATS
132
  for (i = 0; i < MEMP_MAX; ++i) {
133
    lwip_stats.memp[i].used = lwip_stats.memp[i].max =
134
      lwip_stats.memp[i].err = 0;
135
    lwip_stats.memp[i].avail = memp_num[i];
136
  }
137
#endif /* MEMP_STATS */
138
 
139
  memp = MEM_ALIGN(memp_memory);
140
  for (i = 0; i < MEMP_MAX; ++i) {
141
    memp_tab[i] = NULL;
142
    for (j = 0; j < memp_num[i]; ++j) {
143
      memp->next = memp_tab[i];
144
      memp_tab[i] = memp;
145
      memp = (struct memp *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]);
146
    }
147
  }
148
 
149
#if !SYS_LIGHTWEIGHT_PROT
150
  mutex = sys_sem_new(1);
151
#endif
152
}
153
 
154
void *
155
memp_malloc(memp_t type)
156
{
157
  struct memp *memp;
158
  void *mem;
159
#if SYS_LIGHTWEIGHT_PROT
160
  SYS_ARCH_DECL_PROTECT(old_level);
161
#endif
162
 
163
  LWIP_ASSERT("memp_malloc: type < MEMP_MAX", type < MEMP_MAX);
164
 
165
#if SYS_LIGHTWEIGHT_PROT
166
  SYS_ARCH_PROTECT(old_level);
167
#else /* SYS_LIGHTWEIGHT_PROT */  
168
  sys_sem_wait(mutex);
169
#endif /* SYS_LIGHTWEIGHT_PROT */  
170
 
171
  memp = memp_tab[type];
172
 
173
  if (memp != NULL) {
174
    memp_tab[type] = memp->next;
175
    memp->next = NULL;
176
#if MEMP_STATS
177
    ++lwip_stats.memp[type].used;
178
    if (lwip_stats.memp[type].used > lwip_stats.memp[type].max) {
179
      lwip_stats.memp[type].max = lwip_stats.memp[type].used;
180
    }
181
#endif /* MEMP_STATS */
182
    mem = (u8_t *)memp + MEMP_SIZE;
183
    LWIP_ASSERT("memp_malloc: memp properly aligned",
184
                ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
185
  } else {
186
    LWIP_DEBUGF(MEMP_DEBUG | 2, ("memp_malloc: out of memory in pool %"S16_F"\n", type));
187
#if MEMP_STATS
188
    ++lwip_stats.memp[type].err;
189
#endif /* MEMP_STATS */
190
    mem = NULL;
191
  }
192
 
193
#if SYS_LIGHTWEIGHT_PROT
194
  SYS_ARCH_UNPROTECT(old_level);
195
#else /* SYS_LIGHTWEIGHT_PROT */
196
  sys_sem_signal(mutex);
197
#endif /* SYS_LIGHTWEIGHT_PROT */  
198
 
199
  return mem;
200
}
201
 
202
void
203
memp_free(memp_t type, void *mem)
204
{
205
  struct memp *memp;
206
#if SYS_LIGHTWEIGHT_PROT
207
  SYS_ARCH_DECL_PROTECT(old_level);
208
#endif /* SYS_LIGHTWEIGHT_PROT */  
209
 
210
  if (mem == NULL) {
211
    return;
212
  }
213
 
214
  memp = (struct memp *)((u8_t *)mem - MEMP_SIZE);
215
 
216
#if SYS_LIGHTWEIGHT_PROT
217
  SYS_ARCH_PROTECT(old_level);
218
#else /* SYS_LIGHTWEIGHT_PROT */  
219
  sys_sem_wait(mutex);
220
#endif /* SYS_LIGHTWEIGHT_PROT */  
221
 
222
#if MEMP_STATS
223
  lwip_stats.memp[type].used--;
224
#endif /* MEMP_STATS */
225
 
226
  memp->next = memp_tab[type];
227
  memp_tab[type] = memp;
228
 
229
#if MEMP_SANITY_CHECK
230
  LWIP_ASSERT("memp sanity", memp_sanity());
231
#endif  
232
 
233
#if SYS_LIGHTWEIGHT_PROT
234
  SYS_ARCH_UNPROTECT(old_level);
235
#else /* SYS_LIGHTWEIGHT_PROT */
236
  sys_sem_signal(mutex);
237
#endif /* SYS_LIGHTWEIGHT_PROT */  
238
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.