[Bf-blender-cvs] [2b781131edf] functions-experimental-refactor: experimental AnyAllocator

Jacques Lucke noreply at git.blender.org
Wed Oct 16 13:17:42 CEST 2019


Commit: 2b781131edfa1f3d71591a1bade9a8ef74cf0f44
Author: Jacques Lucke
Date:   Wed Oct 16 11:39:27 2019 +0200
Branches: functions-experimental-refactor
https://developer.blender.org/rB2b781131edfa1f3d71591a1bade9a8ef74cf0f44

experimental AnyAllocator

===================================================================

M	source/blender/blenlib/BLI_allocator.h

===================================================================

diff --git a/source/blender/blenlib/BLI_allocator.h b/source/blender/blenlib/BLI_allocator.h
index 98dbe969966..33a57db2c2a 100644
--- a/source/blender/blenlib/BLI_allocator.h
+++ b/source/blender/blenlib/BLI_allocator.h
@@ -139,6 +139,76 @@ class TemporaryAllocator {
   }
 };
 
+class AnyAllocator {
+ public:
+  class VirtualBase {
+   public:
+    virtual void *allocate(uint size, const char *name) = 0;
+
+    virtual void *allocate_aligned(uint size, uint alignment, const char *name) = 0;
+
+    virtual void deallocate(void *ptr) = 0;
+
+    virtual uint min_allocated_size() const = 0;
+  };
+
+  /**
+   * Only take a reference to the underlying allocator. No ownership is transferred.
+   */
+  AnyAllocator(VirtualBase &base) : m_base(&base)
+  {
+  }
+
+  void *allocate(uint size, const char *name)
+  {
+    return m_base->allocate(size, name);
+  }
+
+  void *allocate_aligned(uint size, uint alignment, const char *name)
+  {
+    return m_base->allocate_aligned(size, alignment, name);
+  }
+
+  void deallocate(void *ptr)
+  {
+    m_base->deallocate(ptr);
+  }
+
+  uint min_allocated_size() const
+  {
+    return m_base->min_allocated_size();
+  }
+
+ private:
+  VirtualBase *m_base;
+};
+
+template<typename Allocator> class AnyAllocatorBase : public AnyAllocator::VirtualBase {
+ private:
+  Allocator m_allocator;
+
+ public:
+  void *allocate(uint size, const char *name) final override
+  {
+    return m_allocator.allocate(size, name);
+  }
+
+  void *allocate_aligned(uint size, uint alignment, const char *name) final override
+  {
+    return m_allocator.allocate_aligned(size, alignment, name);
+  }
+
+  void deallocate(void *ptr) final override
+  {
+    m_allocator.deallocate(ptr);
+  }
+
+  uint min_allocated_size() const final override
+  {
+    return m_allocator.min_allocated_size();
+  }
+};
+
 }  // namespace BLI
 
 #endif /* __BLI_ALLOCATOR_H__ */



More information about the Bf-blender-cvs mailing list