Share Library (C++)
File detail
Source code
#ifndef DEFAULT_ALLOCATOR_H
#define DEFAULT_ALLOCATOR_H
/**
* @file DefaultAllocator.h
* @brief Allocator for blocks inside shared segment
* @author Kamil Dudka, xdudka00@gmail.com
* @date 2007-05-15
* @ingroup backend
*/
#include "sharelib.h"
/**
* @brief Define @b memory @b alignment in bits that allocator work with.
* @attention This number could not be greater then OS's alignment used in SHM API
* @ingroup backend
*/
#define BITS_ALIGNMENT 8
namespace Share {
/**
* Template parameter bitsAlignment specifies count of bits from end to align to.
* @brief Generic number alignmenter
* @param input number to align
* @return aligned number
* @ingroup backend
*/
template <
unsigned char bitsAlignment,
typename T >
T genericAlignmenter (T input)
{
return
(
((1 << bitsAlignment) - 1) |
(input - 1)
) + 1;
}
/**
* @brief Number alignmenter using @c BITS_ALIGNMENT macro.
* @param input number to align
* @return aligned number
* @ingroup backend
*/
template <typename T>
inline T alignmenter (T input) {
return genericAlignmenter <BITS_ALIGNMENT, T> (input);
}
/**
* @brief Allocator for blocks inside shared segment
* @note This class can be replaced with more efficient class with equivalet interface by changing Share::TSegmentAllocator
* @ingroup backend
*/
class DefaultAllocator/*: public AbstractAllocator*/ {
public:
/**
* @brief Pointer type used inside segment
*/
typedef RelocPtr<char> TRelocPtr;
/**
* @param atAddr Address of attached shared segment. At this addres is placed SegmentHeader object.
* @param segmentHeaderSize Size of SegmentHeader object.
* @param dataSize Desired size of memory for data inside shared segment.
* @throw ShareException Library-specific exception derived from std::bad_alloc
*/
DefaultAllocator(void *atAddr, size_t segmentHeaderSize, size_t dataSize) throw (ShareException);
~DefaultAllocator();
/**
* @param segmentHeaderSize Size of SegmentHeader object.
* @param dataSize Desired size o memory for data inside shared segment.
* @return Return computed size of shared segment to allocate.
*/
static size_t sizeNeeded (size_t segmentHeaderSize, size_t dataSize);
/**
* Allocate block of memory inside shared segment. ShareException is thrown if not success.
* @param size Desired size of block to allocate.
* @return Return local address of allocated block on success.
* @throw ShareException Library-specific exception derived from std::bad_alloc
*/
void *alloc (size_t size) throw (ShareException);
/**
* Free block of memory previously allocated by alloc() method.
* @param addr Address of block returned by alloc() method.
* @attention All attached application may crash if this method is called with invalid address as parameter
*/
void free (void *addr) throw ();
/**
* @return Return total size of memory for data inside shared segment.
* @note Usable memory inside shared segment could be smaller due to free space fragmentation.
*/
size_t size();
/**
* @return Return size of available memory for data inside shared segment.
*/
size_t available();
private:
DefaultAllocator (const DefaultAllocator&);
void operator= (const DefaultAllocator&);
private:
size_t size_;
size_t available_;
TRelocPtr current_;
};
}
#endif // DEFAULT_ALLOCATOR_H