2012-09-20 05:42:46 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Data Alignment
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
|
|
|
/* Data Alignment: http://msdn.microsoft.com/en-us/library/fs9stz4e/ */
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
2012-10-02 03:10:00 +04:00
|
|
|
#include <stdlib.h>
|
2012-10-09 21:04:41 +04:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <malloc/malloc.h>
|
|
|
|
#else
|
2012-10-02 03:10:00 +04:00
|
|
|
#include <malloc.h>
|
2012-10-09 21:04:41 +04:00
|
|
|
#endif
|
2012-10-02 03:10:00 +04:00
|
|
|
|
2012-09-20 05:42:46 +04:00
|
|
|
void* _aligned_malloc(size_t size, size_t alignment)
|
|
|
|
{
|
2012-10-02 03:10:00 +04:00
|
|
|
void* memptr;
|
|
|
|
|
2012-10-02 07:31:49 +04:00
|
|
|
/* alignment must be a power of 2 */
|
|
|
|
|
|
|
|
if (alignment % 2 == 1)
|
|
|
|
return NULL;
|
|
|
|
|
2012-10-14 04:31:01 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
memptr = memalign(alignment, size);
|
|
|
|
#else
|
2012-10-02 03:10:00 +04:00
|
|
|
if (posix_memalign(&memptr, alignment, size) != 0)
|
|
|
|
return NULL;
|
2012-10-14 04:31:01 +04:00
|
|
|
#endif
|
2012-10-02 03:10:00 +04:00
|
|
|
|
|
|
|
return memptr;
|
2012-09-20 05:42:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void* _aligned_realloc(void* memblock, size_t size, size_t alignment)
|
|
|
|
{
|
2012-10-02 03:10:00 +04:00
|
|
|
void* memptr = NULL;
|
|
|
|
|
|
|
|
memptr = realloc(memblock, size);
|
|
|
|
|
|
|
|
return memptr;
|
2012-09-20 05:42:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void* _aligned_recalloc(void* memblock, size_t num, size_t size, size_t alignment)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* _aligned_offset_malloc(size_t size, size_t alignment, size_t offset)
|
|
|
|
{
|
2012-10-02 03:10:00 +04:00
|
|
|
void* memptr;
|
|
|
|
|
2012-10-02 07:31:49 +04:00
|
|
|
/* 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*);
|
|
|
|
|
2012-10-14 04:31:01 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
memptr = memalign(alignment, size);
|
|
|
|
#else
|
2012-10-02 03:10:00 +04:00
|
|
|
if (posix_memalign(&memptr, alignment, size) != 0)
|
|
|
|
return NULL;
|
2012-10-14 04:31:01 +04:00
|
|
|
#endif
|
2012-10-02 03:10:00 +04:00
|
|
|
|
|
|
|
return memptr;
|
2012-09-20 05:42:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void* _aligned_offset_realloc(void* memblock, size_t size, size_t alignment, size_t offset)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* _aligned_offset_recalloc(void* memblock, size_t num, size_t size, size_t alignment, size_t offset)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t _aligned_msize(void* memblock, size_t alignment, size_t offset)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _aligned_free(void* memblock)
|
|
|
|
{
|
2012-10-02 03:10:00 +04:00
|
|
|
free(memblock);
|
2012-09-20 05:42:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|