15db71995Sopenharmony_ci/* 25db71995Sopenharmony_ci * Copyright (c) 2019-2021 The Khronos Group Inc. 35db71995Sopenharmony_ci * Copyright (c) 2019-2021 Valve Corporation 45db71995Sopenharmony_ci * Copyright (c) 2019-2021 LunarG, Inc. 55db71995Sopenharmony_ci * 65db71995Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 75db71995Sopenharmony_ci * you may not use this file except in compliance with the License. 85db71995Sopenharmony_ci * You may obtain a copy of the License at 95db71995Sopenharmony_ci * 105db71995Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 115db71995Sopenharmony_ci * 125db71995Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 135db71995Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 145db71995Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 155db71995Sopenharmony_ci * See the License for the specific language governing permissions and 165db71995Sopenharmony_ci * limitations under the License. 175db71995Sopenharmony_ci * 185db71995Sopenharmony_ci * Author: Jon Ashburn <jon@lunarg.com> 195db71995Sopenharmony_ci * Author: Courtney Goeltzenleuchter <courtney@LunarG.com> 205db71995Sopenharmony_ci * Author: Chia-I Wu <olvaffe@gmail.com> 215db71995Sopenharmony_ci * Author: Chia-I Wu <olv@lunarg.com> 225db71995Sopenharmony_ci * Author: Mark Lobodzinski <mark@LunarG.com> 235db71995Sopenharmony_ci * Author: Lenny Komow <lenny@lunarg.com> 245db71995Sopenharmony_ci * Author: Charles Giessen <charles@lunarg.com> 255db71995Sopenharmony_ci */ 265db71995Sopenharmony_ci 275db71995Sopenharmony_ci#include "allocation.h" 285db71995Sopenharmony_ci 295db71995Sopenharmony_ci#include <stdlib.h> 305db71995Sopenharmony_ci 315db71995Sopenharmony_ci// A debug option to disable allocators at compile time to investigate future issues. 325db71995Sopenharmony_ci#define DEBUG_DISABLE_APP_ALLOCATORS 0 335db71995Sopenharmony_ci 345db71995Sopenharmony_civoid *loader_alloc(const VkAllocationCallbacks *pAllocator, size_t size, VkSystemAllocationScope allocation_scope) { 355db71995Sopenharmony_ci void *pMemory = NULL; 365db71995Sopenharmony_ci#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) 375db71995Sopenharmony_ci { 385db71995Sopenharmony_ci#else 395db71995Sopenharmony_ci if (pAllocator && pAllocator->pfnAllocation) { 405db71995Sopenharmony_ci // These are internal structures, so it's best to align everything to 415db71995Sopenharmony_ci // the largest unit size which is the size of a uint64_t. 425db71995Sopenharmony_ci pMemory = pAllocator->pfnAllocation(pAllocator->pUserData, size, sizeof(uint64_t), allocation_scope); 435db71995Sopenharmony_ci } else { 445db71995Sopenharmony_ci#endif 455db71995Sopenharmony_ci pMemory = malloc(size); 465db71995Sopenharmony_ci } 475db71995Sopenharmony_ci 485db71995Sopenharmony_ci return pMemory; 495db71995Sopenharmony_ci} 505db71995Sopenharmony_ci 515db71995Sopenharmony_civoid *loader_calloc(const VkAllocationCallbacks *pAllocator, size_t size, VkSystemAllocationScope allocation_scope) { 525db71995Sopenharmony_ci void *pMemory = NULL; 535db71995Sopenharmony_ci#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) 545db71995Sopenharmony_ci { 555db71995Sopenharmony_ci#else 565db71995Sopenharmony_ci if (pAllocator && pAllocator->pfnAllocation) { 575db71995Sopenharmony_ci // These are internal structures, so it's best to align everything to 585db71995Sopenharmony_ci // the largest unit size which is the size of a uint64_t. 595db71995Sopenharmony_ci pMemory = pAllocator->pfnAllocation(pAllocator->pUserData, size, sizeof(uint64_t), allocation_scope); 605db71995Sopenharmony_ci if (pMemory) { 615db71995Sopenharmony_ci memset(pMemory, 0, size); 625db71995Sopenharmony_ci } 635db71995Sopenharmony_ci } else { 645db71995Sopenharmony_ci#endif 655db71995Sopenharmony_ci pMemory = calloc(1, size); 665db71995Sopenharmony_ci } 675db71995Sopenharmony_ci 685db71995Sopenharmony_ci return pMemory; 695db71995Sopenharmony_ci} 705db71995Sopenharmony_ci 715db71995Sopenharmony_civoid loader_free(const VkAllocationCallbacks *pAllocator, void *pMemory) { 725db71995Sopenharmony_ci if (pMemory != NULL) { 735db71995Sopenharmony_ci#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) 745db71995Sopenharmony_ci { 755db71995Sopenharmony_ci#else 765db71995Sopenharmony_ci if (pAllocator && pAllocator->pfnFree) { 775db71995Sopenharmony_ci pAllocator->pfnFree(pAllocator->pUserData, pMemory); 785db71995Sopenharmony_ci } else { 795db71995Sopenharmony_ci#endif 805db71995Sopenharmony_ci free(pMemory); 815db71995Sopenharmony_ci } 825db71995Sopenharmony_ci } 835db71995Sopenharmony_ci} 845db71995Sopenharmony_ci 855db71995Sopenharmony_civoid *loader_realloc(const VkAllocationCallbacks *pAllocator, void *pMemory, size_t orig_size, size_t size, 865db71995Sopenharmony_ci VkSystemAllocationScope allocation_scope) { 875db71995Sopenharmony_ci void *pNewMem = NULL; 885db71995Sopenharmony_ci if (pMemory == NULL || orig_size == 0) { 895db71995Sopenharmony_ci pNewMem = loader_alloc(pAllocator, size, allocation_scope); 905db71995Sopenharmony_ci } else if (size == 0) { 915db71995Sopenharmony_ci loader_free(pAllocator, pMemory); 925db71995Sopenharmony_ci#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) 935db71995Sopenharmony_ci#else 945db71995Sopenharmony_ci } else if (pAllocator && pAllocator->pfnReallocation) { 955db71995Sopenharmony_ci // These are internal structures, so it's best to align everything to 965db71995Sopenharmony_ci // the largest unit size which is the size of a uint64_t. 975db71995Sopenharmony_ci pNewMem = pAllocator->pfnReallocation(pAllocator->pUserData, pMemory, size, sizeof(uint64_t), allocation_scope); 985db71995Sopenharmony_ci#endif 995db71995Sopenharmony_ci } else { 1005db71995Sopenharmony_ci pNewMem = realloc(pMemory, size); 1015db71995Sopenharmony_ci } 1025db71995Sopenharmony_ci return pNewMem; 1035db71995Sopenharmony_ci} 1045db71995Sopenharmony_ci 1055db71995Sopenharmony_civoid *loader_instance_heap_alloc(const struct loader_instance *inst, size_t size, VkSystemAllocationScope allocation_scope) { 1065db71995Sopenharmony_ci return loader_alloc(inst ? &inst->alloc_callbacks : NULL, size, allocation_scope); 1075db71995Sopenharmony_ci} 1085db71995Sopenharmony_ci 1095db71995Sopenharmony_civoid *loader_instance_heap_calloc(const struct loader_instance *inst, size_t size, VkSystemAllocationScope allocation_scope) { 1105db71995Sopenharmony_ci return loader_calloc(inst ? &inst->alloc_callbacks : NULL, size, allocation_scope); 1115db71995Sopenharmony_ci} 1125db71995Sopenharmony_ci 1135db71995Sopenharmony_civoid loader_instance_heap_free(const struct loader_instance *inst, void *pMemory) { 1145db71995Sopenharmony_ci loader_free(inst ? &inst->alloc_callbacks : NULL, pMemory); 1155db71995Sopenharmony_ci} 1165db71995Sopenharmony_civoid *loader_instance_heap_realloc(const struct loader_instance *inst, void *pMemory, size_t orig_size, size_t size, 1175db71995Sopenharmony_ci VkSystemAllocationScope allocation_scope) { 1185db71995Sopenharmony_ci return loader_realloc(inst ? &inst->alloc_callbacks : NULL, pMemory, orig_size, size, allocation_scope); 1195db71995Sopenharmony_ci} 1205db71995Sopenharmony_ci 1215db71995Sopenharmony_civoid *loader_device_heap_alloc(const struct loader_device *dev, size_t size, VkSystemAllocationScope allocation_scope) { 1225db71995Sopenharmony_ci return loader_alloc(dev ? &dev->alloc_callbacks : NULL, size, allocation_scope); 1235db71995Sopenharmony_ci} 1245db71995Sopenharmony_ci 1255db71995Sopenharmony_civoid *loader_device_heap_calloc(const struct loader_device *dev, size_t size, VkSystemAllocationScope allocation_scope) { 1265db71995Sopenharmony_ci return loader_calloc(dev ? &dev->alloc_callbacks : NULL, size, allocation_scope); 1275db71995Sopenharmony_ci} 1285db71995Sopenharmony_ci 1295db71995Sopenharmony_civoid loader_device_heap_free(const struct loader_device *dev, void *pMemory) { 1305db71995Sopenharmony_ci loader_free(dev ? &dev->alloc_callbacks : NULL, pMemory); 1315db71995Sopenharmony_ci} 1325db71995Sopenharmony_civoid *loader_device_heap_realloc(const struct loader_device *dev, void *pMemory, size_t orig_size, size_t size, 1335db71995Sopenharmony_ci VkSystemAllocationScope allocation_scope) { 1345db71995Sopenharmony_ci return loader_realloc(dev ? &dev->alloc_callbacks : NULL, pMemory, orig_size, size, allocation_scope); 1355db71995Sopenharmony_ci} 1365db71995Sopenharmony_ci 1375db71995Sopenharmony_civoid *loader_alloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *inst, size_t size, 1385db71995Sopenharmony_ci VkSystemAllocationScope allocation_scope) { 1395db71995Sopenharmony_ci return loader_alloc(NULL != pAllocator ? pAllocator : &inst->alloc_callbacks, size, allocation_scope); 1405db71995Sopenharmony_ci} 1415db71995Sopenharmony_ci 1425db71995Sopenharmony_civoid *loader_calloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance, 1435db71995Sopenharmony_ci size_t size, VkSystemAllocationScope allocation_scope) { 1445db71995Sopenharmony_ci return loader_calloc(NULL != pAllocator ? pAllocator : &instance->alloc_callbacks, size, allocation_scope); 1455db71995Sopenharmony_ci} 1465db71995Sopenharmony_ci 1475db71995Sopenharmony_civoid loader_free_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance, 1485db71995Sopenharmony_ci void *pMemory) { 1495db71995Sopenharmony_ci loader_free(NULL != pAllocator ? pAllocator : &instance->alloc_callbacks, pMemory); 1505db71995Sopenharmony_ci} 1515db71995Sopenharmony_ci 1525db71995Sopenharmony_civoid *loader_realloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance, 1535db71995Sopenharmony_ci void *pMemory, size_t orig_size, size_t size, 1545db71995Sopenharmony_ci VkSystemAllocationScope allocation_scope) { 1555db71995Sopenharmony_ci return loader_realloc(NULL != pAllocator ? pAllocator : &instance->alloc_callbacks, pMemory, orig_size, size, allocation_scope); 1565db71995Sopenharmony_ci} 157