[2/6] malloc: Inline _int_free

Message ID PAWPR08MB8982A5342B82EEC252060EC083AD2@PAWPR08MB8982.eurprd08.prod.outlook.com (mailing list archive)
State Accepted
Delegated to: Adhemerval Zanella Netto
Headers
Series [1/6] malloc: Move mmap code out of __libc_free hotpath |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed

Commit Message

Wilco Dijkstra March 31, 2025, 12:56 p.m. UTC
  Inline _int_free since it's a small function and only really used by __libc_free.

---
  

Comments

Adhemerval Zanella Netto April 14, 2025, 12:19 p.m. UTC | #1
On 31/03/25 09:56, Wilco Dijkstra wrote:
> 
> Inline _int_free since it's a small function and only really used by __libc_free.

LGTM, just a nit below.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> 
> ---
> 
> diff --git a/malloc/malloc-check.c b/malloc/malloc-check.c
> index 814a916ee504c3004191ffa3aa2ecb5dda0ddda0..c5265ecb91f1ae6eba9589f134158abba1126917 100644
> --- a/malloc/malloc-check.c
> +++ b/malloc/malloc-check.c
> @@ -235,7 +235,7 @@ free_check (void *mem)
>      {
>        /* Mark the chunk as belonging to the library again.  */
>        (void)tag_region (chunk2mem (p), memsize (p));
> -      _int_free (&main_arena, p, 1);
> +      _int_free_chunk (&main_arena, p, chunksize (p), 1);
>        __libc_lock_unlock (main_arena.mutex);
>      }
>    __set_errno (err);
> diff --git a/malloc/malloc.c b/malloc/malloc.c
> index fc2f751174d88d1029f04958ca1ebf37ca48bd87..5a1a65175cbe6715c67a8cc2e26bee64d78aac97 100644
> --- a/malloc/malloc.c
> +++ b/malloc/malloc.c
> @@ -1086,7 +1086,6 @@ typedef struct malloc_chunk* mchunkptr;
>  /* Internal routines.  */
>  
>  static void*  _int_malloc(mstate, size_t);
> -static void _int_free (mstate, mchunkptr, int);
>  static void _int_free_check (mstate, mchunkptr, INTERNAL_SIZE_T);
>  static void _int_free_chunk (mstate, mchunkptr, INTERNAL_SIZE_T, int);
>  static void _int_free_merge_chunk (mstate, mchunkptr, INTERNAL_SIZE_T);
> @@ -1273,7 +1272,6 @@ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
>  
>        sysmalloc: Returns untagged memory.
>        _int_malloc: Returns untagged memory.
> -      _int_free: Takes untagged memory.
>        _int_memalign: Returns untagged memory.
>        _int_memalign: Returns untagged memory.
>        _mid_memalign: Returns tagged memory.
> @@ -3163,7 +3161,7 @@ tcache_put (mchunkptr chunk, size_t tc_idx)
>  {
>    tcache_entry *e = (tcache_entry *) chunk2mem (chunk);
>  
> -  /* Mark this chunk as "in the tcache" so the test in _int_free will
> +  /* Mark this chunk as "in the tcache" so the test in __libc_free will
>       detect a double free.  */
>    e->key = tcache_key;
>  
> @@ -3451,9 +3449,6 @@ libc_hidden_def (__libc_malloc)
>  void
>  __libc_free (void *mem)
>  {
> -  mstate ar_ptr;
> -  mchunkptr p;                          /* chunk corresponding to mem */
> -
>    if (mem == NULL)                              /* free(0) has no effect */
>      return;
>  
> @@ -3462,15 +3457,23 @@ __libc_free (void *mem)
>    if (__glibc_unlikely (mtag_enabled))
>      *(volatile char *)mem;
>  
> -  p = mem2chunk (mem);
> +  mchunkptr p = mem2chunk (mem);
>  

I avoid such changes because it only clutters git history.

>    MAYBE_INIT_TCACHE ();
>  
>    /* Mark the chunk as belonging to the library again.  */
>    (void)tag_region (chunk2mem (p), memsize (p));
>  
> -  ar_ptr = arena_for_chunk (p);
> -  _int_free (ar_ptr, p, 0);
> +  INTERNAL_SIZE_T size = chunksize (p);
> +
> +  _int_free_check (arena_for_chunk (p), p, size);
> +
> +#if USE_TCACHE
> +  if (tcache_free (p, size))
> +    return;
> +#endif
> +
> +  _int_free_chunk (arena_for_chunk (p), p, size, 0);
>  }
>  libc_hidden_def (__libc_free)
>  
> @@ -4699,27 +4702,6 @@ _int_free_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T size, int have_lock)
>    }
>  }
>  
> -/* Free chunk P to its arena AV.  HAVE_LOCK indicates where the arena for
> -   P has already been locked.  It will perform sanity check, then try the
> -   fast path to free into tcache.  If the attempt not success, free the
> -   chunk to arena.  */
> -static __always_inline void
> -_int_free (mstate av, mchunkptr p, int have_lock)
> -{
> -  INTERNAL_SIZE_T size;        /* its size */
> -
> -  size = chunksize (p);
> -
> -  _int_free_check (av, p, size);
> -
> -#if USE_TCACHE
> -  if (tcache_free (p, size))
> -    return;
> -#endif
> -
> -  _int_free_chunk (av, p, size, have_lock);
> -}
> -
>  /* Try to merge chunk P of SIZE bytes with its neighbors.  Put the
>     resulting chunk on the appropriate bin list.  P must not be on a
>     bin list yet, and it can be in use.  */
>
  

Patch

diff --git a/malloc/malloc-check.c b/malloc/malloc-check.c
index 814a916ee504c3004191ffa3aa2ecb5dda0ddda0..c5265ecb91f1ae6eba9589f134158abba1126917 100644
--- a/malloc/malloc-check.c
+++ b/malloc/malloc-check.c
@@ -235,7 +235,7 @@  free_check (void *mem)
     {
       /* Mark the chunk as belonging to the library again.  */
       (void)tag_region (chunk2mem (p), memsize (p));
-      _int_free (&main_arena, p, 1);
+      _int_free_chunk (&main_arena, p, chunksize (p), 1);
       __libc_lock_unlock (main_arena.mutex);
     }
   __set_errno (err);
diff --git a/malloc/malloc.c b/malloc/malloc.c
index fc2f751174d88d1029f04958ca1ebf37ca48bd87..5a1a65175cbe6715c67a8cc2e26bee64d78aac97 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -1086,7 +1086,6 @@  typedef struct malloc_chunk* mchunkptr;
 /* Internal routines.  */
 
 static void*  _int_malloc(mstate, size_t);
-static void _int_free (mstate, mchunkptr, int);
 static void _int_free_check (mstate, mchunkptr, INTERNAL_SIZE_T);
 static void _int_free_chunk (mstate, mchunkptr, INTERNAL_SIZE_T, int);
 static void _int_free_merge_chunk (mstate, mchunkptr, INTERNAL_SIZE_T);
@@ -1273,7 +1272,6 @@  nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
       sysmalloc: Returns untagged memory.
       _int_malloc: Returns untagged memory.
-      _int_free: Takes untagged memory.
       _int_memalign: Returns untagged memory.
       _int_memalign: Returns untagged memory.
       _mid_memalign: Returns tagged memory.
@@ -3163,7 +3161,7 @@  tcache_put (mchunkptr chunk, size_t tc_idx)
 {
   tcache_entry *e = (tcache_entry *) chunk2mem (chunk);
 
-  /* Mark this chunk as "in the tcache" so the test in _int_free will
+  /* Mark this chunk as "in the tcache" so the test in __libc_free will
      detect a double free.  */
   e->key = tcache_key;
 
@@ -3451,9 +3449,6 @@  libc_hidden_def (__libc_malloc)
 void
 __libc_free (void *mem)
 {
-  mstate ar_ptr;
-  mchunkptr p;                          /* chunk corresponding to mem */
-
   if (mem == NULL)                              /* free(0) has no effect */
     return;
 
@@ -3462,15 +3457,23 @@  __libc_free (void *mem)
   if (__glibc_unlikely (mtag_enabled))
     *(volatile char *)mem;
 
-  p = mem2chunk (mem);
+  mchunkptr p = mem2chunk (mem);
 
   MAYBE_INIT_TCACHE ();
 
   /* Mark the chunk as belonging to the library again.  */
   (void)tag_region (chunk2mem (p), memsize (p));
 
-  ar_ptr = arena_for_chunk (p);
-  _int_free (ar_ptr, p, 0);
+  INTERNAL_SIZE_T size = chunksize (p);
+
+  _int_free_check (arena_for_chunk (p), p, size);
+
+#if USE_TCACHE
+  if (tcache_free (p, size))
+    return;
+#endif
+
+  _int_free_chunk (arena_for_chunk (p), p, size, 0);
 }
 libc_hidden_def (__libc_free)
 
@@ -4699,27 +4702,6 @@  _int_free_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T size, int have_lock)
   }
 }
 
-/* Free chunk P to its arena AV.  HAVE_LOCK indicates where the arena for
-   P has already been locked.  It will perform sanity check, then try the
-   fast path to free into tcache.  If the attempt not success, free the
-   chunk to arena.  */
-static __always_inline void
-_int_free (mstate av, mchunkptr p, int have_lock)
-{
-  INTERNAL_SIZE_T size;        /* its size */
-
-  size = chunksize (p);
-
-  _int_free_check (av, p, size);
-
-#if USE_TCACHE
-  if (tcache_free (p, size))
-    return;
-#endif
-
-  _int_free_chunk (av, p, size, have_lock);
-}
-
 /* Try to merge chunk P of SIZE bytes with its neighbors.  Put the
    resulting chunk on the appropriate bin list.  P must not be on a
    bin list yet, and it can be in use.  */