Blender V5.3
resource_scope.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11namespace blender {
12
14 : allocator_(ResourceScope::create_own_allocator(resources_, initial_size))
15{
16}
17
18ResourceScope::ResourceScope(void *buffer, const int64_t size)
19 : allocator_(
20 /* At least the allocator itself has to fit into this buffer, otherwise it's not used. */
21 size >= sizeof(LinearAllocator<>) ?
22 ResourceScope::create_own_allocator_in_buffer(resources_, buffer, size, false) :
23 ResourceScope::create_own_allocator(resources_, 0))
24{
25}
26
27LinearAllocator<> &ResourceScope::create_own_allocator_in_buffer(ResourceDataList &r_resources,
28 void *data,
29 const int64_t size,
30 const bool free_on_destruct)
31{
33 BLI_assert(size >= sizeof(LinearAllocator<>));
34
35 /* Actually construct the allocator. */
37
38 /* Provide the initial buffer to the allocator. */
39 const int64_t extra_size = size - sizeof(LinearAllocator<>);
40 if (extra_size > 0) {
41 void *initial_data = static_cast<char *>(data) + sizeof(LinearAllocator<>);
42 allocator->provide_buffer(initial_data, extra_size);
43 }
44
45 /* Give ownership of the allocator to r_resources. This seems a bit recursive but works out
46 * because `ChunkedList` is trivially destructible by design. */
47 static_assert(std::is_trivially_destructible_v<ResourceDataList>);
48
49 /* The allocator has to be destructed. However, the memory only has to be freed if it's actually
50 * owned by the #ResourceScope. */
51 auto free_fn = free_on_destruct ?
52 [](void *buffer) {
53 LinearAllocator<> *allocator = static_cast<LinearAllocator<> *>(buffer);
54 std::destroy_at(allocator);
55 MEM_delete_void(buffer);
56 } :
57 [](void *buffer) {
58 LinearAllocator<> *allocator = static_cast<LinearAllocator<> *>(buffer);
59 std::destroy_at(allocator);
60 };
61 r_resources.append(*allocator, ResourceData{allocator, free_fn});
62
63 return *allocator;
64}
65
66LinearAllocator<> &ResourceScope::create_own_allocator(ResourceDataList &r_resources,
67 const int64_t initial_size)
68{
69 /* Since we are doing an allocation already, we might as well allocate a slightly larger buffer
70 * to initialize memory in the linear allocator. */
71 const int64_t alloc_size = sizeof(LinearAllocator<>) + sizeof(ResourceDataList::Segment) +
72 initial_size;
73 void *buffer = MEM_new_uninitialized_aligned(alloc_size, alignof(LinearAllocator<>), __func__);
74
75 return ResourceScope::create_own_allocator_in_buffer(r_resources, buffer, alloc_size, true);
76}
77
79{
80 /* Frees all resources in reverse order of construction. Note that #ChunkedList always iterates
81 * in reverse order. */
82 LinearAllocator<> *used_allocator = &allocator_;
83 for (ResourceData &resource : resources_) {
84 void *data = resource.data;
85 resource.free(data);
86 if (data == used_allocator) {
87 break;
88 }
89 }
90}
91
92} // namespace blender
void provide_buffer(void *buffer, const int64_t size)
LinearAllocator & allocator()
ResourceScope(int64_t initial_size=32)
ChunkedListSegment< ResourceData, SegmentCapacity > Segment
#define sizeof
void MEM_delete_void(void *vmemh)
Definition mallocn.cc:109
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
BMesh const char void * data