/* * 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_MANYELEMS_H #define CRYINCLUDE_CRYCOMMON_STLPOOLALLOCATOR_MANYELEMS_H #pragma once //--------------------------------------------------------------------------- // STL-compatible interface for the pool allocator (see PoolAllocator.h). // // this class acts like STLPoolAllocator, but it is also usable for vectors // which means that it can be used as a more efficient allocator for many // implementations of hash_map (typically this uses internally a vector and // a list with the same allocator) //--------------------------------------------------------------------------- #include "STLPoolAllocator.h" namespace stl { template struct STLPoolAllocator_ManyElemsStatic { static PoolAllocator* allocator; }; template class STLPoolAllocator_ManyElems : public STLPoolAllocator { typedef STLPoolAllocator Super; typedef PoolAllocator LargeAllocator; public: typedef typename Super::pointer pointer; typedef typename Super::pointer pointer_type; typedef typename Super::size_type size_type; typedef AZStd::false_type allow_memory_leaks; template struct rebind { typedef STLPoolAllocator_ManyElems other; }; STLPoolAllocator_ManyElems() throw() { } template STLPoolAllocator_ManyElems(const STLPoolAllocator_ManyElems&) throw() { } pointer allocate(size_type n = 1, const void* hint = 0) { if (n == 1) { return Super::allocate(n, hint); } else if (n * sizeof(T) <= LargeAllocationSizeThreshold) { if (!STLPoolAllocator_ManyElemsStatic::allocator) { STLPoolAllocator_ManyElemsStatic::allocator = new LargeAllocator(); } return static_cast(STLPoolAllocator_ManyElemsStatic::allocator->Allocate()); } else { return static_cast(CryModuleMalloc(n * sizeof(T))); } } void deallocate(pointer p, size_type n = 1) { if (n == 1) { Super::deallocate(p); } else if (n * sizeof(T) <= LargeAllocationSizeThreshold) { STLPoolAllocator_ManyElemsStatic::allocator->Deallocate(p); } else { CryModuleFree(p); } } }; template PoolAllocator* STLPoolAllocator_ManyElemsStatic::allocator; } #endif // CRYINCLUDE_CRYCOMMON_STLPOOLALLOCATOR_MANYELEMS_H