Blame malloc/malloc-internal.h

Packit 6c4009
/* Internal declarations for malloc, for use within libc.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public License as
Packit 6c4009
   published by the Free Software Foundation; either version 2.1 of the
Packit 6c4009
   License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _MALLOC_INTERNAL_H
Packit 6c4009
#define _MALLOC_INTERNAL_H
Packit 6c4009
Packit 6c4009
#include <malloc-machine.h>
Packit 6c4009
#include <malloc-sysdep.h>
Packit 6c4009
Packit 6c4009
/* INTERNAL_SIZE_T is the word-size used for internal bookkeeping of
Packit 6c4009
   chunk sizes.
Packit 6c4009
Packit 6c4009
   The default version is the same as size_t.
Packit 6c4009
Packit 6c4009
   While not strictly necessary, it is best to define this as an
Packit 6c4009
   unsigned type, even if size_t is a signed type. This may avoid some
Packit 6c4009
   artificial size limitations on some systems.
Packit 6c4009
Packit 6c4009
   On a 64-bit machine, you may be able to reduce malloc overhead by
Packit 6c4009
   defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the
Packit 6c4009
   expense of not being able to handle more than 2^32 of malloced
Packit 6c4009
   space. If this limitation is acceptable, you are encouraged to set
Packit 6c4009
   this unless you are on a platform requiring 16byte alignments. In
Packit 6c4009
   this case the alignment requirements turn out to negate any
Packit 6c4009
   potential advantages of decreasing size_t word size.
Packit 6c4009
Packit 6c4009
   Implementors: Beware of the possible combinations of:
Packit 6c4009
     - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits,
Packit 6c4009
       and might be the same width as int or as long
Packit 6c4009
     - size_t might have different width and signedness as INTERNAL_SIZE_T
Packit 6c4009
     - int and long might be 32 or 64 bits, and might be the same width
Packit 6c4009
Packit 6c4009
   To deal with this, most comparisons and difference computations
Packit 6c4009
   among INTERNAL_SIZE_Ts should cast them to unsigned long, being
Packit 6c4009
   aware of the fact that casting an unsigned int to a wider long does
Packit 6c4009
   not sign-extend. (This also makes checking for negative numbers
Packit 6c4009
   awkward.) Some of these casts result in harmless compiler warnings
Packit 6c4009
   on some systems.  */
Packit 6c4009
#ifndef INTERNAL_SIZE_T
Packit 6c4009
# define INTERNAL_SIZE_T size_t
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* The corresponding word size.  */
Packit 6c4009
#define SIZE_SZ (sizeof (INTERNAL_SIZE_T))
Packit 6c4009
Packit 6c4009
/* The corresponding bit mask value.  */
Packit 6c4009
#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Called in the parent process before a fork.  */
Packit 6c4009
void __malloc_fork_lock_parent (void) attribute_hidden;
Packit 6c4009
Packit 6c4009
/* Called in the parent process after a fork.  */
Packit 6c4009
void __malloc_fork_unlock_parent (void) attribute_hidden;
Packit 6c4009
Packit 6c4009
/* Called in the child process after a fork.  */
Packit 6c4009
void __malloc_fork_unlock_child (void) attribute_hidden;
Packit 6c4009
Packit 6c4009
/* Called as part of the thread shutdown sequence.  */
Packit 6c4009
void __malloc_arena_thread_freeres (void) attribute_hidden;
Packit 6c4009
Packit 6c4009
/* Set *RESULT to LEFT * RIGHT.  Return true if the multiplication
Packit 6c4009
   overflowed.  */
Packit 6c4009
static inline bool
Packit 6c4009
check_mul_overflow_size_t (size_t left, size_t right, size_t *result)
Packit 6c4009
{
Packit 6c4009
#if __GNUC__ >= 5
Packit 6c4009
  return __builtin_mul_overflow (left, right, result);
Packit 6c4009
#else
Packit 6c4009
  /* size_t is unsigned so the behavior on overflow is defined.  */
Packit 6c4009
  *result = left * right;
Packit 6c4009
  size_t half_size_t = ((size_t) 1) << (8 * sizeof (size_t) / 2);
Packit 6c4009
  if (__glibc_unlikely ((left | right) >= half_size_t))
Packit 6c4009
    {
Packit 6c4009
      if (__glibc_unlikely (right != 0 && *result / right != left))
Packit 6c4009
        return true;
Packit 6c4009
    }
Packit 6c4009
  return false;
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif /* _MALLOC_INTERNAL_H */