1 #include "test/jemalloc_test.h" 2 3 #define CHUNK 0x400000 4 #define MAXALIGN (((size_t)1) << 25) 5 #define MAXSZ (((size_t)1) << 26) 6 #define NITER 4 7 8 TEST_BEGIN(test_basic) 9 { 10 size_t sz; 11 12 for (sz = 1; sz < MAXSZ; sz = nallocx(sz, 0) + 1) { 13 size_t nsz, rsz; 14 void *p; 15 nsz = nallocx(sz, 0); 16 assert_zu_ne(nsz, 0, "Unexpected nallocx() error"); 17 p = mallocx(sz, 0); 18 assert_ptr_not_null(p, "Unexpected mallocx() error"); 19 rsz = sallocx(p, 0); 20 assert_zu_ge(rsz, sz, "Real size smaller than expected"); 21 assert_zu_eq(nsz, rsz, "nallocx()/sallocx() size mismatch"); 22 dallocx(p, 0); 23 24 p = mallocx(sz, 0); 25 assert_ptr_not_null(p, "Unexpected mallocx() error"); 26 dallocx(p, 0); 27 28 nsz = nallocx(sz, MALLOCX_ZERO); 29 assert_zu_ne(nsz, 0, "Unexpected nallocx() error"); 30 p = mallocx(sz, MALLOCX_ZERO); 31 assert_ptr_not_null(p, "Unexpected mallocx() error"); 32 rsz = sallocx(p, 0); 33 assert_zu_eq(nsz, rsz, "nallocx()/sallocx() rsize mismatch"); 34 dallocx(p, 0); 35 } 36 } 37 TEST_END 38 39 TEST_BEGIN(test_alignment_and_size) 40 { 41 size_t nsz, rsz, sz, alignment, total; 42 unsigned i; 43 void *ps[NITER]; 44 45 for (i = 0; i < NITER; i++) 46 ps[i] = NULL; 47 48 for (alignment = 8; 49 alignment <= MAXALIGN; 50 alignment <<= 1) { 51 total = 0; 52 for (sz = 1; 53 sz < 3 * alignment && sz < (1U << 31); 54 sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) { 55 for (i = 0; i < NITER; i++) { 56 nsz = nallocx(sz, MALLOCX_ALIGN(alignment) | 57 MALLOCX_ZERO); 58 assert_zu_ne(nsz, 0, 59 "nallocx() error for alignment=%zu, " 60 "size=%zu (%#zx)", alignment, sz, sz); 61 ps[i] = mallocx(sz, MALLOCX_ALIGN(alignment) | 62 MALLOCX_ZERO); 63 assert_ptr_not_null(ps[i], 64 "mallocx() error for alignment=%zu, " 65 "size=%zu (%#zx)", alignment, sz, sz); 66 rsz = sallocx(ps[i], 0); 67 assert_zu_ge(rsz, sz, 68 "Real size smaller than expected for " 69 "alignment=%zu, size=%zu", alignment, sz); 70 assert_zu_eq(nsz, rsz, 71 "nallocx()/sallocx() size mismatch for " 72 "alignment=%zu, size=%zu", alignment, sz); 73 assert_ptr_null( 74 (void *)((uintptr_t)ps[i] & (alignment-1)), 75 "%p inadequately aligned for" 76 " alignment=%zu, size=%zu", ps[i], 77 alignment, sz); 78 total += rsz; 79 if (total >= (MAXALIGN << 1)) 80 break; 81 } 82 for (i = 0; i < NITER; i++) { 83 if (ps[i] != NULL) { 84 dallocx(ps[i], 0); 85 ps[i] = NULL; 86 } 87 } 88 } 89 } 90 } 91 TEST_END 92 93 int 94 main(void) 95 { 96 97 return (test( 98 test_basic, 99 test_alignment_and_size)); 100 } 101