memory/jemalloc/src/test/aligned_alloc.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 void *p, *ps[NITER];
michael@0 15
michael@0 16 malloc_printf("Test begin\n");
michael@0 17
michael@0 18 /* Test error conditions. */
michael@0 19 alignment = 0;
michael@0 20 set_errno(0);
michael@0 21 p = aligned_alloc(alignment, 1);
michael@0 22 if (p != NULL || get_errno() != EINVAL) {
michael@0 23 malloc_printf(
michael@0 24 "Expected error for invalid alignment %zu\n", alignment);
michael@0 25 }
michael@0 26
michael@0 27 for (alignment = sizeof(size_t); alignment < MAXALIGN;
michael@0 28 alignment <<= 1) {
michael@0 29 set_errno(0);
michael@0 30 p = aligned_alloc(alignment + 1, 1);
michael@0 31 if (p != NULL || get_errno() != EINVAL) {
michael@0 32 malloc_printf(
michael@0 33 "Expected error for invalid alignment %zu\n",
michael@0 34 alignment + 1);
michael@0 35 }
michael@0 36 }
michael@0 37
michael@0 38 #if LG_SIZEOF_PTR == 3
michael@0 39 alignment = UINT64_C(0x8000000000000000);
michael@0 40 size = UINT64_C(0x8000000000000000);
michael@0 41 #else
michael@0 42 alignment = 0x80000000LU;
michael@0 43 size = 0x80000000LU;
michael@0 44 #endif
michael@0 45 set_errno(0);
michael@0 46 p = aligned_alloc(alignment, size);
michael@0 47 if (p != NULL || get_errno() != ENOMEM) {
michael@0 48 malloc_printf(
michael@0 49 "Expected error for aligned_alloc(%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 set_errno(0);
michael@0 61 p = aligned_alloc(alignment, size);
michael@0 62 if (p != NULL || get_errno() != ENOMEM) {
michael@0 63 malloc_printf(
michael@0 64 "Expected error for aligned_alloc(%zu, %zu)\n",
michael@0 65 alignment, size);
michael@0 66 }
michael@0 67
michael@0 68 alignment = 0x10LU;
michael@0 69 #if LG_SIZEOF_PTR == 3
michael@0 70 size = UINT64_C(0xfffffffffffffff0);
michael@0 71 #else
michael@0 72 size = 0xfffffff0LU;
michael@0 73 #endif
michael@0 74 set_errno(0);
michael@0 75 p = aligned_alloc(alignment, size);
michael@0 76 if (p != NULL || get_errno() != ENOMEM) {
michael@0 77 malloc_printf(
michael@0 78 "Expected error for aligned_alloc(&p, %zu, %zu)\n",
michael@0 79 alignment, size);
michael@0 80 }
michael@0 81
michael@0 82 for (i = 0; i < NITER; i++)
michael@0 83 ps[i] = NULL;
michael@0 84
michael@0 85 for (alignment = 8;
michael@0 86 alignment <= MAXALIGN;
michael@0 87 alignment <<= 1) {
michael@0 88 total = 0;
michael@0 89 malloc_printf("Alignment: %zu\n", alignment);
michael@0 90 for (size = 1;
michael@0 91 size < 3 * alignment && size < (1U << 31);
michael@0 92 size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
michael@0 93 for (i = 0; i < NITER; i++) {
michael@0 94 ps[i] = aligned_alloc(alignment, size);
michael@0 95 if (ps[i] == NULL) {
michael@0 96 char buf[BUFERROR_BUF];
michael@0 97
michael@0 98 buferror(buf, sizeof(buf));
michael@0 99 malloc_printf(
michael@0 100 "Error for size %zu (%#zx): %s\n",
michael@0 101 size, size, buf);
michael@0 102 exit(1);
michael@0 103 }
michael@0 104 total += malloc_usable_size(ps[i]);
michael@0 105 if (total >= (MAXALIGN << 1))
michael@0 106 break;
michael@0 107 }
michael@0 108 for (i = 0; i < NITER; i++) {
michael@0 109 if (ps[i] != NULL) {
michael@0 110 free(ps[i]);
michael@0 111 ps[i] = NULL;
michael@0 112 }
michael@0 113 }
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 malloc_printf("Test end\n");
michael@0 118 return (0);
michael@0 119 }

mercurial