memalign2.c revision e739ac0589b4fb43561f801c4faba8c1b89f8680
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 14#include <stdlib.h> 15#include <stdio.h> 16#include <assert.h> 17#include "tests/malloc.h" 18#include <errno.h> 19 20int main ( void ) 21{ 22# if defined(VGO_aix5) 23 // AIX 5.2 has neither memalign() nor posix_memalign(); do nothing. 24 25# elif defined(VGO_darwin) 26 // Likewise for Mac OS X. 27 28# else 29 // Nb: assuming VG_MIN_MALLOC_SZB is 8 or more... 30 int* p; 31 int res; 32 assert(sizeof(long int) == sizeof(void*)); 33 34 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 53# define PM(a,b,c) posix_memalign((void**)a, b, c) 54 55 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 && 65 0 == (long)p % 32); 66 res = PM(&p, 33, 100); assert(EINVAL == res); 67 68 res = PM(&p, 4095, 100); assert(EINVAL == res); 69 res = PM(&p, 4096, 100); assert(0 == res && 70 0 == (long)p % 4096); 71 res = PM(&p, 4097, 100); assert(EINVAL == res); 72 73# endif 74 75 return 0; 76} 77