1 /*
2 * Copyright 2021 Google LLC
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "vk_alloc.h"
7
8 #include <stdlib.h>
9
10 #if __STDC_VERSION__ >= 201112L && !defined(_MSC_VER)
11 #include <stddef.h>
12 #define MAX_ALIGN alignof(max_align_t)
13 #else
14 /* long double might be 128-bit, but our callers do not need that anyway(?) */
15 #include <stdint.h>
16 #define MAX_ALIGN alignof(uint64_t)
17 #endif
18
19 static VKAPI_ATTR void * VKAPI_CALL
vk_default_alloc(void * pUserData,size_t size,size_t alignment,VkSystemAllocationScope allocationScope)20 vk_default_alloc(void *pUserData,
21 size_t size,
22 size_t alignment,
23 VkSystemAllocationScope allocationScope)
24 {
25 assert(MAX_ALIGN % alignment == 0);
26 return malloc(size);
27 }
28
29 static VKAPI_ATTR void * VKAPI_CALL
vk_default_realloc(void * pUserData,void * pOriginal,size_t size,size_t alignment,VkSystemAllocationScope allocationScope)30 vk_default_realloc(void *pUserData,
31 void *pOriginal,
32 size_t size,
33 size_t alignment,
34 VkSystemAllocationScope allocationScope)
35 {
36 assert(MAX_ALIGN % alignment == 0);
37 return realloc(pOriginal, size);
38 }
39
40 static VKAPI_ATTR void VKAPI_CALL
vk_default_free(void * pUserData,void * pMemory)41 vk_default_free(void *pUserData, void *pMemory)
42 {
43 free(pMemory);
44 }
45
46 const VkAllocationCallbacks *
vk_default_allocator(void)47 vk_default_allocator(void)
48 {
49 static const VkAllocationCallbacks allocator = {
50 .pfnAllocation = vk_default_alloc,
51 .pfnReallocation = vk_default_realloc,
52 .pfnFree = vk_default_free,
53 };
54 return &allocator;
55 }
56