1/* Internal declarations for malloc, for use within libc.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <http://www.gnu.org/licenses/>. */
18
19#ifndef _MALLOC_INTERNAL_H
20#define _MALLOC_INTERNAL_H
21
22#include <malloc-machine.h>
23#include <malloc-sysdep.h>
24
25/* INTERNAL_SIZE_T is the word-size used for internal bookkeeping of
26 chunk sizes.
27
28 The default version is the same as size_t.
29
30 While not strictly necessary, it is best to define this as an
31 unsigned type, even if size_t is a signed type. This may avoid some
32 artificial size limitations on some systems.
33
34 On a 64-bit machine, you may be able to reduce malloc overhead by
35 defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the
36 expense of not being able to handle more than 2^32 of malloced
37 space. If this limitation is acceptable, you are encouraged to set
38 this unless you are on a platform requiring 16byte alignments. In
39 this case the alignment requirements turn out to negate any
40 potential advantages of decreasing size_t word size.
41
42 Implementors: Beware of the possible combinations of:
43 - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits,
44 and might be the same width as int or as long
45 - size_t might have different width and signedness as INTERNAL_SIZE_T
46 - int and long might be 32 or 64 bits, and might be the same width
47
48 To deal with this, most comparisons and difference computations
49 among INTERNAL_SIZE_Ts should cast them to unsigned long, being
50 aware of the fact that casting an unsigned int to a wider long does
51 not sign-extend. (This also makes checking for negative numbers
52 awkward.) Some of these casts result in harmless compiler warnings
53 on some systems. */
54#ifndef INTERNAL_SIZE_T
55# define INTERNAL_SIZE_T size_t
56#endif
57
58/* The corresponding word size. */
59#define SIZE_SZ (sizeof (INTERNAL_SIZE_T))
60
61/* MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks. It
62 must be a power of two at least 2 * SIZE_SZ, even on machines for
63 which smaller alignments would suffice. It may be defined as larger
64 than this though. Note however that code and data structures are
65 optimized for the case of 8-byte alignment. */
66#ifndef MALLOC_ALIGNMENT
67# define MALLOC_ALIGNMENT (2 * SIZE_SZ < __alignof__ (long double) \
68 ? __alignof__ (long double) : 2 * SIZE_SZ)
69#endif
70
71/* The corresponding bit mask value. */
72#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
73
74
75/* Called in the parent process before a fork. */
76void __malloc_fork_lock_parent (void) internal_function attribute_hidden;
77
78/* Called in the parent process after a fork. */
79void __malloc_fork_unlock_parent (void) internal_function attribute_hidden;
80
81/* Called in the child process after a fork. */
82void __malloc_fork_unlock_child (void) internal_function attribute_hidden;
83
84/* Set *RESULT to LEFT * RIGHT. Return true if the multiplication
85 overflowed. */
86static inline bool
87check_mul_overflow_size_t (size_t left, size_t right, size_t *result)
88{
89#if __GNUC__ >= 5
90 return __builtin_mul_overflow (left, right, result);
91#else
92 /* size_t is unsigned so the behavior on overflow is defined. */
93 *result = left * right;
94 size_t half_size_t = ((size_t) 1) << (8 * sizeof (size_t) / 2);
95 if (__glibc_unlikely ((left | right) >= half_size_t))
96 {
97 if (__glibc_unlikely (right != 0 && *result / right != left))
98 return true;
99 }
100 return false;
101#endif
102}
103
104#endif /* _MALLOC_INTERNAL_H */
105