Ticket #223: posix_memalign_test.c

File posix_memalign_test.c, 1.1 KB (added by dmik, 14 years ago)
Line 
1#include <stdio.h>
2#include <stdlib.h>
3
4void foo()
5{
6        void *p1, *p2, *p3;
7        int rc1 = posix_memalign(&p1, 16, 32);
8        int rc2 = posix_memalign(&p2, 64 * 1024, 64 * 1024 * 2);
9        //int rc3 = posix_memalign(&p3, 256 * 1024, 256 * 1024 * 2);
10 
11    printf("rc1 %d p1 %p\n", rc1, p1);
12    printf("rc2 %d p2 %p\n", rc2, p2);
13    //printf("rc3 %d p3 %p\n", rc3, p3);
14}
15
16void posix_memalign_test (size_t align)
17{
18        int rc = 0;
19        void *p;
20        size_t sz = align * 2;
21        size_t total_sz = 0;
22       
23        while (!rc)
24        {
25                rc = posix_memalign(&p, align, sz);
26                if (!rc)
27                {
28                        total_sz += sz;
29                        if (((int) p) & (align - 1))
30                        {
31                                printf ("ERROR: Returned pointer %p violates requested alignment 0x%x.\n",
32                                                p, align);
33                                break;
34                        }
35                       
36                        printf ("Allocated next %d bytes at %p.\n", sz, p);
37                }
38                else
39                {
40                        printf ("ERROR: Could not allocate next %d bytes. Error %d (%s).\n",
41                                        sz, errno, strerror(errno));
42                }
43        }
44                       
45        printf ("Total allocated size is %d bytes.\n", total_sz);
46}
47
48int main()
49{
50        //foo();
51        //foo();
52        posix_memalign_test(256 * 1024);
53    return 0;
54}