michael@0: #define JEMALLOC_MANGLE michael@0: #include "jemalloc_test.h" michael@0: michael@0: #define CHUNK 0x400000 michael@0: /* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */ michael@0: #define MAXALIGN ((size_t)0x2000000LU) michael@0: #define NITER 4 michael@0: michael@0: int michael@0: main(void) michael@0: { michael@0: int r; michael@0: void *p; michael@0: size_t nsz, rsz, sz, alignment, total; michael@0: unsigned i; michael@0: void *ps[NITER]; michael@0: michael@0: malloc_printf("Test begin\n"); michael@0: michael@0: sz = 42; michael@0: nsz = 0; michael@0: r = nallocm(&nsz, sz, 0); michael@0: if (r != ALLOCM_SUCCESS) { michael@0: malloc_printf("Unexpected nallocm() error\n"); michael@0: abort(); michael@0: } michael@0: rsz = 0; michael@0: r = allocm(&p, &rsz, sz, 0); michael@0: if (r != ALLOCM_SUCCESS) { michael@0: malloc_printf("Unexpected allocm() error\n"); michael@0: abort(); michael@0: } michael@0: if (rsz < sz) michael@0: malloc_printf("Real size smaller than expected\n"); michael@0: if (nsz != rsz) michael@0: malloc_printf("nallocm()/allocm() rsize mismatch\n"); michael@0: if (dallocm(p, 0) != ALLOCM_SUCCESS) michael@0: malloc_printf("Unexpected dallocm() error\n"); michael@0: michael@0: r = allocm(&p, NULL, sz, 0); michael@0: if (r != ALLOCM_SUCCESS) { michael@0: malloc_printf("Unexpected allocm() error\n"); michael@0: abort(); michael@0: } michael@0: if (dallocm(p, 0) != ALLOCM_SUCCESS) michael@0: malloc_printf("Unexpected dallocm() error\n"); michael@0: michael@0: nsz = 0; michael@0: r = nallocm(&nsz, sz, ALLOCM_ZERO); michael@0: if (r != ALLOCM_SUCCESS) { michael@0: malloc_printf("Unexpected nallocm() error\n"); michael@0: abort(); michael@0: } michael@0: rsz = 0; michael@0: r = allocm(&p, &rsz, sz, ALLOCM_ZERO); michael@0: if (r != ALLOCM_SUCCESS) { michael@0: malloc_printf("Unexpected allocm() error\n"); michael@0: abort(); michael@0: } michael@0: if (nsz != rsz) michael@0: malloc_printf("nallocm()/allocm() rsize mismatch\n"); michael@0: if (dallocm(p, 0) != ALLOCM_SUCCESS) michael@0: malloc_printf("Unexpected dallocm() error\n"); michael@0: michael@0: #if LG_SIZEOF_PTR == 3 michael@0: alignment = UINT64_C(0x8000000000000000); michael@0: sz = UINT64_C(0x8000000000000000); michael@0: #else michael@0: alignment = 0x80000000LU; michael@0: sz = 0x80000000LU; michael@0: #endif michael@0: nsz = 0; michael@0: r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment)); michael@0: if (r == ALLOCM_SUCCESS) { michael@0: malloc_printf( michael@0: "Expected error for nallocm(&nsz, %zu, %#x)\n", michael@0: sz, ALLOCM_ALIGN(alignment)); michael@0: } michael@0: rsz = 0; michael@0: r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment)); michael@0: if (r == ALLOCM_SUCCESS) { michael@0: malloc_printf( michael@0: "Expected error for allocm(&p, %zu, %#x)\n", michael@0: sz, ALLOCM_ALIGN(alignment)); michael@0: } michael@0: if (nsz != rsz) michael@0: malloc_printf("nallocm()/allocm() rsize mismatch\n"); michael@0: michael@0: #if LG_SIZEOF_PTR == 3 michael@0: alignment = UINT64_C(0x4000000000000000); michael@0: sz = UINT64_C(0x8400000000000001); michael@0: #else michael@0: alignment = 0x40000000LU; michael@0: sz = 0x84000001LU; michael@0: #endif michael@0: nsz = 0; michael@0: r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment)); michael@0: if (r != ALLOCM_SUCCESS) michael@0: malloc_printf("Unexpected nallocm() error\n"); michael@0: rsz = 0; michael@0: r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment)); michael@0: if (r == ALLOCM_SUCCESS) { michael@0: malloc_printf( michael@0: "Expected error for allocm(&p, %zu, %#x)\n", michael@0: sz, ALLOCM_ALIGN(alignment)); michael@0: } michael@0: michael@0: alignment = 0x10LU; michael@0: #if LG_SIZEOF_PTR == 3 michael@0: sz = UINT64_C(0xfffffffffffffff0); michael@0: #else michael@0: sz = 0xfffffff0LU; michael@0: #endif michael@0: nsz = 0; michael@0: r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment)); michael@0: if (r == ALLOCM_SUCCESS) { michael@0: malloc_printf( michael@0: "Expected error for nallocm(&nsz, %zu, %#x)\n", michael@0: sz, ALLOCM_ALIGN(alignment)); michael@0: } michael@0: rsz = 0; michael@0: r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment)); michael@0: if (r == ALLOCM_SUCCESS) { michael@0: malloc_printf( michael@0: "Expected error for allocm(&p, %zu, %#x)\n", michael@0: sz, ALLOCM_ALIGN(alignment)); michael@0: } michael@0: if (nsz != rsz) michael@0: malloc_printf("nallocm()/allocm() rsize mismatch\n"); michael@0: michael@0: for (i = 0; i < NITER; i++) michael@0: ps[i] = NULL; michael@0: michael@0: for (alignment = 8; michael@0: alignment <= MAXALIGN; michael@0: alignment <<= 1) { michael@0: total = 0; michael@0: malloc_printf("Alignment: %zu\n", alignment); michael@0: for (sz = 1; michael@0: sz < 3 * alignment && sz < (1U << 31); michael@0: sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) { michael@0: for (i = 0; i < NITER; i++) { michael@0: nsz = 0; michael@0: r = nallocm(&nsz, sz, michael@0: ALLOCM_ALIGN(alignment) | ALLOCM_ZERO); michael@0: if (r != ALLOCM_SUCCESS) { michael@0: malloc_printf( michael@0: "nallocm() error for size %zu" michael@0: " (%#zx): %d\n", michael@0: sz, sz, r); michael@0: exit(1); michael@0: } michael@0: rsz = 0; michael@0: r = allocm(&ps[i], &rsz, sz, michael@0: ALLOCM_ALIGN(alignment) | ALLOCM_ZERO); michael@0: if (r != ALLOCM_SUCCESS) { michael@0: malloc_printf( michael@0: "allocm() error for size %zu" michael@0: " (%#zx): %d\n", michael@0: sz, sz, r); michael@0: exit(1); michael@0: } michael@0: if (rsz < sz) { michael@0: malloc_printf( michael@0: "Real size smaller than" michael@0: " expected\n"); michael@0: } michael@0: if (nsz != rsz) { michael@0: malloc_printf( michael@0: "nallocm()/allocm() rsize" michael@0: " mismatch\n"); michael@0: } michael@0: if ((uintptr_t)p & (alignment-1)) { michael@0: malloc_printf( michael@0: "%p inadequately aligned for" michael@0: " alignment: %zu\n", p, alignment); michael@0: } michael@0: sallocm(ps[i], &rsz, 0); michael@0: total += rsz; michael@0: if (total >= (MAXALIGN << 1)) michael@0: break; michael@0: } michael@0: for (i = 0; i < NITER; i++) { michael@0: if (ps[i] != NULL) { michael@0: dallocm(ps[i], 0); michael@0: ps[i] = NULL; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: malloc_printf("Test end\n"); michael@0: return (0); michael@0: }