diff --git a/test_common/harness/alloc.h b/test_common/harness/alloc.h new file mode 100644 index 00000000..bcd16d5f --- /dev/null +++ b/test_common/harness/alloc.h @@ -0,0 +1,68 @@ +// +// Copyright (c) 2020 The Khronos Group Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef HARNESS_ALLOC_H_ +#define HARNESS_ALLOC_H_ + +#if defined(__linux__) || defined (linux) || defined(__APPLE__) +#if defined(__ANDROID__) +#include +#else +#include +#endif +#endif + +#if defined(__MINGW32__) +#include "mingw_compat.h" +#endif + +static void * align_malloc(size_t size, size_t alignment) +{ +#if defined(_WIN32) && defined(_MSC_VER) + return _aligned_malloc(size, alignment); +#elif defined(__linux__) || defined (linux) || defined(__APPLE__) + void * ptr = NULL; +#if defined(__ANDROID__) + ptr = memalign(alignment, size); + if ( ptr ) + return ptr; +#else + if (0 == posix_memalign(&ptr, alignment, size)) + return ptr; +#endif + return NULL; +#elif defined(__MINGW32__) + return __mingw_aligned_malloc(size, alignment); +#else + #error "Please add support OS for aligned malloc" +#endif +} + +static void align_free(void * ptr) +{ +#if defined(_WIN32) && defined(_MSC_VER) + _aligned_free(ptr); +#elif defined(__linux__) || defined (linux) || defined(__APPLE__) + return free(ptr); +#elif defined(__MINGW32__) + return __mingw_aligned_free(ptr); +#else + #error "Please add support OS for aligned free" +#endif +} + +#endif // #ifndef HARNESS_ALLOC_H_ + diff --git a/test_common/harness/imageHelpers.cpp b/test_common/harness/imageHelpers.cpp index 84fb2c9f..de61e355 100644 --- a/test_common/harness/imageHelpers.cpp +++ b/test_common/harness/imageHelpers.cpp @@ -1234,13 +1234,7 @@ char * generate_random_image_data( image_descriptor *imageInfo, BufferOwningPtr< } #else P.reset( NULL ); // Free already allocated memory first, then try to allocate new block. -#if defined (_WIN32) && defined(_MSC_VER) - char *data = (char *)_aligned_malloc(allocSize, get_pixel_size(imageInfo->format)); -#elif defined(__MINGW32__) - char *data = (char *)__mingw_aligned_malloc(allocSize, get_pixel_size(imageInfo->format)); -#else - char *data = (char *)memalign(get_pixel_size(imageInfo->format), allocSize); -#endif + char *data = (char *)align_malloc(allocSize, get_pixel_size(imageInfo->format)); P.reset(data,NULL,0,allocSize, true); #endif @@ -3163,14 +3157,7 @@ char *create_random_image_data( ExplicitType dataType, image_descriptor *imageIn P.reset(data); } #else -#if defined (_WIN32) && defined(_MSC_VER) - char *data = (char *)_aligned_malloc(allocSize, get_pixel_size(imageInfo->format)); -#elif defined(__MINGW32__) - char *data = (char *)__mingw_aligned_malloc(allocSize, get_pixel_size(imageInfo->format)); -#else - char *data = (char *)memalign(get_pixel_size(imageInfo->format), allocSize); -#endif - + char *data = (char *)align_malloc(allocSize, get_pixel_size(imageInfo->format)); P.reset(data,NULL,0,allocSize,true); #endif diff --git a/test_common/harness/kernelHelpers.cpp b/test_common/harness/kernelHelpers.cpp index 5b756e53..5ca4321b 100644 --- a/test_common/harness/kernelHelpers.cpp +++ b/test_common/harness/kernelHelpers.cpp @@ -29,10 +29,6 @@ #include #include -#if defined(__MINGW32__) -#include "mingw_compat.h" -#endif - #if defined(_WIN32) std::string slash = "\\"; #else @@ -1433,46 +1429,6 @@ int checkFor3DImageSupport( cl_device_id device ) return 0; } -void * align_malloc(size_t size, size_t alignment) -{ -#if defined(_WIN32) && defined(_MSC_VER) - return _aligned_malloc(size, alignment); -#elif defined(__linux__) || defined (linux) || defined(__APPLE__) - void * ptr = NULL; - // alignemnt must be a power of two and multiple of sizeof(void *). - if ( alignment < sizeof( void * ) ) - { - alignment = sizeof( void * ); - } -#if defined(__ANDROID__) - ptr = memalign(alignment, size); - if ( ptr ) - return ptr; -#else - if (0 == posix_memalign(&ptr, alignment, size)) - return ptr; -#endif - return NULL; -#elif defined(__MINGW32__) - return __mingw_aligned_malloc(size, alignment); -#else - #error "Please add support OS for aligned malloc" -#endif -} - -void align_free(void * ptr) -{ -#if defined(_WIN32) && defined(_MSC_VER) - _aligned_free(ptr); -#elif defined(__linux__) || defined (linux) || defined(__APPLE__) - return free(ptr); -#elif defined(__MINGW32__) - return __mingw_aligned_free(ptr); -#else - #error "Please add support OS for aligned free" -#endif -} - size_t get_min_alignment(cl_context context) { static cl_uint align_size = 0; diff --git a/test_common/harness/kernelHelpers.h b/test_common/harness/kernelHelpers.h index cb4c299e..d37b7add 100644 --- a/test_common/harness/kernelHelpers.h +++ b/test_common/harness/kernelHelpers.h @@ -38,6 +38,7 @@ #endif #include "deviceInfo.h" +#include "harness/alloc.h" #ifdef __cplusplus extern "C" { @@ -149,10 +150,6 @@ extern int checkFor3DImageSupport( cl_device_id device ); /* Checks that a given queue property is supported on the specified device. Returns 1 if supported, 0 if not or an error. */ extern int checkDeviceForQueueSupport( cl_device_id device, cl_command_queue_properties prop ); -/* Helper for aligned memory allocation */ -void * align_malloc(size_t size, size_t alignment); -void align_free(void *); - /* Helper to obtain the min alignment for a given context, i.e the max of all min alignments for devices attached to the context*/ size_t get_min_alignment(cl_context context); diff --git a/test_common/harness/mt19937.cpp b/test_common/harness/mt19937.cpp index baa19fb8..a4fbf591 100644 --- a/test_common/harness/mt19937.cpp +++ b/test_common/harness/mt19937.cpp @@ -48,47 +48,12 @@ #include #include "mt19937.h" #include "mingw_compat.h" +#include "harness/alloc.h" #ifdef __SSE2__ #include #endif -static void * align_malloc(size_t size, size_t alignment) -{ -#if defined(_WIN32) && defined(_MSC_VER) - return _aligned_malloc(size, alignment); -#elif defined(__linux__) || defined (linux) || defined(__APPLE__) - void * ptr = NULL; -#if defined(__ANDROID__) - ptr = memalign(alignment, size); - if ( ptr ) - return ptr; -#else - if (0 == posix_memalign(&ptr, alignment, size)) - return ptr; -#endif - return NULL; -#elif defined(__MINGW32__) - return __mingw_aligned_malloc(size, alignment); -#else - #error "Please add support OS for aligned malloc" -#endif -} - -static void align_free(void * ptr) -{ -#if defined(_WIN32) && defined(_MSC_VER) - _aligned_free(ptr); -#elif defined(__linux__) || defined (linux) || defined(__APPLE__) - return free(ptr); -#elif defined(__MINGW32__) - return __mingw_aligned_free(ptr); -#else - #error "Please add support OS for aligned free" -#endif -} - - /* Period parameters */ #define N 624 /* vector code requires multiple of 4 here */ #define M 397