82 |
struct mem *nmem; |
struct mem *nmem; |
83 |
struct mem *pmem; |
struct mem *pmem; |
84 |
|
|
85 |
ASSERT("plug_holes: mem >= ram", (u8_t *)mem >= ram); |
LWIP_ASSERT("plug_holes: mem >= ram", (u8_t *)mem >= ram); |
86 |
ASSERT("plug_holes: mem < ram_end", (u8_t *)mem < (u8_t *)ram_end); |
LWIP_ASSERT("plug_holes: mem < ram_end", (u8_t *)mem < (u8_t *)ram_end); |
87 |
ASSERT("plug_holes: mem->used == 0", mem->used == 0); |
LWIP_ASSERT("plug_holes: mem->used == 0", mem->used == 0); |
88 |
|
|
89 |
/* plug hole forward */ |
/* plug hole forward */ |
90 |
ASSERT("plug_holes: mem->next <= MEM_SIZE", mem->next <= MEM_SIZE); |
LWIP_ASSERT("plug_holes: mem->next <= MEM_SIZE", mem->next <= MEM_SIZE); |
91 |
|
|
92 |
nmem = (struct mem *)&ram[mem->next]; |
nmem = (struct mem *)&ram[mem->next]; |
93 |
if(mem != nmem && nmem->used == 0 && (u8_t *)nmem != (u8_t *)ram_end) { |
if(mem != nmem && nmem->used == 0 && (u8_t *)nmem != (u8_t *)ram_end) { |
134 |
#endif /* MEM_STATS */ |
#endif /* MEM_STATS */ |
135 |
} |
} |
136 |
/*-----------------------------------------------------------------------------------*/ |
/*-----------------------------------------------------------------------------------*/ |
|
void * |
|
|
mem_malloc(mem_size_t size) |
|
|
{ |
|
|
mem_size_t ptr, ptr2; |
|
|
struct mem *mem, *mem2; |
|
|
|
|
|
if(size == 0) { |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
/* Expand the size of the allocated memory region so that we can |
|
|
adjust for alignment. */ |
|
|
if((size % MEM_ALIGNMENT) != 0) { |
|
|
size += MEM_ALIGNMENT - ((size + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT); |
|
|
} |
|
|
|
|
|
if(size > MEM_SIZE) { |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
sys_sem_wait(mem_sem); |
|
|
|
|
|
for(ptr = (u8_t *)lfree - ram; ptr < MEM_SIZE; ptr = ((struct mem *)&ram[ptr])->next) { |
|
|
mem = (struct mem *)&ram[ptr]; |
|
|
if(!mem->used && |
|
|
mem->next - (ptr + SIZEOF_STRUCT_MEM) >= size + SIZEOF_STRUCT_MEM) { |
|
|
ptr2 = ptr + SIZEOF_STRUCT_MEM + size; |
|
|
mem2 = (struct mem *)&ram[ptr2]; |
|
|
|
|
|
mem2->prev = ptr; |
|
|
mem2->next = mem->next; |
|
|
mem->next = ptr2; |
|
|
if(mem2->next != MEM_SIZE) { |
|
|
((struct mem *)&ram[mem2->next])->prev = ptr2; |
|
|
} |
|
|
|
|
|
mem2->used = 0; |
|
|
mem->used = 1; |
|
|
#ifdef MEM_STATS |
|
|
lwip_stats.mem.used += size; |
|
|
/* if(lwip_stats.mem.max < lwip_stats.mem.used) { |
|
|
lwip_stats.mem.max = lwip_stats.mem.used; |
|
|
} */ |
|
|
if(lwip_stats.mem.max < ptr2) { |
|
|
lwip_stats.mem.max = ptr2; |
|
|
} |
|
|
#endif /* MEM_STATS */ |
|
|
|
|
|
if(mem == lfree) { |
|
|
/* Find next free block after mem */ |
|
|
while(lfree->used && lfree != ram_end) { |
|
|
lfree = (struct mem *)&ram[lfree->next]; |
|
|
} |
|
|
ASSERT("mem_malloc: !lfree->used", !lfree->used); |
|
|
} |
|
|
sys_sem_signal(mem_sem); |
|
|
ASSERT("mem_malloc: allocated memory not above ram_end.", |
|
|
(u32_t)mem + SIZEOF_STRUCT_MEM + size <= (u32_t)ram_end); |
|
|
ASSERT("mem_malloc: allocated memory properly aligned.", |
|
|
(unsigned long)((u8_t *)mem + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT == 0); |
|
|
return (u8_t *)mem + SIZEOF_STRUCT_MEM; |
|
|
} |
|
|
} |
|
|
DEBUGF(MEM_DEBUG, ("mem_malloc: could not allocate %d bytes\n", (int)size)); |
|
|
#ifdef MEM_STATS |
|
|
++lwip_stats.mem.err; |
|
|
#endif /* MEM_STATS */ |
|
|
sys_sem_signal(mem_sem); |
|
|
return NULL; |
|
|
} |
|
|
/*-----------------------------------------------------------------------------------*/ |
|
137 |
void |
void |
138 |
mem_free(void *rmem) |
mem_free(void *rmem) |
139 |
{ |
{ |
145 |
|
|
146 |
sys_sem_wait(mem_sem); |
sys_sem_wait(mem_sem); |
147 |
|
|
148 |
ASSERT("mem_free: legal memory", (u8_t *)rmem >= (u8_t *)ram && |
LWIP_ASSERT("mem_free: legal memory", (u8_t *)rmem >= (u8_t *)ram && |
149 |
(u8_t *)rmem < (u8_t *)ram_end); |
(u8_t *)rmem < (u8_t *)ram_end); |
150 |
|
|
151 |
|
|
158 |
} |
} |
159 |
mem = (struct mem *)((u8_t *)rmem - SIZEOF_STRUCT_MEM); |
mem = (struct mem *)((u8_t *)rmem - SIZEOF_STRUCT_MEM); |
160 |
|
|
161 |
ASSERT("mem_free: mem->used", mem->used); |
LWIP_ASSERT("mem_free: mem->used", mem->used); |
162 |
|
|
163 |
mem->used = 0; |
mem->used = 0; |
164 |
|
|
196 |
|
|
197 |
sys_sem_wait(mem_sem); |
sys_sem_wait(mem_sem); |
198 |
|
|
199 |
ASSERT("mem_realloc: legal memory", (u8_t *)rmem >= (u8_t *)ram && |
LWIP_ASSERT("mem_realloc: legal memory", (u8_t *)rmem >= (u8_t *)ram && |
200 |
(u8_t *)rmem < (u8_t *)ram_end); |
(u8_t *)rmem < (u8_t *)ram_end); |
201 |
|
|
202 |
if((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) { |
if((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) { |
229 |
return rmem; |
return rmem; |
230 |
} |
} |
231 |
/*-----------------------------------------------------------------------------------*/ |
/*-----------------------------------------------------------------------------------*/ |
232 |
|
void * |
233 |
|
mem_malloc(mem_size_t size) |
234 |
|
{ |
235 |
|
mem_size_t ptr, ptr2; |
236 |
|
struct mem *mem, *mem2; |
237 |
|
|
238 |
|
if(size == 0) { |
239 |
|
return NULL; |
240 |
|
} |
241 |
|
|
242 |
|
/* Expand the size of the allocated memory region so that we can |
243 |
|
adjust for alignment. */ |
244 |
|
if((size % MEM_ALIGNMENT) != 0) { |
245 |
|
size += MEM_ALIGNMENT - ((size + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT); |
246 |
|
} |
247 |
|
|
248 |
|
if(size > MEM_SIZE) { |
249 |
|
return NULL; |
250 |
|
} |
251 |
|
|
252 |
|
sys_sem_wait(mem_sem); |
253 |
|
|
254 |
|
for(ptr = (u8_t *)lfree - ram; ptr < MEM_SIZE; ptr = ((struct mem *)&ram[ptr])->next) { |
255 |
|
mem = (struct mem *)&ram[ptr]; |
256 |
|
if(!mem->used && |
257 |
|
mem->next - (ptr + SIZEOF_STRUCT_MEM) >= size + SIZEOF_STRUCT_MEM) { |
258 |
|
ptr2 = ptr + SIZEOF_STRUCT_MEM + size; |
259 |
|
mem2 = (struct mem *)&ram[ptr2]; |
260 |
|
|
261 |
|
mem2->prev = ptr; |
262 |
|
mem2->next = mem->next; |
263 |
|
mem->next = ptr2; |
264 |
|
if(mem2->next != MEM_SIZE) { |
265 |
|
((struct mem *)&ram[mem2->next])->prev = ptr2; |
266 |
|
} |
267 |
|
|
268 |
|
mem2->used = 0; |
269 |
|
mem->used = 1; |
270 |
|
#ifdef MEM_STATS |
271 |
|
lwip_stats.mem.used += size; |
272 |
|
/* if(lwip_stats.mem.max < lwip_stats.mem.used) { |
273 |
|
lwip_stats.mem.max = lwip_stats.mem.used; |
274 |
|
} */ |
275 |
|
if(lwip_stats.mem.max < ptr2) { |
276 |
|
lwip_stats.mem.max = ptr2; |
277 |
|
} |
278 |
|
#endif /* MEM_STATS */ |
279 |
|
|
280 |
|
if(mem == lfree) { |
281 |
|
/* Find next free block after mem */ |
282 |
|
while(lfree->used && lfree != ram_end) { |
283 |
|
lfree = (struct mem *)&ram[lfree->next]; |
284 |
|
} |
285 |
|
LWIP_ASSERT("mem_malloc: !lfree->used", !lfree->used); |
286 |
|
} |
287 |
|
sys_sem_signal(mem_sem); |
288 |
|
LWIP_ASSERT("mem_malloc: allocated memory not above ram_end.", |
289 |
|
(u32_t)mem + SIZEOF_STRUCT_MEM + size <= (u32_t)ram_end); |
290 |
|
LWIP_ASSERT("mem_malloc: allocated memory properly aligned.", |
291 |
|
(unsigned long)((u8_t *)mem + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT == 0); |
292 |
|
return (u8_t *)mem + SIZEOF_STRUCT_MEM; |
293 |
|
} |
294 |
|
} |
295 |
|
DEBUGF(MEM_DEBUG, ("mem_malloc: could not allocate %d bytes\n", (int)size)); |
296 |
|
#ifdef MEM_STATS |
297 |
|
++lwip_stats.mem.err; |
298 |
|
#endif /* MEM_STATS */ |
299 |
|
sys_sem_signal(mem_sem); |
300 |
|
return NULL; |
301 |
|
} |
302 |
|
/*-----------------------------------------------------------------------------------*/ |