Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #define JEMALLOC_MANGLE |
michael@0 | 2 | #include "jemalloc_test.h" |
michael@0 | 3 | |
michael@0 | 4 | #define CHUNK 0x400000 |
michael@0 | 5 | /* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */ |
michael@0 | 6 | #define MAXALIGN ((size_t)0x2000000LU) |
michael@0 | 7 | #define NITER 4 |
michael@0 | 8 | |
michael@0 | 9 | int |
michael@0 | 10 | main(void) |
michael@0 | 11 | { |
michael@0 | 12 | size_t alignment, size, total; |
michael@0 | 13 | unsigned i; |
michael@0 | 14 | int err; |
michael@0 | 15 | void *p, *ps[NITER]; |
michael@0 | 16 | |
michael@0 | 17 | malloc_printf("Test begin\n"); |
michael@0 | 18 | |
michael@0 | 19 | /* Test error conditions. */ |
michael@0 | 20 | for (alignment = 0; alignment < sizeof(void *); alignment++) { |
michael@0 | 21 | err = posix_memalign(&p, alignment, 1); |
michael@0 | 22 | if (err != EINVAL) { |
michael@0 | 23 | malloc_printf( |
michael@0 | 24 | "Expected error for invalid alignment %zu\n", |
michael@0 | 25 | alignment); |
michael@0 | 26 | } |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | for (alignment = sizeof(size_t); alignment < MAXALIGN; |
michael@0 | 30 | alignment <<= 1) { |
michael@0 | 31 | err = posix_memalign(&p, alignment + 1, 1); |
michael@0 | 32 | if (err == 0) { |
michael@0 | 33 | malloc_printf( |
michael@0 | 34 | "Expected error for invalid alignment %zu\n", |
michael@0 | 35 | alignment + 1); |
michael@0 | 36 | } |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | #if LG_SIZEOF_PTR == 3 |
michael@0 | 40 | alignment = UINT64_C(0x8000000000000000); |
michael@0 | 41 | size = UINT64_C(0x8000000000000000); |
michael@0 | 42 | #else |
michael@0 | 43 | alignment = 0x80000000LU; |
michael@0 | 44 | size = 0x80000000LU; |
michael@0 | 45 | #endif |
michael@0 | 46 | err = posix_memalign(&p, alignment, size); |
michael@0 | 47 | if (err == 0) { |
michael@0 | 48 | malloc_printf( |
michael@0 | 49 | "Expected error for posix_memalign(&p, %zu, %zu)\n", |
michael@0 | 50 | alignment, size); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | #if LG_SIZEOF_PTR == 3 |
michael@0 | 54 | alignment = UINT64_C(0x4000000000000000); |
michael@0 | 55 | size = UINT64_C(0x8400000000000001); |
michael@0 | 56 | #else |
michael@0 | 57 | alignment = 0x40000000LU; |
michael@0 | 58 | size = 0x84000001LU; |
michael@0 | 59 | #endif |
michael@0 | 60 | err = posix_memalign(&p, alignment, size); |
michael@0 | 61 | if (err == 0) { |
michael@0 | 62 | malloc_printf( |
michael@0 | 63 | "Expected error for posix_memalign(&p, %zu, %zu)\n", |
michael@0 | 64 | alignment, size); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | alignment = 0x10LU; |
michael@0 | 68 | #if LG_SIZEOF_PTR == 3 |
michael@0 | 69 | size = UINT64_C(0xfffffffffffffff0); |
michael@0 | 70 | #else |
michael@0 | 71 | size = 0xfffffff0LU; |
michael@0 | 72 | #endif |
michael@0 | 73 | err = posix_memalign(&p, alignment, size); |
michael@0 | 74 | if (err == 0) { |
michael@0 | 75 | malloc_printf( |
michael@0 | 76 | "Expected error for posix_memalign(&p, %zu, %zu)\n", |
michael@0 | 77 | alignment, size); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | for (i = 0; i < NITER; i++) |
michael@0 | 81 | ps[i] = NULL; |
michael@0 | 82 | |
michael@0 | 83 | for (alignment = 8; |
michael@0 | 84 | alignment <= MAXALIGN; |
michael@0 | 85 | alignment <<= 1) { |
michael@0 | 86 | total = 0; |
michael@0 | 87 | malloc_printf("Alignment: %zu\n", alignment); |
michael@0 | 88 | for (size = 1; |
michael@0 | 89 | size < 3 * alignment && size < (1U << 31); |
michael@0 | 90 | size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) { |
michael@0 | 91 | for (i = 0; i < NITER; i++) { |
michael@0 | 92 | err = posix_memalign(&ps[i], |
michael@0 | 93 | alignment, size); |
michael@0 | 94 | if (err) { |
michael@0 | 95 | malloc_printf( |
michael@0 | 96 | "Error for size %zu (%#zx): %s\n", |
michael@0 | 97 | size, size, strerror(err)); |
michael@0 | 98 | exit(1); |
michael@0 | 99 | } |
michael@0 | 100 | total += malloc_usable_size(ps[i]); |
michael@0 | 101 | if (total >= (MAXALIGN << 1)) |
michael@0 | 102 | break; |
michael@0 | 103 | } |
michael@0 | 104 | for (i = 0; i < NITER; i++) { |
michael@0 | 105 | if (ps[i] != NULL) { |
michael@0 | 106 | free(ps[i]); |
michael@0 | 107 | ps[i] = NULL; |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | } |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | malloc_printf("Test end\n"); |
michael@0 | 114 | return (0); |
michael@0 | 115 | } |