Procházet zdrojové kódy

Complete StdAlloc and StdFrameAlloc

- Rename some template parameters to fix shadowing
- Add a few typedefs
- Add rebind, max_size(), construct(), destroy()
Marc Legendre před 9 roky
rodič
revize
ffe26a04f5

+ 21 - 4
Source/BansheeUtility/Include/BsFrameAlloc.h

@@ -2,6 +2,9 @@
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 #pragma once
 #pragma once
 
 
+#include <limits>
+#include <new>                  /* For 'placement new' */
+
 #include "BsPrerequisitesUtil.h"
 #include "BsPrerequisitesUtil.h"
 
 
 namespace BansheeEngine
 namespace BansheeEngine
@@ -151,6 +154,12 @@ namespace BansheeEngine
 	{
 	{
 	public:
 	public:
 		typedef T value_type;
 		typedef T value_type;
+		typedef value_type* pointer;
+		typedef const value_type* const_pointer;
+		typedef value_type& reference;
+		typedef const value_type& const_reference;
+		typedef std::size_t size_type;
+		typedef std::ptrdiff_t difference_type;
 
 
 		StdFrameAlloc() noexcept 
 		StdFrameAlloc() noexcept 
 			:mFrameAlloc(nullptr)
 			:mFrameAlloc(nullptr)
@@ -160,12 +169,13 @@ namespace BansheeEngine
 			:mFrameAlloc(alloc)
 			:mFrameAlloc(alloc)
 		{ }
 		{ }
 
 
-		template<class T> StdFrameAlloc(const StdFrameAlloc<T>& alloc) noexcept
+		template<class U> StdFrameAlloc(const StdFrameAlloc<U>& alloc) noexcept
 			:mFrameAlloc(alloc.mFrameAlloc)
 			:mFrameAlloc(alloc.mFrameAlloc)
 		{ }
 		{ }
 
 
-		template<class T> bool operator==(const StdFrameAlloc<T>&) const noexcept { return true; }
-		template<class T> bool operator!=(const StdFrameAlloc<T>&) const noexcept { return false; }
+		template<class U> bool operator==(const StdFrameAlloc<U>&) const noexcept { return true; }
+		template<class U> bool operator!=(const StdFrameAlloc<U>&) const noexcept { return false; }
+		template<class U> class rebind { public: typedef StdFrameAlloc<U> other; };
 
 
 		/** Allocate but don't initialize number elements of type T.*/
 		/** Allocate but don't initialize number elements of type T.*/
 		T* allocate(const size_t num) const
 		T* allocate(const size_t num) const
@@ -190,6 +200,13 @@ namespace BansheeEngine
 		}
 		}
 
 
 		FrameAlloc* mFrameAlloc;
 		FrameAlloc* mFrameAlloc;
+
+		size_t max_size() const { return std::numeric_limits<size_type>::max() / sizeof(T); }
+		void construct(pointer p, const_reference t) { new (p) T(t); }
+		void destroy(pointer p) { p->~T(); }
+		template<class U, class... Args>
+		void construct(U* p, Args&&... args) { new(p) U(std::forward<Args>(args)...); }
+
 	};
 	};
 
 
 	/** Return that all specializations of this allocator are interchangeable. */
 	/** Return that all specializations of this allocator are interchangeable. */
@@ -208,4 +225,4 @@ namespace BansheeEngine
 
 
 	/** @} */
 	/** @} */
 	/** @} */
 	/** @} */
-}
+}

+ 21 - 6
Source/BansheeUtility/Include/BsMemoryAllocator.h

@@ -1,11 +1,12 @@
 //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
 //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 #pragma once
 #pragma once
-
 #undef min
 #undef min
 #undef max
 #undef max
 
 
 #include <atomic>
 #include <atomic>
+#include <limits>
+#include <new>                  /* For 'placement new' */
 #include <utility>
 #include <utility>
 
 
 #if BS_PLATFORM == BS_PLATFORM_LINUX
 #if BS_PLATFORM == BS_PLATFORM_LINUX
@@ -377,16 +378,24 @@ namespace BansheeEngine
 	 *  @{
 	 *  @{
 	 */
 	 */
 
 
-    /** Allocator for the standard library that internally uses Banshee memory allocator. */
-    template <class T, class Alloc = GenAlloc>
+	/** Allocator for the standard library that internally uses Banshee memory allocator. */
+	template <class T, class Alloc = GenAlloc>
 	class StdAlloc 
 	class StdAlloc 
 	{
 	{
 	public:
 	public:
 		typedef T value_type;
 		typedef T value_type;
+		typedef T* pointer;
+		typedef const T* const_pointer;
+		typedef T& reference;
+		typedef const T& const_reference;
+		typedef std::size_t size_type;
+		typedef std::ptrdiff_t difference_type;
+
 		StdAlloc() noexcept {}
 		StdAlloc() noexcept {}
-		template<class T, class Alloc> StdAlloc(const StdAlloc<T, Alloc>&) noexcept {}
-		template<class T, class Alloc> bool operator==(const StdAlloc<T, Alloc>&) const noexcept { return true; }
-		template<class T, class Alloc> bool operator!=(const StdAlloc<T, Alloc>&) const noexcept { return false; }
+		template<class U, class Alloc2> StdAlloc(const StdAlloc<U, Alloc2>&) noexcept {}
+		template<class U, class Alloc2> bool operator==(const StdAlloc<U, Alloc2>&) const noexcept { return true; }
+		template<class U, class Alloc2> bool operator!=(const StdAlloc<U, Alloc2>&) const noexcept { return false; }
+		template<class U> class rebind { public: typedef StdAlloc<U, Alloc> other; };
 
 
 		/** Allocate but don't initialize number elements of type T. */
 		/** Allocate but don't initialize number elements of type T. */
 		T* allocate(const size_t num) const
 		T* allocate(const size_t num) const
@@ -409,6 +418,12 @@ namespace BansheeEngine
 		{
 		{
 			bs_free<Alloc>((void*)p);
 			bs_free<Alloc>((void*)p);
 		}
 		}
+
+		size_t max_size() const { return std::numeric_limits<size_type>::max() / sizeof(T); }
+		void construct(pointer p, const_reference t) { new (p) T(t); }
+		void destroy(pointer p) { p->~T(); }
+		template<class U, class... Args>
+		void construct(U* p, Args&&... args) { new(p) U(std::forward<Args>(args)...); }
 	};
 	};
 
 
 	/** @} */
 	/** @} */