blob: ccda426449424fc8c75bfc8d722a7e8d3216225f [file] [log] [blame]
nethercotec4702112004-09-03 14:04:40 +00001
2// These #defines attempt to ensure that posix_memalign() is declared, and
3// so no spurious warning is given about using it.
4
5// Advertise compliance of the code to the XSI (a POSIX superset that
6// defines what a system must be like to be called "UNIX")
7#undef _XOPEN_SOURCE
8#define _XOPEN_SOURCE 600
9
10// Advertise compliance to POSIX
11#undef _POSIX_C_SOURCE
12#define _POSIX_C_SOURCE 200112L
13
nethercote2d5b8162004-08-11 09:40:52 +000014#include <stdlib.h>
15#include <stdio.h>
16#include <assert.h>
17#include <malloc.h>
18#include <errno.h>
19
20int main ( void )
21{
njn685a9592009-02-23 07:17:08 +000022# if defined(_AIX)
njnb3027ea2009-02-26 22:09:53 +000023 // AIX 5.2 has neither memalign() nor posix_memalign(); do nothing.
njn685a9592009-02-23 07:17:08 +000024
25# else
nethercote2d5b8162004-08-11 09:40:52 +000026 // Nb: assuming VG_MIN_MALLOC_SZB is 8!
27 // Should work with both 32-bit and 64-bit pointers, though.
28
29 int* p;
30 int res;
31 assert(sizeof(long int) == sizeof(void*));
sewardjaed05362006-10-17 01:26:12 +000032
nethercote2d5b8162004-08-11 09:40:52 +000033 p = memalign(0, 100); assert(0 == (long)p % 8);
34 p = memalign(1, 100); assert(0 == (long)p % 8);
35 p = memalign(2, 100); assert(0 == (long)p % 8);
36 p = memalign(3, 100); assert(0 == (long)p % 8);
37 p = memalign(4, 100); assert(0 == (long)p % 8);
38 p = memalign(5, 100); assert(0 == (long)p % 8);
39
40 p = memalign(7, 100); assert(0 == (long)p % 8);
41 p = memalign(8, 100); assert(0 == (long)p % 8);
42 p = memalign(9, 100); assert(0 == (long)p % 16);
43
44 p = memalign(31, 100); assert(0 == (long)p % 32);
45 p = memalign(32, 100); assert(0 == (long)p % 32);
46 p = memalign(33, 100); assert(0 == (long)p % 64);
47
48 p = memalign(4095, 100); assert(0 == (long)p % 4096);
49 p = memalign(4096, 100); assert(0 == (long)p % 4096);
50 p = memalign(4097, 100); assert(0 == (long)p % 8192);
51
sewardjaed05362006-10-17 01:26:12 +000052# define PM(a,b,c) posix_memalign((void**)a, b, c)
nethercote2d5b8162004-08-11 09:40:52 +000053
nethercotec4702112004-09-03 14:04:40 +000054 res = PM(&p, -1,100); assert(EINVAL == res);
55 res = PM(&p, 0, 100); assert(0 == res && 0 == (long)p % 8);
56 res = PM(&p, 1, 100); assert(EINVAL == res);
57 res = PM(&p, 2, 100); assert(EINVAL == res);
58 res = PM(&p, 3, 100); assert(EINVAL == res);
59 res = PM(&p, sizeof(void*), 100);
60 assert(0 == res && 0 == (long)p % sizeof(void*));
61
62 res = PM(&p, 31, 100); assert(EINVAL == res);
63 res = PM(&p, 32, 100); assert(0 == res &&
nethercote2d5b8162004-08-11 09:40:52 +000064 0 == (long)p % 32);
nethercotec4702112004-09-03 14:04:40 +000065 res = PM(&p, 33, 100); assert(EINVAL == res);
nethercote2d5b8162004-08-11 09:40:52 +000066
nethercotec4702112004-09-03 14:04:40 +000067 res = PM(&p, 4095, 100); assert(EINVAL == res);
68 res = PM(&p, 4096, 100); assert(0 == res &&
nethercote2d5b8162004-08-11 09:40:52 +000069 0 == (long)p % 4096);
nethercotec4702112004-09-03 14:04:40 +000070 res = PM(&p, 4097, 100); assert(EINVAL == res);
sewardjaed05362006-10-17 01:26:12 +000071
72# endif
nethercote2d5b8162004-08-11 09:40:52 +000073
74 return 0;
75}