FreeRDP
Loading...
Searching...
No Matches
TestAlignment.c
1
2#include <stdio.h>
3#include <winpr/crt.h>
4#include <winpr/windows.h>
5
6int TestAlignment(int argc, char* argv[])
7{
8 void* ptr = NULL;
9 size_t alignment = 0;
10 size_t offset = 0;
11
12 WINPR_UNUSED(argc);
13 WINPR_UNUSED(argv);
14
15 /* Alignment should be 2^N where N is a positive integer */
16
17 alignment = 16;
18 offset = 8;
19
20 /* _aligned_malloc */
21
22 ptr = winpr_aligned_malloc(100, alignment);
23
24 if (ptr == NULL)
25 {
26 printf("Error allocating aligned memory.\n");
27 return -1;
28 }
29
30 if (((size_t)ptr % alignment) != 0)
31 {
32 printf("This pointer, %p, is not aligned on %" PRIuz "\n", ptr, alignment);
33 return -1;
34 }
35
36 /* _aligned_realloc */
37
38 ptr = winpr_aligned_realloc(ptr, 200, alignment);
39
40 if (((size_t)ptr % alignment) != 0)
41 {
42 printf("This pointer, %p, is not aligned on %" PRIuz "\n", ptr, alignment);
43 return -1;
44 }
45
46 winpr_aligned_free(ptr);
47
48 /* _aligned_offset_malloc */
49
50 ptr = winpr_aligned_offset_malloc(200, alignment, offset);
51
52 if (ptr == NULL)
53 {
54 printf("Error reallocating aligned offset memory.");
55 return -1;
56 }
57
58 if (((((size_t)ptr) + offset) % alignment) != 0)
59 {
60 printf("This pointer, %p, does not satisfy offset %" PRIuz " and alignment %" PRIuz "\n",
61 ptr, offset, alignment);
62 return -1;
63 }
64
65 /* _aligned_offset_realloc */
66
67 ptr = winpr_aligned_offset_realloc(ptr, 200, alignment, offset);
68
69 if (ptr == NULL)
70 {
71 printf("Error reallocating aligned offset memory.");
72 return -1;
73 }
74
75 if (((((size_t)ptr) + offset) % alignment) != 0)
76 {
77 printf("This pointer, %p, does not satisfy offset %" PRIuz " and alignment %" PRIuz "\n",
78 ptr, offset, alignment);
79 return -1;
80 }
81
82 /* _aligned_free works for both _aligned_malloc and _aligned_offset_malloc. free() should not be
83 * used. */
84 winpr_aligned_free(ptr);
85
86 return 0;
87}