Skip to content

Commit 3f0dc64

Browse files
committed
Allow setting extent hooks on uninitialized auto arenas.
Setting extent hooks can result in initializing an unused auto arena. This is useful to install extent hooks on auto arenas from the beginning.
1 parent 0258542 commit 3f0dc64

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

src/ctl.c

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,20 +2251,41 @@ arena_i_extent_hooks_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,
22512251

22522252
malloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);
22532253
MIB_UNSIGNED(arena_ind, 1);
2254-
if (arena_ind < narenas_total_get() && (arena =
2255-
arena_get(tsd_tsdn(tsd), arena_ind, false)) != NULL) {
2256-
if (newp != NULL) {
2257-
extent_hooks_t *old_extent_hooks;
2258-
extent_hooks_t *new_extent_hooks
2259-
JEMALLOC_CC_SILENCE_INIT(NULL);
2260-
WRITE(new_extent_hooks, extent_hooks_t *);
2261-
old_extent_hooks = extent_hooks_set(tsd, arena,
2262-
new_extent_hooks);
2254+
if (arena_ind < narenas_total_get()) {
2255+
extent_hooks_t *old_extent_hooks;
2256+
arena = arena_get(tsd_tsdn(tsd), arena_ind, false);
2257+
if (arena == NULL) {
2258+
if (arena_ind >= narenas_auto) {
2259+
ret = EFAULT;
2260+
goto label_return;
2261+
}
2262+
old_extent_hooks =
2263+
(extent_hooks_t *)&extent_hooks_default;
22632264
READ(old_extent_hooks, extent_hooks_t *);
2265+
if (newp != NULL) {
2266+
/* Initialize a new arena as a side effect. */
2267+
extent_hooks_t *new_extent_hooks
2268+
JEMALLOC_CC_SILENCE_INIT(NULL);
2269+
WRITE(new_extent_hooks, extent_hooks_t *);
2270+
arena = arena_init(tsd_tsdn(tsd), arena_ind,
2271+
new_extent_hooks);
2272+
if (arena == NULL) {
2273+
ret = EFAULT;
2274+
goto label_return;
2275+
}
2276+
}
22642277
} else {
2265-
extent_hooks_t *old_extent_hooks =
2266-
extent_hooks_get(arena);
2267-
READ(old_extent_hooks, extent_hooks_t *);
2278+
if (newp != NULL) {
2279+
extent_hooks_t *new_extent_hooks
2280+
JEMALLOC_CC_SILENCE_INIT(NULL);
2281+
WRITE(new_extent_hooks, extent_hooks_t *);
2282+
old_extent_hooks = extent_hooks_set(tsd, arena,
2283+
new_extent_hooks);
2284+
READ(old_extent_hooks, extent_hooks_t *);
2285+
} else {
2286+
old_extent_hooks = extent_hooks_get(arena);
2287+
READ(old_extent_hooks, extent_hooks_t *);
2288+
}
22682289
}
22692290
} else {
22702291
ret = EFAULT;

test/integration/extent.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,43 @@ test_extent_body(unsigned arena_ind) {
9898
dallocx(p, flags);
9999
}
100100

101+
static void
102+
test_manual_hook_auto_arena(void) {
103+
unsigned narenas;
104+
size_t old_size, new_size, sz;
105+
size_t hooks_mib[3];
106+
size_t hooks_miblen;
107+
extent_hooks_t *new_hooks, *old_hooks;
108+
109+
extent_hooks_prep();
110+
111+
sz = sizeof(unsigned);
112+
/* Get number of auto arenas. */
113+
assert_d_eq(mallctl("opt.narenas", (void *)&narenas, &sz, NULL, 0),
114+
0, "Unexpected mallctl() failure");
115+
if (narenas == 1) {
116+
return;
117+
}
118+
119+
/* Install custom extent hooks on arena 1 (might not be initialized). */
120+
hooks_miblen = sizeof(hooks_mib)/sizeof(size_t);
121+
assert_d_eq(mallctlnametomib("arena.0.extent_hooks", hooks_mib,
122+
&hooks_miblen), 0, "Unexpected mallctlnametomib() failure");
123+
hooks_mib[1] = 1;
124+
old_size = sizeof(extent_hooks_t *);
125+
new_hooks = &hooks;
126+
new_size = sizeof(extent_hooks_t *);
127+
assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,
128+
&old_size, (void *)&new_hooks, new_size), 0,
129+
"Unexpected extent_hooks error");
130+
static bool auto_arena_created = false;
131+
if (old_hooks != &hooks) {
132+
assert_b_eq(auto_arena_created, false,
133+
"Expected auto arena 1 created only once.");
134+
auto_arena_created = true;
135+
}
136+
}
137+
101138
static void
102139
test_manual_hook_body(void) {
103140
unsigned arena_ind;
@@ -169,6 +206,7 @@ test_manual_hook_body(void) {
169206
}
170207

171208
TEST_BEGIN(test_extent_manual_hook) {
209+
test_manual_hook_auto_arena();
172210
test_manual_hook_body();
173211

174212
/* Test failure paths. */

0 commit comments

Comments
 (0)