vulkan/util: add vk_default_allocator

We cannot use os_{malloc,free,realloc}_aligned because
os_realloc_aligned needs the old size (for memcpy).

v2: no max_align_t on MSVC

Signed-off-by: Chia-I Wu <olvaffe@gmail.com>
Acked-by: Jason Ekstrand <jason@jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11117>
This commit is contained in:
Chia-I Wu 2021-06-01 09:30:04 -07:00 committed by Marge Bot
parent 5054f74ce1
commit d3acc73455
3 changed files with 60 additions and 0 deletions

View file

@ -40,6 +40,7 @@ vk_extensions_gen = files('vk_extensions_gen.py')
vk_icd_gen = files('vk_icd_gen.py')
files_vulkan_util = files(
'vk_alloc.c',
'vk_alloc.h',
'vk_cmd_copy.c',
'vk_debug_report.c',

View file

@ -0,0 +1,55 @@
/*
* Copyright 2021 Google LLC
* SPDX-License-Identifier: MIT
*/
#include "vk_alloc.h"
#include <stdlib.h>
#if __STDC_VERSION__ >= 201112L && !defined(_MSC_VER)
#include <stddef.h>
#define MAX_ALIGN alignof(max_align_t)
#else
/* long double might be 128-bit, but our callers do not need that anyway(?) */
#include <stdint.h>
#define MAX_ALIGN alignof(uint64_t)
#endif
static VKAPI_ATTR void * VKAPI_CALL
vk_default_alloc(void *pUserData,
size_t size,
size_t alignment,
VkSystemAllocationScope allocationScope)
{
assert(MAX_ALIGN % alignment == 0);
return malloc(size);
}
static VKAPI_ATTR void * VKAPI_CALL
vk_default_realloc(void *pUserData,
void *pOriginal,
size_t size,
size_t alignment,
VkSystemAllocationScope allocationScope)
{
assert(MAX_ALIGN % alignment == 0);
return realloc(pOriginal, size);
}
static VKAPI_ATTR void VKAPI_CALL
vk_default_free(void *pUserData, void *pMemory)
{
free(pMemory);
}
const VkAllocationCallbacks *
vk_default_allocator(void)
{
static const VkAllocationCallbacks allocator = {
.pfnAllocation = vk_default_alloc,
.pfnReallocation = vk_default_realloc,
.pfnFree = vk_default_free,
};
return &allocator;
}

View file

@ -31,6 +31,10 @@
#include "util/u_math.h"
#include "util/macros.h"
const VkAllocationCallbacks *
vk_default_allocator(void);
static inline void *
vk_alloc(const VkAllocationCallbacks *alloc,
size_t size, size_t align,