| 308 | } |
| 309 | |
| 310 | void *realloc(void *ptr, size_t size) |
| 311 | { |
| 312 | void *ret, *pptr; |
| 313 | hdrtype_t volatile *block; |
| 314 | unsigned int osize; |
| 315 | struct memory_type *type = heap; |
| 316 | |
| 317 | if (ptr == NULL) |
| 318 | return alloc(size, type); |
| 319 | |
| 320 | pptr = ptr - HDRSIZE; |
| 321 | |
| 322 | if (!HAS_MAGIC(*((hdrtype_t *) pptr))) |
| 323 | return NULL; |
| 324 | |
| 325 | if (ptr < type->start || ptr >= type->end) |
| 326 | type = dma; |
| 327 | |
| 328 | /* Get the original size of the block. */ |
| 329 | osize = SIZE(*((hdrtype_t *) pptr)); |
| 330 | |
| 331 | /* |
| 332 | * Free the memory to update the tables - this won't touch the actual |
| 333 | * memory, so we can still use it for the copy after we have |
| 334 | * reallocated the new space. |
| 335 | */ |
| 336 | free(ptr); |
| 337 | |
| 338 | block = find_free_block(size, type); |
| 339 | if (block == NULL) |
| 340 | return NULL; |
| 341 | |
| 342 | ret = (void *)((uintptr_t)block + HDRSIZE); |
| 343 | |
| 344 | /* |
| 345 | * If ret == ptr, then no copy is needed. Otherwise, move the memory to |
| 346 | * the new location, which might be before the old one and overlap since |
| 347 | * the free() above includes a _consolidate(). |
| 348 | */ |
| 349 | if (ret != ptr) |
| 350 | memmove(ret, ptr, osize > size ? size : osize); |
| 351 | |
| 352 | /* Mark the block as used. */ |
| 353 | use_block(block, size); |
| 354 | |
| 355 | return ret; |
| 356 | } |
| 357 | |
| 358 | struct align_region_t |
| 359 | { |
no test coverage detected