Coverage Report

Created: 2025-07-11 06:49

/src/vulkan-loader/loader/allocation.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2019-2021 The Khronos Group Inc.
3
 * Copyright (c) 2019-2021 Valve Corporation
4
 * Copyright (c) 2019-2021 LunarG, Inc.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 * Author: Jon Ashburn <jon@lunarg.com>
19
 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20
 * Author: Chia-I Wu <olvaffe@gmail.com>
21
 * Author: Chia-I Wu <olv@lunarg.com>
22
 * Author: Mark Lobodzinski <mark@LunarG.com>
23
 * Author: Lenny Komow <lenny@lunarg.com>
24
 * Author: Charles Giessen <charles@lunarg.com>
25
 */
26
27
#include "allocation.h"
28
29
#include <stdlib.h>
30
31
// A debug option to disable allocators at compile time to investigate future issues.
32
#define DEBUG_DISABLE_APP_ALLOCATORS 0
33
34
49.6k
void *loader_alloc(const VkAllocationCallbacks *pAllocator, size_t size, VkSystemAllocationScope allocation_scope) {
35
49.6k
    void *pMemory = NULL;
36
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
37
    {
38
#else
39
49.6k
    if (pAllocator && pAllocator->pfnAllocation) {
40
        // These are internal structures, so it's best to align everything to
41
        // the largest unit size which is the size of a uint64_t.
42
0
        pMemory = pAllocator->pfnAllocation(pAllocator->pUserData, size, sizeof(uint64_t), allocation_scope);
43
49.6k
    } else {
44
49.6k
#endif
45
49.6k
        pMemory = malloc(size);
46
49.6k
    }
47
48
49.6k
    return pMemory;
49
49.6k
}
50
51
70.5M
void *loader_calloc(const VkAllocationCallbacks *pAllocator, size_t size, VkSystemAllocationScope allocation_scope) {
52
70.5M
    void *pMemory = NULL;
53
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
54
    {
55
#else
56
70.5M
    if (pAllocator && pAllocator->pfnAllocation) {
57
        // These are internal structures, so it's best to align everything to
58
        // the largest unit size which is the size of a uint64_t.
59
0
        pMemory = pAllocator->pfnAllocation(pAllocator->pUserData, size, sizeof(uint64_t), allocation_scope);
60
0
        if (pMemory) {
61
0
            memset(pMemory, 0, size);
62
0
        }
63
70.5M
    } else {
64
70.5M
#endif
65
70.5M
        pMemory = calloc(1, size);
66
70.5M
    }
67
68
70.5M
    return pMemory;
69
70.5M
}
70
71
101M
void loader_free(const VkAllocationCallbacks *pAllocator, void *pMemory) {
72
101M
    if (pMemory != NULL) {
73
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
74
        {
75
#else
76
70.5M
        if (pAllocator && pAllocator->pfnFree) {
77
0
            pAllocator->pfnFree(pAllocator->pUserData, pMemory);
78
70.5M
        } else {
79
70.5M
#endif
80
70.5M
            free(pMemory);
81
70.5M
        }
82
70.5M
    }
83
101M
}
84
85
void *loader_realloc(const VkAllocationCallbacks *pAllocator, void *pMemory, size_t orig_size, size_t size,
86
2.03M
                     VkSystemAllocationScope allocation_scope) {
87
2.03M
    void *pNewMem = NULL;
88
2.03M
    if (pMemory == NULL || orig_size == 0) {
89
0
        pNewMem = loader_alloc(pAllocator, size, allocation_scope);
90
2.03M
    } else if (size == 0) {
91
0
        loader_free(pAllocator, pMemory);
92
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
93
#else
94
2.03M
    } else if (pAllocator && pAllocator->pfnReallocation) {
95
        // These are internal structures, so it's best to align everything to
96
        // the largest unit size which is the size of a uint64_t.
97
0
        pNewMem = pAllocator->pfnReallocation(pAllocator->pUserData, pMemory, size, sizeof(uint64_t), allocation_scope);
98
0
#endif
99
2.03M
    } else {
100
2.03M
        pNewMem = realloc(pMemory, size);
101
        // Clear out the newly allocated memory
102
2.03M
        if (size > orig_size) {
103
26.5k
            memset((uint8_t *)pNewMem + orig_size, 0, size - orig_size);
104
26.5k
        }
105
2.03M
    }
106
2.03M
    return pNewMem;
107
2.03M
}
108
109
49.6k
void *loader_instance_heap_alloc(const struct loader_instance *inst, size_t size, VkSystemAllocationScope allocation_scope) {
110
49.6k
    return loader_alloc(inst ? &inst->alloc_callbacks : NULL, size, allocation_scope);
111
49.6k
}
112
113
2.47M
void *loader_instance_heap_calloc(const struct loader_instance *inst, size_t size, VkSystemAllocationScope allocation_scope) {
114
2.47M
    return loader_calloc(inst ? &inst->alloc_callbacks : NULL, size, allocation_scope);
115
2.47M
}
116
117
35.0M
void loader_instance_heap_free(const struct loader_instance *inst, void *pMemory) {
118
35.0M
    loader_free(inst ? &inst->alloc_callbacks : NULL, pMemory);
119
35.0M
}
120
void *loader_instance_heap_realloc(const struct loader_instance *inst, void *pMemory, size_t orig_size, size_t size,
121
6.21k
                                   VkSystemAllocationScope allocation_scope) {
122
6.21k
    return loader_realloc(inst ? &inst->alloc_callbacks : NULL, pMemory, orig_size, size, allocation_scope);
123
6.21k
}
124
125
0
void *loader_device_heap_alloc(const struct loader_device *dev, size_t size, VkSystemAllocationScope allocation_scope) {
126
0
    return loader_alloc(dev ? &dev->alloc_callbacks : NULL, size, allocation_scope);
127
0
}
128
129
0
void *loader_device_heap_calloc(const struct loader_device *dev, size_t size, VkSystemAllocationScope allocation_scope) {
130
0
    return loader_calloc(dev ? &dev->alloc_callbacks : NULL, size, allocation_scope);
131
0
}
132
133
0
void loader_device_heap_free(const struct loader_device *dev, void *pMemory) {
134
0
    loader_free(dev ? &dev->alloc_callbacks : NULL, pMemory);
135
0
}
136
void *loader_device_heap_realloc(const struct loader_device *dev, void *pMemory, size_t orig_size, size_t size,
137
0
                                 VkSystemAllocationScope allocation_scope) {
138
0
    return loader_realloc(dev ? &dev->alloc_callbacks : NULL, pMemory, orig_size, size, allocation_scope);
139
0
}
140
141
void *loader_alloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *inst, size_t size,
142
0
                                          VkSystemAllocationScope allocation_scope) {
143
0
    return loader_alloc(NULL != pAllocator ? pAllocator : &inst->alloc_callbacks, size, allocation_scope);
144
0
}
145
146
void *loader_calloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance,
147
0
                                           size_t size, VkSystemAllocationScope allocation_scope) {
148
0
    return loader_calloc(NULL != pAllocator ? pAllocator : &instance->alloc_callbacks, size, allocation_scope);
149
0
}
150
151
void loader_free_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance,
152
0
                                        void *pMemory) {
153
0
    loader_free(NULL != pAllocator ? pAllocator : &instance->alloc_callbacks, pMemory);
154
0
}
155
156
void *loader_realloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance,
157
                                            void *pMemory, size_t orig_size, size_t size,
158
0
                                            VkSystemAllocationScope allocation_scope) {
159
0
    return loader_realloc(NULL != pAllocator ? pAllocator : &instance->alloc_callbacks, pMemory, orig_size, size, allocation_scope);
160
0
}