axmol/cocos/base/allocator/CCAllocatorStrategyPool.h

173 lines
5.3 KiB
C
Raw Normal View History

2014-12-13 00:57:06 +08:00
/****************************************************************************
2015-03-24 15:13:05 +08:00
Copyright (c) 2014-2015 Chukong Technologies Inc.
2014-12-13 00:57:06 +08:00
Author: Justin Graham (https://github.com/mannewalis)
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2015-03-24 15:13:05 +08:00
#ifndef CC_ALLOCATOR_STRATEGY_POOL_H
#define CC_ALLOCATOR_STRATEGY_POOL_H
2015-03-24 20:23:51 +08:00
/// @cond DO_NOT_SHOW
2015-03-24 15:13:05 +08:00
2014-12-19 11:49:40 +08:00
#include <vector>
#include <typeinfo>
#include <sstream>
2014-12-13 00:57:06 +08:00
#include "base/allocator/CCAllocatorMacros.h"
#include "base/allocator/CCAllocatorGlobal.h"
#include "base/allocator/CCAllocatorStrategyFixedBlock.h"
#include "base/allocator/CCAllocatorDiagnostics.h"
#include "base/CCConfiguration.h"
NS_CC_BEGIN
NS_CC_ALLOCATOR_BEGIN
2015-03-24 15:13:05 +08:00
/**
* ObjectTraits describes an allocatable object.
*
* Template class that represents a default allocatable object.
* Provide custom implementations to change the constructor/destructor behavior,
* or to change the default alignment of the object in memory.
*
* @param T Type of object.
* @param _alignment Alignment of object T.
*/
2014-12-13 00:57:06 +08:00
template <typename T, size_t _alignment = sizeof(uint32_t)>
class ObjectTraits
{
public:
typedef T value_type;
2014-12-19 11:49:40 +08:00
static const size_t alignment = _alignment;
2014-12-13 00:57:06 +08:00
virtual ~ObjectTraits()
{}
2015-03-24 15:13:05 +08:00
/** Constructor implementation for type T.*/
2014-12-13 00:57:06 +08:00
void construct(T* address)
{
::new(address) T();
}
2015-03-24 15:13:05 +08:00
/** Destructor implementation for type T.*/
2014-12-13 00:57:06 +08:00
void destroy(T* address)
{
address->~T();
}
2015-03-24 15:13:05 +08:00
/** Returns the name of this object type T.*/
2014-12-13 00:57:06 +08:00
const char* name() const
{
return typeid(T).name();
}
};
2015-03-24 15:13:05 +08:00
/**
* Fixed sized pool allocator strategy for objects of type T.
*
* Optionally takes a page size which determines how many objects are added when the allocator needs more storage.
* ObjectTraits allows you to control the alignment, construction and destruction of an object in the pool.
*
* @param T Type of object.
* @param _page_size Number of objects of T in each page.
* @param O ObjectTraits for type T
* @see CC_USE_ALLOCATOR_POOL
*/
template <typename T, typename O = ObjectTraits<T>, typename locking_traits = lockless_semantics>
2014-12-13 00:57:06 +08:00
class AllocatorStrategyPool
: public AllocatorStrategyFixedBlock<sizeof(T), O::alignment, locking_traits>
2014-12-13 00:57:06 +08:00
, public O
{
public:
typedef T value_type;
typedef value_type* pointer;
2015-03-24 15:13:05 +08:00
/** Ugh wish I knew a way that I could declare this just once.*/
typedef AllocatorStrategyFixedBlock<sizeof(T), O::alignment, locking_traits> tParentStrategy;
2014-12-13 00:57:06 +08:00
AllocatorStrategyPool(const char* tag = nullptr, size_t poolSize = 100)
: tParentStrategy(tag)
{
poolSize = Configuration::getInstance()->getValue(tag, Value((int)poolSize)).asInt();
tParentStrategy::_pageSize = poolSize;
}
2015-03-24 15:13:05 +08:00
/**
* Allocate block of size T.
*
* If size does not match sizeof(T) then the global allocator is called instead.
* @see CC_USE_ALLOCATOR_POOL
*/
2014-12-13 00:57:06 +08:00
CC_ALLOCATOR_INLINE void* allocate(size_t size)
{
T* object;
if (sizeof(T) == size)
{
object = (pointer)tParentStrategy::allocate(sizeof(T));
}
else
{
object = (T*)ccAllocatorGlobal.allocate(size);
}
O::construct(object);
return object;
}
2015-03-24 15:13:05 +08:00
/**
* Deallocate block of size T.
*
* If size does not match sizeof(T) then the global allocator is called instead.
* @see CC_USE_ALLOCATOR_POOL
*/
2014-12-13 00:57:06 +08:00
CC_ALLOCATOR_INLINE void deallocate(void* address, size_t size = 0)
{
if (address)
{
O::destroy((T*)address);
if (sizeof(T) == size)
{
tParentStrategy::deallocate(address, sizeof(T));
}
else
{
ccAllocatorGlobal.deallocate(address, size);
}
}
}
#if CC_ENABLE_ALLOCATOR_DIAGNOSTICS
std::string diagnostics() const
{
std::stringstream s;
s << AllocatorBase::tag() << " initial:" << tParentStrategy::_pageSize << " count:" << tParentStrategy::_allocated << " highest:" << tParentStrategy::_highestCount << "\n";
return s.str();
}
#endif
};
NS_CC_ALLOCATOR_END
NS_CC_END
2015-03-24 15:13:05 +08:00
/// @endcond
2014-12-13 00:57:06 +08:00
#endif//CC_ALLOCATOR_STRATEGY_POOL_H