xref: /aosp_15_r20/external/executorch/backends/vulkan/runtime/vk_api/memory/Allocation.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
11 // @lint-ignore-every CLANGTIDY facebook-hte-BadMemberName
12 
13 #include <executorch/backends/vulkan/runtime/vk_api/vk_api.h>
14 
15 #include <executorch/backends/vulkan/runtime/vk_api/Exception.h>
16 
17 #include <executorch/backends/vulkan/runtime/vk_api/memory/vma_api.h>
18 
19 #include <ostream>
20 
21 std::ostream& operator<<(std::ostream& out, VmaTotalStatistics stats);
22 
23 namespace vkcompute {
24 namespace vkapi {
25 
26 struct Allocation final {
27   explicit Allocation();
28 
29   explicit Allocation(
30       const VmaAllocator,
31       const VkMemoryRequirements&,
32       const VmaAllocationCreateInfo&);
33 
34  protected:
35   /*
36    * The Copy constructor allows for creation of a class instance that are
37    * "aliases" of another class instance. The resulting class instance will not
38    * have ownership of the underlying VmaAllocation.
39    *
40    * This behaviour is analogous to creating a copy of a pointer, thus it is
41    * unsafe, as the original class instance may be destroyed before the copy.
42    * These constructors are therefore marked protected so that they may be used
43    * only in situations where the lifetime of the original class instance is
44    * guaranteed to exceed, or at least be the same as, the lifetime of the
45    * copied class instance.
46    */
47   Allocation(const Allocation&) noexcept;
48 
49  public:
50   // To discourage creating copies, the assignment operator is still deleted.
51   Allocation& operator=(const Allocation&) = delete;
52 
53   Allocation(Allocation&&) noexcept;
54   Allocation& operator=(Allocation&&) noexcept;
55 
56   ~Allocation();
57 
58   // The allocator object this was allocated from
59   VmaAllocator allocator;
60   // Handles to the allocated memory
61   VmaAllocation allocation;
62 
63  private:
64   // Indicates whether this class instance is a copy of another class instance,
65   // in which case it does not have ownership of the underlying VmaAllocation
66   bool is_copy_;
67 
68  public:
69   operator bool() const {
70     return (allocation != VK_NULL_HANDLE);
71   }
72 
is_copyfinal73   inline bool is_copy() const {
74     return is_copy_;
75   }
76 
77   friend class VulkanBuffer;
78   friend class VulkanImage;
79 };
80 
81 } // namespace vkapi
82 } // namespace vkcompute
83