Commits

gyulalaszlo committed e91c467

added scoped proxy allocator

Comments (0)

Files changed (5)

 enable_testing()
 
 
-file(GLOB headers "*.h")
+file(GLOB headers "*.h" "allocators/*.h")
 
 add_library( bitsquid_foundation
     memory.cpp
     murmur_hash.cpp
     string_stream.cpp
 
+		allocators/proxy_allocator.cc
+
 		${headers}
   )
 

allocators/proxy_allocator.cc

+#include "proxy_allocator.h"
+
+// (C) 2013 - Monkeykingz Audio
+// See LICENSE for more information.
+
+#include <new>
+#include <assert.h>
+
+#include "foundation/memory.h"
+
+using namespace foundation;
+
+namespace foundation
+{
+
+    ProxyAllocator::ProxyAllocator( Allocator& parent, const char* name )
+        : name( name )
+        , allocator( parent )
+        , allocated(0)
+        , deallocated(0)
+    {
+
+    }
+
+    ProxyAllocator::~ProxyAllocator()
+    {
+        assert( allocated == deallocated );
+    }
+
+    void* ProxyAllocator::allocate( uint32_t size, uint32_t align )
+    {
+        void* allocated_ptr = allocator.allocate( size, align );
+        allocated += allocator.allocated_size( allocated_ptr );
+        return allocated_ptr;
+    }
+
+    void ProxyAllocator::deallocate( void* p )
+    {
+        deallocated += allocator.allocated_size( p );
+        allocator.deallocate( p );
+    }
+
+    uint32_t ProxyAllocator::allocated_size( void* p )
+    {
+        return allocator.allocated_size( p );
+    }
+
+    uint32_t ProxyAllocator::total_allocated()
+    {
+        return allocator.total_allocated();
+    }
+
+}

allocators/proxy_allocator.h

+#pragma once
+#include "../memory.h"
+
+// (C) 2013 - Monkeykingz Audio
+// See LICENSE for more information.
+
+
+
+
+namespace foundation
+{
+    /// ProxyAllocator is a thin wrapper around any parent allocator
+    /// designed to be used as a scoped memory allocation checker.
+    ///
+    ///     void foo( Allocator& a )
+    ///     {
+    ///         ProxyAllocator tmp_allocator( parent_allocator, "Foo temporaries" );
+    ///         // ... do something
+    ///         // And tmp_allocator checks that all memory allocated by
+    ///         // him is deallocated.
+    ///     }
+    class ProxyAllocator : public Allocator
+    {
+    public:
+        ProxyAllocator( Allocator& parent, const char* name );
+        ~ProxyAllocator();
+
+        virtual void* allocate( uint32_t size, uint32_t align = DEFAULT_ALIGN );
+        virtual void deallocate( void* p );
+        virtual uint32_t allocated_size( void* p );
+        virtual uint32_t total_allocated();
+
+        //////////////////////////////////////////////////////////////////////////
+
+        const char* name;
+    private:
+        Allocator& allocator;
+
+        uint32_t allocated;
+        uint32_t deallocated;
+    };
+
+}
 
 }
 
-namespace
-{
-    class ProxyAllocator : public Allocator
-    {
-        friend Allocator& foundation::make_proxy_allocator( Allocator& parent, const char* name );
-        friend void foundation::destroy_proxy_allocator( Allocator& proxy_allocator );
-
-        ProxyAllocator( Allocator& parent, const char* name )
-          : allocator( parent )
-          , name( name )
-          , allocated(0)
-          , deallocated(0)
-        {
-        }
-
-        ~ProxyAllocator() 
-        {
-            assert( allocated == deallocated );
-        }
-
-        virtual void* allocate( uint32_t size, uint32_t align = DEFAULT_ALIGN )
-        {
-            void* allocated_ptr = allocator.allocate( size, align );
-            allocated += allocator.allocated_size( allocated_ptr );
-            return allocated_ptr;
-        }
-
-        virtual void deallocate( void* p )
-        {
-            deallocated += allocator.allocated_size( p );
-            allocator.deallocate( p );
-        }
-
-        virtual uint32_t allocated_size( void* p )
-        {
-            return allocator.allocated_size( p );
-        }
-
-        virtual uint32_t total_allocated()
-        {
-            return allocator.total_allocated();
-        }
-
-        //////////////////////////////////////////////////////////////////////////
-
-        Allocator& allocator;
-        const char* name;
-
-        uint32_t allocated;
-        uint32_t deallocated;
-    };
-}
-
-namespace foundation
-{
-    Allocator& make_proxy_allocator( Allocator& parent, const char* name )
-    {
-        Allocator* proxy_allocator = MAKE_NEW( parent, ProxyAllocator, parent, name );
-        return *proxy_allocator;
-    }
-
-    void destroy_proxy_allocator( Allocator& proxy_allocator )
-    {
-        ProxyAllocator* p = dynamic_cast<ProxyAllocator*>(&proxy_allocator);
-        if (p != nullptr)
-        {
-            MAKE_DELETE( p->allocator, ProxyAllocator, p );
-        }
-    }
-
-}
         Allocator& operator=( const Allocator& other );
     };
 
-    /// Creates a proxy allocator that can be used to track local allocations.
-    Allocator& make_proxy_allocator( Allocator& parent, const char* name );
-
-    /// Destroys a proxy allocator created with the make_proxy_allocator function
-    void destroy_proxy_allocator( Allocator& proxy_allocator );
 
     /// Creates a new object of type T using the allocator a to allocate the memory.
 #define MAKE_NEW(a, T, ...)     (new ((a).allocate(sizeof(T), alignof(T))) T(__VA_ARGS__))