1#include "test/jemalloc_test.h" 2 3#define CHUNK 0x400000 4/* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */ 5#define MAXALIGN ((size_t)0x2000000LU) 6#define NITER 4 7 8TEST_BEGIN(test_alignment_errors) 9{ 10 size_t alignment; 11 void *p; 12 13 for (alignment = 0; alignment < sizeof(void *); alignment++) { 14 assert_d_eq(posix_memalign(&p, alignment, 1), EINVAL, 15 "Expected error for invalid alignment %zu", 16 alignment); 17 } 18 19 for (alignment = sizeof(size_t); alignment < MAXALIGN; 20 alignment <<= 1) { 21 assert_d_ne(posix_memalign(&p, alignment + 1, 1), 0, 22 "Expected error for invalid alignment %zu", 23 alignment + 1); 24 } 25} 26TEST_END 27 28TEST_BEGIN(test_oom_errors) 29{ 30 size_t alignment, size; 31 void *p; 32 33#if LG_SIZEOF_PTR == 3 34 alignment = UINT64_C(0x8000000000000000); 35 size = UINT64_C(0x8000000000000000); 36#else 37 alignment = 0x80000000LU; 38 size = 0x80000000LU; 39#endif 40 assert_d_ne(posix_memalign(&p, alignment, size), 0, 41 "Expected error for posix_memalign(&p, %zu, %zu)", 42 alignment, size); 43 44#if LG_SIZEOF_PTR == 3 45 alignment = UINT64_C(0x4000000000000000); 46 size = UINT64_C(0xc000000000000001); 47#else 48 alignment = 0x40000000LU; 49 size = 0xc0000001LU; 50#endif 51 assert_d_ne(posix_memalign(&p, alignment, size), 0, 52 "Expected error for posix_memalign(&p, %zu, %zu)", 53 alignment, size); 54 55 alignment = 0x10LU; 56#if LG_SIZEOF_PTR == 3 57 size = UINT64_C(0xfffffffffffffff0); 58#else 59 size = 0xfffffff0LU; 60#endif 61 assert_d_ne(posix_memalign(&p, alignment, size), 0, 62 "Expected error for posix_memalign(&p, %zu, %zu)", 63 alignment, size); 64} 65TEST_END 66 67TEST_BEGIN(test_alignment_and_size) 68{ 69 size_t alignment, size, total; 70 unsigned i; 71 int err; 72 void *ps[NITER]; 73 74 for (i = 0; i < NITER; i++) 75 ps[i] = NULL; 76 77 for (alignment = 8; 78 alignment <= MAXALIGN; 79 alignment <<= 1) { 80 total = 0; 81 for (size = 1; 82 size < 3 * alignment && size < (1U << 31); 83 size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) { 84 for (i = 0; i < NITER; i++) { 85 err = posix_memalign(&ps[i], 86 alignment, size); 87 if (err) { 88 char buf[BUFERROR_BUF]; 89 90 buferror(get_errno(), buf, sizeof(buf)); 91 test_fail( 92 "Error for alignment=%zu, " 93 "size=%zu (%#zx): %s", 94 alignment, size, size, buf); 95 } 96 total += malloc_usable_size(ps[i]); 97 if (total >= (MAXALIGN << 1)) 98 break; 99 } 100 for (i = 0; i < NITER; i++) { 101 if (ps[i] != NULL) { 102 free(ps[i]); 103 ps[i] = NULL; 104 } 105 } 106 } 107 } 108} 109TEST_END 110 111int 112main(void) 113{ 114 115 return (test( 116 test_alignment_errors, 117 test_oom_errors, 118 test_alignment_and_size)); 119} 120