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 31static inline void * 32vk_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 39static inline void * 40vk_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 53static inline void * 54vk_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 61static inline void 62vk_free(const VkAllocationCallbacks *alloc, void *data) 63{ 64 if (data == NULL) 65 return; 66 67 alloc->pfnFree(alloc->pUserData, data); 68} 69 70static inline char * 71vk_strdup(const VkAllocationCallbacks *alloc, const char *s, 72 VkSystemAllocationScope scope) 73{ 74 if (s == NULL) 75 return NULL; 76 77 size_t size = strlen(s) + 1; 78 char *copy = vk_alloc(alloc, size, 1, scope); 79 if (copy == NULL) 80 return NULL; 81 82 memcpy(copy, s, size); 83 84 return copy; 85} 86 87static inline void * 88vk_alloc2(const VkAllocationCallbacks *parent_alloc, 89 const VkAllocationCallbacks *alloc, 90 size_t size, size_t align, 91 VkSystemAllocationScope scope) 92{ 93 if (alloc) 94 return vk_alloc(alloc, size, align, scope); 95 else 96 return vk_alloc(parent_alloc, size, align, scope); 97} 98 99static inline void * 100vk_zalloc2(const VkAllocationCallbacks *parent_alloc, 101 const VkAllocationCallbacks *alloc, 102 size_t size, size_t align, 103 VkSystemAllocationScope scope) 104{ 105 void *mem = vk_alloc2(parent_alloc, alloc, size, align, scope); 106 if (mem == NULL) 107 return NULL; 108 109 memset(mem, 0, size); 110 111 return mem; 112} 113 114static inline void 115vk_free2(const VkAllocationCallbacks *parent_alloc, 116 const VkAllocationCallbacks *alloc, 117 void *data) 118{ 119 if (alloc) 120 vk_free(alloc, data); 121 else 122 vk_free(parent_alloc, data); 123} 124 125#endif 126