nethercote | c470211 | 2004-09-03 14:04:40 +0000 | [diff] [blame] | 1 | |
| 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 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
| 15 | #include <stdio.h> |
| 16 | #include <assert.h> |
| 17 | #include <malloc.h> |
| 18 | #include <errno.h> |
| 19 | |
| 20 | int main ( void ) |
| 21 | { |
njn | 685a959 | 2009-02-23 07:17:08 +0000 | [diff] [blame] | 22 | # if defined(_AIX) |
njn | b3027ea | 2009-02-26 22:09:53 +0000 | [diff] [blame^] | 23 | // AIX 5.2 has neither memalign() nor posix_memalign(); do nothing. |
njn | 685a959 | 2009-02-23 07:17:08 +0000 | [diff] [blame] | 24 | |
| 25 | # else |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 26 | // 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*)); |
sewardj | aed0536 | 2006-10-17 01:26:12 +0000 | [diff] [blame] | 32 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 33 | 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 | |
sewardj | aed0536 | 2006-10-17 01:26:12 +0000 | [diff] [blame] | 52 | # define PM(a,b,c) posix_memalign((void**)a, b, c) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 53 | |
nethercote | c470211 | 2004-09-03 14:04:40 +0000 | [diff] [blame] | 54 | 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 && |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 64 | 0 == (long)p % 32); |
nethercote | c470211 | 2004-09-03 14:04:40 +0000 | [diff] [blame] | 65 | res = PM(&p, 33, 100); assert(EINVAL == res); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 66 | |
nethercote | c470211 | 2004-09-03 14:04:40 +0000 | [diff] [blame] | 67 | res = PM(&p, 4095, 100); assert(EINVAL == res); |
| 68 | res = PM(&p, 4096, 100); assert(0 == res && |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 69 | 0 == (long)p % 4096); |
nethercote | c470211 | 2004-09-03 14:04:40 +0000 | [diff] [blame] | 70 | res = PM(&p, 4097, 100); assert(EINVAL == res); |
sewardj | aed0536 | 2006-10-17 01:26:12 +0000 | [diff] [blame] | 71 | |
| 72 | # endif |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 73 | |
| 74 | return 0; |
| 75 | } |