1 /* 2 * Copyright 2015 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 #ifndef VK_ALLOC_H 24 #define VK_ALLOC_H 25 26 /* common allocation inlines for vulkan drivers */ 27 28 #include <string.h> 29 #include <vulkan/vulkan.h> 30 31 static inline void * 32 vk_alloc(const VkAllocationCallbacks *alloc, 33 size_t size, size_t align, 34 VkSystemAllocationScope scope) 35 { 36 return alloc->pfnAllocation(alloc->pUserData, size, align, scope); 37 } 38 39 static inline void * 40 vk_zalloc(const VkAllocationCallbacks *alloc, 41 size_t size, size_t align, 42 VkSystemAllocationScope scope) 43 { 44 void *mem = vk_alloc(alloc, size, align, scope); 45 if (mem == NULL) 46 return NULL; 47 48 memset(mem, 0, size); 49 50 return mem; 51 } 52 53 static inline void * 54 vk_realloc(const VkAllocationCallbacks *alloc, 55 void *ptr, size_t size, size_t align, 56 VkSystemAllocationScope scope) 57 { 58 return alloc->pfnReallocation(alloc->pUserData, ptr, size, align, scope); 59 } 60 61 static inline void 62 vk_free(const VkAllocationCallbacks *alloc, void *data) 63 { 64 if (data == NULL) 65 return; 66 67 alloc->pfnFree(alloc->pUserData, data); 68 } 69 70 static inline void * 71 vk_alloc2(const VkAllocationCallbacks *parent_alloc, 72 const VkAllocationCallbacks *alloc, 73 size_t size, size_t align, 74 VkSystemAllocationScope scope) 75 { 76 if (alloc) 77 return vk_alloc(alloc, size, align, scope); 78 else 79 return vk_alloc(parent_alloc, size, align, scope); 80 } 81 82 static inline void * 83 vk_zalloc2(const VkAllocationCallbacks *parent_alloc, 84 const VkAllocationCallbacks *alloc, 85 size_t size, size_t align, 86 VkSystemAllocationScope scope) 87 { 88 void *mem = vk_alloc2(parent_alloc, alloc, size, align, scope); 89 if (mem == NULL) 90 return NULL; 91 92 memset(mem, 0, size); 93 94 return mem; 95 } 96 97 static inline void 98 vk_free2(const VkAllocationCallbacks *parent_alloc, 99 const VkAllocationCallbacks *alloc, 100 void *data) 101 { 102 if (alloc) 103 vk_free(alloc, data); 104 else 105 vk_free(parent_alloc, data); 106 } 107 108 #endif 109