diff options
| author | Wilco Dijkstra <wilco.dijkstra@arm.com> | 2025-09-10 09:11:27 +0000 |
|---|---|---|
| committer | Wilco Dijkstra <wilco.dijkstra@arm.com> | 2025-09-10 09:18:06 +0000 |
| commit | 19442c052c40f2088e265a11daf2e3669f32ddbd (patch) | |
| tree | 76a3c82f271fdbd866ccf6d770014857864ba83d | |
| parent | 210ee295033c8fb068529f8f408078c418ceb46c (diff) | |
malloc: Cleanup libc_realloc
Minor cleanup of libc_realloc: remove unnecessary special cases for mmap, move
ar_ptr initialization, first check for oldmem == NULL.
Reviewed-by: DJ Delorie <dj@redhat.com>
| -rw-r--r-- | malloc/malloc.c | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/malloc/malloc.c b/malloc/malloc.c index ac90688126..802318d143 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3559,17 +3559,17 @@ __libc_realloc (void *oldmem, size_t bytes) void *newp; /* chunk to return */ + /* realloc of null is supposed to be same as malloc */ + if (oldmem == NULL) + return __libc_malloc (bytes); + #if REALLOC_ZERO_BYTES_FREES - if (bytes == 0 && oldmem != NULL) + if (bytes == 0) { __libc_free (oldmem); return NULL; } #endif - /* realloc of null is supposed to be same as malloc */ - if (oldmem == NULL) - return __libc_malloc (bytes); - /* Perform a quick check to ensure that the pointer's tag matches the memory's tag. */ if (__glibc_unlikely (mtag_enabled)) @@ -3587,19 +3587,13 @@ __libc_realloc (void *oldmem, size_t bytes) if (bytes <= usable) { size_t difference = usable - bytes; - if ((unsigned long) difference < 2 * sizeof (INTERNAL_SIZE_T) - || (chunk_is_mmapped (oldp) && difference <= GLRO (dl_pagesize))) + if ((unsigned long) difference < 2 * sizeof (INTERNAL_SIZE_T)) return oldmem; } /* its size */ const INTERNAL_SIZE_T oldsize = chunksize (oldp); - if (chunk_is_mmapped (oldp)) - ar_ptr = NULL; - else - ar_ptr = arena_for_chunk (oldp); - /* Little security check which won't hurt performance: the allocator never wraps around at the end of the address space. Therefore we can exclude some size values which might appear here by @@ -3632,9 +3626,9 @@ __libc_realloc (void *oldmem, size_t bytes) return tag_new_usable (newmem); } #endif - /* Note the extra SIZE_SZ overhead. */ - if (oldsize - SIZE_SZ >= nb) - return oldmem; /* do nothing */ + /* Return if shrinking and mremap was unsuccessful. */ + if (bytes <= usable) + return oldmem; /* Must alloc, copy, free. */ newmem = __libc_malloc (bytes); @@ -3646,6 +3640,8 @@ __libc_realloc (void *oldmem, size_t bytes) return newmem; } + ar_ptr = arena_for_chunk (oldp); + if (SINGLE_THREAD_P) { newp = _int_realloc (ar_ptr, oldp, oldsize, nb); |
