added the HeapMmgr class

This commit is contained in:
2015-02-02 10:45:57 +00:00
parent 65b4e57a79
commit 8fc288e750
12 changed files with 80 additions and 41 deletions

View File

@ -28,7 +28,6 @@
#define _QSE_CMN_EXCMMGR_HPP_
#include <qse/cmn/Mmgr.hpp>
#include <qse/Exception.hpp>
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
@ -45,9 +44,9 @@ public:
void* reallocMem (void* ptr, qse_size_t n);
void freeMem (void* ptr);
static ExcMmgr* getDFL();
QSE_EXCEPTION (Error);
/// The getInstance() function returns the stock instance of the StdMmgr
/// class.
static ExcMmgr* getInstance();
};
/////////////////////////////////

View File

@ -64,7 +64,7 @@ public:
Mmgr* mmgr = QSE_NULL,
qse_size_t node_capacity = 10,
qse_size_t load_factor = 75,
qse_size_t mpb_size = 0): Mmged(mmgr) /*: datum_list (mpb_size)*/
qse_size_t mpb_size = 0): Mmged(mmgr)
{
this->nodes = QSE_NULL;
this->node_capacity = 0;
@ -73,9 +73,16 @@ public:
try
{
qse_size_t total_count = node_capacity << 1;
// Node* is a plain type that doesn't have any constructors and destructors.
// it should be safe to call the memory manager bypassing the new operator.
//this->nodes = new Node*[total_count];
this->nodes = (Node**)this->getMmgr()->allocMem (QSE_SIZEOF(Node*) * total_count);
// NOTE: something wil go wrong if the memory manager doesn't raise an exception
// upon memory allocation failure. Make sure to use a memory allocation
// that raises an exception.
this->node_capacity = node_capacity;
for (qse_size_t i = 0; i < total_count; i++)
{
@ -88,15 +95,14 @@ public:
{
if (this->nodes)
{
//delete[] this->nodes;
this->getMmgr()->freeMem (this->nodes);
this->node_capacity = 0;
this->getMmgr()->freeMem (this->nodes); //delete[] this->nodes;
this->nodes = QSE_NULL;
this->node_capacity = 0;
}
if (this->datum_list)
{
this->delete_datum_list ();
this->free_datum_list ();
this->datum_list = QSE_NULL;
}
@ -107,7 +113,7 @@ public:
this->threshold = node_capacity * load_factor / 100;
}
HashList (const SelfType& list): Mmged (list)/*: datum_list (list.datum_list.getMPBlockSize()) */
HashList (const SelfType& list): Mmged (list)
{
this->nodes = QSE_NULL;
this->node_capacity = 0;
@ -125,20 +131,21 @@ public:
this->nodes[i] = QSE_NULL;
}
this->datum_list = new(list.getMmgr()) DatumList (list.getMmgr(), list.datum_list->getMPBlockSize());
// placement new
this->datum_list = new(list.getMmgr())
DatumList (list.getMmgr(), list.datum_list->getMPBlockSize());
}
catch (...)
{
if (this->nodes)
{
//delete[] this->nodes;
this->getMmgr()->freeMem (this->nodes);
this->node_capacity = 0;
this->getMmgr()->freeMem (this->nodes); //delete[] this->nodes;
this->nodes = QSE_NULL;
this->node_capacity = 0;
}
if (this->datum_list)
{
this->delete_datum_list ();
this->free_datum_list ();
this->datum_list = QSE_NULL;
}
@ -170,13 +177,15 @@ public:
{
this->clear ();
if (this->nodes) this->getMmgr()->freeMem (this->nodes); //delete[] this->nodes;
if (this->datum_list) this->delete_datum_list ();
if (this->datum_list) this->free_datum_list ();
}
SelfType& operator= (const SelfType& list)
{
this->clear ();
// note that the memory pool itself is not copied.
for (qse_size_t i = 0; i < list.node_capacity; i++)
{
qse_size_t head = i << 1;
@ -187,7 +196,7 @@ public:
do
{
copy_datum (np, this->node_capacity, this->nodes, this->datum_list);
this->copy_datum (np, this->node_capacity, this->nodes, this->datum_list);
if (np == list.nodes[tail]) break;
np = np->getNextNode ();
}
@ -504,9 +513,13 @@ protected:
}
private:
void delete_datum_list ()
void free_datum_list ()
{
// destruction in response to 'placement new'
// call the destructor
this->datum_list->~DatumList();
// free the memory
::operator delete (this->datum_list, this->getMmgr());
}
};

View File

@ -51,7 +51,7 @@ pkginclude_HEADERS = \
if ENABLE_CXX
pkginclude_HEADERS += \
Mmgr.hpp StdMmgr.hpp ExcMmgr.hpp Mmged.hpp Mpool.hpp Mpoolable.hpp \
LinkedList.hpp HashList.hpp
Mmgr.hpp StdMmgr.hpp ExcMmgr.hpp HeapMmgr.hpp Mmged.hpp \
Mpool.hpp Mpoolable.hpp LinkedList.hpp HashList.hpp
endif

View File

@ -51,8 +51,8 @@ POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
@ENABLE_CXX_TRUE@am__append_1 = \
@ENABLE_CXX_TRUE@ Mmgr.hpp StdMmgr.hpp ExcMmgr.hpp Mmged.hpp Mpool.hpp Mpoolable.hpp \
@ENABLE_CXX_TRUE@ LinkedList.hpp HashList.hpp
@ENABLE_CXX_TRUE@ Mmgr.hpp StdMmgr.hpp ExcMmgr.hpp HeapMmgr.hpp Mmged.hpp \
@ENABLE_CXX_TRUE@ Mpool.hpp Mpoolable.hpp LinkedList.hpp HashList.hpp
subdir = include/qse/cmn
DIST_COMMON = $(am__pkginclude_HEADERS_DIST) $(srcdir)/Makefile.am \
@ -90,8 +90,8 @@ am__pkginclude_HEADERS_DIST = alg.h chr.h cp949.h cp950.h dir.h dll.h \
lda.h main.h map.h mb8.h mbwc.h mem.h mux.h nwad.h nwif.h \
nwio.h oht.h opt.h path.h pio.h pma.h rbt.h rex.h sck.h sio.h \
sll.h slmb.h str.h task.h time.h tio.h tmr.h tre.h uni.h uri.h \
utf8.h xma.h Mmgr.hpp StdMmgr.hpp ExcMmgr.hpp Mmged.hpp \
Mpool.hpp Mpoolable.hpp LinkedList.hpp HashList.hpp
utf8.h xma.h Mmgr.hpp StdMmgr.hpp ExcMmgr.hpp HeapMmgr.hpp \
Mmged.hpp Mpool.hpp Mpoolable.hpp LinkedList.hpp HashList.hpp
am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
am__vpath_adj = case $$p in \
$(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \

View File

@ -29,6 +29,7 @@
#include <qse/types.h>
#include <qse/macros.h>
#include <qse/Exception.hpp>
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
@ -47,6 +48,8 @@ public:
/// defines an alias type to #qse_mmgr_t
typedef qse_mmgr_t mmgr_t;
QSE_EXCEPTION (MemoryError);
///
/// The Mmgr() function builds a memory manager composed of bridge
/// functions connecting itself with it.
@ -108,6 +111,13 @@ protected:
/// bridge function from the #qse_mmgr_t type the freeMem() function.
///
static void free_mem (mmgr_t* mmgr, void* ptr);
public:
static Mmgr* getDFL ();
static void setDFL (Mmgr* mmgr);
protected:
static Mmgr* dfl_mmgr;
};
/////////////////////////////////
@ -115,9 +125,11 @@ QSE_END_NAMESPACE(QSE)
/////////////////////////////////
void* operator new (qse_size_t size, QSE::Mmgr* mmgr);
void* operator new[] (qse_size_t size, QSE::Mmgr* mmgr);
void operator delete (void* ptr, QSE::Mmgr* mmgr);
#if 0
void* operator new[] (qse_size_t size, QSE::Mmgr* mmgr);
void operator delete[] (void* ptr, QSE::Mmgr* mmgr);
#endif
#endif

View File

@ -44,7 +44,9 @@ public:
void* reallocMem (void* ptr, qse_size_t n);
void freeMem (void* ptr);
static StdMmgr* getDFL();
/// The getInstance() function returns the stock instance of the StdMmgr
/// class.
static StdMmgr* getInstance ();
};
/////////////////////////////////