libwinpr-crt: improve aligned memory tests

This commit is contained in:
Marc-André Moreau 2012-10-01 23:31:49 -04:00
parent ebbdc8dca9
commit f31b5c7f5d
3 changed files with 72 additions and 13 deletions

View File

@ -34,6 +34,16 @@ void* _aligned_malloc(size_t size, size_t alignment)
{
void* memptr;
/* alignment must be a power of 2 */
if (alignment % 2 == 1)
return NULL;
/* offset must be less than size */
if (offset >= size)
return NULL;
if (posix_memalign(&memptr, alignment, size) != 0)
return NULL;
@ -58,6 +68,21 @@ void* _aligned_offset_malloc(size_t size, size_t alignment, size_t offset)
{
void* memptr;
/* alignment must be a power of 2 */
if (alignment % 2 == 1)
return NULL;
/* offset must be less than size */
if (offset >= size)
return NULL;
/* minimum alignment is pointer size */
if (alignment < sizeof(void*))
alignment = sizeof(void*);
if (posix_memalign(&memptr, alignment, size) != 0)
return NULL;

View File

@ -25,19 +25,21 @@ int TestAlignment(int argc, char* argv[])
return -1;
}
if (((size_t) ptr % alignment) == 0)
printf("This pointer, %d, is aligned on %d\n", ptr, alignment);
else
if (((size_t) ptr % alignment) != 0)
{
printf("This pointer, %d, is not aligned on %d\n", ptr, alignment);
return -1;
}
/* _aligned_realloc */
ptr = _aligned_realloc(ptr, 200, alignment);
if (((size_t) ptr % alignment) == 0)
printf("This pointer, %d, is aligned on %d\n", ptr, alignment);
else
if (((size_t) ptr % alignment) != 0)
{
printf("This pointer, %d, is not aligned on %d\n", ptr, alignment);
return -1;
}
_aligned_free(ptr);
@ -51,10 +53,11 @@ int TestAlignment(int argc, char* argv[])
return -1;
}
if (((((size_t) ptr) + offset) % alignment) == 0)
printf("This pointer, %d, is offset by %d on alignment of %d\n", ptr, offset, alignment);
else
if (((((size_t) ptr) + offset) % alignment) != 0)
{
printf("This pointer, %d, does not satisfy offset %d and alignment %d\n", ptr, offset, alignment);
return -1;
}
/* _aligned_offset_realloc */
@ -66,10 +69,11 @@ int TestAlignment(int argc, char* argv[])
return -1;
}
if (((((size_t) ptr) + offset) % alignment) == 0)
printf("This pointer, %d, is offset by %d on alignment of %d\n", ptr, offset, alignment);
else
if (((((size_t) ptr) + offset) % alignment) != 0)
{
printf("This pointer, %d, does not satisfy offset %d and alignment %d\n", ptr, offset, alignment);
return -1;
}
/* _aligned_free works for both _aligned_malloc and _aligned_offset_malloc. free() should not be used. */
_aligned_free(ptr);

View File

@ -154,12 +154,42 @@ PSLIST_ENTRY InterlockedPopEntrySList(PSLIST_HEADER ListHead)
PSLIST_ENTRY InterlockedFlushSList(PSLIST_HEADER ListHead)
{
SLIST_HEADER old;
SLIST_HEADER new;
if (!QueryDepthSList(ListHead))
return NULL;
#ifdef _WIN64
new.s.Alignment = 0;
new.s.Region = 0;
new.HeaderX64.HeaderType = 1;
while (1)
{
old = *ListHead;
new.HeaderX64.Sequence = old.HeaderX64.Sequence + 1;
if (InterlockedCompareExchange64((LONGLONG*) ListHead, new.s.Alignment, old.s.Alignment))
{
InterlockedCompareExchange64(&((LONGLONG*) ListHead)[1], new.s.Region, old.s.Region);
break;
}
}
return (PSLIST_ENTRY) (((ULONG_PTR) old.HeaderX64.NextEntry) << 4);
#else
new.Alignment = 0;
do
{
old = *ListHead;
new.s.Sequence = old.s.Sequence + 1;
}
while(InterlockedCompareExchange64(&ListHead->Alignment, new.Alignment, old.Alignment) != old.Alignment);
return old.s.Next.Next;
#endif
return NULL;
}
USHORT QueryDepthSList(PSLIST_HEADER ListHead)