server-skynet-source-3rd-je.../test/integration/posix_memalign.c

129 lines
2.8 KiB
C
Raw Normal View History

Refactor to support more varied testing. Refactor the test harness to support three types of tests: - unit: White box unit tests. These tests have full access to all internal jemalloc library symbols. Though in actuality all symbols are prefixed by jet_, macro-based name mangling abstracts this away from test code. - integration: Black box integration tests. These tests link with the installable shared jemalloc library, and with the exception of some utility code and configure-generated macro definitions, they have no access to jemalloc internals. - stress: Black box stress tests. These tests link with the installable shared jemalloc library, as well as with an internal allocator with symbols prefixed by jet_ (same as for unit tests) that can be used to allocate data structures that are internal to the test code. Move existing tests into test/{unit,integration}/ as appropriate. Split out internal parts of jemalloc_defs.h.in and put them in jemalloc_internal_defs.h.in. This reduces internals exposure to applications that #include <jemalloc/jemalloc.h>. Refactor jemalloc.h header generation so that a single header file results, and the prototypes can be used to generate jet_ prototypes for tests. Split jemalloc.h.in into multiple parts (jemalloc_defs.h.in, jemalloc_macros.h.in, jemalloc_protos.h.in, jemalloc_mangle.h.in) and use a shell script to generate a unified jemalloc.h at configure time. Change the default private namespace prefix from "" to "je_". Add missing private namespace mangling. Remove hard-coded private_namespace.h. Instead generate it and private_unnamespace.h from private_symbols.txt. Use similar logic for public symbols, which aids in name mangling for jet_ symbols. Add test_warn() and test_fail(). Replace existing exit(1) calls with test_fail() calls.
2013-12-01 07:25:42 +08:00
#include "test/jemalloc_test.h"
2010-09-12 11:59:16 +08:00
#define MAXALIGN (((size_t)1) << 23)
/*
* On systems which can't merge extents, tests that call this function generate
* a lot of dirty memory very quickly. Purging between cycles mitigates
* potential OOM on e.g. 32-bit Windows.
*/
static void
purge(void) {
assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
"Unexpected mallctl error");
}
2010-09-12 11:59:16 +08:00
TEST_BEGIN(test_alignment_errors) {
size_t alignment;
void *p;
2010-09-12 11:59:16 +08:00
for (alignment = 0; alignment < sizeof(void *); alignment++) {
assert_d_eq(posix_memalign(&p, alignment, 1), EINVAL,
"Expected error for invalid alignment %zu",
alignment);
2010-09-12 11:59:16 +08:00
}
for (alignment = sizeof(size_t); alignment < MAXALIGN;
alignment <<= 1) {
assert_d_ne(posix_memalign(&p, alignment + 1, 1), 0,
"Expected error for invalid alignment %zu",
alignment + 1);
2010-09-12 11:59:16 +08:00
}
}
TEST_END
TEST_BEGIN(test_oom_errors) {
size_t alignment, size;
void *p;
2010-09-12 11:59:16 +08:00
#if LG_SIZEOF_PTR == 3
alignment = UINT64_C(0x8000000000000000);
size = UINT64_C(0x8000000000000000);
2010-09-12 11:59:16 +08:00
#else
alignment = 0x80000000LU;
size = 0x80000000LU;
2010-09-12 11:59:16 +08:00
#endif
assert_d_ne(posix_memalign(&p, alignment, size), 0,
"Expected error for posix_memalign(&p, %zu, %zu)",
alignment, size);
2010-09-12 11:59:16 +08:00
#if LG_SIZEOF_PTR == 3
alignment = UINT64_C(0x4000000000000000);
size = UINT64_C(0xc000000000000001);
2010-09-12 11:59:16 +08:00
#else
alignment = 0x40000000LU;
size = 0xc0000001LU;
2010-09-12 11:59:16 +08:00
#endif
assert_d_ne(posix_memalign(&p, alignment, size), 0,
"Expected error for posix_memalign(&p, %zu, %zu)",
alignment, size);
2010-09-12 11:59:16 +08:00
alignment = 0x10LU;
2010-09-12 11:59:16 +08:00
#if LG_SIZEOF_PTR == 3
size = UINT64_C(0xfffffffffffffff0);
2010-09-12 11:59:16 +08:00
#else
size = 0xfffffff0LU;
#endif
assert_d_ne(posix_memalign(&p, alignment, size), 0,
"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;
unsigned i;
int err;
void *ps[NITER];
2010-09-12 11:59:16 +08:00
for (i = 0; i < NITER; i++) {
2010-09-12 11:59:16 +08:00
ps[i] = NULL;
}
2010-09-12 11:59:16 +08:00
for (alignment = 8;
2010-09-12 11:59:16 +08:00
alignment <= MAXALIGN;
alignment <<= 1) {
total = 0;
for (size = 0;
2010-09-12 11:59:16 +08:00
size < 3 * alignment && size < (1U << 31);
size += ((size == 0) ? 1 :
(alignment >> (LG_SIZEOF_PTR-1)) - 1)) {
2010-09-12 11:59:16 +08:00
for (i = 0; i < NITER; i++) {
err = posix_memalign(&ps[i],
2010-09-12 11:59:16 +08:00
alignment, size);
if (err) {
char buf[BUFERROR_BUF];
buferror(get_errno(), buf, sizeof(buf));
Refactor to support more varied testing. Refactor the test harness to support three types of tests: - unit: White box unit tests. These tests have full access to all internal jemalloc library symbols. Though in actuality all symbols are prefixed by jet_, macro-based name mangling abstracts this away from test code. - integration: Black box integration tests. These tests link with the installable shared jemalloc library, and with the exception of some utility code and configure-generated macro definitions, they have no access to jemalloc internals. - stress: Black box stress tests. These tests link with the installable shared jemalloc library, as well as with an internal allocator with symbols prefixed by jet_ (same as for unit tests) that can be used to allocate data structures that are internal to the test code. Move existing tests into test/{unit,integration}/ as appropriate. Split out internal parts of jemalloc_defs.h.in and put them in jemalloc_internal_defs.h.in. This reduces internals exposure to applications that #include <jemalloc/jemalloc.h>. Refactor jemalloc.h header generation so that a single header file results, and the prototypes can be used to generate jet_ prototypes for tests. Split jemalloc.h.in into multiple parts (jemalloc_defs.h.in, jemalloc_macros.h.in, jemalloc_protos.h.in, jemalloc_mangle.h.in) and use a shell script to generate a unified jemalloc.h at configure time. Change the default private namespace prefix from "" to "je_". Add missing private namespace mangling. Remove hard-coded private_namespace.h. Instead generate it and private_unnamespace.h from private_symbols.txt. Use similar logic for public symbols, which aids in name mangling for jet_ symbols. Add test_warn() and test_fail(). Replace existing exit(1) calls with test_fail() calls.
2013-12-01 07:25:42 +08:00
test_fail(
"Error for alignment=%zu, "
"size=%zu (%#zx): %s",
alignment, size, size, buf);
2010-09-12 11:59:16 +08:00
}
total += malloc_usable_size(ps[i]);
if (total >= (MAXALIGN << 1)) {
2010-09-12 11:59:16 +08:00
break;
}
2010-09-12 11:59:16 +08:00
}
for (i = 0; i < NITER; i++) {
if (ps[i] != NULL) {
free(ps[i]);
2010-09-12 11:59:16 +08:00
ps[i] = NULL;
}
}
}
purge();
2010-09-12 11:59:16 +08:00
}
#undef NITER
}
TEST_END
int
main(void) {
return test(
test_alignment_errors,
test_oom_errors,
test_alignment_and_size);
2010-09-12 11:59:16 +08:00
}