89 |
LWIP_ASSERT("plug_holes: mem->next <= MEM_SIZE", mem->next <= MEM_SIZE); |
LWIP_ASSERT("plug_holes: mem->next <= MEM_SIZE", mem->next <= MEM_SIZE); |
90 |
|
|
91 |
nmem = (struct mem *)&ram[mem->next]; |
nmem = (struct mem *)&ram[mem->next]; |
92 |
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) { |
93 |
if(lfree == nmem) { |
if (lfree == nmem) { |
94 |
lfree = mem; |
lfree = mem; |
95 |
} |
} |
96 |
mem->next = nmem->next; |
mem->next = nmem->next; |
99 |
|
|
100 |
/* plug hole backward */ |
/* plug hole backward */ |
101 |
pmem = (struct mem *)&ram[mem->prev]; |
pmem = (struct mem *)&ram[mem->prev]; |
102 |
if(pmem != mem && pmem->used == 0) { |
if (pmem != mem && pmem->used == 0) { |
103 |
if(lfree == mem) { |
if (lfree == mem) { |
104 |
lfree = pmem; |
lfree = pmem; |
105 |
} |
} |
106 |
pmem->next = mem->next; |
pmem->next = mem->next; |
138 |
{ |
{ |
139 |
struct mem *mem; |
struct mem *mem; |
140 |
|
|
141 |
if(rmem == NULL) { |
if (rmem == NULL) { |
142 |
return; |
return; |
143 |
} |
} |
144 |
|
|
148 |
(u8_t *)rmem < (u8_t *)ram_end); |
(u8_t *)rmem < (u8_t *)ram_end); |
149 |
|
|
150 |
|
|
151 |
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) { |
152 |
DEBUGF(MEM_DEBUG | 3, ("mem_free: illegal memory\n")); |
DEBUGF(MEM_DEBUG | 3, ("mem_free: illegal memory\n")); |
153 |
#ifdef MEM_STATS |
#ifdef MEM_STATS |
154 |
++lwip_stats.mem.err; |
++lwip_stats.mem.err; |
162 |
|
|
163 |
mem->used = 0; |
mem->used = 0; |
164 |
|
|
165 |
if(mem < lfree) { |
if (mem < lfree) { |
166 |
lfree = mem; |
lfree = mem; |
167 |
} |
} |
168 |
|
|
179 |
{ |
{ |
180 |
void *nmem; |
void *nmem; |
181 |
nmem = mem_malloc(newsize); |
nmem = mem_malloc(newsize); |
182 |
if(nmem == NULL) { |
if (nmem == NULL) { |
183 |
return mem_realloc(rmem, newsize); |
return mem_realloc(rmem, newsize); |
184 |
} |
} |
185 |
memcpy(nmem, rmem, newsize); |
memcpy(nmem, rmem, newsize); |
196 |
|
|
197 |
/* Expand the size of the allocated memory region so that we can |
/* Expand the size of the allocated memory region so that we can |
198 |
adjust for alignment. */ |
adjust for alignment. */ |
199 |
if((newsize % MEM_ALIGNMENT) != 0) { |
if ((newsize % MEM_ALIGNMENT) != 0) { |
200 |
newsize += MEM_ALIGNMENT - ((newsize + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT); |
newsize += MEM_ALIGNMENT - ((newsize + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT); |
201 |
} |
} |
202 |
|
|
203 |
if(newsize > MEM_SIZE) { |
if (newsize > MEM_SIZE) { |
204 |
return NULL; |
return NULL; |
205 |
} |
} |
206 |
|
|
209 |
LWIP_ASSERT("mem_realloc: legal memory", (u8_t *)rmem >= (u8_t *)ram && |
LWIP_ASSERT("mem_realloc: legal memory", (u8_t *)rmem >= (u8_t *)ram && |
210 |
(u8_t *)rmem < (u8_t *)ram_end); |
(u8_t *)rmem < (u8_t *)ram_end); |
211 |
|
|
212 |
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) { |
213 |
DEBUGF(MEM_DEBUG | 3, ("mem_realloc: illegal memory\n")); |
DEBUGF(MEM_DEBUG | 3, ("mem_realloc: illegal memory\n")); |
214 |
return rmem; |
return rmem; |
215 |
} |
} |
222 |
lwip_stats.mem.used -= (size - newsize); |
lwip_stats.mem.used -= (size - newsize); |
223 |
#endif /* MEM_STATS */ |
#endif /* MEM_STATS */ |
224 |
|
|
225 |
if(newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size) { |
if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size) { |
226 |
ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize; |
ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize; |
227 |
mem2 = (struct mem *)&ram[ptr2]; |
mem2 = (struct mem *)&ram[ptr2]; |
228 |
mem2->used = 0; |
mem2->used = 0; |
229 |
mem2->next = mem->next; |
mem2->next = mem->next; |
230 |
mem2->prev = ptr; |
mem2->prev = ptr; |
231 |
mem->next = ptr2; |
mem->next = ptr2; |
232 |
if(mem2->next != MEM_SIZE) { |
if (mem2->next != MEM_SIZE) { |
233 |
((struct mem *)&ram[mem2->next])->prev = ptr2; |
((struct mem *)&ram[mem2->next])->prev = ptr2; |
234 |
} |
} |
235 |
|
|
245 |
mem_size_t ptr, ptr2; |
mem_size_t ptr, ptr2; |
246 |
struct mem *mem, *mem2; |
struct mem *mem, *mem2; |
247 |
|
|
248 |
if(size == 0) { |
if (size == 0) { |
249 |
return NULL; |
return NULL; |
250 |
} |
} |
251 |
|
|
252 |
/* Expand the size of the allocated memory region so that we can |
/* Expand the size of the allocated memory region so that we can |
253 |
adjust for alignment. */ |
adjust for alignment. */ |
254 |
if((size % MEM_ALIGNMENT) != 0) { |
if ((size % MEM_ALIGNMENT) != 0) { |
255 |
size += MEM_ALIGNMENT - ((size + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT); |
size += MEM_ALIGNMENT - ((size + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT); |
256 |
} |
} |
257 |
|
|
258 |
if(size > MEM_SIZE) { |
if (size > MEM_SIZE) { |
259 |
return NULL; |
return NULL; |
260 |
} |
} |
261 |
|
|
263 |
|
|
264 |
for(ptr = (u8_t *)lfree - ram; ptr < MEM_SIZE; ptr = ((struct mem *)&ram[ptr])->next) { |
for(ptr = (u8_t *)lfree - ram; ptr < MEM_SIZE; ptr = ((struct mem *)&ram[ptr])->next) { |
265 |
mem = (struct mem *)&ram[ptr]; |
mem = (struct mem *)&ram[ptr]; |
266 |
if(!mem->used && |
if (!mem->used && |
267 |
mem->next - (ptr + SIZEOF_STRUCT_MEM) >= size + SIZEOF_STRUCT_MEM) { |
mem->next - (ptr + SIZEOF_STRUCT_MEM) >= size + SIZEOF_STRUCT_MEM) { |
268 |
ptr2 = ptr + SIZEOF_STRUCT_MEM + size; |
ptr2 = ptr + SIZEOF_STRUCT_MEM + size; |
269 |
mem2 = (struct mem *)&ram[ptr2]; |
mem2 = (struct mem *)&ram[ptr2]; |
271 |
mem2->prev = ptr; |
mem2->prev = ptr; |
272 |
mem2->next = mem->next; |
mem2->next = mem->next; |
273 |
mem->next = ptr2; |
mem->next = ptr2; |
274 |
if(mem2->next != MEM_SIZE) { |
if (mem2->next != MEM_SIZE) { |
275 |
((struct mem *)&ram[mem2->next])->prev = ptr2; |
((struct mem *)&ram[mem2->next])->prev = ptr2; |
276 |
} |
} |
277 |
|
|
279 |
mem->used = 1; |
mem->used = 1; |
280 |
#ifdef MEM_STATS |
#ifdef MEM_STATS |
281 |
lwip_stats.mem.used += (size + SIZEOF_STRUCT_MEM); |
lwip_stats.mem.used += (size + SIZEOF_STRUCT_MEM); |
282 |
/* if(lwip_stats.mem.max < lwip_stats.mem.used) { |
/* if (lwip_stats.mem.max < lwip_stats.mem.used) { |
283 |
lwip_stats.mem.max = lwip_stats.mem.used; |
lwip_stats.mem.max = lwip_stats.mem.used; |
284 |
} */ |
} */ |
285 |
if(lwip_stats.mem.max < ptr2) { |
if (lwip_stats.mem.max < ptr2) { |
286 |
lwip_stats.mem.max = ptr2; |
lwip_stats.mem.max = ptr2; |
287 |
} |
} |
288 |
#endif /* MEM_STATS */ |
#endif /* MEM_STATS */ |
289 |
|
|
290 |
if(mem == lfree) { |
if (mem == lfree) { |
291 |
/* Find next free block after mem */ |
/* Find next free block after mem */ |
292 |
while(lfree->used && lfree != ram_end) { |
while (lfree->used && lfree != ram_end) { |
293 |
lfree = (struct mem *)&ram[lfree->next]; |
lfree = (struct mem *)&ram[lfree->next]; |
294 |
} |
} |
295 |
LWIP_ASSERT("mem_malloc: !lfree->used", !lfree->used); |
LWIP_ASSERT("mem_malloc: !lfree->used", !lfree->used); |