Coverage Report

Created: 2023-06-07 06:30

/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
1.25M
void *loader_alloc(const VkAllocationCallbacks *pAllocator, size_t size, VkSystemAllocationScope allocation_scope) {
35
1.25M
    void *pMemory = NULL;
36
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
37
    {
38
#else
39
1.25M
    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
1.25M
    } else {
44
1.25M
#endif
45
1.25M
        pMemory = malloc(size);
46
1.25M
    }
47
48
1.25M
    return pMemory;
49
1.25M
}
50
51
1.40k
void *loader_calloc(const VkAllocationCallbacks *pAllocator, size_t size, VkSystemAllocationScope allocation_scope) {
52
1.40k
    void *pMemory = NULL;
53
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
54
    {
55
#else
56
1.40k
    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
1.40k
    } else {
64
1.40k
#endif
65
1.40k
        pMemory = calloc(1, size);
66
1.40k
    }
67
68
1.40k
    return pMemory;
69
1.40k
}
70
71
1.25M
void loader_free(const VkAllocationCallbacks *pAllocator, void *pMemory) {
72
1.25M
    if (pMemory != NULL) {
73
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
74
        {
75
#else
76
1.25M
        if (pAllocator && pAllocator->pfnFree) {
77
0
            pAllocator->pfnFree(pAllocator->pUserData, pMemory);
78
1.25M
        } else {
79
1.25M
#endif
80
1.25M
            free(pMemory);
81
1.25M
        }
82
1.25M
    }
83
1.25M
}
84
85
void *loader_realloc(const VkAllocationCallbacks *pAllocator, void *pMemory, size_t orig_size, size_t size,
86
0
                     VkSystemAllocationScope allocation_scope) {
87
0
    void *pNewMem = NULL;
88
0
    if (pMemory == NULL || orig_size == 0) {
89
0
        pNewMem = loader_alloc(pAllocator, size, allocation_scope);
90
0
    } else if (size == 0) {
91
0
        loader_free(pAllocator, pMemory);
92
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
93
#else
94
0
    } 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
0
    } else {
100
0
        pNewMem = realloc(pMemory, size);
101
0
    }
102
0
    return pNewMem;
103
0
}
104
105
0
void *loader_instance_heap_alloc(const struct loader_instance *inst, size_t size, VkSystemAllocationScope allocation_scope) {
106
0
    return loader_alloc(inst ? &inst->alloc_callbacks : NULL, size, allocation_scope);
107
0
}
108
109
1.40k
void *loader_instance_heap_calloc(const struct loader_instance *inst, size_t size, VkSystemAllocationScope allocation_scope) {
110
1.40k
    return loader_calloc(inst ? &inst->alloc_callbacks : NULL, size, allocation_scope);
111
1.40k
}
112
113
1.41k
void loader_instance_heap_free(const struct loader_instance *inst, void *pMemory) {
114
1.41k
    loader_free(inst ? &inst->alloc_callbacks : NULL, pMemory);
115
1.41k
}
116
void *loader_instance_heap_realloc(const struct loader_instance *inst, void *pMemory, size_t orig_size, size_t size,
117
0
                                   VkSystemAllocationScope allocation_scope) {
118
0
    return loader_realloc(inst ? &inst->alloc_callbacks : NULL, pMemory, orig_size, size, allocation_scope);
119
0
}
120
121
0
void *loader_device_heap_alloc(const struct loader_device *dev, size_t size, VkSystemAllocationScope allocation_scope) {
122
0
    return loader_alloc(dev ? &dev->alloc_callbacks : NULL, size, allocation_scope);
123
0
}
124
125
0
void *loader_device_heap_calloc(const struct loader_device *dev, size_t size, VkSystemAllocationScope allocation_scope) {
126
0
    return loader_calloc(dev ? &dev->alloc_callbacks : NULL, size, allocation_scope);
127
0
}
128
129
0
void loader_device_heap_free(const struct loader_device *dev, void *pMemory) {
130
0
    loader_free(dev ? &dev->alloc_callbacks : NULL, pMemory);
131
0
}
132
void *loader_device_heap_realloc(const struct loader_device *dev, void *pMemory, size_t orig_size, size_t size,
133
0
                                 VkSystemAllocationScope allocation_scope) {
134
0
    return loader_realloc(dev ? &dev->alloc_callbacks : NULL, pMemory, orig_size, size, allocation_scope);
135
0
}
136
137
void *loader_alloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *inst, size_t size,
138
0
                                          VkSystemAllocationScope allocation_scope) {
139
0
    return loader_alloc(NULL != pAllocator ? pAllocator : &inst->alloc_callbacks, size, allocation_scope);
140
0
}
141
142
void *loader_calloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance,
143
0
                                           size_t size, VkSystemAllocationScope allocation_scope) {
144
0
    return loader_calloc(NULL != pAllocator ? pAllocator : &instance->alloc_callbacks, size, allocation_scope);
145
0
}
146
147
void loader_free_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance,
148
0
                                        void *pMemory) {
149
0
    loader_free(NULL != pAllocator ? pAllocator : &instance->alloc_callbacks, pMemory);
150
0
}
151
152
void *loader_realloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance,
153
                                            void *pMemory, size_t orig_size, size_t size,
154
0
                                            VkSystemAllocationScope allocation_scope) {
155
0
    return loader_realloc(NULL != pAllocator ? pAllocator : &instance->alloc_callbacks, pMemory, orig_size, size, allocation_scope);
156
0
}