Blender V5.3
BLI_allocator.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#pragma once
5
27
28#include <algorithm>
29#include <cstdlib>
30
31#include "MEM_guardedalloc.h"
32
33#include "BLI_utildefines.hh"
34
35namespace blender {
36
42 public:
43 void *allocate(size_t size, size_t alignment, const char *name)
44 {
45 /* Should we use MEM_new_uninitialized, when alignment is small? If yes, how small must
46 * alignment be? */
47 return MEM_new_uninitialized_aligned(size, alignment, name);
48 }
49 void *allocate_zero(size_t size, size_t alignment, const char *name)
50 {
51 if (alignment > MEM_MIN_CPP_ALIGNMENT) {
52 /* There is no version of calloc with a specific alignment argument. */
53 void *ptr = this->allocate(size, alignment, name);
54 memset(ptr, 0, size);
55 return ptr;
56 }
57 return MEM_new_zeroed(size, name);
58 }
59
60 void deallocate(void *ptr)
61 {
62 MEM_delete_void(ptr);
63 }
64};
65
71template<size_t Alignment = 64ul> class GuardedAlignedAllocator {
72 public:
73 static constexpr size_t min_alignment = Alignment;
74
75 void *allocate(size_t size, size_t alignment, const char *name)
76 {
77 return MEM_new_uninitialized_aligned(size, std::max(alignment, min_alignment), name);
78 }
79 void *allocate_zero(size_t size, size_t alignment, const char *name)
80 {
81 if (std::max(alignment, Alignment) > MEM_MIN_CPP_ALIGNMENT) {
82 /* There is no version of calloc with a specific alignment argument. */
83 void *ptr = this->allocate(size, alignment, name);
84 memset(ptr, 0, size);
85 return ptr;
86 }
87 return MEM_new_zeroed(size, name);
88 }
89
90 void deallocate(void *ptr)
91 {
92 MEM_delete_void(ptr);
93 }
94};
95
102 private:
103 struct MemHead {
104 int offset;
105 };
106
107 public:
108 void *allocate(size_t size, size_t alignment, const char * /*name*/)
109 {
110 BLI_assert(is_power_of_2(int(alignment)));
111 void *ptr = malloc(size + alignment + sizeof(MemHead));
112 void *used_ptr = reinterpret_cast<void *>(
113 uintptr_t(POINTER_OFFSET(ptr, alignment + sizeof(MemHead))) & ~(uintptr_t(alignment) - 1));
114 int offset = int(intptr_t(used_ptr) - intptr_t(ptr));
115 BLI_assert(offset >= int(sizeof(MemHead)));
116 (static_cast<MemHead *>(used_ptr) - 1)->offset = offset;
117 return used_ptr;
118 }
119 void *allocate_zero(size_t size, size_t alignment, const char *name)
120 {
121 void *ptr = this->allocate(size, alignment, name);
122 memset(ptr, 0, size);
123 return ptr;
124 }
125
126 void deallocate(void *ptr)
127 {
128 MemHead *head = static_cast<MemHead *>(ptr) - 1;
129 int offset = -head->offset;
130 void *actual_pointer = POINTER_OFFSET(ptr, offset);
131 free(actual_pointer);
132 }
133};
134
135} // namespace blender
Read Blender memory allocation and freeing.
#define POINTER_OFFSET(v, ofs)
void * allocate_zero(size_t size, size_t alignment, const char *name)
static constexpr size_t min_alignment
void * allocate(size_t size, size_t alignment, const char *name)
void * allocate(size_t size, size_t alignment, const char *name)
void * allocate_zero(size_t size, size_t alignment, const char *name)
void * allocate_zero(size_t size, size_t alignment, const char *name)
void deallocate(void *ptr)
void * allocate(size_t size, size_t alignment, const char *)
void free(Subdiv *subdiv)
Definition subdiv.cc:192
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void MEM_delete_void(void *vmemh)
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)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
constexpr int64_t is_power_of_2(const int64_t x)
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
_W64 int intptr_t
Definition BKE_text.h:119