]> git.saurik.com Git - redis.git/blame - deps/jemalloc/test/posix_memalign.c
Whitespace
[redis.git] / deps / jemalloc / test / posix_memalign.c
CommitLineData
a78e148b 1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <string.h>
6
7#define JEMALLOC_MANGLE
8#include "jemalloc_test.h"
9
10#define CHUNK 0x400000
11/* #define MAXALIGN ((size_t)0x80000000000LLU) */
12#define MAXALIGN ((size_t)0x2000000LLU)
13#define NITER 4
14
15int
16main(void)
17{
18 size_t alignment, size, total;
19 unsigned i;
20 int err;
21 void *p, *ps[NITER];
22
23 fprintf(stderr, "Test begin\n");
24
25 /* Test error conditions. */
26 for (alignment = 0; alignment < sizeof(void *); alignment++) {
27 err = JEMALLOC_P(posix_memalign)(&p, alignment, 1);
28 if (err != EINVAL) {
29 fprintf(stderr,
30 "Expected error for invalid alignment %zu\n",
31 alignment);
32 }
33 }
34
35 for (alignment = sizeof(size_t); alignment < MAXALIGN;
36 alignment <<= 1) {
37 err = JEMALLOC_P(posix_memalign)(&p, alignment + 1, 1);
38 if (err == 0) {
39 fprintf(stderr,
40 "Expected error for invalid alignment %zu\n",
41 alignment + 1);
42 }
43 }
44
45#if LG_SIZEOF_PTR == 3
46 alignment = 0x8000000000000000LLU;
47 size = 0x8000000000000000LLU;
48#else
49 alignment = 0x80000000LU;
50 size = 0x80000000LU;
51#endif
52 err = JEMALLOC_P(posix_memalign)(&p, alignment, size);
53 if (err == 0) {
54 fprintf(stderr,
55 "Expected error for posix_memalign(&p, %zu, %zu)\n",
56 alignment, size);
57 }
58
59#if LG_SIZEOF_PTR == 3
60 alignment = 0x4000000000000000LLU;
61 size = 0x8400000000000001LLU;
62#else
63 alignment = 0x40000000LU;
64 size = 0x84000001LU;
65#endif
66 err = JEMALLOC_P(posix_memalign)(&p, alignment, size);
67 if (err == 0) {
68 fprintf(stderr,
69 "Expected error for posix_memalign(&p, %zu, %zu)\n",
70 alignment, size);
71 }
72
73 alignment = 0x10LLU;
74#if LG_SIZEOF_PTR == 3
75 size = 0xfffffffffffffff0LLU;
76#else
77 size = 0xfffffff0LU;
78#endif
79 err = JEMALLOC_P(posix_memalign)(&p, alignment, size);
80 if (err == 0) {
81 fprintf(stderr,
82 "Expected error for posix_memalign(&p, %zu, %zu)\n",
83 alignment, size);
84 }
85
86 for (i = 0; i < NITER; i++)
87 ps[i] = NULL;
88
89 for (alignment = 8;
90 alignment <= MAXALIGN;
91 alignment <<= 1) {
92 total = 0;
93 fprintf(stderr, "Alignment: %zu\n", alignment);
94 for (size = 1;
95 size < 3 * alignment && size < (1U << 31);
96 size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
97 for (i = 0; i < NITER; i++) {
98 err = JEMALLOC_P(posix_memalign)(&ps[i],
99 alignment, size);
100 if (err) {
101 fprintf(stderr,
102 "Error for size %zu (0x%zx): %s\n",
103 size, size, strerror(err));
104 exit(1);
105 }
106 total += JEMALLOC_P(malloc_usable_size)(ps[i]);
107 if (total >= (MAXALIGN << 1))
108 break;
109 }
110 for (i = 0; i < NITER; i++) {
111 if (ps[i] != NULL) {
112 JEMALLOC_P(free)(ps[i]);
113 ps[i] = NULL;
114 }
115 }
116 }
117 }
118
119 fprintf(stderr, "Test end\n");
120 return (0);
121}