Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2020-07-05 17:30:26 +0300
committerJacques Lucke <jacques@blender.org>2020-07-05 17:30:26 +0300
commit5d79f9f276b4b3e6289308c534c58e7ee3bb5e2d (patch)
tree333d19482218b868d0a602fd3c7869551a94d632 /source/blender/blenlib/BLI_memory_utils.hh
parent464aaf27016fdaeae94f701195c289660cf4474e (diff)
BLI: refactor how buffers for small object optimization are stored
Diffstat (limited to 'source/blender/blenlib/BLI_memory_utils.hh')
-rw-r--r--source/blender/blenlib/BLI_memory_utils.hh67
1 files changed, 62 insertions, 5 deletions
diff --git a/source/blender/blenlib/BLI_memory_utils.hh b/source/blender/blenlib/BLI_memory_utils.hh
index 44d25340778..5e713461083 100644
--- a/source/blender/blenlib/BLI_memory_utils.hh
+++ b/source/blender/blenlib/BLI_memory_utils.hh
@@ -218,12 +218,8 @@ template<typename T> struct DestructValueAtAddress {
template<typename T> using destruct_ptr = std::unique_ptr<T, DestructValueAtAddress<T>>;
/**
- * An `AlignedBuffer` is simply a byte array with the given size and alignment. The buffer will
+ * An `AlignedBuffer` is a byte array with at least the given size and alignment. The buffer will
* not be initialized by the default constructor.
- *
- * This can be used to reserve memory for C++ objects whose lifetime is different from the
- * lifetime of the object they are embedded in. It's used by containers with small buffer
- * optimization and hash table implementations.
*/
template<size_t Size, size_t Alignment> class alignas(Alignment) AlignedBuffer {
private:
@@ -231,6 +227,16 @@ template<size_t Size, size_t Alignment> class alignas(Alignment) AlignedBuffer {
char buffer_[(Size > 0) ? Size : 1];
public:
+ operator void *()
+ {
+ return (void *)buffer_;
+ }
+
+ operator const void *() const
+ {
+ return (void *)buffer_;
+ }
+
void *ptr()
{
return (void *)buffer_;
@@ -243,6 +249,57 @@ template<size_t Size, size_t Alignment> class alignas(Alignment) AlignedBuffer {
};
/**
+ * This can be used to reserve memory for C++ objects whose lifetime is different from the
+ * lifetime of the object they are embedded in. It's used by containers with small buffer
+ * optimization and hash table implementations.
+ */
+template<typename T, size_t Size = 1> class TypedBuffer {
+ private:
+ AlignedBuffer<sizeof(T) * Size, alignof(T)> buffer_;
+
+ public:
+ operator T *()
+ {
+ return (T *)&buffer_;
+ }
+
+ operator const T *() const
+ {
+ return (const T *)&buffer_;
+ }
+
+ T *operator->()
+ {
+ return (T *)&buffer_;
+ }
+
+ const T *operator->() const
+ {
+ return (const T *)&buffer_;
+ }
+
+ T &operator*()
+ {
+ return *(T *)&buffer_;
+ }
+
+ const T &operator*() const
+ {
+ return *(const T *)&buffer_;
+ }
+
+ T *ptr()
+ {
+ return (T *)&buffer_;
+ }
+
+ const T *ptr() const
+ {
+ return (const T *)&buffer_;
+ }
+};
+
+/**
* This can be used by container constructors. A parameter of this type should be used to indicate
* that the constructor does not construct the elements.
*/