Home | History | Annotate | Download | only in util
      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_realloc(const VkAllocationCallbacks *alloc,
     41            void *ptr, size_t size, size_t align,
     42            VkSystemAllocationScope scope)
     43 {
     44    return alloc->pfnReallocation(alloc->pUserData, ptr, size, align, scope);
     45 }
     46 
     47 static inline void
     48 vk_free(const VkAllocationCallbacks *alloc, void *data)
     49 {
     50    if (data == NULL)
     51       return;
     52 
     53    alloc->pfnFree(alloc->pUserData, data);
     54 }
     55 
     56 static inline void *
     57 vk_alloc2(const VkAllocationCallbacks *parent_alloc,
     58           const VkAllocationCallbacks *alloc,
     59           size_t size, size_t align,
     60           VkSystemAllocationScope scope)
     61 {
     62    if (alloc)
     63       return vk_alloc(alloc, size, align, scope);
     64    else
     65       return vk_alloc(parent_alloc, size, align, scope);
     66 }
     67 
     68 static inline void *
     69 vk_zalloc2(const VkAllocationCallbacks *parent_alloc,
     70            const VkAllocationCallbacks *alloc,
     71            size_t size, size_t align,
     72            VkSystemAllocationScope scope)
     73 {
     74    void *mem = vk_alloc2(parent_alloc, alloc, size, align, scope);
     75    if (mem == NULL)
     76       return NULL;
     77 
     78    memset(mem, 0, size);
     79 
     80    return mem;
     81 }
     82 
     83 static inline void
     84 vk_free2(const VkAllocationCallbacks *parent_alloc,
     85          const VkAllocationCallbacks *alloc,
     86          void *data)
     87 {
     88    if (alloc)
     89       vk_free(alloc, data);
     90    else
     91       vk_free(parent_alloc, data);
     92 }
     93 
     94 #endif
     95