Reformat the codebase with the clang-format 18.

This commit is contained in:
guangli-dai 2025-06-13 12:31:12 -07:00 committed by Guangli Dai
parent 0a6215c171
commit f1bba4a87c
346 changed files with 18286 additions and 17770 deletions

View file

@ -6,27 +6,27 @@ void *
thd_start(void *arg) {
unsigned thread_ind = (unsigned)(uintptr_t)arg;
unsigned arena_ind;
void *p;
size_t sz;
void *p;
size_t sz;
sz = sizeof(arena_ind);
expect_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
0, "Error in arenas.create");
if (thread_ind % 4 != 3) {
size_t mib[3];
size_t miblen = sizeof(mib) / sizeof(size_t);
size_t mib[3];
size_t miblen = sizeof(mib) / sizeof(size_t);
const char *dss_precs[] = {"disabled", "primary", "secondary"};
unsigned prec_ind = thread_ind %
(sizeof(dss_precs)/sizeof(char*));
unsigned prec_ind = thread_ind
% (sizeof(dss_precs) / sizeof(char *));
const char *dss = dss_precs[prec_ind];
int expected_err = (have_dss || prec_ind == 0) ? 0 : EFAULT;
expect_d_eq(mallctlnametomib("arena.0.dss", mib, &miblen), 0,
"Error in mallctlnametomib()");
mib[1] = arena_ind;
expect_d_eq(mallctlbymib(mib, miblen, NULL, NULL, (void *)&dss,
sizeof(const char *)), expected_err,
"Error in mallctlbymib()");
sizeof(const char *)),
expected_err, "Error in mallctlbymib()");
}
p = mallocx(1, MALLOCX_ARENA(arena_ind));
@ -37,12 +37,11 @@ thd_start(void *arg) {
}
TEST_BEGIN(test_MALLOCX_ARENA) {
thd_t thds[NTHREADS];
thd_t thds[NTHREADS];
unsigned i;
for (i = 0; i < NTHREADS; i++) {
thd_create(&thds[i], thd_start,
(void *)(uintptr_t)i);
thd_create(&thds[i], thd_start, (void *)(uintptr_t)i);
}
for (i = 0; i < NTHREADS; i++) {
@ -53,6 +52,5 @@ TEST_END
int
main(void) {
return test(
test_MALLOCX_ARENA);
return test(test_MALLOCX_ARENA);
}

View file

@ -15,7 +15,7 @@ purge(void) {
TEST_BEGIN(test_alignment_errors) {
size_t alignment;
void *p;
void *p;
alignment = 0;
set_errno(0);
@ -24,17 +24,15 @@ TEST_BEGIN(test_alignment_errors) {
"Expected error for invalid alignment %zu", alignment);
for (alignment = sizeof(size_t); alignment < MAXALIGN;
alignment <<= 1) {
alignment <<= 1) {
set_errno(0);
p = aligned_alloc(alignment + 1, 1);
expect_false(p != NULL || get_errno() != EINVAL,
"Expected error for invalid alignment %zu",
alignment + 1);
"Expected error for invalid alignment %zu", alignment + 1);
}
}
TEST_END
/*
* GCC "-Walloc-size-larger-than" warning detects when one of the memory
* allocation functions is called with a size larger than the maximum size that
@ -47,33 +45,31 @@ JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
TEST_BEGIN(test_oom_errors) {
size_t alignment, size;
void *p;
void *p;
#if LG_SIZEOF_PTR == 3
alignment = UINT64_C(0x8000000000000000);
size = UINT64_C(0x8000000000000000);
size = UINT64_C(0x8000000000000000);
#else
alignment = 0x80000000LU;
size = 0x80000000LU;
size = 0x80000000LU;
#endif
set_errno(0);
p = aligned_alloc(alignment, size);
expect_false(p != NULL || get_errno() != ENOMEM,
"Expected error for aligned_alloc(%zu, %zu)",
alignment, size);
"Expected error for aligned_alloc(%zu, %zu)", alignment, size);
#if LG_SIZEOF_PTR == 3
alignment = UINT64_C(0x4000000000000000);
size = UINT64_C(0xc000000000000001);
size = UINT64_C(0xc000000000000001);
#else
alignment = 0x40000000LU;
size = 0xc0000001LU;
size = 0xc0000001LU;
#endif
set_errno(0);
p = aligned_alloc(alignment, size);
expect_false(p != NULL || get_errno() != ENOMEM,
"Expected error for aligned_alloc(%zu, %zu)",
alignment, size);
"Expected error for aligned_alloc(%zu, %zu)", alignment, size);
alignment = 0x10LU;
#if LG_SIZEOF_PTR == 3
@ -84,8 +80,7 @@ TEST_BEGIN(test_oom_errors) {
set_errno(0);
p = aligned_alloc(alignment, size);
expect_false(p != NULL || get_errno() != ENOMEM,
"Expected error for aligned_alloc(&p, %zu, %zu)",
alignment, size);
"Expected error for aligned_alloc(&p, %zu, %zu)", alignment, size);
}
TEST_END
@ -94,21 +89,18 @@ JEMALLOC_DIAGNOSTIC_POP
TEST_BEGIN(test_alignment_and_size) {
#define NITER 4
size_t alignment, size, total;
size_t alignment, size, total;
unsigned i;
void *ps[NITER];
void *ps[NITER];
for (i = 0; i < NITER; i++) {
ps[i] = NULL;
}
for (alignment = 8;
alignment <= MAXALIGN;
alignment <<= 1) {
for (alignment = 8; alignment <= MAXALIGN; alignment <<= 1) {
total = 0;
for (size = 1;
size < 3 * alignment && size < (1U << 31);
size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
for (size = 1; size < 3 * alignment && size < (1U << 31);
size += (alignment >> (LG_SIZEOF_PTR - 1)) - 1) {
for (i = 0; i < NITER; i++) {
ps[i] = aligned_alloc(alignment, size);
if (ps[i] == NULL) {
@ -149,9 +141,6 @@ TEST_END
int
main(void) {
return test(
test_alignment_errors,
test_oom_errors,
test_alignment_and_size,
test_zero_alloc);
return test(test_alignment_errors, test_oom_errors,
test_alignment_and_size, test_zero_alloc);
}

View file

@ -2,27 +2,27 @@
void *
thd_start(void *arg) {
int err;
void *p;
uint64_t a0, a1, d0, d1;
int err;
void *p;
uint64_t a0, a1, d0, d1;
uint64_t *ap0, *ap1, *dp0, *dp1;
size_t sz, usize;
size_t sz, usize;
sz = sizeof(a0);
if ((err = mallctl("thread.allocated", (void *)&a0, &sz, NULL, 0))) {
if (err == ENOENT) {
goto label_ENOENT;
}
test_fail("%s(): Error in mallctl(): %s", __func__,
strerror(err));
test_fail(
"%s(): Error in mallctl(): %s", __func__, strerror(err));
}
sz = sizeof(ap0);
if ((err = mallctl("thread.allocatedp", (void *)&ap0, &sz, NULL, 0))) {
if (err == ENOENT) {
goto label_ENOENT;
}
test_fail("%s(): Error in mallctl(): %s", __func__,
strerror(err));
test_fail(
"%s(): Error in mallctl(): %s", __func__, strerror(err));
}
expect_u64_eq(*ap0, a0,
"\"thread.allocatedp\" should provide a pointer to internal "
@ -33,17 +33,17 @@ thd_start(void *arg) {
if (err == ENOENT) {
goto label_ENOENT;
}
test_fail("%s(): Error in mallctl(): %s", __func__,
strerror(err));
test_fail(
"%s(): Error in mallctl(): %s", __func__, strerror(err));
}
sz = sizeof(dp0);
if ((err = mallctl("thread.deallocatedp", (void *)&dp0, &sz, NULL,
0))) {
if ((err = mallctl(
"thread.deallocatedp", (void *)&dp0, &sz, NULL, 0))) {
if (err == ENOENT) {
goto label_ENOENT;
}
test_fail("%s(): Error in mallctl(): %s", __func__,
strerror(err));
test_fail(
"%s(): Error in mallctl(): %s", __func__, strerror(err));
}
expect_u64_eq(*dp0, d0,
"\"thread.deallocatedp\" should provide a pointer to internal "
@ -107,10 +107,6 @@ TEST_END
int
main(void) {
/* Run tests multiple times to check for bad interactions. */
return test(
test_main_thread,
test_subthread,
test_main_thread,
test_subthread,
test_main_thread);
return test(test_main_thread, test_subthread, test_main_thread,
test_subthread, test_main_thread);
}

View file

@ -19,6 +19,5 @@ TEST_END
int
main() {
return test(
test_basic);
return test(test_basic);
}

View file

@ -17,7 +17,5 @@ TEST_END
int
main(void) {
return test(
test_failing_alloc);
return test(test_failing_alloc);
}

View file

@ -8,7 +8,8 @@
*/
typedef void (*abort_hook_t)(const char *message);
bool fake_abort_called;
void fake_abort(const char *message) {
void
fake_abort(const char *message) {
const char *expected_start = "<jemalloc>: Allocation of size";
if (strncmp(message, expected_start, strlen(expected_start)) != 0) {
abort();
@ -19,7 +20,7 @@ void fake_abort(const char *message) {
static bool
own_operator_new(void) {
uint64_t before, after;
size_t sz = sizeof(before);
size_t sz = sizeof(before);
/* thread.allocated is always available, even w/o config_stats. */
expect_d_eq(mallctl("thread.allocated", (void *)&before, &sz, NULL, 0),
@ -35,8 +36,8 @@ own_operator_new(void) {
TEST_BEGIN(test_failing_alloc) {
abort_hook_t abort_hook = &fake_abort;
expect_d_eq(mallctl("experimental.hooks.safety_check_abort", NULL, NULL,
(void *)&abort_hook, sizeof(abort_hook)), 0,
"Unexpected mallctl failure setting abort hook");
(void *)&abort_hook, sizeof(abort_hook)),
0, "Unexpected mallctl failure setting abort hook");
/*
* Not owning operator new is only expected to happen on MinGW which
@ -61,6 +62,5 @@ TEST_END
int
main(void) {
return test(
test_failing_alloc);
return test(test_failing_alloc);
}

View file

@ -6,26 +6,29 @@
static void
test_extent_body(unsigned arena_ind) {
void *p;
void *p;
size_t large0, large1, large2, sz;
size_t purge_mib[3];
size_t purge_miblen;
int flags;
bool xallocx_success_a, xallocx_success_b, xallocx_success_c;
int flags;
bool xallocx_success_a, xallocx_success_b, xallocx_success_c;
flags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE;
/* Get large size classes. */
sz = sizeof(size_t);
expect_d_eq(mallctl("arenas.lextent.0.size", (void *)&large0, &sz, NULL,
0), 0, "Unexpected arenas.lextent.0.size failure");
expect_d_eq(mallctl("arenas.lextent.1.size", (void *)&large1, &sz, NULL,
0), 0, "Unexpected arenas.lextent.1.size failure");
expect_d_eq(mallctl("arenas.lextent.2.size", (void *)&large2, &sz, NULL,
0), 0, "Unexpected arenas.lextent.2.size failure");
expect_d_eq(
mallctl("arenas.lextent.0.size", (void *)&large0, &sz, NULL, 0), 0,
"Unexpected arenas.lextent.0.size failure");
expect_d_eq(
mallctl("arenas.lextent.1.size", (void *)&large1, &sz, NULL, 0), 0,
"Unexpected arenas.lextent.1.size failure");
expect_d_eq(
mallctl("arenas.lextent.2.size", (void *)&large2, &sz, NULL, 0), 0,
"Unexpected arenas.lextent.2.size failure");
/* Test dalloc/decommit/purge cascade. */
purge_miblen = sizeof(purge_mib)/sizeof(size_t);
purge_miblen = sizeof(purge_mib) / sizeof(size_t);
expect_d_eq(mallctlnametomib("arena.0.purge", purge_mib, &purge_miblen),
0, "Unexpected mallctlnametomib() failure");
purge_mib[1] = (size_t)arena_ind;
@ -47,8 +50,8 @@ test_extent_body(unsigned arena_ind) {
if (xallocx_success_a) {
expect_true(called_dalloc, "Expected dalloc call");
expect_true(called_decommit, "Expected decommit call");
expect_true(did_purge_lazy || did_purge_forced,
"Expected purge");
expect_true(
did_purge_lazy || did_purge_forced, "Expected purge");
expect_true(called_split, "Expected split call");
}
dallocx(p, flags);
@ -72,8 +75,8 @@ test_extent_body(unsigned arena_ind) {
}
xallocx_success_c = (xallocx(p, large0 * 2, 0, flags) == large0 * 2);
if (did_split) {
expect_b_eq(did_decommit, did_commit,
"Expected decommit/commit match");
expect_b_eq(
did_decommit, did_commit, "Expected decommit/commit match");
}
if (xallocx_success_b && xallocx_success_c) {
expect_true(did_merge, "Expected merge");
@ -90,33 +93,34 @@ test_extent_body(unsigned arena_ind) {
static void
test_manual_hook_auto_arena(void) {
unsigned narenas;
size_t old_size, new_size, sz;
size_t hooks_mib[3];
size_t hooks_miblen;
unsigned narenas;
size_t old_size, new_size, sz;
size_t hooks_mib[3];
size_t hooks_miblen;
extent_hooks_t *new_hooks, *old_hooks;
extent_hooks_prep();
sz = sizeof(unsigned);
/* Get number of auto arenas. */
expect_d_eq(mallctl("opt.narenas", (void *)&narenas, &sz, NULL, 0),
0, "Unexpected mallctl() failure");
expect_d_eq(mallctl("opt.narenas", (void *)&narenas, &sz, NULL, 0), 0,
"Unexpected mallctl() failure");
if (narenas == 1) {
return;
}
/* Install custom extent hooks on arena 1 (might not be initialized). */
hooks_miblen = sizeof(hooks_mib)/sizeof(size_t);
expect_d_eq(mallctlnametomib("arena.0.extent_hooks", hooks_mib,
&hooks_miblen), 0, "Unexpected mallctlnametomib() failure");
hooks_miblen = sizeof(hooks_mib) / sizeof(size_t);
expect_d_eq(
mallctlnametomib("arena.0.extent_hooks", hooks_mib, &hooks_miblen),
0, "Unexpected mallctlnametomib() failure");
hooks_mib[1] = 1;
old_size = sizeof(extent_hooks_t *);
new_hooks = &hooks;
new_size = sizeof(extent_hooks_t *);
expect_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,
&old_size, (void *)&new_hooks, new_size), 0,
"Unexpected extent_hooks error");
&old_size, (void *)&new_hooks, new_size),
0, "Unexpected extent_hooks error");
static bool auto_arena_created = false;
if (old_hooks != &hooks) {
expect_b_eq(auto_arena_created, false,
@ -127,10 +131,10 @@ test_manual_hook_auto_arena(void) {
static void
test_manual_hook_body(void) {
unsigned arena_ind;
size_t old_size, new_size, sz;
size_t hooks_mib[3];
size_t hooks_miblen;
unsigned arena_ind;
size_t old_size, new_size, sz;
size_t hooks_mib[3];
size_t hooks_miblen;
extent_hooks_t *new_hooks, *old_hooks;
extent_hooks_prep();
@ -140,16 +144,17 @@ test_manual_hook_body(void) {
0, "Unexpected mallctl() failure");
/* Install custom extent hooks. */
hooks_miblen = sizeof(hooks_mib)/sizeof(size_t);
expect_d_eq(mallctlnametomib("arena.0.extent_hooks", hooks_mib,
&hooks_miblen), 0, "Unexpected mallctlnametomib() failure");
hooks_miblen = sizeof(hooks_mib) / sizeof(size_t);
expect_d_eq(
mallctlnametomib("arena.0.extent_hooks", hooks_mib, &hooks_miblen),
0, "Unexpected mallctlnametomib() failure");
hooks_mib[1] = (size_t)arena_ind;
old_size = sizeof(extent_hooks_t *);
new_hooks = &hooks;
new_size = sizeof(extent_hooks_t *);
expect_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,
&old_size, (void *)&new_hooks, new_size), 0,
"Unexpected extent_hooks error");
&old_size, (void *)&new_hooks, new_size),
0, "Unexpected extent_hooks error");
expect_ptr_ne(old_hooks->alloc, extent_alloc_hook,
"Unexpected extent_hooks error");
expect_ptr_ne(old_hooks->dalloc, extent_dalloc_hook,
@ -173,10 +178,13 @@ test_manual_hook_body(void) {
/* Restore extent hooks. */
expect_d_eq(mallctlbymib(hooks_mib, hooks_miblen, NULL, NULL,
(void *)&old_hooks, new_size), 0, "Unexpected extent_hooks error");
(void *)&old_hooks, new_size),
0, "Unexpected extent_hooks error");
expect_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,
&old_size, NULL, 0), 0, "Unexpected extent_hooks error");
expect_ptr_eq(old_hooks, default_hooks, "Unexpected extent_hooks error");
&old_size, NULL, 0),
0, "Unexpected extent_hooks error");
expect_ptr_eq(
old_hooks, default_hooks, "Unexpected extent_hooks error");
expect_ptr_eq(old_hooks->alloc, default_hooks->alloc,
"Unexpected extent_hooks error");
expect_ptr_eq(old_hooks->dalloc, default_hooks->dalloc,
@ -213,8 +221,8 @@ TEST_BEGIN(test_extent_manual_hook) {
TEST_END
TEST_BEGIN(test_extent_auto_hook) {
unsigned arena_ind;
size_t new_size, sz;
unsigned arena_ind;
size_t new_size, sz;
extent_hooks_t *new_hooks;
extent_hooks_prep();
@ -223,7 +231,8 @@ TEST_BEGIN(test_extent_auto_hook) {
new_hooks = &hooks;
new_size = sizeof(extent_hooks_t *);
expect_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz,
(void *)&new_hooks, new_size), 0, "Unexpected mallctl() failure");
(void *)&new_hooks, new_size),
0, "Unexpected mallctl() failure");
test_skip_if(is_background_thread_enabled());
test_extent_body(arena_ind);
@ -231,19 +240,18 @@ TEST_BEGIN(test_extent_auto_hook) {
TEST_END
static void
test_arenas_create_ext_base(arena_config_t config,
bool expect_hook_data, bool expect_hook_metadata)
{
test_arenas_create_ext_base(
arena_config_t config, bool expect_hook_data, bool expect_hook_metadata) {
unsigned arena, arena1;
void *ptr;
size_t sz = sizeof(unsigned);
void *ptr;
size_t sz = sizeof(unsigned);
extent_hooks_prep();
called_alloc = false;
expect_d_eq(mallctl("experimental.arenas_create_ext",
(void *)&arena, &sz, &config, sizeof(arena_config_t)), 0,
"Unexpected mallctl() failure");
expect_d_eq(mallctl("experimental.arenas_create_ext", (void *)&arena,
&sz, &config, sizeof(arena_config_t)),
0, "Unexpected mallctl() failure");
expect_b_eq(called_alloc, expect_hook_metadata,
"expected hook metadata alloc mismatch");
@ -279,9 +287,7 @@ TEST_END
int
main(void) {
return test(
test_extent_manual_hook,
test_extent_auto_hook,
return test(test_extent_manual_hook, test_extent_auto_hook,
test_arenas_create_ext_with_ehooks_no_metadata,
test_arenas_create_ext_with_ehooks_with_metadata);
}

View file

@ -11,6 +11,5 @@ TEST_END
int
main(void) {
return test(
test_zero_alloc);
return test(test_zero_alloc);
}

View file

@ -3,7 +3,7 @@
static unsigned
get_nsizes_impl(const char *cmd) {
unsigned ret;
size_t z;
size_t z;
z = sizeof(unsigned);
expect_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,
@ -25,12 +25,12 @@ get_size_impl(const char *cmd, size_t ind) {
size_t miblen = 4;
z = sizeof(size_t);
expect_d_eq(mallctlnametomib(cmd, mib, &miblen),
0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
expect_d_eq(mallctlnametomib(cmd, mib, &miblen), 0,
"Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
mib[2] = ind;
z = sizeof(size_t);
expect_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),
0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
expect_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0), 0,
"Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
return ret;
}
@ -64,36 +64,37 @@ JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
TEST_BEGIN(test_overflow) {
size_t largemax;
largemax = get_large_size(get_nlarge()-1);
largemax = get_large_size(get_nlarge() - 1);
expect_ptr_null(mallocx(largemax+1, 0),
"Expected OOM for mallocx(size=%#zx, 0)", largemax+1);
expect_ptr_null(mallocx(largemax + 1, 0),
"Expected OOM for mallocx(size=%#zx, 0)", largemax + 1);
expect_ptr_null(mallocx(ZU(PTRDIFF_MAX)+1, 0),
"Expected OOM for mallocx(size=%#zx, 0)", ZU(PTRDIFF_MAX)+1);
expect_ptr_null(mallocx(ZU(PTRDIFF_MAX) + 1, 0),
"Expected OOM for mallocx(size=%#zx, 0)", ZU(PTRDIFF_MAX) + 1);
expect_ptr_null(mallocx(SIZE_T_MAX, 0),
"Expected OOM for mallocx(size=%#zx, 0)", SIZE_T_MAX);
expect_ptr_null(mallocx(1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)),
expect_ptr_null(mallocx(1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX) + 1)),
"Expected OOM for mallocx(size=1, MALLOCX_ALIGN(%#zx))",
ZU(PTRDIFF_MAX)+1);
ZU(PTRDIFF_MAX) + 1);
}
TEST_END
static void *
remote_alloc(void *arg) {
unsigned arena;
size_t sz = sizeof(unsigned);
size_t sz = sizeof(unsigned);
expect_d_eq(mallctl("arenas.create", (void *)&arena, &sz, NULL, 0), 0,
"Unexpected mallctl() failure");
size_t large_sz;
sz = sizeof(size_t);
expect_d_eq(mallctl("arenas.lextent.0.size", (void *)&large_sz, &sz,
NULL, 0), 0, "Unexpected mallctl failure");
expect_d_eq(
mallctl("arenas.lextent.0.size", (void *)&large_sz, &sz, NULL, 0),
0, "Unexpected mallctl failure");
void *ptr = mallocx(large_sz, MALLOCX_ARENA(arena)
| MALLOCX_TCACHE_NONE);
void *ptr = mallocx(
large_sz, MALLOCX_ARENA(arena) | MALLOCX_TCACHE_NONE);
void **ret = (void **)arg;
*ret = ptr;
@ -114,16 +115,16 @@ TEST_BEGIN(test_remote_free) {
TEST_END
TEST_BEGIN(test_oom) {
size_t largemax;
bool oom;
void *ptrs[3];
size_t largemax;
bool oom;
void *ptrs[3];
unsigned i;
/*
* It should be impossible to allocate three objects that each consume
* nearly half the virtual address space.
*/
largemax = get_large_size(get_nlarge()-1);
largemax = get_large_size(get_nlarge() - 1);
oom = false;
for (i = 0; i < sizeof(ptrs) / sizeof(void *); i++) {
ptrs[i] = mallocx(largemax, MALLOCX_ARENA(0));
@ -143,10 +144,10 @@ TEST_BEGIN(test_oom) {
#if LG_SIZEOF_PTR == 3
expect_ptr_null(mallocx(0x8000000000000000ULL,
MALLOCX_ALIGN(0x8000000000000000ULL)),
MALLOCX_ALIGN(0x8000000000000000ULL)),
"Expected OOM for mallocx()");
expect_ptr_null(mallocx(0x8000000000000000ULL,
MALLOCX_ALIGN(0x80000000)),
expect_ptr_null(
mallocx(0x8000000000000000ULL, MALLOCX_ALIGN(0x80000000)),
"Expected OOM for mallocx()");
#else
expect_ptr_null(mallocx(0x80000000UL, MALLOCX_ALIGN(0x80000000UL)),
@ -164,20 +165,20 @@ TEST_BEGIN(test_basic) {
for (sz = 1; sz < MAXSZ; sz = nallocx(sz, 0) + 1) {
size_t nsz, rsz;
void *p;
void *p;
nsz = nallocx(sz, 0);
expect_zu_ne(nsz, 0, "Unexpected nallocx() error");
p = mallocx(sz, 0);
expect_ptr_not_null(p,
"Unexpected mallocx(size=%zx, flags=0) error", sz);
expect_ptr_not_null(
p, "Unexpected mallocx(size=%zx, flags=0) error", sz);
rsz = sallocx(p, 0);
expect_zu_ge(rsz, sz, "Real size smaller than expected");
expect_zu_eq(nsz, rsz, "nallocx()/sallocx() size mismatch");
dallocx(p, 0);
p = mallocx(sz, 0);
expect_ptr_not_null(p,
"Unexpected mallocx(size=%zx, flags=0) error", sz);
expect_ptr_not_null(
p, "Unexpected mallocx(size=%zx, flags=0) error", sz);
dallocx(p, 0);
nsz = nallocx(sz, MALLOCX_ZERO);
@ -197,53 +198,57 @@ TEST_END
TEST_BEGIN(test_alignment_and_size) {
const char *percpu_arena;
size_t sz = sizeof(percpu_arena);
size_t sz = sizeof(percpu_arena);
if(mallctl("opt.percpu_arena", (void *)&percpu_arena, &sz, NULL, 0) ||
strcmp(percpu_arena, "disabled") != 0) {
test_skip("test_alignment_and_size skipped: "
if (mallctl("opt.percpu_arena", (void *)&percpu_arena, &sz, NULL, 0)
|| strcmp(percpu_arena, "disabled") != 0) {
test_skip(
"test_alignment_and_size skipped: "
"not working with percpu arena.");
};
#define MAXALIGN (((size_t)1) << 23)
#define NITER 4
size_t nsz, rsz, alignment, total;
size_t nsz, rsz, alignment, total;
unsigned i;
void *ps[NITER];
void *ps[NITER];
for (i = 0; i < NITER; i++) {
ps[i] = NULL;
}
for (alignment = 8;
alignment <= MAXALIGN;
alignment <<= 1) {
for (alignment = 8; alignment <= MAXALIGN; alignment <<= 1) {
total = 0;
for (sz = 1;
sz < 3 * alignment && sz < (1U << 31);
sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
for (sz = 1; sz < 3 * alignment && sz < (1U << 31);
sz += (alignment >> (LG_SIZEOF_PTR - 1)) - 1) {
for (i = 0; i < NITER; i++) {
nsz = nallocx(sz, MALLOCX_ALIGN(alignment) |
MALLOCX_ZERO | MALLOCX_ARENA(0));
nsz = nallocx(sz,
MALLOCX_ALIGN(alignment) | MALLOCX_ZERO
| MALLOCX_ARENA(0));
expect_zu_ne(nsz, 0,
"nallocx() error for alignment=%zu, "
"size=%zu (%#zx)", alignment, sz, sz);
ps[i] = mallocx(sz, MALLOCX_ALIGN(alignment) |
MALLOCX_ZERO | MALLOCX_ARENA(0));
"size=%zu (%#zx)",
alignment, sz, sz);
ps[i] = mallocx(sz,
MALLOCX_ALIGN(alignment) | MALLOCX_ZERO
| MALLOCX_ARENA(0));
expect_ptr_not_null(ps[i],
"mallocx() error for alignment=%zu, "
"size=%zu (%#zx)", alignment, sz, sz);
"size=%zu (%#zx)",
alignment, sz, sz);
rsz = sallocx(ps[i], 0);
expect_zu_ge(rsz, sz,
"Real size smaller than expected for "
"alignment=%zu, size=%zu", alignment, sz);
"alignment=%zu, size=%zu",
alignment, sz);
expect_zu_eq(nsz, rsz,
"nallocx()/sallocx() size mismatch for "
"alignment=%zu, size=%zu", alignment, sz);
expect_ptr_null(
(void *)((uintptr_t)ps[i] & (alignment-1)),
"%p inadequately aligned for"
" alignment=%zu, size=%zu", ps[i],
"alignment=%zu, size=%zu",
alignment, sz);
expect_ptr_null((void *)((uintptr_t)ps[i]
& (alignment - 1)),
"%p inadequately aligned for"
" alignment=%zu, size=%zu",
ps[i], alignment, sz);
total += rsz;
if (total >= (MAXALIGN << 1)) {
break;
@ -265,10 +270,6 @@ TEST_END
int
main(void) {
return test(
test_overflow,
test_oom,
test_remote_free,
test_basic,
return test(test_overflow, test_oom, test_remote_free, test_basic,
test_alignment_and_size);
}

View file

@ -12,13 +12,14 @@ JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
TEST_BEGIN(test_overflow) {
unsigned nlextents;
size_t mib[4];
size_t sz, miblen, max_size_class;
void *p;
size_t mib[4];
size_t sz, miblen, max_size_class;
void *p;
sz = sizeof(unsigned);
expect_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL,
0), 0, "Unexpected mallctl() error");
expect_d_eq(
mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL, 0), 0,
"Unexpected mallctl() error");
miblen = sizeof(mib) / sizeof(size_t);
expect_d_eq(mallctlnametomib("arenas.lextent.0.size", mib, &miblen), 0,
@ -26,8 +27,9 @@ TEST_BEGIN(test_overflow) {
mib[2] = nlextents - 1;
sz = sizeof(size_t);
expect_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,
NULL, 0), 0, "Unexpected mallctlbymib() error");
expect_d_eq(
mallctlbymib(mib, miblen, (void *)&max_size_class, &sz, NULL, 0), 0,
"Unexpected mallctlbymib() error");
expect_ptr_null(malloc(max_size_class + 1),
"Expected OOM due to over-sized allocation request");
@ -54,6 +56,5 @@ JEMALLOC_DIAGNOSTIC_POP
int
main(void) {
return test(
test_overflow);
return test(test_overflow);
}

View file

@ -15,48 +15,44 @@ purge(void) {
TEST_BEGIN(test_alignment_errors) {
size_t alignment;
void *p;
void *p;
for (alignment = 0; alignment < sizeof(void *); alignment++) {
expect_d_eq(posix_memalign(&p, alignment, 1), EINVAL,
"Expected error for invalid alignment %zu",
alignment);
"Expected error for invalid alignment %zu", alignment);
}
for (alignment = sizeof(size_t); alignment < MAXALIGN;
alignment <<= 1) {
alignment <<= 1) {
expect_d_ne(posix_memalign(&p, alignment + 1, 1), 0,
"Expected error for invalid alignment %zu",
alignment + 1);
"Expected error for invalid alignment %zu", alignment + 1);
}
}
TEST_END
TEST_BEGIN(test_oom_errors) {
size_t alignment, size;
void *p;
void *p;
#if LG_SIZEOF_PTR == 3
alignment = UINT64_C(0x8000000000000000);
size = UINT64_C(0x8000000000000000);
size = UINT64_C(0x8000000000000000);
#else
alignment = 0x80000000LU;
size = 0x80000000LU;
size = 0x80000000LU;
#endif
expect_d_ne(posix_memalign(&p, alignment, size), 0,
"Expected error for posix_memalign(&p, %zu, %zu)",
alignment, size);
"Expected error for posix_memalign(&p, %zu, %zu)", alignment, size);
#if LG_SIZEOF_PTR == 3
alignment = UINT64_C(0x4000000000000000);
size = UINT64_C(0xc000000000000001);
size = UINT64_C(0xc000000000000001);
#else
alignment = 0x40000000LU;
size = 0xc0000001LU;
size = 0xc0000001LU;
#endif
expect_d_ne(posix_memalign(&p, alignment, size), 0,
"Expected error for posix_memalign(&p, %zu, %zu)",
alignment, size);
"Expected error for posix_memalign(&p, %zu, %zu)", alignment, size);
alignment = 0x10LU;
#if LG_SIZEOF_PTR == 3
@ -65,33 +61,29 @@ TEST_BEGIN(test_oom_errors) {
size = 0xfffffff0LU;
#endif
expect_d_ne(posix_memalign(&p, alignment, size), 0,
"Expected error for posix_memalign(&p, %zu, %zu)",
alignment, size);
"Expected error for posix_memalign(&p, %zu, %zu)", alignment, size);
}
TEST_END
TEST_BEGIN(test_alignment_and_size) {
#define NITER 4
size_t alignment, size, total;
size_t alignment, size, total;
unsigned i;
int err;
void *ps[NITER];
int err;
void *ps[NITER];
for (i = 0; i < NITER; i++) {
ps[i] = NULL;
}
for (alignment = 8;
alignment <= MAXALIGN;
alignment <<= 1) {
for (alignment = 8; alignment <= MAXALIGN; alignment <<= 1) {
total = 0;
for (size = 0;
size < 3 * alignment && size < (1U << 31);
size += ((size == 0) ? 1 :
(alignment >> (LG_SIZEOF_PTR-1)) - 1)) {
for (size = 0; size < 3 * alignment && size < (1U << 31);
size += ((size == 0)
? 1
: (alignment >> (LG_SIZEOF_PTR - 1)) - 1)) {
for (i = 0; i < NITER; i++) {
err = posix_memalign(&ps[i],
alignment, size);
err = posix_memalign(&ps[i], alignment, size);
if (err) {
char buf[BUFERROR_BUF];
@ -122,7 +114,5 @@ TEST_END
int
main(void) {
return test(
test_alignment_errors,
test_oom_errors,
test_alignment_and_size);
test_alignment_errors, test_oom_errors, test_alignment_and_size);
}

View file

@ -3,7 +3,7 @@
static unsigned
get_nsizes_impl(const char *cmd) {
unsigned ret;
size_t z;
size_t z;
z = sizeof(unsigned);
expect_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,
@ -25,12 +25,12 @@ get_size_impl(const char *cmd, size_t ind) {
size_t miblen = 4;
z = sizeof(size_t);
expect_d_eq(mallctlnametomib(cmd, mib, &miblen),
0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
expect_d_eq(mallctlnametomib(cmd, mib, &miblen), 0,
"Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
mib[2] = ind;
z = sizeof(size_t);
expect_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),
0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
expect_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0), 0,
"Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
return ret;
}
@ -58,25 +58,26 @@ TEST_BEGIN(test_grow_and_shrink) {
szs[0] = sallocx(p, 0);
for (i = 0; i < NCYCLES; i++) {
for (j = 1; j < NSZS && szs[j-1] < MAXSZ; j++) {
q = rallocx(p, szs[j-1]+1, 0);
for (j = 1; j < NSZS && szs[j - 1] < MAXSZ; j++) {
q = rallocx(p, szs[j - 1] + 1, 0);
expect_ptr_not_null(q,
"Unexpected rallocx() error for size=%zu-->%zu",
szs[j-1], szs[j-1]+1);
szs[j - 1], szs[j - 1] + 1);
szs[j] = sallocx(q, 0);
expect_zu_ne(szs[j], szs[j-1]+1,
"Expected size to be at least: %zu", szs[j-1]+1);
expect_zu_ne(szs[j], szs[j - 1] + 1,
"Expected size to be at least: %zu",
szs[j - 1] + 1);
p = q;
}
for (j--; j > 0; j--) {
q = rallocx(p, szs[j-1], 0);
q = rallocx(p, szs[j - 1], 0);
expect_ptr_not_null(q,
"Unexpected rallocx() error for size=%zu-->%zu",
szs[j], szs[j-1]);
szs[j], szs[j - 1]);
tsz = sallocx(q, 0);
expect_zu_eq(tsz, szs[j-1],
"Expected size=%zu, got size=%zu", szs[j-1], tsz);
expect_zu_eq(tsz, szs[j - 1],
"Expected size=%zu, got size=%zu", szs[j - 1], tsz);
p = q;
}
}
@ -99,11 +100,12 @@ validate_fill(void *p, uint8_t c, size_t offset, size_t len) {
size_t i;
for (i = 0; i < len; i++) {
uint8_t b = buf[offset+i];
uint8_t b = buf[offset + i];
if (b != c) {
test_fail("Allocation at %p (len=%zu) contains %#x "
"rather than %#x at offset %zu", p, len, b, c,
offset+i);
test_fail(
"Allocation at %p (len=%zu) contains %#x "
"rather than %#x at offset %zu",
p, len, b, c, offset + i);
ret = true;
}
}
@ -118,35 +120,37 @@ TEST_BEGIN(test_zero) {
*/
void *volatile p, *volatile q;
size_t psz, qsz, i, j;
size_t start_sizes[] = {1, 3*1024, 63*1024, 4095*1024};
size_t start_sizes[] = {1, 3 * 1024, 63 * 1024, 4095 * 1024};
#define FILL_BYTE 0xaaU
#define RANGE 2048
for (i = 0; i < sizeof(start_sizes)/sizeof(size_t); i++) {
for (i = 0; i < sizeof(start_sizes) / sizeof(size_t); i++) {
size_t start_size = start_sizes[i];
p = mallocx(start_size, MALLOCX_ZERO);
expect_ptr_not_null(p, "Unexpected mallocx() error");
psz = sallocx(p, 0);
expect_false(validate_fill(p, 0, 0, psz),
"Expected zeroed memory");
expect_false(
validate_fill(p, 0, 0, psz), "Expected zeroed memory");
memset(p, FILL_BYTE, psz);
expect_false(validate_fill(p, FILL_BYTE, 0, psz),
"Expected filled memory");
for (j = 1; j < RANGE; j++) {
q = rallocx(p, start_size+j, MALLOCX_ZERO);
q = rallocx(p, start_size + j, MALLOCX_ZERO);
expect_ptr_not_null(q, "Unexpected rallocx() error");
qsz = sallocx(q, 0);
if (q != p || qsz != psz) {
expect_false(validate_fill(q, FILL_BYTE, 0,
psz), "Expected filled memory");
expect_false(validate_fill(q, 0, psz, qsz-psz),
expect_false(
validate_fill(q, FILL_BYTE, 0, psz),
"Expected filled memory");
expect_false(
validate_fill(q, 0, psz, qsz - psz),
"Expected zeroed memory");
}
if (psz != qsz) {
memset((void *)((uintptr_t)q+psz), FILL_BYTE,
qsz-psz);
memset((void *)((uintptr_t)q + psz), FILL_BYTE,
qsz - psz);
psz = qsz;
}
p = q;
@ -160,7 +164,7 @@ TEST_BEGIN(test_zero) {
TEST_END
TEST_BEGIN(test_align) {
void *p, *q;
void *p, *q;
size_t align;
#define MAX_ALIGN (ZU(1) << 25)
@ -170,12 +174,10 @@ TEST_BEGIN(test_align) {
for (align <<= 1; align <= MAX_ALIGN; align <<= 1) {
q = rallocx(p, 1, MALLOCX_ALIGN(align));
expect_ptr_not_null(q,
"Unexpected rallocx() error for align=%zu", align);
expect_ptr_null(
(void *)((uintptr_t)q & (align-1)),
"%p inadequately aligned for align=%zu",
q, align);
expect_ptr_not_null(
q, "Unexpected rallocx() error for align=%zu", align);
expect_ptr_null((void *)((uintptr_t)q & (align - 1)),
"%p inadequately aligned for align=%zu", q, align);
p = q;
}
dallocx(p, 0);
@ -191,19 +193,19 @@ TEST_BEGIN(test_align_enum) {
for (size_t lg_size = LG_MIN; lg_size <= LG_MAX; ++lg_size) {
size_t size = 1 << lg_size;
for (size_t lg_align_next = LG_MIN;
lg_align_next <= LG_MAX; ++lg_align_next) {
int flags = MALLOCX_LG_ALIGN(lg_align);
lg_align_next <= LG_MAX; ++lg_align_next) {
int flags = MALLOCX_LG_ALIGN(lg_align);
void *p = mallocx(1, flags);
assert_ptr_not_null(p,
"Unexpected mallocx() error");
assert_ptr_not_null(
p, "Unexpected mallocx() error");
assert_zu_eq(nallocx(1, flags),
TEST_MALLOC_SIZE(p),
"Wrong mallocx() usable size");
int flags_next =
MALLOCX_LG_ALIGN(lg_align_next);
int flags_next = MALLOCX_LG_ALIGN(
lg_align_next);
p = rallocx(p, size, flags_next);
assert_ptr_not_null(p,
"Unexpected rallocx() error");
assert_ptr_not_null(
p, "Unexpected rallocx() error");
expect_zu_eq(nallocx(size, flags_next),
TEST_MALLOC_SIZE(p),
"Wrong rallocx() usable size");
@ -223,20 +225,20 @@ TEST_BEGIN(test_lg_align_and_zero) {
*/
void *volatile p, *volatile q;
unsigned lg_align;
size_t sz;
size_t sz;
#define MAX_LG_ALIGN 25
#define MAX_VALIDATE (ZU(1) << 22)
lg_align = 0;
p = mallocx(1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
p = mallocx(1, MALLOCX_LG_ALIGN(lg_align) | MALLOCX_ZERO);
expect_ptr_not_null(p, "Unexpected mallocx() error");
for (lg_align++; lg_align <= MAX_LG_ALIGN; lg_align++) {
q = rallocx(p, 1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
expect_ptr_not_null(q,
"Unexpected rallocx() error for lg_align=%u", lg_align);
q = rallocx(p, 1, MALLOCX_LG_ALIGN(lg_align) | MALLOCX_ZERO);
expect_ptr_not_null(
q, "Unexpected rallocx() error for lg_align=%u", lg_align);
expect_ptr_null(
(void *)((uintptr_t)q & ((ZU(1) << lg_align)-1)),
(void *)((uintptr_t)q & ((ZU(1) << lg_align) - 1)),
"%p inadequately aligned for lg_align=%u", q, lg_align);
sz = sallocx(q, 0);
if ((sz << 1) <= MAX_VALIDATE) {
@ -245,9 +247,10 @@ TEST_BEGIN(test_lg_align_and_zero) {
} else {
expect_false(validate_fill(q, 0, 0, MAX_VALIDATE),
"Expected zeroed memory");
expect_false(validate_fill(
(void *)((uintptr_t)q+sz-MAX_VALIDATE),
0, 0, MAX_VALIDATE), "Expected zeroed memory");
expect_false(validate_fill((void *)((uintptr_t)q + sz
- MAX_VALIDATE),
0, 0, MAX_VALIDATE),
"Expected zeroed memory");
}
p = q;
}
@ -269,25 +272,25 @@ JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
TEST_BEGIN(test_overflow) {
size_t largemax;
void *p;
void *p;
largemax = get_large_size(get_nlarge()-1);
largemax = get_large_size(get_nlarge() - 1);
p = mallocx(1, 0);
expect_ptr_not_null(p, "Unexpected mallocx() failure");
expect_ptr_null(rallocx(p, largemax+1, 0),
"Expected OOM for rallocx(p, size=%#zx, 0)", largemax+1);
expect_ptr_null(rallocx(p, largemax + 1, 0),
"Expected OOM for rallocx(p, size=%#zx, 0)", largemax + 1);
expect_ptr_null(rallocx(p, ZU(PTRDIFF_MAX)+1, 0),
"Expected OOM for rallocx(p, size=%#zx, 0)", ZU(PTRDIFF_MAX)+1);
expect_ptr_null(rallocx(p, ZU(PTRDIFF_MAX) + 1, 0),
"Expected OOM for rallocx(p, size=%#zx, 0)", ZU(PTRDIFF_MAX) + 1);
expect_ptr_null(rallocx(p, SIZE_T_MAX, 0),
"Expected OOM for rallocx(p, size=%#zx, 0)", SIZE_T_MAX);
expect_ptr_null(rallocx(p, 1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)),
expect_ptr_null(rallocx(p, 1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX) + 1)),
"Expected OOM for rallocx(p, size=1, MALLOCX_ALIGN(%#zx))",
ZU(PTRDIFF_MAX)+1);
ZU(PTRDIFF_MAX) + 1);
dallocx(p, 0);
}
@ -298,11 +301,6 @@ JEMALLOC_DIAGNOSTIC_POP
int
main(void) {
return test(
test_grow_and_shrink,
test_zero,
test_align,
test_align_enum,
test_lg_align_and_zero,
test_overflow);
return test(test_grow_and_shrink, test_zero, test_align,
test_align_enum, test_lg_align_and_zero, test_overflow);
}

View file

@ -10,26 +10,23 @@ TEST_BEGIN(test_basic) {
TEST_END
TEST_BEGIN(test_alignment_and_size) {
size_t nsz, sz, alignment, total;
size_t nsz, sz, alignment, total;
unsigned i;
void *ps[NITER];
void *ps[NITER];
for (i = 0; i < NITER; i++) {
ps[i] = NULL;
}
for (alignment = 8;
alignment <= MAXALIGN;
alignment <<= 1) {
for (alignment = 8; alignment <= MAXALIGN; alignment <<= 1) {
total = 0;
for (sz = 1;
sz < 3 * alignment && sz < (1U << 31);
sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
for (sz = 1; sz < 3 * alignment && sz < (1U << 31);
sz += (alignment >> (LG_SIZEOF_PTR - 1)) - 1) {
for (i = 0; i < NITER; i++) {
nsz = nallocx(sz, MALLOCX_ALIGN(alignment) |
MALLOCX_ZERO);
ps[i] = mallocx(sz, MALLOCX_ALIGN(alignment) |
MALLOCX_ZERO);
nsz = nallocx(sz,
MALLOCX_ALIGN(alignment) | MALLOCX_ZERO);
ps[i] = mallocx(sz,
MALLOCX_ALIGN(alignment) | MALLOCX_ZERO);
total += nsz;
if (total >= (MAXALIGN << 1)) {
break;
@ -49,7 +46,5 @@ TEST_END
int
main(void) {
return test_no_reentrancy(
test_basic,
test_alignment_and_size);
return test_no_reentrancy(test_basic, test_alignment_and_size);
}

View file

@ -4,10 +4,10 @@
TEST_BEGIN(test_slab_sizes) {
unsigned nbins;
size_t page;
size_t sizemib[4];
size_t slabmib[4];
size_t len;
size_t page;
size_t sizemib[4];
size_t slabmib[4];
size_t len;
len = sizeof(nbins);
expect_d_eq(mallctl("arenas.nbins", &nbins, &len, NULL, 0), 0,
@ -33,12 +33,14 @@ TEST_BEGIN(test_slab_sizes) {
len = sizeof(size_t);
sizemib[2] = i;
slabmib[2] = i;
expect_d_eq(mallctlbymib(sizemib, 4, (void *)&bin_size, &len,
NULL, 0), 0, "bin size mallctlbymib failure");
expect_d_eq(
mallctlbymib(sizemib, 4, (void *)&bin_size, &len, NULL, 0),
0, "bin size mallctlbymib failure");
len = sizeof(size_t);
expect_d_eq(mallctlbymib(slabmib, 4, (void *)&slab_size, &len,
NULL, 0), 0, "slab size mallctlbymib failure");
expect_d_eq(
mallctlbymib(slabmib, 4, (void *)&slab_size, &len, NULL, 0),
0, "slab size mallctlbymib failure");
if (bin_size < 100) {
/*
@ -51,8 +53,7 @@ TEST_BEGIN(test_slab_sizes) {
expect_zu_ge(slab_size, biggest_slab_seen,
"Slab sizes should go up");
biggest_slab_seen = slab_size;
} else if (
(100 <= bin_size && bin_size < 128)
} else if ((100 <= bin_size && bin_size < 128)
|| (128 < bin_size && bin_size <= 200)) {
expect_zu_eq(slab_size, page,
"Forced-small slabs should be small");
@ -75,6 +76,5 @@ TEST_END
int
main(void) {
return test(
test_slab_sizes);
return test(test_slab_sizes);
}

View file

@ -5,25 +5,24 @@
#define STR(x) STR_HELPER(x)
#ifndef JEMALLOC_VERSION_GID_IDENT
#error "JEMALLOC_VERSION_GID_IDENT not defined"
# error "JEMALLOC_VERSION_GID_IDENT not defined"
#endif
#define JOIN(x, y) x ## y
#define JOIN(x, y) x##y
#define JOIN2(x, y) JOIN(x, y)
#define smallocx JOIN2(smallocx_, JEMALLOC_VERSION_GID_IDENT)
typedef struct {
void *ptr;
void *ptr;
size_t size;
} smallocx_return_t;
extern smallocx_return_t
smallocx(size_t size, int flags);
extern smallocx_return_t smallocx(size_t size, int flags);
static unsigned
get_nsizes_impl(const char *cmd) {
unsigned ret;
size_t z;
size_t z;
z = sizeof(unsigned);
expect_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,
@ -45,12 +44,12 @@ get_size_impl(const char *cmd, size_t ind) {
size_t miblen = 4;
z = sizeof(size_t);
expect_d_eq(mallctlnametomib(cmd, mib, &miblen),
0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
expect_d_eq(mallctlnametomib(cmd, mib, &miblen), 0,
"Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
mib[2] = ind;
z = sizeof(size_t);
expect_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),
0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
expect_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0), 0,
"Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
return ret;
}
@ -84,36 +83,37 @@ JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
TEST_BEGIN(test_overflow) {
size_t largemax;
largemax = get_large_size(get_nlarge()-1);
largemax = get_large_size(get_nlarge() - 1);
expect_ptr_null(smallocx(largemax+1, 0).ptr,
"Expected OOM for smallocx(size=%#zx, 0)", largemax+1);
expect_ptr_null(smallocx(largemax + 1, 0).ptr,
"Expected OOM for smallocx(size=%#zx, 0)", largemax + 1);
expect_ptr_null(smallocx(ZU(PTRDIFF_MAX)+1, 0).ptr,
"Expected OOM for smallocx(size=%#zx, 0)", ZU(PTRDIFF_MAX)+1);
expect_ptr_null(smallocx(ZU(PTRDIFF_MAX) + 1, 0).ptr,
"Expected OOM for smallocx(size=%#zx, 0)", ZU(PTRDIFF_MAX) + 1);
expect_ptr_null(smallocx(SIZE_T_MAX, 0).ptr,
"Expected OOM for smallocx(size=%#zx, 0)", SIZE_T_MAX);
expect_ptr_null(smallocx(1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)).ptr,
expect_ptr_null(smallocx(1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX) + 1)).ptr,
"Expected OOM for smallocx(size=1, MALLOCX_ALIGN(%#zx))",
ZU(PTRDIFF_MAX)+1);
ZU(PTRDIFF_MAX) + 1);
}
TEST_END
static void *
remote_alloc(void *arg) {
unsigned arena;
size_t sz = sizeof(unsigned);
size_t sz = sizeof(unsigned);
expect_d_eq(mallctl("arenas.create", (void *)&arena, &sz, NULL, 0), 0,
"Unexpected mallctl() failure");
size_t large_sz;
sz = sizeof(size_t);
expect_d_eq(mallctl("arenas.lextent.0.size", (void *)&large_sz, &sz,
NULL, 0), 0, "Unexpected mallctl failure");
expect_d_eq(
mallctl("arenas.lextent.0.size", (void *)&large_sz, &sz, NULL, 0),
0, "Unexpected mallctl failure");
smallocx_return_t r
= smallocx(large_sz, MALLOCX_ARENA(arena) | MALLOCX_TCACHE_NONE);
smallocx_return_t r = smallocx(
large_sz, MALLOCX_ARENA(arena) | MALLOCX_TCACHE_NONE);
void *ptr = r.ptr;
expect_zu_eq(r.size,
nallocx(large_sz, MALLOCX_ARENA(arena) | MALLOCX_TCACHE_NONE),
@ -138,16 +138,16 @@ TEST_BEGIN(test_remote_free) {
TEST_END
TEST_BEGIN(test_oom) {
size_t largemax;
bool oom;
void *ptrs[3];
size_t largemax;
bool oom;
void *ptrs[3];
unsigned i;
/*
* It should be impossible to allocate three objects that each consume
* nearly half the virtual address space.
*/
largemax = get_large_size(get_nlarge()-1);
largemax = get_large_size(get_nlarge() - 1);
oom = false;
for (i = 0; i < sizeof(ptrs) / sizeof(void *); i++) {
ptrs[i] = smallocx(largemax, 0).ptr;
@ -167,10 +167,11 @@ TEST_BEGIN(test_oom) {
#if LG_SIZEOF_PTR == 3
expect_ptr_null(smallocx(0x8000000000000000ULL,
MALLOCX_ALIGN(0x8000000000000000ULL)).ptr,
MALLOCX_ALIGN(0x8000000000000000ULL))
.ptr,
"Expected OOM for smallocx()");
expect_ptr_null(smallocx(0x8000000000000000ULL,
MALLOCX_ALIGN(0x80000000)).ptr,
expect_ptr_null(
smallocx(0x8000000000000000ULL, MALLOCX_ALIGN(0x80000000)).ptr,
"Expected OOM for smallocx()");
#else
expect_ptr_null(smallocx(0x80000000UL, MALLOCX_ALIGN(0x80000000UL)).ptr,
@ -188,15 +189,15 @@ TEST_BEGIN(test_basic) {
for (sz = 1; sz < MAXSZ; sz = nallocx(sz, 0) + 1) {
smallocx_return_t ret;
size_t nsz, rsz, smz;
void *p;
size_t nsz, rsz, smz;
void *p;
nsz = nallocx(sz, 0);
expect_zu_ne(nsz, 0, "Unexpected nallocx() error");
ret = smallocx(sz, 0);
p = ret.ptr;
smz = ret.size;
expect_ptr_not_null(p,
"Unexpected smallocx(size=%zx, flags=0) error", sz);
expect_ptr_not_null(
p, "Unexpected smallocx(size=%zx, flags=0) error", sz);
rsz = sallocx(p, 0);
expect_zu_ge(rsz, sz, "Real size smaller than expected");
expect_zu_eq(nsz, rsz, "nallocx()/sallocx() size mismatch");
@ -206,8 +207,8 @@ TEST_BEGIN(test_basic) {
ret = smallocx(sz, 0);
p = ret.ptr;
smz = ret.size;
expect_ptr_not_null(p,
"Unexpected smallocx(size=%zx, flags=0) error", sz);
expect_ptr_not_null(
p, "Unexpected smallocx(size=%zx, flags=0) error", sz);
dallocx(p, 0);
nsz = nallocx(sz, MALLOCX_ZERO);
@ -230,58 +231,61 @@ TEST_END
TEST_BEGIN(test_alignment_and_size) {
const char *percpu_arena;
size_t sz = sizeof(percpu_arena);
size_t sz = sizeof(percpu_arena);
if(mallctl("opt.percpu_arena", (void *)&percpu_arena, &sz, NULL, 0) ||
strcmp(percpu_arena, "disabled") != 0) {
test_skip("test_alignment_and_size skipped: "
if (mallctl("opt.percpu_arena", (void *)&percpu_arena, &sz, NULL, 0)
|| strcmp(percpu_arena, "disabled") != 0) {
test_skip(
"test_alignment_and_size skipped: "
"not working with percpu arena.");
};
#define MAXALIGN (((size_t)1) << 23)
#define NITER 4
size_t nsz, rsz, smz, alignment, total;
size_t nsz, rsz, smz, alignment, total;
unsigned i;
void *ps[NITER];
void *ps[NITER];
for (i = 0; i < NITER; i++) {
ps[i] = NULL;
}
for (alignment = 8;
alignment <= MAXALIGN;
alignment <<= 1) {
for (alignment = 8; alignment <= MAXALIGN; alignment <<= 1) {
total = 0;
for (sz = 1;
sz < 3 * alignment && sz < (1U << 31);
sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
for (sz = 1; sz < 3 * alignment && sz < (1U << 31);
sz += (alignment >> (LG_SIZEOF_PTR - 1)) - 1) {
for (i = 0; i < NITER; i++) {
nsz = nallocx(sz, MALLOCX_ALIGN(alignment) |
MALLOCX_ZERO);
nsz = nallocx(sz,
MALLOCX_ALIGN(alignment) | MALLOCX_ZERO);
expect_zu_ne(nsz, 0,
"nallocx() error for alignment=%zu, "
"size=%zu (%#zx)", alignment, sz, sz);
smallocx_return_t ret
= smallocx(sz, MALLOCX_ALIGN(alignment) | MALLOCX_ZERO);
"size=%zu (%#zx)",
alignment, sz, sz);
smallocx_return_t ret = smallocx(sz,
MALLOCX_ALIGN(alignment) | MALLOCX_ZERO);
ps[i] = ret.ptr;
expect_ptr_not_null(ps[i],
"smallocx() error for alignment=%zu, "
"size=%zu (%#zx)", alignment, sz, sz);
"size=%zu (%#zx)",
alignment, sz, sz);
rsz = sallocx(ps[i], 0);
smz = ret.size;
expect_zu_ge(rsz, sz,
"Real size smaller than expected for "
"alignment=%zu, size=%zu", alignment, sz);
"alignment=%zu, size=%zu",
alignment, sz);
expect_zu_eq(nsz, rsz,
"nallocx()/sallocx() size mismatch for "
"alignment=%zu, size=%zu", alignment, sz);
"alignment=%zu, size=%zu",
alignment, sz);
expect_zu_eq(nsz, smz,
"nallocx()/smallocx() size mismatch for "
"alignment=%zu, size=%zu", alignment, sz);
expect_ptr_null(
(void *)((uintptr_t)ps[i] & (alignment-1)),
"%p inadequately aligned for"
" alignment=%zu, size=%zu", ps[i],
"alignment=%zu, size=%zu",
alignment, sz);
expect_ptr_null((void *)((uintptr_t)ps[i]
& (alignment - 1)),
"%p inadequately aligned for"
" alignment=%zu, size=%zu",
ps[i], alignment, sz);
total += rsz;
if (total >= (MAXALIGN << 1)) {
break;
@ -303,10 +307,6 @@ TEST_END
int
main(void) {
return test(
test_overflow,
test_oom,
test_remote_free,
test_basic,
return test(test_overflow, test_oom, test_remote_free, test_basic,
test_alignment_and_size);
}

View file

@ -5,10 +5,10 @@
void *
thd_start(void *arg) {
unsigned main_arena_ind = *(unsigned *)arg;
void *p;
void *p;
unsigned arena_ind;
size_t size;
int err;
size_t size;
int err;
p = malloc(1);
expect_ptr_not_null(p, "Error in malloc()");
@ -16,7 +16,7 @@ thd_start(void *arg) {
size = sizeof(arena_ind);
if ((err = mallctl("thread.arena", (void *)&arena_ind, &size,
(void *)&main_arena_ind, sizeof(main_arena_ind)))) {
(void *)&main_arena_ind, sizeof(main_arena_ind)))) {
char buf[BUFERROR_BUF];
buferror(err, buf, sizeof(buf));
@ -24,8 +24,8 @@ thd_start(void *arg) {
}
size = sizeof(arena_ind);
if ((err = mallctl("thread.arena", (void *)&arena_ind, &size, NULL,
0))) {
if ((err = mallctl(
"thread.arena", (void *)&arena_ind, &size, NULL, 0))) {
char buf[BUFERROR_BUF];
buferror(err, buf, sizeof(buf));
@ -46,28 +46,28 @@ mallctl_failure(int err) {
}
TEST_BEGIN(test_thread_arena) {
void *p;
int err;
thd_t thds[NTHREADS];
void *p;
int err;
thd_t thds[NTHREADS];
unsigned i;
p = malloc(1);
expect_ptr_not_null(p, "Error in malloc()");
unsigned arena_ind, old_arena_ind;
size_t sz = sizeof(unsigned);
size_t sz = sizeof(unsigned);
expect_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
0, "Arena creation failure");
size_t size = sizeof(arena_ind);
if ((err = mallctl("thread.arena", (void *)&old_arena_ind, &size,
(void *)&arena_ind, sizeof(arena_ind))) != 0) {
(void *)&arena_ind, sizeof(arena_ind)))
!= 0) {
mallctl_failure(err);
}
for (i = 0; i < NTHREADS; i++) {
thd_create(&thds[i], thd_start,
(void *)&arena_ind);
thd_create(&thds[i], thd_start, (void *)&arena_ind);
}
for (i = 0; i < NTHREADS; i++) {
@ -81,6 +81,5 @@ TEST_END
int
main(void) {
return test(
test_thread_arena);
return test(test_thread_arena);
}

View file

@ -2,60 +2,69 @@
void *
thd_start(void *arg) {
bool e0, e1;
bool e0, e1;
size_t sz = sizeof(bool);
expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz, NULL,
0), 0, "Unexpected mallctl failure");
expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz, NULL, 0),
0, "Unexpected mallctl failure");
if (e0) {
e1 = false;
expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
(void *)&e1, sz), 0, "Unexpected mallctl() error");
(void *)&e1, sz),
0, "Unexpected mallctl() error");
expect_true(e0, "tcache should be enabled");
}
e1 = true;
expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
(void *)&e1, sz), 0, "Unexpected mallctl() error");
expect_d_eq(
mallctl("thread.tcache.enabled", (void *)&e0, &sz, (void *)&e1, sz),
0, "Unexpected mallctl() error");
expect_false(e0, "tcache should be disabled");
e1 = true;
expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
(void *)&e1, sz), 0, "Unexpected mallctl() error");
expect_d_eq(
mallctl("thread.tcache.enabled", (void *)&e0, &sz, (void *)&e1, sz),
0, "Unexpected mallctl() error");
expect_true(e0, "tcache should be enabled");
e1 = false;
expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
(void *)&e1, sz), 0, "Unexpected mallctl() error");
expect_d_eq(
mallctl("thread.tcache.enabled", (void *)&e0, &sz, (void *)&e1, sz),
0, "Unexpected mallctl() error");
expect_true(e0, "tcache should be enabled");
e1 = false;
expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
(void *)&e1, sz), 0, "Unexpected mallctl() error");
expect_d_eq(
mallctl("thread.tcache.enabled", (void *)&e0, &sz, (void *)&e1, sz),
0, "Unexpected mallctl() error");
expect_false(e0, "tcache should be disabled");
free(malloc(1));
e1 = true;
expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
(void *)&e1, sz), 0, "Unexpected mallctl() error");
expect_d_eq(
mallctl("thread.tcache.enabled", (void *)&e0, &sz, (void *)&e1, sz),
0, "Unexpected mallctl() error");
expect_false(e0, "tcache should be disabled");
free(malloc(1));
e1 = true;
expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
(void *)&e1, sz), 0, "Unexpected mallctl() error");
expect_d_eq(
mallctl("thread.tcache.enabled", (void *)&e0, &sz, (void *)&e1, sz),
0, "Unexpected mallctl() error");
expect_true(e0, "tcache should be enabled");
free(malloc(1));
e1 = false;
expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
(void *)&e1, sz), 0, "Unexpected mallctl() error");
expect_d_eq(
mallctl("thread.tcache.enabled", (void *)&e0, &sz, (void *)&e1, sz),
0, "Unexpected mallctl() error");
expect_true(e0, "tcache should be enabled");
free(malloc(1));
e1 = false;
expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
(void *)&e1, sz), 0, "Unexpected mallctl() error");
expect_d_eq(
mallctl("thread.tcache.enabled", (void *)&e0, &sz, (void *)&e1, sz),
0, "Unexpected mallctl() error");
expect_false(e0, "tcache should be disabled");
free(malloc(1));
@ -78,10 +87,6 @@ TEST_END
int
main(void) {
/* Run tests multiple times to check for bad interactions. */
return test(
test_main_thread,
test_subthread,
test_main_thread,
test_subthread,
test_main_thread);
return test(test_main_thread, test_subthread, test_main_thread,
test_subthread, test_main_thread);
}

View file

@ -11,15 +11,16 @@ arena_ind(void) {
if (ind == 0) {
size_t sz = sizeof(ind);
expect_d_eq(mallctl("arenas.create", (void *)&ind, &sz, NULL,
0), 0, "Unexpected mallctl failure creating arena");
expect_d_eq(
mallctl("arenas.create", (void *)&ind, &sz, NULL, 0), 0,
"Unexpected mallctl failure creating arena");
}
return ind;
}
TEST_BEGIN(test_same_size) {
void *p;
void *p;
size_t sz, tsz;
p = mallocx(42, 0);
@ -34,14 +35,14 @@ TEST_BEGIN(test_same_size) {
TEST_END
TEST_BEGIN(test_extra_no_move) {
void *p;
void *p;
size_t sz, tsz;
p = mallocx(42, 0);
expect_ptr_not_null(p, "Unexpected mallocx() error");
sz = sallocx(p, 0);
tsz = xallocx(p, sz, sz-42, 0);
tsz = xallocx(p, sz, sz - 42, 0);
expect_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
dallocx(p, 0);
@ -49,7 +50,7 @@ TEST_BEGIN(test_extra_no_move) {
TEST_END
TEST_BEGIN(test_no_move_fail) {
void *p;
void *p;
size_t sz, tsz;
p = mallocx(42, 0);
@ -66,7 +67,7 @@ TEST_END
static unsigned
get_nsizes_impl(const char *cmd) {
unsigned ret;
size_t z;
size_t z;
z = sizeof(unsigned);
expect_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,
@ -93,12 +94,12 @@ get_size_impl(const char *cmd, size_t ind) {
size_t miblen = 4;
z = sizeof(size_t);
expect_d_eq(mallctlnametomib(cmd, mib, &miblen),
0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
expect_d_eq(mallctlnametomib(cmd, mib, &miblen), 0,
"Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
mib[2] = ind;
z = sizeof(size_t);
expect_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),
0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
expect_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0), 0,
"Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
return ret;
}
@ -115,25 +116,25 @@ get_large_size(size_t ind) {
TEST_BEGIN(test_size) {
size_t small0, largemax;
void *p;
void *p;
/* Get size classes. */
small0 = get_small_size(0);
largemax = get_large_size(get_nlarge()-1);
largemax = get_large_size(get_nlarge() - 1);
p = mallocx(small0, 0);
expect_ptr_not_null(p, "Unexpected mallocx() error");
/* Test smallest supported size. */
expect_zu_eq(xallocx(p, 1, 0, 0), small0,
"Unexpected xallocx() behavior");
expect_zu_eq(
xallocx(p, 1, 0, 0), small0, "Unexpected xallocx() behavior");
/* Test largest supported size. */
expect_zu_le(xallocx(p, largemax, 0, 0), largemax,
"Unexpected xallocx() behavior");
/* Test size overflow. */
expect_zu_le(xallocx(p, largemax+1, 0, 0), largemax,
expect_zu_le(xallocx(p, largemax + 1, 0, 0), largemax,
"Unexpected xallocx() behavior");
expect_zu_le(xallocx(p, SIZE_T_MAX, 0, 0), largemax,
"Unexpected xallocx() behavior");
@ -144,29 +145,29 @@ TEST_END
TEST_BEGIN(test_size_extra_overflow) {
size_t small0, largemax;
void *p;
void *p;
/* Get size classes. */
small0 = get_small_size(0);
largemax = get_large_size(get_nlarge()-1);
largemax = get_large_size(get_nlarge() - 1);
p = mallocx(small0, 0);
expect_ptr_not_null(p, "Unexpected mallocx() error");
/* Test overflows that can be resolved by clamping extra. */
expect_zu_le(xallocx(p, largemax-1, 2, 0), largemax,
expect_zu_le(xallocx(p, largemax - 1, 2, 0), largemax,
"Unexpected xallocx() behavior");
expect_zu_le(xallocx(p, largemax, 1, 0), largemax,
"Unexpected xallocx() behavior");
/* Test overflow such that largemax-size underflows. */
expect_zu_le(xallocx(p, largemax+1, 2, 0), largemax,
expect_zu_le(xallocx(p, largemax + 1, 2, 0), largemax,
"Unexpected xallocx() behavior");
expect_zu_le(xallocx(p, largemax+2, 3, 0), largemax,
expect_zu_le(xallocx(p, largemax + 2, 3, 0), largemax,
"Unexpected xallocx() behavior");
expect_zu_le(xallocx(p, SIZE_T_MAX-2, 2, 0), largemax,
expect_zu_le(xallocx(p, SIZE_T_MAX - 2, 2, 0), largemax,
"Unexpected xallocx() behavior");
expect_zu_le(xallocx(p, SIZE_T_MAX-1, 1, 0), largemax,
expect_zu_le(xallocx(p, SIZE_T_MAX - 1, 1, 0), largemax,
"Unexpected xallocx() behavior");
dallocx(p, 0);
@ -175,21 +176,21 @@ TEST_END
TEST_BEGIN(test_extra_small) {
size_t small0, small1, largemax;
void *p;
void *p;
/* Get size classes. */
small0 = get_small_size(0);
small1 = get_small_size(1);
largemax = get_large_size(get_nlarge()-1);
largemax = get_large_size(get_nlarge() - 1);
p = mallocx(small0, 0);
expect_ptr_not_null(p, "Unexpected mallocx() error");
expect_zu_eq(xallocx(p, small1, 0, 0), small0,
"Unexpected xallocx() behavior");
expect_zu_eq(
xallocx(p, small1, 0, 0), small0, "Unexpected xallocx() behavior");
expect_zu_eq(xallocx(p, small1, 0, 0), small0,
"Unexpected xallocx() behavior");
expect_zu_eq(
xallocx(p, small1, 0, 0), small0, "Unexpected xallocx() behavior");
expect_zu_eq(xallocx(p, small0, small1 - small0, 0), small0,
"Unexpected xallocx() behavior");
@ -205,16 +206,16 @@ TEST_BEGIN(test_extra_small) {
TEST_END
TEST_BEGIN(test_extra_large) {
int flags = MALLOCX_ARENA(arena_ind());
int flags = MALLOCX_ARENA(arena_ind());
size_t smallmax, large1, large2, large3, largemax;
void *p;
void *p;
/* Get size classes. */
smallmax = get_small_size(get_nsmall()-1);
smallmax = get_small_size(get_nsmall() - 1);
large1 = get_large_size(1);
large2 = get_large_size(2);
large3 = get_large_size(3);
largemax = get_large_size(get_nlarge()-1);
largemax = get_large_size(get_nlarge() - 1);
p = mallocx(large3, flags);
expect_ptr_not_null(p, "Unexpected mallocx() error");
@ -246,7 +247,7 @@ TEST_BEGIN(test_extra_large) {
/* Test size increase with zero extra. */
expect_zu_le(xallocx(p, large3, 0, flags), large3,
"Unexpected xallocx() behavior");
expect_zu_le(xallocx(p, largemax+1, 0, flags), large3,
expect_zu_le(xallocx(p, largemax + 1, 0, flags), large3,
"Unexpected xallocx() behavior");
expect_zu_ge(xallocx(p, large1, 0, flags), large1,
@ -276,8 +277,8 @@ TEST_END
static void
print_filled_extents(const void *p, uint8_t c, size_t len) {
const uint8_t *pc = (const uint8_t *)p;
size_t i, range0;
uint8_t c0;
size_t i, range0;
uint8_t c0;
malloc_printf(" p=%p, c=%#x, len=%zu:", p, c, len);
range0 = 0;
@ -295,10 +296,10 @@ print_filled_extents(const void *p, uint8_t c, size_t len) {
static bool
validate_fill(const void *p, uint8_t c, size_t offset, size_t len) {
const uint8_t *pc = (const uint8_t *)p;
bool err;
size_t i;
bool err;
size_t i;
for (i = offset, err = false; i < offset+len; i++) {
for (i = offset, err = false; i < offset + len; i++) {
if (pc[i] != c) {
err = true;
}
@ -313,16 +314,16 @@ validate_fill(const void *p, uint8_t c, size_t offset, size_t len) {
static void
test_zero(size_t szmin, size_t szmax) {
int flags = MALLOCX_ARENA(arena_ind()) | MALLOCX_ZERO;
int flags = MALLOCX_ARENA(arena_ind()) | MALLOCX_ZERO;
size_t sz, nsz;
void *p;
void *p;
#define FILL_BYTE 0x7aU
sz = szmax;
p = mallocx(sz, flags);
expect_ptr_not_null(p, "Unexpected mallocx() error");
expect_false(validate_fill(p, 0x00, 0, sz), "Memory not filled: sz=%zu",
sz);
expect_false(
validate_fill(p, 0x00, 0, sz), "Memory not filled: sz=%zu", sz);
/*
* Fill with non-zero so that non-debug builds are more likely to detect
@ -342,16 +343,16 @@ test_zero(size_t szmin, size_t szmax) {
"Memory not filled: sz=%zu", sz);
for (sz = szmin; sz < szmax; sz = nsz) {
nsz = nallocx(sz+1, flags);
if (xallocx(p, sz+1, 0, flags) != nsz) {
p = rallocx(p, sz+1, flags);
nsz = nallocx(sz + 1, flags);
if (xallocx(p, sz + 1, 0, flags) != nsz) {
p = rallocx(p, sz + 1, flags);
expect_ptr_not_null(p, "Unexpected rallocx() failure");
}
expect_false(validate_fill(p, FILL_BYTE, 0, sz),
"Memory not filled: sz=%zu", sz);
expect_false(validate_fill(p, 0x00, sz, nsz-sz),
"Memory not filled: sz=%zu, nsz-sz=%zu", sz, nsz-sz);
memset((void *)((uintptr_t)p + sz), FILL_BYTE, nsz-sz);
expect_false(validate_fill(p, 0x00, sz, nsz - sz),
"Memory not filled: sz=%zu, nsz-sz=%zu", sz, nsz - sz);
memset((void *)((uintptr_t)p + sz), FILL_BYTE, nsz - sz);
expect_false(validate_fill(p, FILL_BYTE, 0, nsz),
"Memory not filled: nsz=%zu", nsz);
}
@ -372,13 +373,7 @@ TEST_END
int
main(void) {
return test(
test_same_size,
test_extra_no_move,
test_no_move_fail,
test_size,
test_size_extra_overflow,
test_extra_small,
test_extra_large,
test_zero_large);
return test(test_same_size, test_extra_no_move, test_no_move_fail,
test_size, test_size_extra_overflow, test_extra_small,
test_extra_large, test_zero_large);
}