/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ // Original file Copyright Crytek GMBH or its affiliates, used under license. #ifndef CRYINCLUDE_CRYCOMMON_STLPOOLALLOCATOR_H #define CRYINCLUDE_CRYCOMMON_STLPOOLALLOCATOR_H #pragma once //--------------------------------------------------------------------------- // STL-compatible interface for the pool allocator (see PoolAllocator.h). // // This class is suitable for use as an allocator for STL lists. Note it will // not work with vectors, since it allocates fixed-size blocks, while vectors // allocate elements in variable-sized contiguous chunks. // // To create a list of type UserDataType using this allocator, use the // following syntax: // // std::list > myList; //--------------------------------------------------------------------------- #include "PoolAllocator.h" #include "MetaUtils.h" #include #include namespace stl { namespace STLPoolAllocatorHelper { inline void destruct(char*) {} inline void destruct(wchar_t*) {} template inline void destruct(T* t) {t->~T(); } } template struct STLPoolAllocatorStatic { // Non-freeing stl pool allocators should just go on the global heap - only if they've been explicitly // set to cleanup should they go on the default heap. typedef SizePoolAllocator< HeapAllocator< L, typename metautils::select::type> > AllocatorType; static AllocatorType* GetOrCreateAllocator() { if (allocator) { return allocator; } allocator = new AllocatorType(S, A, FHeap().FreeWhenEmpty(FreeWhenEmpty)); return allocator; } static AllocatorType* allocator; }; template struct STLPoolAllocatorKungFu : public STLPoolAllocatorStatic { }; template class STLPoolAllocator { public: typedef size_t size_type; typedef ptrdiff_t difference_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; template struct rebind { typedef STLPoolAllocator other; }; STLPoolAllocator() throw() { } STLPoolAllocator(const STLPoolAllocator&) throw() { } template STLPoolAllocator(const STLPoolAllocator&) throw() { } ~STLPoolAllocator() throw() { } pointer address(reference x) const { return &x; } const_pointer address(const_reference x) const { return &x; } pointer allocate(size_type n = 1, const void* hint = 0) { assert(n == 1); typename STLPoolAllocatorKungFu::AllocatorType * allocator = STLPoolAllocatorKungFu::GetOrCreateAllocator(); return static_cast(allocator->Allocate()); } void deallocate(pointer p, size_type n = 1) { assert(n == 1); typename STLPoolAllocatorKungFu::AllocatorType * allocator = STLPoolAllocatorKungFu::allocator; allocator->Deallocate(p); } size_type max_size() const throw() { return INT_MAX; } #ifndef _LIBCPP_VERSION void construct(pointer p, const T& val) { new(static_cast(p))T(val); } void construct(pointer p) { new(static_cast(p))T(); } #endif // !(_LIBCPP_VERSION) void destroy(pointer p) { STLPoolAllocatorHelper::destruct(p); } pointer new_pointer() { return new(allocate())T(); } pointer new_pointer(const T& val) { return new(allocate())T(val); } void delete_pointer(pointer p) { p->~T(); deallocate(p); } bool operator==(const STLPoolAllocator&) {return true; } bool operator!=(const STLPoolAllocator&) {return false; } static void GetMemoryUsage(ICrySizer* pSizer) { pSizer->AddObject(STLPoolAllocatorKungFu::allocator); } }; template class STLPoolAllocatorNoMT : public STLPoolAllocator { }; template <> class STLPoolAllocator { public: typedef void* pointer; typedef const void* const_pointer; typedef void value_type; template struct rebind { typedef STLPoolAllocator other; }; }; template typename STLPoolAllocatorStatic::AllocatorType * STLPoolAllocatorStatic::allocator; } #endif // CRYINCLUDE_CRYCOMMON_STLPOOLALLOCATOR_H