Skip to content

Commit a32b7bd

Browse files
Latchesar Ionkovinterwq
authored andcommitted
Mallctl: Add arenas.lookup
Implement a new mallctl operation that allows looking up the arena a region of memory belongs to.
1 parent 6df9060 commit a32b7bd

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

doc/jemalloc.xml.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,15 @@ struct extent_hooks_s {
21302130
and return the new arena index.</para></listitem>
21312131
</varlistentry>
21322132

2133+
<varlistentry id="arenas.lookup">
2134+
<term>
2135+
<mallctl>arenas.lookup</mallctl>
2136+
(<type>unsigned</type>, <type>void*</type>)
2137+
<literal>rw</literal>
2138+
</term>
2139+
<listitem><para>Index of the arena to which an allocation belongs to.</para></listitem>
2140+
</varlistentry>
2141+
21332142
<varlistentry id="prof.thread_active_init">
21342143
<term>
21352144
<mallctl>prof.thread_active_init</mallctl>

src/ctl.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ CTL_PROTO(arenas_nbins)
139139
CTL_PROTO(arenas_nhbins)
140140
CTL_PROTO(arenas_nlextents)
141141
CTL_PROTO(arenas_create)
142+
CTL_PROTO(arenas_lookup)
142143
CTL_PROTO(prof_thread_active_init)
143144
CTL_PROTO(prof_active)
144145
CTL_PROTO(prof_dump)
@@ -373,7 +374,8 @@ static const ctl_named_node_t arenas_node[] = {
373374
{NAME("bin"), CHILD(indexed, arenas_bin)},
374375
{NAME("nlextents"), CTL(arenas_nlextents)},
375376
{NAME("lextent"), CHILD(indexed, arenas_lextent)},
376-
{NAME("create"), CTL(arenas_create)}
377+
{NAME("create"), CTL(arenas_create)},
378+
{NAME("lookup"), CTL(arenas_lookup)}
377379
};
378380

379381
static const ctl_named_node_t prof_node[] = {
@@ -2471,6 +2473,36 @@ arenas_create_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,
24712473
return ret;
24722474
}
24732475

2476+
static int
2477+
arenas_lookup_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,
2478+
size_t *oldlenp, void *newp, size_t newlen) {
2479+
int ret;
2480+
unsigned arena_ind;
2481+
void *ptr;
2482+
extent_t *extent;
2483+
arena_t *arena;
2484+
2485+
ptr = NULL;
2486+
ret = EINVAL;
2487+
malloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);
2488+
WRITE(ptr, void *);
2489+
extent = iealloc(tsd_tsdn(tsd), ptr);
2490+
if (extent == NULL)
2491+
goto label_return;
2492+
2493+
arena = extent_arena_get(extent);
2494+
if (arena == NULL)
2495+
goto label_return;
2496+
2497+
arena_ind = arena_ind_get(arena);
2498+
READ(arena_ind, unsigned);
2499+
2500+
ret = 0;
2501+
label_return:
2502+
malloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);
2503+
return ret;
2504+
}
2505+
24742506
/******************************************************************************/
24752507

24762508
static int

test/unit/mallctl.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,22 @@ TEST_BEGIN(test_arenas_create) {
738738
}
739739
TEST_END
740740

741+
TEST_BEGIN(test_arenas_lookup) {
742+
unsigned arena, arena1;
743+
void *ptr;
744+
size_t sz = sizeof(unsigned);
745+
746+
assert_d_eq(mallctl("arenas.create", (void *)&arena, &sz, NULL, 0), 0,
747+
"Unexpected mallctl() failure");
748+
ptr = mallocx(42, MALLOCX_ARENA(arena) | MALLOCX_TCACHE_NONE);
749+
assert_ptr_not_null(ptr, "Unexpected mallocx() failure");
750+
assert_d_eq(mallctl("arenas.lookup", &arena1, &sz, &ptr, sizeof(ptr)),
751+
0, "Unexpected mallctl() failure");
752+
assert_u_eq(arena, arena1, "Unexpected arena index");
753+
dallocx(ptr, 0);
754+
}
755+
TEST_END
756+
741757
TEST_BEGIN(test_stats_arenas) {
742758
#define TEST_STATS_ARENAS(t, name) do { \
743759
t name; \
@@ -784,5 +800,6 @@ main(void) {
784800
test_arenas_bin_constants,
785801
test_arenas_lextent_constants,
786802
test_arenas_create,
803+
test_arenas_lookup,
787804
test_stats_arenas);
788805
}

0 commit comments

Comments
 (0)