TestAlignment.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <stdio.h>
  2. #include <winpr/crt.h>
  3. #include <winpr/windows.h>
  4. int TestAlignment(int argc, char* argv[])
  5. {
  6. void* ptr;
  7. size_t alignment;
  8. size_t offset;
  9. /* Alignment should be 2^N where N is a positive integer */
  10. alignment = 16;
  11. offset = 5;
  12. /* _aligned_malloc */
  13. ptr = _aligned_malloc(100, alignment);
  14. if (ptr == NULL)
  15. {
  16. printf("Error allocating aligned memory.\n");
  17. return -1;
  18. }
  19. if (((size_t)ptr % alignment) != 0)
  20. {
  21. printf("This pointer, %p, is not aligned on %" PRIuz "\n", ptr, alignment);
  22. return -1;
  23. }
  24. /* _aligned_realloc */
  25. ptr = _aligned_realloc(ptr, 200, alignment);
  26. if (((size_t)ptr % alignment) != 0)
  27. {
  28. printf("This pointer, %p, is not aligned on %" PRIuz "\n", ptr, alignment);
  29. return -1;
  30. }
  31. _aligned_free(ptr);
  32. /* _aligned_offset_malloc */
  33. ptr = _aligned_offset_malloc(200, alignment, offset);
  34. if (ptr == NULL)
  35. {
  36. printf("Error reallocating aligned offset memory.");
  37. return -1;
  38. }
  39. if (((((size_t)ptr) + offset) % alignment) != 0)
  40. {
  41. printf("This pointer, %p, does not satisfy offset %" PRIuz " and alignment %" PRIuz "\n",
  42. ptr, offset, alignment);
  43. return -1;
  44. }
  45. /* _aligned_offset_realloc */
  46. ptr = _aligned_offset_realloc(ptr, 200, alignment, offset);
  47. if (ptr == NULL)
  48. {
  49. printf("Error reallocating aligned offset memory.");
  50. return -1;
  51. }
  52. if (((((size_t)ptr) + offset) % alignment) != 0)
  53. {
  54. printf("This pointer, %p, does not satisfy offset %" PRIuz " and alignment %" PRIuz "\n",
  55. ptr, offset, alignment);
  56. return -1;
  57. }
  58. /* _aligned_free works for both _aligned_malloc and _aligned_offset_malloc. free() should not be
  59. * used. */
  60. _aligned_free(ptr);
  61. return 0;
  62. }