Remove duplicate definition of align_{malloc,free} (#631)

Also use it instead of duplicating the code.

Fixes #326

Signed-off-by: Kevin Petit <kevin.petit@arm.com>
This commit is contained in:
Kévin Petit
2020-02-28 12:22:38 +00:00
committed by GitHub
parent b95ef1ad04
commit b93c1df933
5 changed files with 72 additions and 99 deletions

View File

@@ -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 <malloc.h>
#else
#include <stdlib.h>
#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_

View File

@@ -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

View File

@@ -29,10 +29,6 @@
#include <sstream>
#include <iomanip>
#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;

View File

@@ -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);

View File

@@ -48,47 +48,12 @@
#include <stdlib.h>
#include "mt19937.h"
#include "mingw_compat.h"
#include "harness/alloc.h"
#ifdef __SSE2__
#include <emmintrin.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
}
/* Period parameters */
#define N 624 /* vector code requires multiple of 4 here */
#define M 397