summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorJason Evans <jasone@canonware.com>2013-12-13 06:35:52 (GMT)
committerJason Evans <jasone@canonware.com>2013-12-13 06:35:52 (GMT)
commitd82a5e6a34f20698ab9368bb2b4953b81d175552 (patch)
tree23cbe8892adf46196cc6b2cf977704405c7798b7 /test
parent0ac396a06a10f8a8c1d41c8771367625e7d49d07 (diff)
downloadjemalloc-d82a5e6a34f20698ab9368bb2b4953b81d175552.zip
jemalloc-d82a5e6a34f20698ab9368bb2b4953b81d175552.tar.gz
jemalloc-d82a5e6a34f20698ab9368bb2b4953b81d175552.tar.bz2
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API. The *allocx() functions are slightly simpler to use because they have fewer parameters, they directly return the results of primary interest, and mallocx()/rallocx() avoid the strict aliasing pitfall that allocm()/rallocx() share with posix_memalign(). The following code violates strict aliasing rules: foo_t *foo; allocm((void **)&foo, NULL, 42, 0); whereas the following is safe: foo_t *foo; void *p; allocm(&p, NULL, 42, 0); foo = (foo_t *)p; mallocx() does not have this problem: foo_t *foo = (foo_t *)mallocx(42, 0);
Diffstat (limited to 'test')
-rw-r--r--test/integration/mallocx.c149
-rw-r--r--test/integration/rallocm.c2
-rw-r--r--test/integration/rallocx.c51
-rw-r--r--test/integration/xallocx.c59
-rw-r--r--test/unit/mq.c7
5 files changed, 262 insertions, 6 deletions
diff --git a/test/integration/mallocx.c b/test/integration/mallocx.c
new file mode 100644
index 0000000..f12855e
--- /dev/null
+++ b/test/integration/mallocx.c
@@ -0,0 +1,149 @@
+#include "test/jemalloc_test.h"
+
+#define CHUNK 0x400000
+/* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */
+#define MAXALIGN ((size_t)0x2000000LU)
+#define NITER 4
+
+TEST_BEGIN(test_basic)
+{
+ size_t nsz, rsz, sz;
+ void *p;
+
+ sz = 42;
+ nsz = nallocx(sz, 0);
+ assert_zu_ne(nsz, 0, "Unexpected nallocx() error");
+ p = mallocx(sz, 0);
+ assert_ptr_not_null(p, "Unexpected mallocx() error");
+ rsz = sallocx(p, 0);
+ assert_zu_ge(rsz, sz, "Real size smaller than expected");
+ assert_zu_eq(nsz, rsz, "nallocx()/sallocx() size mismatch");
+ dallocx(p, 0);
+
+ p = mallocx(sz, 0);
+ assert_ptr_not_null(p, "Unexpected mallocx() error");
+ dallocx(p, 0);
+
+ nsz = nallocx(sz, MALLOCX_ZERO);
+ assert_zu_ne(nsz, 0, "Unexpected nallocx() error");
+ p = mallocx(sz, MALLOCX_ZERO);
+ assert_ptr_not_null(p, "Unexpected mallocx() error");
+ rsz = sallocx(p, 0);
+ assert_zu_eq(nsz, rsz, "nallocx()/sallocx() rsize mismatch");
+ dallocx(p, 0);
+}
+TEST_END
+
+TEST_BEGIN(test_alignment_errors)
+{
+ void *p;
+ size_t nsz, sz, alignment;
+
+#if LG_SIZEOF_PTR == 3
+ alignment = UINT64_C(0x8000000000000000);
+ sz = UINT64_C(0x8000000000000000);
+#else
+ alignment = 0x80000000LU;
+ sz = 0x80000000LU;
+#endif
+ nsz = nallocx(sz, MALLOCX_ALIGN(alignment));
+ assert_zu_eq(nsz, 0, "Expected error for nallocx(%zu, %#x)", sz,
+ MALLOCX_ALIGN(alignment));
+ p = mallocx(sz, MALLOCX_ALIGN(alignment));
+ assert_ptr_null(p, "Expected error for mallocx(%zu, %#x)", sz,
+ MALLOCX_ALIGN(alignment));
+
+#if LG_SIZEOF_PTR == 3
+ alignment = UINT64_C(0x4000000000000000);
+ sz = UINT64_C(0x8400000000000001);
+#else
+ alignment = 0x40000000LU;
+ sz = 0x84000001LU;
+#endif
+ nsz = nallocx(sz, MALLOCX_ALIGN(alignment));
+ assert_zu_ne(nsz, 0, "Unexpected nallocx() error");
+ p = mallocx(sz, MALLOCX_ALIGN(alignment));
+ assert_ptr_null(p, "Expected error for mallocx(%zu, %#x)", sz,
+ MALLOCX_ALIGN(alignment));
+
+ alignment = 0x10LU;
+#if LG_SIZEOF_PTR == 3
+ sz = UINT64_C(0xfffffffffffffff0);
+#else
+ sz = 0xfffffff0LU;
+#endif
+ nsz = nallocx(sz, MALLOCX_ALIGN(alignment));
+ assert_zu_eq(nsz, 0, "Expected error for nallocx(%zu, %#x)", sz,
+ MALLOCX_ALIGN(alignment));
+ nsz = nallocx(sz, MALLOCX_ALIGN(alignment));
+ assert_zu_eq(nsz, 0, "Expected error for nallocx(%zu, %#x)", sz,
+ MALLOCX_ALIGN(alignment));
+ p = mallocx(sz, MALLOCX_ALIGN(alignment));
+ assert_ptr_null(p, "Expected error for mallocx(%zu, %#x)", sz,
+ MALLOCX_ALIGN(alignment));
+}
+TEST_END
+
+TEST_BEGIN(test_alignment_and_size)
+{
+ size_t nsz, rsz, sz, alignment, total;
+ unsigned i;
+ void *ps[NITER];
+
+ for (i = 0; i < NITER; i++)
+ ps[i] = NULL;
+
+ 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 (i = 0; i < NITER; i++) {
+ nsz = nallocx(sz, MALLOCX_ALIGN(alignment) |
+ MALLOCX_ZERO);
+ assert_zu_ne(nsz, 0,
+ "nallocx() error for alignment=%zu, "
+ "size=%zu (%#zx)", alignment, sz, sz);
+ ps[i] = mallocx(sz, MALLOCX_ALIGN(alignment) |
+ MALLOCX_ZERO);
+ assert_ptr_not_null(ps[i],
+ "mallocx() error for alignment=%zu, "
+ "size=%zu (%#zx)", alignment, sz, sz);
+ rsz = sallocx(ps[i], 0);
+ assert_zu_ge(rsz, sz,
+ "Real size smaller than expected for "
+ "alignment=%zu, size=%zu", alignment, sz);
+ assert_zu_eq(nsz, rsz,
+ "nallocx()/sallocx() size mismatch for "
+ "alignment=%zu, size=%zu", alignment, sz);
+ assert_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;
+ }
+ for (i = 0; i < NITER; i++) {
+ if (ps[i] != NULL) {
+ dallocx(ps[i], 0);
+ ps[i] = NULL;
+ }
+ }
+ }
+ }
+}
+TEST_END
+
+int
+main(void)
+{
+
+ return (test(
+ test_basic,
+ test_alignment_errors,
+ test_alignment_and_size));
+}
diff --git a/test/integration/rallocm.c b/test/integration/rallocm.c
index c13cd69..33c11bb 100644
--- a/test/integration/rallocm.c
+++ b/test/integration/rallocm.c
@@ -1,5 +1,3 @@
-#include <unistd.h>
-
#include "test/jemalloc_test.h"
TEST_BEGIN(test_same_size)
diff --git a/test/integration/rallocx.c b/test/integration/rallocx.c
new file mode 100644
index 0000000..cc9138b
--- /dev/null
+++ b/test/integration/rallocx.c
@@ -0,0 +1,51 @@
+#include "test/jemalloc_test.h"
+
+TEST_BEGIN(test_grow_and_shrink)
+{
+ void *p, *q;
+ size_t tsz;
+#define NCYCLES 3
+ unsigned i, j;
+#define NSZS 2500
+ size_t szs[NSZS];
+#define MAXSZ ZU(12 * 1024 * 1024)
+
+ p = mallocx(1, 0);
+ assert_ptr_not_null(p, "Unexpected mallocx() error");
+ 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);
+ assert_ptr_not_null(q,
+ "Unexpected rallocx() error for size=%zu-->%zu",
+ szs[j-1], szs[j-1]+1);
+ szs[j] = sallocx(q, 0);
+ assert_zu_ne(szs[j], szs[j-1]+1,
+ "Expected size to at least: %zu", szs[j-1]+1);
+ p = q;
+ }
+
+ for (j--; j > 0; j--) {
+ q = rallocx(p, szs[j-1], 0);
+ assert_ptr_not_null(q,
+ "Unexpected rallocx() error for size=%zu-->%zu",
+ szs[j], szs[j-1]);
+ tsz = sallocx(q, 0);
+ assert_zu_eq(tsz, szs[j-1],
+ "Expected size=%zu, got size=%zu", szs[j-1], tsz);
+ p = q;
+ }
+ }
+
+ dallocx(p, 0);
+}
+TEST_END
+
+int
+main(void)
+{
+
+ return (test(
+ test_grow_and_shrink));
+}
diff --git a/test/integration/xallocx.c b/test/integration/xallocx.c
new file mode 100644
index 0000000..ab4cf94
--- /dev/null
+++ b/test/integration/xallocx.c
@@ -0,0 +1,59 @@
+#include "test/jemalloc_test.h"
+
+TEST_BEGIN(test_same_size)
+{
+ void *p;
+ size_t sz, tsz;
+
+ p = mallocx(42, 0);
+ assert_ptr_not_null(p, "Unexpected mallocx() error");
+ sz = sallocx(p, 0);
+
+ tsz = xallocx(p, sz, 0, 0);
+ assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
+
+ dallocx(p, 0);
+}
+TEST_END
+
+TEST_BEGIN(test_extra_no_move)
+{
+ void *p;
+ size_t sz, tsz;
+
+ p = mallocx(42, 0);
+ assert_ptr_not_null(p, "Unexpected mallocx() error");
+ sz = sallocx(p, 0);
+
+ tsz = xallocx(p, sz, sz-42, 0);
+ assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
+
+ dallocx(p, 0);
+}
+TEST_END
+
+TEST_BEGIN(test_no_move_fail)
+{
+ void *p;
+ size_t sz, tsz;
+
+ p = mallocx(42, 0);
+ assert_ptr_not_null(p, "Unexpected mallocx() error");
+ sz = sallocx(p, 0);
+
+ tsz = xallocx(p, sz + 5, 0, 0);
+ assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
+
+ dallocx(p, 0);
+}
+TEST_END
+
+int
+main(void)
+{
+
+ return (test(
+ test_same_size,
+ test_extra_no_move,
+ test_no_move_fail));
+}
diff --git a/test/unit/mq.c b/test/unit/mq.c
index 01e72fd..e6cba10 100644
--- a/test/unit/mq.c
+++ b/test/unit/mq.c
@@ -39,8 +39,7 @@ thd_receiver_start(void *arg)
for (i = 0; i < (NSENDERS * NMSGS); i++) {
mq_msg_t *msg = mq_get(mq);
assert_ptr_not_null(msg, "mq_get() should never return NULL");
- assert_d_eq(jet_dallocm(msg, 0), ALLOCM_SUCCESS,
- "Unexpected dallocm() failure");
+ jet_dallocx(msg, 0);
}
return (NULL);
}
@@ -54,8 +53,8 @@ thd_sender_start(void *arg)
for (i = 0; i < NMSGS; i++) {
mq_msg_t *msg;
void *p;
- assert_d_eq(jet_allocm(&p, NULL, sizeof(mq_msg_t), 0),
- ALLOCM_SUCCESS, "Unexpected allocm() failure");
+ p = jet_mallocx(sizeof(mq_msg_t), 0);
+ assert_ptr_not_null(p, "Unexpected allocm() failure");
msg = (mq_msg_t *)p;
mq_put(mq, msg);
}