memory/jemalloc/src/test/aligned_alloc.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/memory/jemalloc/src/test/aligned_alloc.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,119 @@
     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 +	size_t alignment, size, total;
    1.16 +	unsigned i;
    1.17 +	void *p, *ps[NITER];
    1.18 +
    1.19 +	malloc_printf("Test begin\n");
    1.20 +
    1.21 +	/* Test error conditions. */
    1.22 +	alignment = 0;
    1.23 +	set_errno(0);
    1.24 +	p = aligned_alloc(alignment, 1);
    1.25 +	if (p != NULL || get_errno() != EINVAL) {
    1.26 +		malloc_printf(
    1.27 +		    "Expected error for invalid alignment %zu\n", alignment);
    1.28 +	}
    1.29 +
    1.30 +	for (alignment = sizeof(size_t); alignment < MAXALIGN;
    1.31 +	    alignment <<= 1) {
    1.32 +		set_errno(0);
    1.33 +		p = aligned_alloc(alignment + 1, 1);
    1.34 +		if (p != NULL || get_errno() != EINVAL) {
    1.35 +			malloc_printf(
    1.36 +			    "Expected error for invalid alignment %zu\n",
    1.37 +			    alignment + 1);
    1.38 +		}
    1.39 +	}
    1.40 +
    1.41 +#if LG_SIZEOF_PTR == 3
    1.42 +	alignment = UINT64_C(0x8000000000000000);
    1.43 +	size      = UINT64_C(0x8000000000000000);
    1.44 +#else
    1.45 +	alignment = 0x80000000LU;
    1.46 +	size      = 0x80000000LU;
    1.47 +#endif
    1.48 +	set_errno(0);
    1.49 +	p = aligned_alloc(alignment, size);
    1.50 +	if (p != NULL || get_errno() != ENOMEM) {
    1.51 +		malloc_printf(
    1.52 +		    "Expected error for aligned_alloc(%zu, %zu)\n",
    1.53 +		    alignment, size);
    1.54 +	}
    1.55 +
    1.56 +#if LG_SIZEOF_PTR == 3
    1.57 +	alignment = UINT64_C(0x4000000000000000);
    1.58 +	size      = UINT64_C(0x8400000000000001);
    1.59 +#else
    1.60 +	alignment = 0x40000000LU;
    1.61 +	size      = 0x84000001LU;
    1.62 +#endif
    1.63 +	set_errno(0);
    1.64 +	p = aligned_alloc(alignment, size);
    1.65 +	if (p != NULL || get_errno() != ENOMEM) {
    1.66 +		malloc_printf(
    1.67 +		    "Expected error for aligned_alloc(%zu, %zu)\n",
    1.68 +		    alignment, size);
    1.69 +	}
    1.70 +
    1.71 +	alignment = 0x10LU;
    1.72 +#if LG_SIZEOF_PTR == 3
    1.73 +	size = UINT64_C(0xfffffffffffffff0);
    1.74 +#else
    1.75 +	size = 0xfffffff0LU;
    1.76 +#endif
    1.77 +	set_errno(0);
    1.78 +	p = aligned_alloc(alignment, size);
    1.79 +	if (p != NULL || get_errno() != ENOMEM) {
    1.80 +		malloc_printf(
    1.81 +		    "Expected error for aligned_alloc(&p, %zu, %zu)\n",
    1.82 +		    alignment, size);
    1.83 +	}
    1.84 +
    1.85 +	for (i = 0; i < NITER; i++)
    1.86 +		ps[i] = NULL;
    1.87 +
    1.88 +	for (alignment = 8;
    1.89 +	    alignment <= MAXALIGN;
    1.90 +	    alignment <<= 1) {
    1.91 +		total = 0;
    1.92 +		malloc_printf("Alignment: %zu\n", alignment);
    1.93 +		for (size = 1;
    1.94 +		    size < 3 * alignment && size < (1U << 31);
    1.95 +		    size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
    1.96 +			for (i = 0; i < NITER; i++) {
    1.97 +				ps[i] = aligned_alloc(alignment, size);
    1.98 +				if (ps[i] == NULL) {
    1.99 +					char buf[BUFERROR_BUF];
   1.100 +
   1.101 +					buferror(buf, sizeof(buf));
   1.102 +					malloc_printf(
   1.103 +					    "Error for size %zu (%#zx): %s\n",
   1.104 +					    size, size, buf);
   1.105 +					exit(1);
   1.106 +				}
   1.107 +				total += malloc_usable_size(ps[i]);
   1.108 +				if (total >= (MAXALIGN << 1))
   1.109 +					break;
   1.110 +			}
   1.111 +			for (i = 0; i < NITER; i++) {
   1.112 +				if (ps[i] != NULL) {
   1.113 +					free(ps[i]);
   1.114 +					ps[i] = NULL;
   1.115 +				}
   1.116 +			}
   1.117 +		}
   1.118 +	}
   1.119 +
   1.120 +	malloc_printf("Test end\n");
   1.121 +	return (0);
   1.122 +}

mercurial