Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / modules / network / SMSTCPIP / mem.c
blob06a5994fbd1552ed8c5e01fff4025f8d91e6ccca
1 /** @file
3 * Dynamic memory manager
5 */
7 /*
8 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * OF SUCH DAMAGE.
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
40 #include "lwip/arch.h"
41 #include "lwip/opt.h"
42 #include "lwip/def.h"
43 #include "lwip/mem.h"
45 #include "lwip/sys.h"
47 #include "lwip/stats.h"
49 #include <thsemap.h>
50 #include <sysclib.h>
52 #include "smsutils.h"
54 struct mem {
55 mem_size_t next, prev;
56 #if MEM_ALIGNMENT == 1
57 u8_t used;
58 #elif MEM_ALIGNMENT == 2
59 u16_t used;
60 #elif MEM_ALIGNMENT == 4
61 u32_t used;
62 #else
63 #error "unhandled MEM_ALIGNMENT size"
64 #endif /* MEM_ALIGNMENT */
65 };
67 static struct mem *ram_end;
68 static u8_t* ram;
69 static u8_t ramblock[MEM_SIZE + sizeof(struct mem) + MEM_ALIGNMENT];
71 #define MIN_SIZE 12
72 #if 0 /* this one does not align correctly for some, resulting in crashes */
73 #define SIZEOF_STRUCT_MEM (unsigned int)MEM_ALIGN_SIZE(sizeof(struct mem))
74 #else
75 #define SIZEOF_STRUCT_MEM (sizeof(struct mem) + \
76 (((sizeof(struct mem) % MEM_ALIGNMENT) == 0)? 0 : \
77 (4 - (sizeof(struct mem) % MEM_ALIGNMENT))))
78 #endif
81 static struct mem *lfree; /* pointer to the lowest free block */
83 static sys_sem_t mem_sem;
85 static void
86 plug_holes(struct mem *mem)
88 struct mem *nmem;
89 struct mem *pmem;
91 LWIP_ASSERT("plug_holes: mem >= ram", (u8_t *)mem >= ram);
92 LWIP_ASSERT("plug_holes: mem < ram_end", (u8_t *)mem < (u8_t *)ram_end);
93 LWIP_ASSERT("plug_holes: mem->used == 0", mem->used == 0);
95 /* plug hole forward */
96 LWIP_ASSERT("plug_holes: mem->next <= MEM_SIZE", mem->next <= MEM_SIZE);
98 nmem = (struct mem *)&ram[mem->next];
99 if (mem != nmem && nmem->used == 0 && (u8_t *)nmem != (u8_t *)ram_end) {
100 if (lfree == nmem) {
101 lfree = mem;
103 mem->next = nmem->next;
104 ((struct mem *)&ram[nmem->next])->prev = (u8_t *)mem - ram;
107 /* plug hole backward */
108 pmem = (struct mem *)&ram[mem->prev];
109 if (pmem != mem && pmem->used == 0) {
110 if (lfree == mem) {
111 lfree = pmem;
113 pmem->next = mem->next;
114 ((struct mem *)&ram[mem->next])->prev = (u8_t *)pmem - ram;
118 void
119 mem_init(void)
121 struct mem *mem;
123 //Boman666: Originally ram was the array now called ramblock. I didn't experience any problem but ram could end up incorrecly
124 //aligned, causing a crash.
125 ram=MEM_ALIGN(ramblock+MEM_ALIGNMENT-1);
126 mips_memset(ram, 0, MEM_SIZE);
127 mem = (struct mem *)ram;
128 mem->next = MEM_SIZE;
129 mem->prev = 0;
130 mem->used = 0;
131 ram_end = (struct mem *)&ram[MEM_SIZE];
132 ram_end->used = 1;
133 ram_end->next = MEM_SIZE;
134 ram_end->prev = MEM_SIZE;
136 mem_sem = sys_sem_new(1);
138 lfree = (struct mem *)ram;
140 #if MEM_STATS
141 lwip_stats.mem.avail = MEM_SIZE;
142 #endif /* MEM_STATS */
145 void
146 mem_free(void *rmem)
148 struct mem *mem;
150 if (rmem == NULL) {
151 LWIP_DEBUGF(MEM_DEBUG | DBG_TRACE | 2, ("mem_free(p == NULL) was called.\n"));
152 return;
155 sys_sem_wait(mem_sem);
157 LWIP_ASSERT("mem_free: legal memory", (u8_t *)rmem >= (u8_t *)ram &&
158 (u8_t *)rmem < (u8_t *)ram_end);
160 if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) {
161 LWIP_DEBUGF(MEM_DEBUG | 3, ("mem_free: illegal memory\n"));
162 #if MEM_STATS
163 ++lwip_stats.mem.err;
164 #endif /* MEM_STATS */
165 sys_sem_signal(mem_sem);
166 return;
168 mem = (struct mem *)((u8_t *)rmem - SIZEOF_STRUCT_MEM);
170 LWIP_ASSERT("mem_free: mem->used", mem->used);
172 mem->used = 0;
174 if (mem < lfree) {
175 lfree = mem;
178 #if MEM_STATS
179 lwip_stats.mem.used -= mem->next - ((u8_t *)mem - ram);
181 #endif /* MEM_STATS */
182 plug_holes(mem);
183 sys_sem_signal(mem_sem);
185 void *
186 mem_reallocm(void *rmem, mem_size_t newsize)
188 void *nmem;
189 nmem = mem_malloc(newsize);
190 if (nmem == NULL) {
191 return mem_realloc(rmem, newsize);
193 mips_memcpy(nmem, rmem, newsize);
194 mem_free(rmem);
195 return nmem;
198 void *
199 mem_realloc(void *rmem, mem_size_t newsize)
201 mem_size_t size;
202 mem_size_t ptr, ptr2;
203 struct mem *mem, *mem2;
205 /* Expand the size of the allocated memory region so that we can
206 adjust for alignment. */
207 if ((newsize % MEM_ALIGNMENT) != 0) {
208 newsize += MEM_ALIGNMENT - ((newsize + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT);
211 if (newsize > MEM_SIZE) {
212 return NULL;
215 sys_sem_wait(mem_sem);
217 LWIP_ASSERT("mem_realloc: legal memory", (u8_t *)rmem >= (u8_t *)ram &&
218 (u8_t *)rmem < (u8_t *)ram_end);
220 if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) {
221 LWIP_DEBUGF(MEM_DEBUG | 3, ("mem_realloc: illegal memory\n"));
222 return rmem;
224 mem = (struct mem *)((u8_t *)rmem - SIZEOF_STRUCT_MEM);
226 ptr = (u8_t *)mem - ram;
228 size = mem->next - ptr - SIZEOF_STRUCT_MEM;
229 #if MEM_STATS
230 lwip_stats.mem.used -= (size - newsize);
231 #endif /* MEM_STATS */
233 if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size) {
234 ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
235 mem2 = (struct mem *)&ram[ptr2];
236 mem2->used = 0;
237 mem2->next = mem->next;
238 mem2->prev = ptr;
239 mem->next = ptr2;
240 if (mem2->next != MEM_SIZE) {
241 ((struct mem *)&ram[mem2->next])->prev = ptr2;
244 plug_holes(mem2);
246 sys_sem_signal(mem_sem);
247 return rmem;
249 void *
250 mem_malloc(mem_size_t size)
252 mem_size_t ptr, ptr2;
253 struct mem *mem, *mem2;
255 if (size == 0) {
256 return NULL;
259 /* Expand the size of the allocated memory region so that we can
260 adjust for alignment. */
261 if ((size % MEM_ALIGNMENT) != 0) {
262 size += MEM_ALIGNMENT - ((size + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT);
265 if (size > MEM_SIZE) {
266 return NULL;
269 sys_sem_wait(mem_sem);
271 for (ptr = (u8_t *)lfree - ram; ptr < MEM_SIZE; ptr = ((struct mem *)&ram[ptr])->next) {
272 mem = (struct mem *)&ram[ptr];
273 if (!mem->used &&
274 mem->next - (ptr + SIZEOF_STRUCT_MEM) >= size + SIZEOF_STRUCT_MEM) {
276 ptr2 = ptr + SIZEOF_STRUCT_MEM + size;
277 mem2 = (struct mem *)&ram[ptr2];
279 mem2->prev = ptr;
280 mem2->next = mem->next;
281 mem->next = ptr2;
282 if (mem2->next != MEM_SIZE) {
283 ((struct mem *)&ram[mem2->next])->prev = ptr2;
286 mem2->used = 0;
287 mem->used = 1;
288 #if MEM_STATS
289 lwip_stats.mem.used += (size + SIZEOF_STRUCT_MEM);
290 /* if (lwip_stats.mem.max < lwip_stats.mem.used) {
291 lwip_stats.mem.max = lwip_stats.mem.used;
292 } */
293 if (lwip_stats.mem.max < ptr2) {
294 lwip_stats.mem.max = ptr2;
296 #endif /* MEM_STATS */
298 if (mem == lfree) {
299 /* Find next free block after mem */
300 while (lfree->used && lfree != ram_end) {
301 lfree = (struct mem *)&ram[lfree->next];
303 LWIP_ASSERT("mem_malloc: !lfree->used", !lfree->used);
305 sys_sem_signal(mem_sem);
307 LWIP_ASSERT("mem_malloc: allocated memory not above ram_end.",
308 (u32_t)mem + SIZEOF_STRUCT_MEM + size <= (u32_t)ram_end);
309 LWIP_ASSERT("mem_malloc: allocated memory properly aligned.",
310 (unsigned long)((u8_t *)mem + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT == 0);
311 return (u8_t *)mem + SIZEOF_STRUCT_MEM;
314 LWIP_DEBUGF(MEM_DEBUG | 2, ("mem_malloc: could not allocate %d bytes\n", (int)size));
315 #if MEM_STATS
316 ++lwip_stats.mem.err;
317 #endif /* MEM_STATS */
318 sys_sem_signal(mem_sem);
319 return NULL;