memory/jemalloc/src/test/allocm.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 int r;
michael@0 13 void *p;
michael@0 14 size_t nsz, rsz, sz, alignment, total;
michael@0 15 unsigned i;
michael@0 16 void *ps[NITER];
michael@0 17
michael@0 18 malloc_printf("Test begin\n");
michael@0 19
michael@0 20 sz = 42;
michael@0 21 nsz = 0;
michael@0 22 r = nallocm(&nsz, sz, 0);
michael@0 23 if (r != ALLOCM_SUCCESS) {
michael@0 24 malloc_printf("Unexpected nallocm() error\n");
michael@0 25 abort();
michael@0 26 }
michael@0 27 rsz = 0;
michael@0 28 r = allocm(&p, &rsz, sz, 0);
michael@0 29 if (r != ALLOCM_SUCCESS) {
michael@0 30 malloc_printf("Unexpected allocm() error\n");
michael@0 31 abort();
michael@0 32 }
michael@0 33 if (rsz < sz)
michael@0 34 malloc_printf("Real size smaller than expected\n");
michael@0 35 if (nsz != rsz)
michael@0 36 malloc_printf("nallocm()/allocm() rsize mismatch\n");
michael@0 37 if (dallocm(p, 0) != ALLOCM_SUCCESS)
michael@0 38 malloc_printf("Unexpected dallocm() error\n");
michael@0 39
michael@0 40 r = allocm(&p, NULL, sz, 0);
michael@0 41 if (r != ALLOCM_SUCCESS) {
michael@0 42 malloc_printf("Unexpected allocm() error\n");
michael@0 43 abort();
michael@0 44 }
michael@0 45 if (dallocm(p, 0) != ALLOCM_SUCCESS)
michael@0 46 malloc_printf("Unexpected dallocm() error\n");
michael@0 47
michael@0 48 nsz = 0;
michael@0 49 r = nallocm(&nsz, sz, ALLOCM_ZERO);
michael@0 50 if (r != ALLOCM_SUCCESS) {
michael@0 51 malloc_printf("Unexpected nallocm() error\n");
michael@0 52 abort();
michael@0 53 }
michael@0 54 rsz = 0;
michael@0 55 r = allocm(&p, &rsz, sz, ALLOCM_ZERO);
michael@0 56 if (r != ALLOCM_SUCCESS) {
michael@0 57 malloc_printf("Unexpected allocm() error\n");
michael@0 58 abort();
michael@0 59 }
michael@0 60 if (nsz != rsz)
michael@0 61 malloc_printf("nallocm()/allocm() rsize mismatch\n");
michael@0 62 if (dallocm(p, 0) != ALLOCM_SUCCESS)
michael@0 63 malloc_printf("Unexpected dallocm() error\n");
michael@0 64
michael@0 65 #if LG_SIZEOF_PTR == 3
michael@0 66 alignment = UINT64_C(0x8000000000000000);
michael@0 67 sz = UINT64_C(0x8000000000000000);
michael@0 68 #else
michael@0 69 alignment = 0x80000000LU;
michael@0 70 sz = 0x80000000LU;
michael@0 71 #endif
michael@0 72 nsz = 0;
michael@0 73 r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
michael@0 74 if (r == ALLOCM_SUCCESS) {
michael@0 75 malloc_printf(
michael@0 76 "Expected error for nallocm(&nsz, %zu, %#x)\n",
michael@0 77 sz, ALLOCM_ALIGN(alignment));
michael@0 78 }
michael@0 79 rsz = 0;
michael@0 80 r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
michael@0 81 if (r == ALLOCM_SUCCESS) {
michael@0 82 malloc_printf(
michael@0 83 "Expected error for allocm(&p, %zu, %#x)\n",
michael@0 84 sz, ALLOCM_ALIGN(alignment));
michael@0 85 }
michael@0 86 if (nsz != rsz)
michael@0 87 malloc_printf("nallocm()/allocm() rsize mismatch\n");
michael@0 88
michael@0 89 #if LG_SIZEOF_PTR == 3
michael@0 90 alignment = UINT64_C(0x4000000000000000);
michael@0 91 sz = UINT64_C(0x8400000000000001);
michael@0 92 #else
michael@0 93 alignment = 0x40000000LU;
michael@0 94 sz = 0x84000001LU;
michael@0 95 #endif
michael@0 96 nsz = 0;
michael@0 97 r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
michael@0 98 if (r != ALLOCM_SUCCESS)
michael@0 99 malloc_printf("Unexpected nallocm() error\n");
michael@0 100 rsz = 0;
michael@0 101 r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
michael@0 102 if (r == ALLOCM_SUCCESS) {
michael@0 103 malloc_printf(
michael@0 104 "Expected error for allocm(&p, %zu, %#x)\n",
michael@0 105 sz, ALLOCM_ALIGN(alignment));
michael@0 106 }
michael@0 107
michael@0 108 alignment = 0x10LU;
michael@0 109 #if LG_SIZEOF_PTR == 3
michael@0 110 sz = UINT64_C(0xfffffffffffffff0);
michael@0 111 #else
michael@0 112 sz = 0xfffffff0LU;
michael@0 113 #endif
michael@0 114 nsz = 0;
michael@0 115 r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
michael@0 116 if (r == ALLOCM_SUCCESS) {
michael@0 117 malloc_printf(
michael@0 118 "Expected error for nallocm(&nsz, %zu, %#x)\n",
michael@0 119 sz, ALLOCM_ALIGN(alignment));
michael@0 120 }
michael@0 121 rsz = 0;
michael@0 122 r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
michael@0 123 if (r == ALLOCM_SUCCESS) {
michael@0 124 malloc_printf(
michael@0 125 "Expected error for allocm(&p, %zu, %#x)\n",
michael@0 126 sz, ALLOCM_ALIGN(alignment));
michael@0 127 }
michael@0 128 if (nsz != rsz)
michael@0 129 malloc_printf("nallocm()/allocm() rsize mismatch\n");
michael@0 130
michael@0 131 for (i = 0; i < NITER; i++)
michael@0 132 ps[i] = NULL;
michael@0 133
michael@0 134 for (alignment = 8;
michael@0 135 alignment <= MAXALIGN;
michael@0 136 alignment <<= 1) {
michael@0 137 total = 0;
michael@0 138 malloc_printf("Alignment: %zu\n", alignment);
michael@0 139 for (sz = 1;
michael@0 140 sz < 3 * alignment && sz < (1U << 31);
michael@0 141 sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
michael@0 142 for (i = 0; i < NITER; i++) {
michael@0 143 nsz = 0;
michael@0 144 r = nallocm(&nsz, sz,
michael@0 145 ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
michael@0 146 if (r != ALLOCM_SUCCESS) {
michael@0 147 malloc_printf(
michael@0 148 "nallocm() error for size %zu"
michael@0 149 " (%#zx): %d\n",
michael@0 150 sz, sz, r);
michael@0 151 exit(1);
michael@0 152 }
michael@0 153 rsz = 0;
michael@0 154 r = allocm(&ps[i], &rsz, sz,
michael@0 155 ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
michael@0 156 if (r != ALLOCM_SUCCESS) {
michael@0 157 malloc_printf(
michael@0 158 "allocm() error for size %zu"
michael@0 159 " (%#zx): %d\n",
michael@0 160 sz, sz, r);
michael@0 161 exit(1);
michael@0 162 }
michael@0 163 if (rsz < sz) {
michael@0 164 malloc_printf(
michael@0 165 "Real size smaller than"
michael@0 166 " expected\n");
michael@0 167 }
michael@0 168 if (nsz != rsz) {
michael@0 169 malloc_printf(
michael@0 170 "nallocm()/allocm() rsize"
michael@0 171 " mismatch\n");
michael@0 172 }
michael@0 173 if ((uintptr_t)p & (alignment-1)) {
michael@0 174 malloc_printf(
michael@0 175 "%p inadequately aligned for"
michael@0 176 " alignment: %zu\n", p, alignment);
michael@0 177 }
michael@0 178 sallocm(ps[i], &rsz, 0);
michael@0 179 total += rsz;
michael@0 180 if (total >= (MAXALIGN << 1))
michael@0 181 break;
michael@0 182 }
michael@0 183 for (i = 0; i < NITER; i++) {
michael@0 184 if (ps[i] != NULL) {
michael@0 185 dallocm(ps[i], 0);
michael@0 186 ps[i] = NULL;
michael@0 187 }
michael@0 188 }
michael@0 189 }
michael@0 190 }
michael@0 191
michael@0 192 malloc_printf("Test end\n");
michael@0 193 return (0);
michael@0 194 }

mercurial