Files

276 lines
5.2 KiB
C++

// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
//
// This software owned by Onix Solutions Limited [OnixS] and is
// protected by copyright law and international copyright treaties.
//
// Access to and use of the software is governed by the terms of the applicable
// OnixS Software Services Agreement (the Agreement) and Customer end user license
// agreements granting a non-assignable, non-transferable and non-exclusive license
// to use the software for it's own data processing purposes under the terms defined
// in the Agreement.
//
// Except as otherwise granted within the terms of the Agreement, copying or
// reproduction of any part of this source code or associated reference material
// to any other location for further reproduction or redistribution, and any
// amendments to this copyright notice, are expressly prohibited.
//
// Any reproduction or redistribution for sale or hiring of the Software not in
// accordance with the terms of the Agreement is a violation of copyright law.
//
#pragma once
#if defined(ONIXS_CMEMDH_ENABLE_TRACING)
#include <ostream>
#endif
#include <cassert>
#include <OnixS/CME/MDH/Bootstrap.h>
ONIXS_CMEMDH_NAMESPACE_BEGIN
/// Memory pool abstraction.
///
/// Defines interface for memory block
/// allocation and disposing.
ONIXS_CMEMDH_EXPORTED_CLASS MemoryPool
{
// Copies are disallowed.
MemoryPool(
const MemoryPool&)
{
}
// Assignment is disallowed.
MemoryPool&
operator =(
const MemoryPool&)
{
return *this;
}
protected:
/// Default initialization.
///
/// Instance construction is
/// assumed through descendants.
MemoryPool()
{
}
/// Destruction is through descendants.
virtual ~MemoryPool()
{
}
public:
/// Releases given instance.
virtual void release() = 0;
/// Allocates memory block of given size.
virtual void* allocate(size_t) = 0;
/// Releases previously allocated memory block.
virtual void deallocate(void*) = 0;
#if defined(ONIXS_CMEMDH_ENABLE_TRACING)
virtual void trace(std::ostream&) = 0;
#endif
};
// Implements STL allocator over MemoryPool.
template
<
class Object
>
class MemoryPoolAllocator
{
MemoryPool* pool_;
public:
typedef Object value_type;
typedef Object* pointer;
typedef const Object* const_pointer;
typedef Object& reference;
typedef const Object& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template
<
class Other
>
struct rebind
{
typedef
MemoryPoolAllocator<Other>
other;
};
MemoryPoolAllocator()
: pool_(NULL)
{
}
MemoryPoolAllocator(
MemoryPool& memoryPool)
: pool_(&memoryPool)
{
}
MemoryPoolAllocator(
const MemoryPoolAllocator& other)
: pool_(other.pool_)
{
}
template
<
class Other
>
MemoryPoolAllocator(
const MemoryPoolAllocator<Other>& other)
: pool_(&other.memoryPool())
{
}
~MemoryPoolAllocator()
{
}
MemoryPool& memoryPool() const
{
return *pool_;
}
pointer
address(
reference obj) const
{
return &obj;
}
const_pointer
address(
const_reference x) const
{
return &x;
}
pointer
allocate(
size_type qty)
{
assert(NULL != pool_);
return static_cast<pointer>
(
pool_->allocate
(
qty * sizeof(value_type)
)
);
}
void
deallocate(
pointer block,
size_type)
{
assert(NULL != pool_);
pool_->deallocate(block);
}
void
construct(
pointer block,
const value_type& obj)
{
new (block) value_type(obj);
}
void
destroy(
pointer obj)
{
obj->~value_type();
(void) obj;
}
size_type max_size() const
{
return
static_cast<size_type>(-1) /
sizeof(value_type);
}
MemoryPoolAllocator&
operator =(
const MemoryPoolAllocator& other)
{
pool_ = other.pool_;
return *this;
}
template
<
class Other
>
MemoryPoolAllocator&
operator =(
const MemoryPoolAllocator<Other>& other)
{
pool_ = &other.memoryPool();
return *this;
}
};
template
<
class Object,
class OtherObject
>
inline
bool
operator ==(
const MemoryPoolAllocator<Object>& left,
const MemoryPoolAllocator<OtherObject>& right)
{
return
(
&left.memoryPool() == &right.memoryPool()
);
}
template
<
class Object,
class OtherObject
>
inline
bool
operator !=(
const MemoryPoolAllocator<Object>& left,
const MemoryPoolAllocator<OtherObject>& right)
{
return
(
&left.memoryPool() != &right.memoryPool()
);
}
/// Constructs memory pool instance
/// according to given configuration.
ONIXS_CMEMDH_EXPORTED
MemoryPool*
makeMemoryPool(
size_t, size_t, size_t, size_t);
ONIXS_CMEMDH_NAMESPACE_END