memory/jemalloc/src/test/allocm.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/memory/jemalloc/src/test/allocm.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,194 @@
     1.4 +#define	JEMALLOC_MANGLE
     1.5 +#include "jemalloc_test.h"
     1.6 +
     1.7 +#define CHUNK 0x400000
     1.8 +/* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */
     1.9 +#define MAXALIGN ((size_t)0x2000000LU)
    1.10 +#define NITER 4
    1.11 +
    1.12 +int
    1.13 +main(void)
    1.14 +{
    1.15 +	int r;
    1.16 +	void *p;
    1.17 +	size_t nsz, rsz, sz, alignment, total;
    1.18 +	unsigned i;
    1.19 +	void *ps[NITER];
    1.20 +
    1.21 +	malloc_printf("Test begin\n");
    1.22 +
    1.23 +	sz = 42;
    1.24 +	nsz = 0;
    1.25 +	r = nallocm(&nsz, sz, 0);
    1.26 +	if (r != ALLOCM_SUCCESS) {
    1.27 +		malloc_printf("Unexpected nallocm() error\n");
    1.28 +		abort();
    1.29 +	}
    1.30 +	rsz = 0;
    1.31 +	r = allocm(&p, &rsz, sz, 0);
    1.32 +	if (r != ALLOCM_SUCCESS) {
    1.33 +		malloc_printf("Unexpected allocm() error\n");
    1.34 +		abort();
    1.35 +	}
    1.36 +	if (rsz < sz)
    1.37 +		malloc_printf("Real size smaller than expected\n");
    1.38 +	if (nsz != rsz)
    1.39 +		malloc_printf("nallocm()/allocm() rsize mismatch\n");
    1.40 +	if (dallocm(p, 0) != ALLOCM_SUCCESS)
    1.41 +		malloc_printf("Unexpected dallocm() error\n");
    1.42 +
    1.43 +	r = allocm(&p, NULL, sz, 0);
    1.44 +	if (r != ALLOCM_SUCCESS) {
    1.45 +		malloc_printf("Unexpected allocm() error\n");
    1.46 +		abort();
    1.47 +	}
    1.48 +	if (dallocm(p, 0) != ALLOCM_SUCCESS)
    1.49 +		malloc_printf("Unexpected dallocm() error\n");
    1.50 +
    1.51 +	nsz = 0;
    1.52 +	r = nallocm(&nsz, sz, ALLOCM_ZERO);
    1.53 +	if (r != ALLOCM_SUCCESS) {
    1.54 +		malloc_printf("Unexpected nallocm() error\n");
    1.55 +		abort();
    1.56 +	}
    1.57 +	rsz = 0;
    1.58 +	r = allocm(&p, &rsz, sz, ALLOCM_ZERO);
    1.59 +	if (r != ALLOCM_SUCCESS) {
    1.60 +		malloc_printf("Unexpected allocm() error\n");
    1.61 +		abort();
    1.62 +	}
    1.63 +	if (nsz != rsz)
    1.64 +		malloc_printf("nallocm()/allocm() rsize mismatch\n");
    1.65 +	if (dallocm(p, 0) != ALLOCM_SUCCESS)
    1.66 +		malloc_printf("Unexpected dallocm() error\n");
    1.67 +
    1.68 +#if LG_SIZEOF_PTR == 3
    1.69 +	alignment = UINT64_C(0x8000000000000000);
    1.70 +	sz        = UINT64_C(0x8000000000000000);
    1.71 +#else
    1.72 +	alignment = 0x80000000LU;
    1.73 +	sz        = 0x80000000LU;
    1.74 +#endif
    1.75 +	nsz = 0;
    1.76 +	r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
    1.77 +	if (r == ALLOCM_SUCCESS) {
    1.78 +		malloc_printf(
    1.79 +		    "Expected error for nallocm(&nsz, %zu, %#x)\n",
    1.80 +		    sz, ALLOCM_ALIGN(alignment));
    1.81 +	}
    1.82 +	rsz = 0;
    1.83 +	r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
    1.84 +	if (r == ALLOCM_SUCCESS) {
    1.85 +		malloc_printf(
    1.86 +		    "Expected error for allocm(&p, %zu, %#x)\n",
    1.87 +		    sz, ALLOCM_ALIGN(alignment));
    1.88 +	}
    1.89 +	if (nsz != rsz)
    1.90 +		malloc_printf("nallocm()/allocm() rsize mismatch\n");
    1.91 +
    1.92 +#if LG_SIZEOF_PTR == 3
    1.93 +	alignment = UINT64_C(0x4000000000000000);
    1.94 +	sz        = UINT64_C(0x8400000000000001);
    1.95 +#else
    1.96 +	alignment = 0x40000000LU;
    1.97 +	sz        = 0x84000001LU;
    1.98 +#endif
    1.99 +	nsz = 0;
   1.100 +	r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
   1.101 +	if (r != ALLOCM_SUCCESS)
   1.102 +		malloc_printf("Unexpected nallocm() error\n");
   1.103 +	rsz = 0;
   1.104 +	r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
   1.105 +	if (r == ALLOCM_SUCCESS) {
   1.106 +		malloc_printf(
   1.107 +		    "Expected error for allocm(&p, %zu, %#x)\n",
   1.108 +		    sz, ALLOCM_ALIGN(alignment));
   1.109 +	}
   1.110 +
   1.111 +	alignment = 0x10LU;
   1.112 +#if LG_SIZEOF_PTR == 3
   1.113 +	sz = UINT64_C(0xfffffffffffffff0);
   1.114 +#else
   1.115 +	sz = 0xfffffff0LU;
   1.116 +#endif
   1.117 +	nsz = 0;
   1.118 +	r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
   1.119 +	if (r == ALLOCM_SUCCESS) {
   1.120 +		malloc_printf(
   1.121 +		    "Expected error for nallocm(&nsz, %zu, %#x)\n",
   1.122 +		    sz, ALLOCM_ALIGN(alignment));
   1.123 +	}
   1.124 +	rsz = 0;
   1.125 +	r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
   1.126 +	if (r == ALLOCM_SUCCESS) {
   1.127 +		malloc_printf(
   1.128 +		    "Expected error for allocm(&p, %zu, %#x)\n",
   1.129 +		    sz, ALLOCM_ALIGN(alignment));
   1.130 +	}
   1.131 +	if (nsz != rsz)
   1.132 +		malloc_printf("nallocm()/allocm() rsize mismatch\n");
   1.133 +
   1.134 +	for (i = 0; i < NITER; i++)
   1.135 +		ps[i] = NULL;
   1.136 +
   1.137 +	for (alignment = 8;
   1.138 +	    alignment <= MAXALIGN;
   1.139 +	    alignment <<= 1) {
   1.140 +		total = 0;
   1.141 +		malloc_printf("Alignment: %zu\n", alignment);
   1.142 +		for (sz = 1;
   1.143 +		    sz < 3 * alignment && sz < (1U << 31);
   1.144 +		    sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
   1.145 +			for (i = 0; i < NITER; i++) {
   1.146 +				nsz = 0;
   1.147 +				r = nallocm(&nsz, sz,
   1.148 +				    ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
   1.149 +				if (r != ALLOCM_SUCCESS) {
   1.150 +					malloc_printf(
   1.151 +					    "nallocm() error for size %zu"
   1.152 +					    " (%#zx): %d\n",
   1.153 +					    sz, sz, r);
   1.154 +					exit(1);
   1.155 +				}
   1.156 +				rsz = 0;
   1.157 +				r = allocm(&ps[i], &rsz, sz,
   1.158 +				    ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
   1.159 +				if (r != ALLOCM_SUCCESS) {
   1.160 +					malloc_printf(
   1.161 +					    "allocm() error for size %zu"
   1.162 +					    " (%#zx): %d\n",
   1.163 +					    sz, sz, r);
   1.164 +					exit(1);
   1.165 +				}
   1.166 +				if (rsz < sz) {
   1.167 +					malloc_printf(
   1.168 +					    "Real size smaller than"
   1.169 +					    " expected\n");
   1.170 +				}
   1.171 +				if (nsz != rsz) {
   1.172 +					malloc_printf(
   1.173 +					    "nallocm()/allocm() rsize"
   1.174 +					    " mismatch\n");
   1.175 +				}
   1.176 +				if ((uintptr_t)p & (alignment-1)) {
   1.177 +					malloc_printf(
   1.178 +					    "%p inadequately aligned for"
   1.179 +					    " alignment: %zu\n", p, alignment);
   1.180 +				}
   1.181 +				sallocm(ps[i], &rsz, 0);
   1.182 +				total += rsz;
   1.183 +				if (total >= (MAXALIGN << 1))
   1.184 +					break;
   1.185 +			}
   1.186 +			for (i = 0; i < NITER; i++) {
   1.187 +				if (ps[i] != NULL) {
   1.188 +					dallocm(ps[i], 0);
   1.189 +					ps[i] = NULL;
   1.190 +				}
   1.191 +			}
   1.192 +		}
   1.193 +	}
   1.194 +
   1.195 +	malloc_printf("Test end\n");
   1.196 +	return (0);
   1.197 +}

mercurial