Share Library (C++)
File detail
Source code
/**
* @file DefaultAllocator.cpp
* @brief Allocator for blocks inside shared segment
* @author Kamil Dudka, xdudka00@gmail.com
* @date 2007-05-15
* @ingroup backend
*/
#include "DefaultAllocator.h"
using namespace Share;
DefaultAllocator::DefaultAllocator(void *atAddr, size_t shareMangerSize, size_t dataSize) throw (ShareException):
size_ (dataSize),
available_ (dataSize)
{
typedef TRelocPtr::pointer TAbsPtr;
typedef TRelocPtr::relative_pointer TRelPtr;
// Check alignment
const TRelocPtr ptr (atAddr, 0);
const TRelPtr rel = ptr.toRel();
if (alignmenter (rel) != rel)
throw ShareException ("DefaultAllocator: Address alignment assertion failed. Try to recompile Share library with lower value of BITS_ALIGNMENT macro defined.");
current_ = TRelocPtr::fromRel (shareMangerSize);
}
DefaultAllocator::~DefaultAllocator()
{
}
size_t DefaultAllocator::sizeNeeded (size_t shareMangerSize, size_t dataSize)
{
return
alignmenter(shareMangerSize) +
alignmenter(dataSize);
}
void* DefaultAllocator::alloc (size_t size) throw (ShareException)
{
// Align size
const size_t alignedSize = alignmenter (size);
TRelocPtr tmp = current_;
tmp += alignedSize;
// Check available space
if (tmp.toRel() > size_)
throw ShareException ("DefaultAllocator: Not enough space inside shared segment - alloc() call failed");
// Move next
void *addr= current_;
current_+= alignedSize;
available_ -= alignedSize;
return addr;
}
void DefaultAllocator::free (void *) throw ()
{
}
size_t DefaultAllocator::size()
{
return size_;
}
size_t DefaultAllocator::available ()
{
return available_;
}