Blame malloc/malloc-internal.h

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