libwinpr-crt: add tests for memory aligned allocation functions

This commit is contained in:
Marc-André Moreau 2012-10-01 19:10:00 -04:00
parent 423585a6a1
commit a80eeabc48
5 changed files with 134 additions and 4 deletions

View File

@ -38,3 +38,8 @@ else()
endif()
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR")
if(BUILD_TESTING)
add_subdirectory(test)
endif()

View File

@ -27,14 +27,26 @@
#ifndef _WIN32
#include <stdlib.h>
#include <malloc.h>
void* _aligned_malloc(size_t size, size_t alignment)
{
return NULL;
void* memptr;
if (posix_memalign(&memptr, alignment, size) != 0)
return NULL;
return memptr;
}
void* _aligned_realloc(void* memblock, size_t size, size_t alignment)
{
return NULL;
void* memptr = NULL;
memptr = realloc(memblock, size);
return memptr;
}
void* _aligned_recalloc(void* memblock, size_t num, size_t size, size_t alignment)
@ -44,7 +56,12 @@ void* _aligned_recalloc(void* memblock, size_t num, size_t size, size_t alignmen
void* _aligned_offset_malloc(size_t size, size_t alignment, size_t offset)
{
return NULL;
void* memptr;
if (posix_memalign(&memptr, alignment, size) != 0)
return NULL;
return memptr;
}
void* _aligned_offset_realloc(void* memblock, size_t size, size_t alignment, size_t offset)
@ -64,7 +81,7 @@ size_t _aligned_msize(void* memblock, size_t alignment, size_t offset)
void _aligned_free(void* memblock)
{
free(memblock);
}
#endif

3
winpr/libwinpr/crt/test/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
TestCrt
TestCrt.c

View File

@ -0,0 +1,26 @@
set(MODULE_NAME "TestCrt")
set(MODULE_PREFIX "TEST_CRT")
set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c)
set(${MODULE_PREFIX}_TESTS
TestAlignment.c)
create_test_sourcelist(${MODULE_PREFIX}_SRCS
${${MODULE_PREFIX}_DRIVER}
${${MODULE_PREFIX}_TESTS})
add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS})
if(NOT WIN32)
target_link_libraries(${MODULE_NAME} winpr-crt)
endif()
foreach(test ${${MODULE_PREFIX}_TESTS})
get_filename_component(TestName ${test} NAME_WE)
add_test(${TestName} ${EXECUTABLE_OUTPUT_PATH}/${MODULE_NAME} ${TestName})
endforeach()
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test")

View File

@ -0,0 +1,79 @@
#include <stdio.h>
#include <malloc.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;
}
if (((size_t) ptr % alignment) == 0)
printf("This pointer, %d, is aligned on %d\n", ptr, alignment);
else
printf("This pointer, %d, is not aligned on %d\n", ptr, alignment);
/* _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
printf("This pointer, %d, is not aligned on %d\n", ptr, alignment);
_aligned_free(ptr);
/* _aligned_offset_malloc */
ptr = _aligned_offset_malloc(200, alignment, offset);
if (ptr == NULL)
{
printf("Error reallocating aligned offset memory.");
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
printf("This pointer, %d, does not satisfy offset %d and alignment %d\n", ptr, offset, alignment);
/* _aligned_offset_realloc */
ptr = _aligned_offset_realloc(ptr, 200, alignment, offset);
if (ptr == NULL)
{
printf("Error reallocating aligned offset memory.");
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
printf("This pointer, %d, does not satisfy offset %d and alignment %d\n", ptr, offset, alignment);
/* _aligned_free works for both _aligned_malloc and _aligned_offset_malloc. free() should not be used. */
_aligned_free(ptr);
return 0;
}