2012-10-02 03:10:00 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/windows.h>
|
|
|
|
|
|
|
|
int TestAlignment(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
void* ptr;
|
|
|
|
size_t alignment;
|
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
/* Alignment should be 2^N where N is a positive integer */
|
|
|
|
|
|
|
|
alignment = 16;
|
|
|
|
offset = 5;
|
|
|
|
|
|
|
|
/* _aligned_malloc */
|
|
|
|
|
|
|
|
ptr = _aligned_malloc(100, alignment);
|
|
|
|
|
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
|
|
|
printf("Error allocating aligned memory.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (((size_t)ptr % alignment) != 0)
|
2012-10-02 07:31:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("This pointer, %p, is not aligned on %" PRIuz "\n", ptr, alignment);
|
2012-10-02 07:31:49 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-10-02 03:10:00 +04:00
|
|
|
|
|
|
|
/* _aligned_realloc */
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-10-02 03:10:00 +04:00
|
|
|
ptr = _aligned_realloc(ptr, 200, alignment);
|
2019-11-06 17:24:51 +03:00
|
|
|
|
|
|
|
if (((size_t)ptr % alignment) != 0)
|
2012-10-02 07:31:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("This pointer, %p, is not aligned on %" PRIuz "\n", ptr, alignment);
|
2012-10-02 07:31:49 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-10-02 03:10:00 +04:00
|
|
|
_aligned_free(ptr);
|
|
|
|
|
|
|
|
/* _aligned_offset_malloc */
|
|
|
|
|
|
|
|
ptr = _aligned_offset_malloc(200, alignment, offset);
|
|
|
|
|
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
|
|
|
printf("Error reallocating aligned offset memory.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (((((size_t)ptr) + offset) % alignment) != 0)
|
2012-10-02 07:31:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("This pointer, %p, does not satisfy offset %" PRIuz " and alignment %" PRIuz "\n",
|
|
|
|
ptr, offset, alignment);
|
2012-10-02 07:31:49 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-10-02 03:10:00 +04:00
|
|
|
|
|
|
|
/* _aligned_offset_realloc */
|
|
|
|
|
|
|
|
ptr = _aligned_offset_realloc(ptr, 200, alignment, offset);
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-10-02 03:10:00 +04:00
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
|
|
|
printf("Error reallocating aligned offset memory.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (((((size_t)ptr) + offset) % alignment) != 0)
|
2012-10-02 07:31:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("This pointer, %p, does not satisfy offset %" PRIuz " and alignment %" PRIuz "\n",
|
|
|
|
ptr, offset, alignment);
|
2012-10-02 07:31:49 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-10-02 03:10:00 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
/* _aligned_free works for both _aligned_malloc and _aligned_offset_malloc. free() should not be
|
|
|
|
* used. */
|
2012-10-02 03:10:00 +04:00
|
|
|
_aligned_free(ptr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|