re-visit the command impl allocation; add a quick-n-dirty implementation based on heap allocation

This commit is contained in:
Fischlurch 2009-08-29 02:16:28 +02:00
parent 5c7e0d1870
commit 45c04b4463
4 changed files with 43 additions and 33 deletions

View file

@ -83,7 +83,7 @@ namespace lib {
void
AllocationCluster::MemoryManager::reset (TypeInfo info)
{
Lock instance();
Lock sync(this);
if (0 < mem_.size()) purge();
type_ = info;
@ -98,7 +98,7 @@ namespace lib {
void
AllocationCluster::MemoryManager::purge()
{
Lock instance();
Lock sync(this);
REQUIRE (type_.killIt, "we need a deleter function");
REQUIRE (0 < type_.allocSize, "allocation size unknown");
@ -122,7 +122,7 @@ namespace lib {
inline void*
AllocationCluster::MemoryManager::allocate()
{
Lock instance();
Lock sync(this);
REQUIRE (0 < type_.allocSize);
REQUIRE (top_ <= mem_.size());
@ -142,7 +142,7 @@ namespace lib {
inline void
AllocationCluster::MemoryManager::commit (void* pendingAlloc)
{
Lock instance();
Lock sync(this);
REQUIRE (pendingAlloc);
ASSERT (top_ < mem_.size());

View file

@ -197,21 +197,21 @@ namespace lib {
//-----implementation-details------------------------
struct AllocationCluster::TypeInfo
{
size_t allocSize;
void (*killIt)(void*); ///< deleter function
template<class TY>
TypeInfo(TY*)
: allocSize(sizeof(TY)),
killIt(&TypeSlot<TY>::kill)
{ }
TypeInfo() ///< denotes "unknown" type
: allocSize(0),
killIt(0)
{ }
};
{
size_t allocSize;
void (*killIt)(void*); ///< deleter function
template<class TY>
TypeInfo(TY*)
: allocSize(sizeof(TY)),
killIt(&TypeSlot<TY>::kill)
{ }
TypeInfo() ///< denotes "unknown" type
: allocSize(0),
killIt(0)
{ }
};

View file

@ -142,9 +142,9 @@ namespace control {
/** set up a new command implementation frame
* @return pointer to a newly created CommandImpl, allocated
* through the registry. The caller is responsible for
* deallocating this frame by calling #killCommandImpl
* @return shared-ptr owning a newly created CommandImpl, allocated
* through the registry and wired internally to invoke
* TypedAllocationManager#destroyElement for cleanup.
*/
template< typename SIG_OPER ///< signature of the command operation
, typename SIG_CAPT ///< signature for capturing undo state

View file

@ -54,6 +54,9 @@
** there is a facility allowing to create ref-counting handles and smart-pointers,
** which are internally tied to this memory manager through a deleter function.
**
** @todo using a quick-n-dirty heap allocation implementation for now (8/09),
** but should write a custom allocator based on cehteh's mpool!
**
** @see CommandRegistry
** @see AllocationCluster (another custom allocation scheme, which could be united)
**
@ -66,30 +69,35 @@
//#include "pre.hpp"
#include "lib/error.hpp"
//#include "lib/singleton.hpp"
//#include "lib/sync.hpp"
#include "lib/format.hpp"
#include "include/logging.h"
//#include "lib/bool-checkable.hpp"
#include <tr1/memory>
//#include <iostream>
//#include <string>
namespace control {
using std::tr1::shared_ptr;
// using std::ostream;
// using std::string;
/**
* TODO type comment
* Foundation for a custom allocation manager,
* tracking the created objects by smart-ptrs.
* The public interface provides forwarding functions
* to invoke the ctor of the objects to be created, thereby
* placing them into the storage maintained by a low-level
* allocator or pooled storage manager. The created smart-ptr
* owns the new object and is wired internally to #releaseSlot.
* Subclasses may also directly allocate and de-allocate
* such a (typed) storage slot.
*
* @todo currently (as of 8/09) the low-level pooled allocator
* isn't implemented; instead we do just heap allocations.
* see Ticket 231
*/
class TypedAllocationManager
{
@ -259,8 +267,8 @@ namespace control {
Slot<XX>
allocateSlot ()
{
UNIMPLEMENTED ("redirect to the corresponding pool allocator");
void* space = 0;
TODO ("redirect to the corresponding pool allocator");
void* space = new char[sizeof(XX)];
return Slot<XX> (this, space);
}
@ -268,7 +276,9 @@ namespace control {
void
releaseSlot (void* entry)
{
UNIMPLEMENTED ("redirect to the corresponding pool allocator");
TODO ("redirect to the corresponding pool allocator");
typedef char Storage[sizeof(XX)];
delete[] reinterpret_cast<Storage*> (entry);
}