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