blob: a8d14dbfcd268492fd59e4d2f8c6cdfbab6f1462 [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>
njn83b62cb2009-04-15 03:12:43 +000017#include "tests/malloc.h"
nethercote2d5b8162004-08-11 09:40:52 +000018#include <errno.h>
19
20int main ( void )
21{
njn83b62cb2009-04-15 03:12:43 +000022# if defined(VGO_aix5)
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!
njn83b62cb2009-04-15 03:12:43 +000027 // DDD: (this is no longer true)
nethercote2d5b8162004-08-11 09:40:52 +000028 // Should work with both 32-bit and 64-bit pointers, though.
29
30 int* p;
31 int res;
32 assert(sizeof(long int) == sizeof(void*));
sewardjaed05362006-10-17 01:26:12 +000033
nethercote2d5b8162004-08-11 09:40:52 +000034 p = memalign(0, 100); assert(0 == (long)p % 8);
35 p = memalign(1, 100); assert(0 == (long)p % 8);
36 p = memalign(2, 100); assert(0 == (long)p % 8);
37 p = memalign(3, 100); assert(0 == (long)p % 8);
38 p = memalign(4, 100); assert(0 == (long)p % 8);
39 p = memalign(5, 100); assert(0 == (long)p % 8);
40
41 p = memalign(7, 100); assert(0 == (long)p % 8);
42 p = memalign(8, 100); assert(0 == (long)p % 8);
43 p = memalign(9, 100); assert(0 == (long)p % 16);
44
45 p = memalign(31, 100); assert(0 == (long)p % 32);
46 p = memalign(32, 100); assert(0 == (long)p % 32);
47 p = memalign(33, 100); assert(0 == (long)p % 64);
48
49 p = memalign(4095, 100); assert(0 == (long)p % 4096);
50 p = memalign(4096, 100); assert(0 == (long)p % 4096);
51 p = memalign(4097, 100); assert(0 == (long)p % 8192);
52
sewardjaed05362006-10-17 01:26:12 +000053# define PM(a,b,c) posix_memalign((void**)a, b, c)
nethercote2d5b8162004-08-11 09:40:52 +000054
nethercotec4702112004-09-03 14:04:40 +000055 res = PM(&p, -1,100); assert(EINVAL == res);
56 res = PM(&p, 0, 100); assert(0 == res && 0 == (long)p % 8);
57 res = PM(&p, 1, 100); assert(EINVAL == res);
58 res = PM(&p, 2, 100); assert(EINVAL == res);
59 res = PM(&p, 3, 100); assert(EINVAL == res);
60 res = PM(&p, sizeof(void*), 100);
61 assert(0 == res && 0 == (long)p % sizeof(void*));
62
63 res = PM(&p, 31, 100); assert(EINVAL == res);
64 res = PM(&p, 32, 100); assert(0 == res &&
nethercote2d5b8162004-08-11 09:40:52 +000065 0 == (long)p % 32);
nethercotec4702112004-09-03 14:04:40 +000066 res = PM(&p, 33, 100); assert(EINVAL == res);
nethercote2d5b8162004-08-11 09:40:52 +000067
nethercotec4702112004-09-03 14:04:40 +000068 res = PM(&p, 4095, 100); assert(EINVAL == res);
69 res = PM(&p, 4096, 100); assert(0 == res &&
nethercote2d5b8162004-08-11 09:40:52 +000070 0 == (long)p % 4096);
nethercotec4702112004-09-03 14:04:40 +000071 res = PM(&p, 4097, 100); assert(EINVAL == res);
sewardjaed05362006-10-17 01:26:12 +000072
73# endif
nethercote2d5b8162004-08-11 09:40:52 +000074
75 return 0;
76}