1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-08-08 17:42:12 +03:00

malloc: Cleanup libc_realloc

Minor cleanup of libc_realloc: remove unnecessary special cases for mmap, move
ar_ptr initialization, first check for oldmem == NULL.
This commit is contained in:
Wilco Dijkstra
2025-05-28 15:17:43 +00:00
parent 4b74591022
commit dea1e52af3

View File

@@ -3501,17 +3501,17 @@ __libc_realloc (void *oldmem, size_t bytes)
void *newp; /* chunk to return */ 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 REALLOC_ZERO_BYTES_FREES
if (bytes == 0 && oldmem != NULL) if (bytes == 0)
{ {
__libc_free (oldmem); return NULL; __libc_free (oldmem); return NULL;
} }
#endif #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 /* Perform a quick check to ensure that the pointer's tag matches the
memory's tag. */ memory's tag. */
if (__glibc_unlikely (mtag_enabled)) if (__glibc_unlikely (mtag_enabled))
@@ -3529,19 +3529,13 @@ __libc_realloc (void *oldmem, size_t bytes)
if (bytes <= usable) if (bytes <= usable)
{ {
size_t difference = usable - bytes; size_t difference = usable - bytes;
if ((unsigned long) difference < 2 * sizeof (INTERNAL_SIZE_T) if ((unsigned long) difference < 2 * sizeof (INTERNAL_SIZE_T))
|| (chunk_is_mmapped (oldp) && difference <= GLRO (dl_pagesize)))
return oldmem; return oldmem;
} }
/* its size */ /* its size */
const INTERNAL_SIZE_T oldsize = chunksize (oldp); 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 /* Little security check which won't hurt performance: the allocator
never wraps around at the end of the address space. Therefore never wraps around at the end of the address space. Therefore
we can exclude some size values which might appear here by we can exclude some size values which might appear here by
@@ -3574,9 +3568,9 @@ __libc_realloc (void *oldmem, size_t bytes)
return tag_new_usable (newmem); return tag_new_usable (newmem);
} }
#endif #endif
/* Note the extra SIZE_SZ overhead. */ /* Return if shrinking and mremap was unsuccessful. */
if (oldsize - SIZE_SZ >= nb) if (bytes <= usable)
return oldmem; /* do nothing */ return oldmem;
/* Must alloc, copy, free. */ /* Must alloc, copy, free. */
newmem = __libc_malloc (bytes); newmem = __libc_malloc (bytes);
@@ -3588,6 +3582,8 @@ __libc_realloc (void *oldmem, size_t bytes)
return newmem; return newmem;
} }
ar_ptr = arena_for_chunk (oldp);
if (SINGLE_THREAD_P) if (SINGLE_THREAD_P)
{ {
newp = _int_realloc (ar_ptr, oldp, oldsize, nb); newp = _int_realloc (ar_ptr, oldp, oldsize, nb);