Debian Codegraph @

memory/build/mozjemalloc_types.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 // Portions of this file were originally under the following license:
8 //
9 // Copyright (C) 2006-2008 Jason Evans <[email protected]>.
10 // All rights reserved.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions
14 // are met:
15 // 1. Redistributions of source code must retain the above copyright
16 // notice(s), this list of conditions and the following disclaimer as
17 // the first lines of this file unmodified other than the possible
18 // addition of one or more copyright notices.
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice(s), this list of conditions and the following disclaimer in
21 // the documentation and/or other materials provided with the
22 // distribution.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
28 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 // BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33 // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
34 // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 #ifndef _JEMALLOC_TYPES_H_
37 #define _JEMALLOC_TYPES_H_
38
39 #include <stdint.h>
40
41 // grab size_t
42 #ifdef _MSC_VER
43 # include <crtdefs.h>
44 #else
45 # include <stddef.h>
46 #endif
47 #include <stdbool.h>
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 #ifndef MALLOC_USABLE_SIZE_CONST_PTR
54 # define MALLOC_USABLE_SIZE_CONST_PTR const
55 #endif
56
57 typedef MALLOC_USABLE_SIZE_CONST_PTR void* usable_ptr_t;
58
59 typedef size_t arena_id_t;
60
61 #define ARENA_FLAG_RANDOMIZE_SMALL_MASK 0x3
62 #define ARENA_FLAG_RANDOMIZE_SMALL_DEFAULT 0
63 #define ARENA_FLAG_RANDOMIZE_SMALL_ENABLED 1
64 #define ARENA_FLAG_RANDOMIZE_SMALL_DISABLED 2
65
66 // Arenas are usually protected by a lock (ARENA_FLAG_THREAD_SAFE) however some
67 // arenas are accessed by only the main thread
68 // (ARENA_FLAG_THREAD_MAIN_THREAD_ONLY) and their locking can be skipped.
69 #define ARENA_FLAG_THREAD_MASK 0x4
70 #define ARENA_FLAG_THREAD_MAIN_THREAD_ONLY 0x4
71 #define ARENA_FLAG_THREAD_SAFE 0x0
72
73 typedef struct arena_params_s {
74 size_t mMaxDirty;
75 // Arena specific modifiers which override the value passed to
76 // moz_set_max_dirty_page_modifier. If value > 0 is passed to that function,
77 // and mMaxDirtyIncreaseOverride != 0, mMaxDirtyIncreaseOverride will be used
78 // instead, and similarly if value < 0 is passed and mMaxDirtyDecreaseOverride
79 // != 0, mMaxDirtyDecreaseOverride will be used as the modifier.
80 int32_t mMaxDirtyIncreaseOverride;
81 int32_t mMaxDirtyDecreaseOverride;
82
83 uint32_t mFlags;
84
85 #ifdef __cplusplus
86 arena_params_s()
87 : mMaxDirty(0),
88 mMaxDirtyIncreaseOverride(0),
89 mMaxDirtyDecreaseOverride(0),
90 mFlags(0) {}
91 #endif
92 } arena_params_t;
93
94 // jemalloc_stats() is not a stable interface. When using jemalloc_stats_t, be
95 // sure that the compiled results of mozjemalloc.cpp are in sync with this
96 // header file.
97 typedef struct {
98 // Run-time configuration settings.
99 bool opt_junk; // Fill allocated memory with kAllocJunk?
100 bool opt_randomize_small; // Randomization of small allocations?
101 bool opt_zero; // Fill allocated memory with 0x0?
102 size_t narenas; // Number of arenas.
103 size_t quantum; // Allocation quantum.
104 size_t quantum_max; // Max quantum-spaced allocation size.
105 size_t quantum_wide; // Allocation quantum (QuantuWide).
106 size_t quantum_wide_max; // Max quantum-wide-spaced allocation size.
107 size_t subpage_max; // Max subpage allocation size.
108 size_t large_max; // Max sub-chunksize allocation size.
109 size_t chunksize; // Size of each virtual memory mapping.
110 size_t page_size; // Size of pages.
111 size_t dirty_max; // Max dirty pages per arena.
112
113 // Current memory usage statistics.
114 size_t mapped; // Bytes mapped (not necessarily committed).
115 size_t allocated; // Bytes allocated (committed, in use by application).
116 size_t waste; // Bytes committed, not in use by the
117 // application, and not intentionally left
118 // unused (i.e., not dirty).
119 size_t pages_dirty; // Committed, unused pages kept around as a cache.
120 size_t pages_fresh; // Unused pages that have never been touched.
121 size_t pages_madvised; // Unsed pages we told the kernel we don't need.
122 size_t bookkeeping; // Committed bytes used internally by the
123 // allocator.
124 size_t bin_unused; // Bytes committed to a bin but currently unused.
125
126 size_t num_operations; // The number of malloc()+free() calls. Note that
127 // realloc calls
128 // count as 0, 1 or 2 operations depending on internal
129 // operations. Which internal operations (eg in place
130 // or move, or different size classes) require
131 // different internal operations is unspecified.
132 } jemalloc_stats_t;
133
134 typedef struct {
135 size_t size; // The size of objects in this bin, zero if this
136 // bin stats array entry is unused (no more bins).
137 size_t num_non_full_runs; // The number of non-full runs
138 size_t num_runs; // The number of runs in this bin
139 size_t bytes_unused; // The unallocated bytes across all these bins
140 size_t bytes_total; // The total storage area for runs in this bin,
141 size_t bytes_per_run; // The number of bytes per run, including headers.
142 size_t regions_per_run; // The number of regions (aka cells) per run.
143 } jemalloc_bin_stats_t;
144
145 // jemalloc_stats_lite() is not a stable interface. When using
146 // jemalloc_stats_lite_t, be sure that the compiled results of mozjemalloc.cpp
147 // are in sync with this header file.
148 typedef struct {
149 size_t allocated_bytes;
150
151 // The number of malloc()+free() calls. realloc calls count as 0, 1 or 2
152 // operations depending on whether they do nothing, resize in-place, or move
153 // the memory.
154 uint64_t num_operations;
155 } jemalloc_stats_lite_t;
156
157 enum PtrInfoTag {
158 // The pointer is not currently known to the allocator.
159 // 'addr', 'size', and 'arenaId' are always 0.
160 TagUnknown,
161
162 // The pointer is within a live allocation.
163 // 'addr', 'size', and 'arenaId' describe the allocation.
164 TagLiveAlloc,
165
166 // The pointer is within a small freed allocation.
167 // 'addr', 'size', and 'arenaId' describe the allocation.
168 TagFreedAlloc,
169
170 // The pointer is within a freed page. Details about the original
171 // allocation, including its size, are not available.
172 // 'addr', 'size', and 'arenaId' describe the page.
173 TagFreedPage,
174 };
175
176 // The information in jemalloc_ptr_info_t could be represented in a variety of
177 // ways. The chosen representation has the following properties.
178 // - The number of fields is minimized.
179 // - The 'tag' field unambiguously defines the meaning of the subsequent fields.
180 // Helper functions are used to group together related categories of tags.
181 typedef struct jemalloc_ptr_info_s {
182 enum PtrInfoTag tag;
183 void* addr; // meaning depends on tag; see above
184 size_t size; // meaning depends on tag; see above
185
186 #ifdef MOZ_DEBUG
187 arena_id_t arenaId; // meaning depends on tag; see above
188 #endif
189
190 #ifdef __cplusplus
191 jemalloc_ptr_info_s() = default;
192 jemalloc_ptr_info_s(enum PtrInfoTag aTag, void* aAddr, size_t aSize,
193 arena_id_t aArenaId)
194 : tag(aTag),
195 addr(aAddr),
196 size(aSize)
197 # ifdef MOZ_DEBUG
198 ,
199 arenaId(aArenaId)
200 # endif
201 {
202 }
203 #endif
204 } jemalloc_ptr_info_t;
205
206 static inline bool jemalloc_ptr_is_live(jemalloc_ptr_info_t* info) {
207 return info->tag == TagLiveAlloc;
208 }
209
210 static inline bool jemalloc_ptr_is_freed(jemalloc_ptr_info_t* info) {
211 return info->tag == TagFreedAlloc || info->tag == TagFreedPage;
212 }
213
214 static inline bool jemalloc_ptr_is_freed_page(jemalloc_ptr_info_t* info) {
215 return info->tag == TagFreedPage;
216 }
217
218 // The result of a purge step.
219 enum purge_result_t {
220 // Done: No more purge requests are pending.
221 Done,
222
223 // There may be one or more arenas whose reuse grace period expired and
224 // needs purging asap.
225 NeedsMore,
226
227 // There is at least one arena that waits either for its reuse grace to
228 // expire or for significant reuse to happen. As we cannot foresee the
229 // future, whatever schedules the purges should come back later to check
230 // if we need a purge.
231 WantsLater,
232 };
233
234 #ifdef __cplusplus
235 } // extern "C"
236 #endif
237
238 #endif // _JEMALLOC_TYPES_H_