xref: /freebsd/contrib/jemalloc/include/jemalloc/internal/jemalloc_internal_types.h (revision c43cad87172039ccf38172129c79755ea79e6102)
1 #ifndef JEMALLOC_INTERNAL_TYPES_H
2 #define JEMALLOC_INTERNAL_TYPES_H
3 
4 #include "jemalloc/internal/quantum.h"
5 
6 /* Processor / core id type. */
7 typedef int malloc_cpuid_t;
8 
9 /* When realloc(non-null-ptr, 0) is called, what happens? */
10 enum zero_realloc_action_e {
11 	/* Realloc(ptr, 0) is free(ptr); return malloc(0); */
12 	zero_realloc_action_alloc = 0,
13 	/* Realloc(ptr, 0) is free(ptr); */
14 	zero_realloc_action_free = 1,
15 	/* Realloc(ptr, 0) aborts. */
16 	zero_realloc_action_abort = 2
17 };
18 typedef enum zero_realloc_action_e zero_realloc_action_t;
19 
20 /* Signature of write callback. */
21 typedef void (write_cb_t)(void *, const char *);
22 
23 enum malloc_init_e {
24 	malloc_init_uninitialized	= 3,
25 	malloc_init_a0_initialized	= 2,
26 	malloc_init_recursible		= 1,
27 	malloc_init_initialized		= 0 /* Common case --> jnz. */
28 };
29 typedef enum malloc_init_e malloc_init_t;
30 
31 /*
32  * Flags bits:
33  *
34  * a: arena
35  * t: tcache
36  * 0: unused
37  * z: zero
38  * n: alignment
39  *
40  * aaaaaaaa aaaatttt tttttttt 0znnnnnn
41  */
42 #define MALLOCX_ARENA_BITS	12
43 #define MALLOCX_TCACHE_BITS	12
44 #define MALLOCX_LG_ALIGN_BITS	6
45 #define MALLOCX_ARENA_SHIFT	20
46 #define MALLOCX_TCACHE_SHIFT	8
47 #define MALLOCX_ARENA_MASK \
48     (((1 << MALLOCX_ARENA_BITS) - 1) << MALLOCX_ARENA_SHIFT)
49 /* NB: Arena index bias decreases the maximum number of arenas by 1. */
50 #define MALLOCX_ARENA_LIMIT	((1 << MALLOCX_ARENA_BITS) - 1)
51 #define MALLOCX_TCACHE_MASK \
52     (((1 << MALLOCX_TCACHE_BITS) - 1) << MALLOCX_TCACHE_SHIFT)
53 #define MALLOCX_TCACHE_MAX	((1 << MALLOCX_TCACHE_BITS) - 3)
54 #define MALLOCX_LG_ALIGN_MASK	((1 << MALLOCX_LG_ALIGN_BITS) - 1)
55 /* Use MALLOCX_ALIGN_GET() if alignment may not be specified in flags. */
56 #define MALLOCX_ALIGN_GET_SPECIFIED(flags)				\
57     (ZU(1) << (flags & MALLOCX_LG_ALIGN_MASK))
58 #define MALLOCX_ALIGN_GET(flags)					\
59     (MALLOCX_ALIGN_GET_SPECIFIED(flags) & (SIZE_T_MAX-1))
60 #define MALLOCX_ZERO_GET(flags)						\
61     ((bool)(flags & MALLOCX_ZERO))
62 
63 #define MALLOCX_TCACHE_GET(flags)					\
64     (((unsigned)((flags & MALLOCX_TCACHE_MASK) >> MALLOCX_TCACHE_SHIFT)) - 2)
65 #define MALLOCX_ARENA_GET(flags)					\
66     (((unsigned)(((unsigned)flags) >> MALLOCX_ARENA_SHIFT)) - 1)
67 
68 /* Smallest size class to support. */
69 #define TINY_MIN		(1U << LG_TINY_MIN)
70 
71 #define LONG			((size_t)(1U << LG_SIZEOF_LONG))
72 #define LONG_MASK		(LONG - 1)
73 
74 /* Return the smallest long multiple that is >= a. */
75 #define LONG_CEILING(a)							\
76 	(((a) + LONG_MASK) & ~LONG_MASK)
77 
78 #define SIZEOF_PTR		(1U << LG_SIZEOF_PTR)
79 #define PTR_MASK		(SIZEOF_PTR - 1)
80 
81 /* Return the smallest (void *) multiple that is >= a. */
82 #define PTR_CEILING(a)							\
83 	(((a) + PTR_MASK) & ~PTR_MASK)
84 
85 /*
86  * Maximum size of L1 cache line.  This is used to avoid cache line aliasing.
87  * In addition, this controls the spacing of cacheline-spaced size classes.
88  *
89  * CACHELINE cannot be based on LG_CACHELINE because __declspec(align()) can
90  * only handle raw constants.
91  */
92 #define LG_CACHELINE		6
93 #define CACHELINE		64
94 #define CACHELINE_MASK		(CACHELINE - 1)
95 
96 /* Return the smallest cacheline multiple that is >= s. */
97 #define CACHELINE_CEILING(s)						\
98 	(((s) + CACHELINE_MASK) & ~CACHELINE_MASK)
99 
100 /* Return the nearest aligned address at or below a. */
101 #define ALIGNMENT_ADDR2BASE(a, alignment)				\
102 	((void *)((uintptr_t)(a) & ((~(alignment)) + 1)))
103 
104 /* Return the offset between a and the nearest aligned address at or below a. */
105 #define ALIGNMENT_ADDR2OFFSET(a, alignment)				\
106 	((size_t)((uintptr_t)(a) & (alignment - 1)))
107 
108 /* Return the smallest alignment multiple that is >= s. */
109 #define ALIGNMENT_CEILING(s, alignment)					\
110 	(((s) + (alignment - 1)) & ((~(alignment)) + 1))
111 
112 /* Declare a variable-length array. */
113 #if __STDC_VERSION__ < 199901L
114 #  ifdef _MSC_VER
115 #    include <malloc.h>
116 #    define alloca _alloca
117 #  else
118 #    ifdef JEMALLOC_HAS_ALLOCA_H
119 #      include <alloca.h>
120 #    else
121 #      include <stdlib.h>
122 #    endif
123 #  endif
124 #  define VARIABLE_ARRAY(type, name, count) \
125 	type *name = alloca(sizeof(type) * (count))
126 #else
127 #  define VARIABLE_ARRAY(type, name, count) type name[(count)]
128 #endif
129 
130 #endif /* JEMALLOC_INTERNAL_TYPES_H */
131