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.
|
|
|
|
*/
|
|
|
|
|
2023-03-24 14:43:44 +03:00
|
|
|
#include <stdlib.h>
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2012-09-20 05:42:46 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
|
|
|
/* Data Alignment: http://msdn.microsoft.com/en-us/library/fs9stz4e/ */
|
|
|
|
|
2022-06-23 08:57:38 +03:00
|
|
|
#if !defined(_WIN32) || (defined(__MINGW32__) && !defined(_UCRT))
|
2012-09-20 05:42:46 +04:00
|
|
|
|
2018-10-24 11:59:54 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define WINPR_ALIGNED_MEM_SIGNATURE 0x0BA0BAB
|
2014-07-10 04:10:33 +04:00
|
|
|
|
|
|
|
#define WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(_memptr) \
|
2024-08-29 16:09:58 +03:00
|
|
|
(WINPR_ALIGNED_MEM*)(((size_t)(((BYTE*)(_memptr)) - sizeof(WINPR_ALIGNED_MEM))))
|
2014-07-08 23:07:19 +04:00
|
|
|
|
2012-10-02 03:10:00 +04:00
|
|
|
#include <stdlib.h>
|
2012-10-09 21:04:41 +04:00
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("crt")
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-07-10 04:10:33 +04:00
|
|
|
struct winpr_aligned_mem
|
2013-08-15 21:13:02 +04:00
|
|
|
{
|
2014-07-08 23:07:19 +04:00
|
|
|
UINT32 sig;
|
2013-06-19 21:07:31 +04:00
|
|
|
size_t size;
|
2014-08-18 21:34:47 +04:00
|
|
|
void* base_addr;
|
2013-06-18 20:43:59 +04:00
|
|
|
};
|
2014-07-10 04:10:33 +04:00
|
|
|
typedef struct winpr_aligned_mem WINPR_ALIGNED_MEM;
|
2012-10-02 03:10:00 +04:00
|
|
|
|
2022-05-11 12:28:55 +03:00
|
|
|
void* winpr_aligned_malloc(size_t size, size_t alignment)
|
2013-06-18 20:43:59 +04:00
|
|
|
{
|
2022-05-11 12:28:55 +03:00
|
|
|
return winpr_aligned_offset_malloc(size, alignment, 0);
|
2012-09-20 05:42:46 +04:00
|
|
|
}
|
|
|
|
|
2023-03-24 14:43:44 +03:00
|
|
|
void* winpr_aligned_calloc(size_t count, size_t size, size_t alignment)
|
|
|
|
{
|
|
|
|
return winpr_aligned_recalloc(NULL, count, size, alignment);
|
|
|
|
}
|
|
|
|
|
2022-05-11 12:28:55 +03:00
|
|
|
void* winpr_aligned_realloc(void* memblock, size_t size, size_t alignment)
|
2012-09-20 05:42:46 +04:00
|
|
|
{
|
2022-05-11 12:28:55 +03:00
|
|
|
return winpr_aligned_offset_realloc(memblock, size, alignment, 0);
|
2012-09-20 05:42:46 +04:00
|
|
|
}
|
|
|
|
|
2022-05-11 12:28:55 +03:00
|
|
|
void* winpr_aligned_recalloc(void* memblock, size_t num, size_t size, size_t alignment)
|
2012-09-20 05:42:46 +04:00
|
|
|
{
|
2022-05-11 12:28:55 +03:00
|
|
|
return winpr_aligned_offset_recalloc(memblock, num, size, alignment, 0);
|
2012-09-20 05:42:46 +04:00
|
|
|
}
|
|
|
|
|
2022-05-11 12:28:55 +03:00
|
|
|
void* winpr_aligned_offset_malloc(size_t size, size_t alignment, size_t offset)
|
2012-09-20 05:42:46 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
size_t header = 0;
|
|
|
|
size_t alignsize = 0;
|
|
|
|
uintptr_t basesize = 0;
|
|
|
|
void* base = NULL;
|
|
|
|
void* memblock = NULL;
|
|
|
|
WINPR_ALIGNED_MEM* pMem = NULL;
|
2012-10-02 03:10:00 +04:00
|
|
|
|
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 */
|
2014-08-18 21:34:47 +04:00
|
|
|
if (alignment < sizeof(void*))
|
|
|
|
alignment = sizeof(void*);
|
2012-10-02 07:31:49 +04:00
|
|
|
|
2018-10-24 11:59:54 +03:00
|
|
|
if (alignment > SIZE_MAX - sizeof(WINPR_ALIGNED_MEM))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
header = sizeof(WINPR_ALIGNED_MEM) + alignment;
|
|
|
|
|
|
|
|
if (size > SIZE_MAX - header)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
alignsize = size + header;
|
2013-06-18 20:43:59 +04:00
|
|
|
/* malloc size + alignment to make sure we can align afterwards */
|
2023-03-24 14:43:44 +03:00
|
|
|
#if defined(_ISOC11_SOURCE)
|
|
|
|
base = aligned_alloc(alignment, alignsize);
|
2024-03-19 11:51:11 +03:00
|
|
|
#elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) || (_XOPEN_SOURCE >= 600)
|
2023-03-24 14:43:44 +03:00
|
|
|
if (posix_memalign(&base, alignment, alignsize) != 0)
|
|
|
|
return NULL;
|
|
|
|
#else
|
2018-10-24 11:59:54 +03:00
|
|
|
base = malloc(alignsize);
|
2023-03-24 14:43:44 +03:00
|
|
|
#endif
|
2014-07-10 04:10:33 +04:00
|
|
|
if (!base)
|
2012-10-02 03:10:00 +04:00
|
|
|
return NULL;
|
2013-06-18 20:43:59 +04:00
|
|
|
|
2018-10-24 11:59:54 +03:00
|
|
|
basesize = (uintptr_t)base;
|
|
|
|
|
|
|
|
if ((offset > UINTPTR_MAX) || (header > UINTPTR_MAX - offset) ||
|
|
|
|
(basesize > UINTPTR_MAX - header - offset))
|
|
|
|
{
|
|
|
|
free(base);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memblock = (void*)(((basesize + header + offset) & ~(alignment - 1)) - offset);
|
2014-07-10 04:10:33 +04:00
|
|
|
pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);
|
|
|
|
pMem->sig = WINPR_ALIGNED_MEM_SIGNATURE;
|
|
|
|
pMem->base_addr = base;
|
|
|
|
pMem->size = size;
|
|
|
|
return memblock;
|
2012-09-20 05:42:46 +04:00
|
|
|
}
|
|
|
|
|
2022-05-11 12:28:55 +03:00
|
|
|
void* winpr_aligned_offset_realloc(void* memblock, size_t size, size_t alignment, size_t offset)
|
2012-09-20 05:42:46 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
size_t copySize = 0;
|
|
|
|
void* newMemblock = NULL;
|
|
|
|
WINPR_ALIGNED_MEM* pMem = NULL;
|
|
|
|
WINPR_ALIGNED_MEM* pNewMem = NULL;
|
2013-06-18 20:43:59 +04:00
|
|
|
|
|
|
|
if (!memblock)
|
2022-05-11 12:28:55 +03:00
|
|
|
return winpr_aligned_offset_malloc(size, alignment, offset);
|
2013-06-18 20:43:59 +04:00
|
|
|
|
2015-03-30 15:21:11 +03:00
|
|
|
pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);
|
2018-10-24 11:59:54 +03:00
|
|
|
|
2015-03-30 15:21:11 +03:00
|
|
|
if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"_aligned_offset_realloc: memory block was not allocated by _aligned_malloc!");
|
2015-03-30 15:21:11 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-18 20:43:59 +04:00
|
|
|
if (size == 0)
|
|
|
|
{
|
2022-05-11 12:28:55 +03:00
|
|
|
winpr_aligned_free(memblock);
|
2013-06-18 20:43:59 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-15 21:13:02 +04:00
|
|
|
|
2022-05-11 12:28:55 +03:00
|
|
|
newMemblock = winpr_aligned_offset_malloc(size, alignment, offset);
|
2018-10-24 11:59:54 +03:00
|
|
|
|
2014-07-10 04:10:33 +04:00
|
|
|
if (!newMemblock)
|
2013-06-18 20:43:59 +04:00
|
|
|
return NULL;
|
|
|
|
|
2014-07-10 04:10:33 +04:00
|
|
|
pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock);
|
|
|
|
copySize = (pNewMem->size < pMem->size) ? pNewMem->size : pMem->size;
|
|
|
|
CopyMemory(newMemblock, memblock, copySize);
|
2022-05-11 12:28:55 +03:00
|
|
|
winpr_aligned_free(memblock);
|
2014-07-10 04:10:33 +04:00
|
|
|
return newMemblock;
|
2012-09-20 05:42:46 +04:00
|
|
|
}
|
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
static INLINE size_t cMIN(size_t a, size_t b)
|
|
|
|
{
|
|
|
|
if (a > b)
|
|
|
|
return b;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2022-05-11 12:28:55 +03:00
|
|
|
void* winpr_aligned_offset_recalloc(void* memblock, size_t num, size_t size, size_t alignment,
|
2022-06-23 08:57:38 +03:00
|
|
|
size_t offset)
|
2012-09-20 05:42:46 +04:00
|
|
|
{
|
2022-04-28 00:26:28 +03:00
|
|
|
char* newMemblock = NULL;
|
2020-11-20 14:37:21 +03:00
|
|
|
WINPR_ALIGNED_MEM* pMem = NULL;
|
|
|
|
WINPR_ALIGNED_MEM* pNewMem = NULL;
|
2014-07-10 04:10:33 +04:00
|
|
|
|
|
|
|
if (!memblock)
|
2015-03-30 15:21:11 +03:00
|
|
|
{
|
2022-05-11 12:28:55 +03:00
|
|
|
newMemblock = winpr_aligned_offset_malloc(size * num, alignment, offset);
|
2018-10-24 11:59:54 +03:00
|
|
|
|
2015-03-30 15:21:11 +03:00
|
|
|
if (newMemblock)
|
|
|
|
{
|
|
|
|
pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock);
|
|
|
|
ZeroMemory(newMemblock, pNewMem->size);
|
|
|
|
}
|
2018-10-24 11:59:54 +03:00
|
|
|
|
2020-09-07 12:05:45 +03:00
|
|
|
return newMemblock;
|
2015-03-30 15:21:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);
|
2018-10-24 11:59:54 +03:00
|
|
|
|
2015-03-30 15:21:11 +03:00
|
|
|
if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"_aligned_offset_recalloc: memory block was not allocated by _aligned_malloc!");
|
2020-11-20 14:37:21 +03:00
|
|
|
goto fail;
|
2015-03-30 15:21:11 +03:00
|
|
|
}
|
2014-07-10 04:10:33 +04:00
|
|
|
|
2023-06-12 11:03:37 +03:00
|
|
|
if ((num == 0) || (size == 0))
|
2020-11-20 14:37:21 +03:00
|
|
|
goto fail;
|
2014-07-10 04:10:33 +04:00
|
|
|
|
2023-03-24 14:43:44 +03:00
|
|
|
if (pMem->size > (1ull * num * size) + alignment)
|
|
|
|
return memblock;
|
|
|
|
|
2022-05-11 12:28:55 +03:00
|
|
|
newMemblock = winpr_aligned_offset_malloc(size * num, alignment, offset);
|
2014-07-10 04:10:33 +04:00
|
|
|
|
|
|
|
if (!newMemblock)
|
2020-11-20 14:37:21 +03:00
|
|
|
goto fail;
|
2014-07-10 04:10:33 +04:00
|
|
|
|
|
|
|
pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock);
|
2020-11-20 14:37:21 +03:00
|
|
|
{
|
2021-07-29 11:07:04 +03:00
|
|
|
const size_t csize = cMIN(pMem->size, pNewMem->size);
|
2023-06-12 11:03:37 +03:00
|
|
|
memcpy(newMemblock, memblock, csize);
|
2021-07-29 11:07:04 +03:00
|
|
|
ZeroMemory(newMemblock + csize, pNewMem->size - csize);
|
2020-11-20 14:37:21 +03:00
|
|
|
}
|
|
|
|
fail:
|
2022-05-11 12:28:55 +03:00
|
|
|
winpr_aligned_free(memblock);
|
2014-07-10 04:10:33 +04:00
|
|
|
return newMemblock;
|
2012-09-20 05:42:46 +04:00
|
|
|
}
|
|
|
|
|
2022-05-11 12:28:55 +03:00
|
|
|
size_t winpr_aligned_msize(void* memblock, size_t alignment, size_t offset)
|
2012-09-20 05:42:46 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
WINPR_ALIGNED_MEM* pMem = NULL;
|
2014-07-10 04:10:33 +04:00
|
|
|
|
|
|
|
if (!memblock)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);
|
|
|
|
|
|
|
|
if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "_aligned_msize: memory block was not allocated by _aligned_malloc!");
|
2014-07-10 04:10:33 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pMem->size;
|
2012-09-20 05:42:46 +04:00
|
|
|
}
|
|
|
|
|
2022-05-11 12:28:55 +03:00
|
|
|
void winpr_aligned_free(void* memblock)
|
2012-09-20 05:42:46 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
WINPR_ALIGNED_MEM* pMem = NULL;
|
2013-08-15 21:13:02 +04:00
|
|
|
|
2013-06-18 20:43:59 +04:00
|
|
|
if (!memblock)
|
|
|
|
return;
|
|
|
|
|
2014-07-10 04:10:33 +04:00
|
|
|
pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);
|
2013-06-18 20:43:59 +04:00
|
|
|
|
2014-07-10 04:10:33 +04:00
|
|
|
if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE)
|
2014-07-08 23:07:19 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "_aligned_free: memory block was not allocated by _aligned_malloc!");
|
2014-07-08 23:07:19 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-10 04:10:33 +04:00
|
|
|
free(pMem->base_addr);
|
2012-09-20 05:42:46 +04:00
|
|
|
}
|
|
|
|
|
2022-05-04 23:29:16 +03:00
|
|
|
#endif /* _WIN32 */
|