summaryrefslogtreecommitdiff
path: root/src/vulkan/util/vk_alloc.c
blob: 8ab116656c2cb31cb01274be8363d40d4da8ee52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * Copyright 2021 Google LLC
 * SPDX-License-Identifier: MIT
 */

#include "vk_alloc.h"

#include <stdlib.h>

#ifndef _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;
}