changed the memory allocation scheme for some c++ classes

This commit is contained in:
hyung-hwan 2015-02-01 16:07:26 +00:00
parent 0585bf00ae
commit fbd7f3ccae
12 changed files with 143 additions and 59 deletions

View File

@ -25,8 +25,8 @@
*/ */
#ifndef _QSE_EXCEPTION_CLASS_ #ifndef _QSE_EXCEPTION_HPP_
#define _QSE_EXCEPTION_CLASS_ #define _QSE_EXCEPTION_HPP_
#include <qse/types.h> #include <qse/types.h>
#include <qse/macros.h> #include <qse/macros.h>

View File

@ -52,10 +52,8 @@ struct HashListComparator
} }
}; };
// TODO: use MPOLL for nodes???
template <typename T, typename MPOOL = Mpool, typename HASHER = HashListHasher<T>, typename COMPARATOR = HashListComparator<T> > template <typename T, typename MPOOL = Mpool, typename HASHER = HashListHasher<T>, typename COMPARATOR = HashListComparator<T> >
class HashList class HashList: public Mmged
{ {
public: public:
typedef LinkedList<T,MPOOL> DatumList; typedef LinkedList<T,MPOOL> DatumList;
@ -63,9 +61,10 @@ public:
typedef HashList<T,MPOOL,HASHER,COMPARATOR> SelfType; typedef HashList<T,MPOOL,HASHER,COMPARATOR> SelfType;
HashList ( HashList (
Mmgr* mmgr = QSE_NULL,
qse_size_t node_capacity = 10, qse_size_t node_capacity = 10,
qse_size_t load_factor = 75, qse_size_t load_factor = 75,
qse_size_t mpb_size = 0)/*: datum_list (mpb_size)*/ qse_size_t mpb_size = 0): Mmged(mmgr) /*: datum_list (mpb_size)*/
{ {
this->nodes = QSE_NULL; this->nodes = QSE_NULL;
this->node_capacity = 0; this->node_capacity = 0;
@ -73,28 +72,31 @@ public:
try try
{ {
this->nodes = new Node*[node_capacity << 1]; qse_size_t total_count = node_capacity << 1;
//this->nodes = new Node*[total_count];
this->nodes = (Node**)this->getMmgr()->allocMem (QSE_SIZEOF(Node*) * total_count);
this->node_capacity = node_capacity; this->node_capacity = node_capacity;
for (qse_size_t i = 0; i < (node_capacity << 1); i++) for (qse_size_t i = 0; i < total_count; i++)
{ {
this->nodes[i] = QSE_NULL; this->nodes[i] = QSE_NULL;
} }
this->datum_list = new DatumList (mpb_size); this->datum_list = new(this->getMmgr()) DatumList (this->getMmgr(), mpb_size);
} }
catch (...) catch (...)
{ {
if (this->nodes) if (this->nodes)
{ {
delete[] this->nodes; //delete[] this->nodes;
this->getMmgr()->freeMem (this->nodes);
this->node_capacity = 0; this->node_capacity = 0;
this->nodes = QSE_NULL; this->nodes = QSE_NULL;
} }
if (this->datum_list) if (this->datum_list)
{ {
delete this->datum_list; this->delete_datum_list ();
this->datum_list = QSE_NULL; this->datum_list = QSE_NULL;
} }
@ -105,7 +107,7 @@ public:
this->threshold = node_capacity * load_factor / 100; this->threshold = node_capacity * load_factor / 100;
} }
HashList (const SelfType& list)/*: datum_list (list.datum_list.getMPBlockSize()) */ HashList (const SelfType& list): Mmged (list)/*: datum_list (list.datum_list.getMPBlockSize()) */
{ {
this->nodes = QSE_NULL; this->nodes = QSE_NULL;
this->node_capacity = 0; this->node_capacity = 0;
@ -113,26 +115,30 @@ public:
try try
{ {
this->nodes = new Node*[list.node_capacity << 1]; qse_size_t total_count = list.node_capacity << 1;
//this->nodes = new Node*[total_count];
this->nodes = (Node**)this->getMmgr()->allocMem (QSE_SIZEOF(Node*) * total_count);
this->node_capacity = list.node_capacity; this->node_capacity = list.node_capacity;
for (qse_size_t i = 0; i < list.node_capacity << 1; i++) for (qse_size_t i = 0; i < total_count; i++)
{ {
this->nodes[i] = QSE_NULL; this->nodes[i] = QSE_NULL;
} }
this->datum_list = new DatumList (list.datum_list->getMPBlockSize()); this->datum_list = new(list.getMmgr()) DatumList (list.getMmgr(), list.datum_list->getMPBlockSize());
} }
catch (...) catch (...)
{ {
if (this->nodes) if (this->nodes)
{ {
delete[] this->nodes; //delete[] this->nodes;
this->getMmgr()->freeMem (this->nodes);
this->node_capacity = 0; this->node_capacity = 0;
this->nodes = QSE_NULL; this->nodes = QSE_NULL;
} }
if (this->datum_list) if (this->datum_list)
{ {
delete this->datum_list; this->delete_datum_list ();
this->datum_list = QSE_NULL; this->datum_list = QSE_NULL;
} }
@ -163,8 +169,8 @@ public:
~HashList () ~HashList ()
{ {
this->clear (); this->clear ();
if (this->nodes) delete[] this->nodes; if (this->nodes) this->getMmgr()->freeMem (this->nodes); //delete[] this->nodes;
if (this->datum_list) delete this->datum_list; if (this->datum_list) this->delete_datum_list ();
} }
SelfType& operator= (const SelfType& list) SelfType& operator= (const SelfType& list)
@ -416,7 +422,7 @@ protected:
// Move nodes around instead of values to prevent // Move nodes around instead of values to prevent
// existing values from being copied over and destroyed. // existing values from being copied over and destroyed.
// this incurs less number of memory allocations also. // this incurs less number of memory allocations also.
SelfType temp (this->node_capacity << 1, this->load_factor, this->datum_list->getMPBlockSize()); SelfType temp (this->getMmgr(), this->node_capacity << 1, this->load_factor, this->datum_list->getMPBlockSize());
Node* p = this->datum_list->getHeadNode(); Node* p = this->datum_list->getHeadNode();
while (p) while (p)
{ {
@ -497,6 +503,12 @@ protected:
} }
} }
private:
void delete_datum_list ()
{
this->datum_list->~DatumList();
::operator delete (this->datum_list, this->getMmgr());
}
}; };

View File

@ -29,8 +29,6 @@
#include <qse/Types.hpp> #include <qse/Types.hpp>
#include <qse/cmn/Mpool.hpp> #include <qse/cmn/Mpool.hpp>
#include <qse/cmn/Mpoolable.hpp>
///////////////////////////////// /////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE) QSE_BEGIN_NAMESPACE(QSE)
@ -39,7 +37,7 @@ QSE_BEGIN_NAMESPACE(QSE)
template <typename T, typename MPOOL> class LinkedList; template <typename T, typename MPOOL> class LinkedList;
template <typename T,typename MPOOL> template <typename T,typename MPOOL>
class LinkedListNode: protected Mpoolable class LinkedListNode
{ {
public: public:
friend class LinkedList<T,MPOOL>; friend class LinkedList<T,MPOOL>;
@ -84,7 +82,7 @@ protected:
/// ///
/// The LinkedList<T,MPOOL> class provides a template for a doubly-linked list. /// The LinkedList<T,MPOOL> class provides a template for a doubly-linked list.
/// ///
template <typename T, typename MPOOL = Mpool> class LinkedList template <typename T, typename MPOOL = Mpool> class LinkedList: public Mmged
{ {
public: public:
typedef LinkedList<T,MPOOL> SelfType; typedef LinkedList<T,MPOOL> SelfType;
@ -100,14 +98,14 @@ public:
this->clearout (); this->clearout ();
} }
LinkedList (qse_size_t mpb_size = 0): mp (QSE_SIZEOF(Node), mpb_size) LinkedList (Mmgr* mmgr = QSE_NULL, qse_size_t mpb_size = 0): Mmged(mmgr), mp (mmgr, QSE_SIZEOF(Node), mpb_size)
{ {
this->node_count = 0; this->node_count = 0;
this->head_node = QSE_NULL; this->head_node = QSE_NULL;
this->tail_node = QSE_NULL; this->tail_node = QSE_NULL;
} }
LinkedList (const SelfType& ll): mp (ll.mp.getDatumSize(), ll.mp.getBlockSize()) LinkedList (const SelfType& ll): Mmged(ll.getMmgr()), mp (ll.getMmgr(), ll.mp.getDatumSize(), ll.mp.getBlockSize())
{ {
this->node_count = 0; this->node_count = 0;
this->head_node = QSE_NULL; this->head_node = QSE_NULL;
@ -119,6 +117,7 @@ public:
SelfType& operator= (const SelfType& ll) SelfType& operator= (const SelfType& ll)
{ {
this->clear (); this->clear ();
// note that the memory pool itself is not copied.
for (Node* p = ll.head_node; p != QSE_NULL; p = p->next) for (Node* p = ll.head_node; p != QSE_NULL; p = p->next)
this->append (p->value); this->append (p->value);
return *this; return *this;
@ -172,7 +171,7 @@ public:
// create a new node to hold the value and insert it. // create a new node to hold the value and insert it.
Node* insertValue (Node* pos, const T& value) Node* insertValue (Node* pos, const T& value)
{ {
Node* node = new(&mp) Node(value); Node* node = new(&this->mp) Node(value);
return this->insertNode (pos, node); return this->insertNode (pos, node);
} }
@ -307,13 +306,8 @@ public:
//call the destructor //call the destructor
node->~Node (); node->~Node ();
// free the memory
// cal the deallocator ::operator delete (node, &this->mp);
#if defined(_MSC_VER)
node->operator delete (node, &this->mp);
#else
node->dispose (node, &this->mp);
#endif
} }
void remove (qse_size_t index) void remove (qse_size_t index)
@ -499,16 +493,9 @@ public:
{ {
saved = p->next; saved = p->next;
if (this->mp.isDisabled()) delete p; // placement new/delete handling
else p->~Node (); // call the destructor
{ ::operator delete (p, &this->mp); // free the memory
p->~Node ();
#if defined(_MSC_VER)
p->operator delete (p, &this->mp);
#else
p->dispose (p, &this->mp);
#endif
}
this->node_count--; this->node_count--;
p = saved; p = saved;
@ -545,7 +532,6 @@ public:
} }
protected: protected:
//Mpool mp;
MPOOL mp; MPOOL mp;
Node* head_node; Node* head_node;
Node* tail_node; Node* tail_node;

View File

@ -41,7 +41,7 @@ QSE_BEGIN_NAMESPACE(QSE)
class QSE_EXPORT Mmged class QSE_EXPORT Mmged
{ {
public: public:
Mmged (Mmgr* mmgr): mmgr(mmgr) {} Mmged (Mmgr* mmgr = QSE_NULL);
/// ///
/// The getMmgr() function returns the memory manager associated. /// The getMmgr() function returns the memory manager associated.

View File

@ -64,11 +64,11 @@ public:
/// ///
virtual ~Mmgr () {} virtual ~Mmgr () {}
protected: //protected:
/// ///
/// The allocMem() function allocates a chunk of memory of the /// The allocMem() function allocates a chunk of memory of the
/// size @a n and return the pointer to the beginning of the chunk. /// size \a n and return the pointer to the beginning of the chunk.
/// If it fails to allocate memory, it should return QSE_NULL. /// If it fails to allocate memory, it should return #QSE_NULL.
/// ///
virtual void* allocMem ( virtual void* allocMem (
qse_size_t n ///< size of memory chunk to allocate in bytes qse_size_t n ///< size of memory chunk to allocate in bytes
@ -114,4 +114,10 @@ protected:
QSE_END_NAMESPACE(QSE) 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);
void operator delete[] (void* ptr, QSE::Mmgr* mmgr);
#endif #endif

View File

@ -28,7 +28,7 @@
#define _QSE_CMN_MPOOL_HPP_ #define _QSE_CMN_MPOOL_HPP_
#include <qse/Uncopyable.hpp> #include <qse/Uncopyable.hpp>
#include <qse/cmn/ExcMmgr.hpp> #include <qse/cmn/Mmged.hpp>
///////////////////////////////// /////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE) QSE_BEGIN_NAMESPACE(QSE)
@ -38,7 +38,7 @@ QSE_BEGIN_NAMESPACE(QSE)
// allocator for fixed-size data // allocator for fixed-size data
// //
class QSE_EXPORT Mpool: public Uncopyable, protected ExcMmgr class QSE_EXPORT Mpool: public Uncopyable, public Mmged
{ {
public: public:
enum enum
@ -47,6 +47,7 @@ public:
}; };
Mpool ( Mpool (
Mmgr* mmgr,
qse_size_t datum_size, qse_size_t datum_size,
qse_size_t block_size = DEFAULT_BLOCK_SIZE); qse_size_t block_size = DEFAULT_BLOCK_SIZE);
~Mpool (); ~Mpool ();
@ -107,5 +108,9 @@ protected:
///////////////////////////////// /////////////////////////////////
QSE_END_NAMESPACE(QSE) QSE_END_NAMESPACE(QSE)
//////////////////////////////// ////////////////////////////////
void* operator new (qse_size_t size, QSE::Mpool* mp);
void operator delete (void* ptr, QSE::Mpool* mp);
#endif #endif

View File

@ -141,7 +141,7 @@ if ENABLE_CXX
lib_LTLIBRARIES += libqsecmnxx.la lib_LTLIBRARIES += libqsecmnxx.la
libqsecmnxx_la_SOURCES = \ libqsecmnxx_la_SOURCES = \
Mmgr.cpp StdMmgr.cpp ExcMmgr.cpp Mpool.cpp Mmgr.cpp StdMmgr.cpp ExcMmgr.cpp Mmged.cpp Mpool.cpp
libqsecmnxx_la_LDFLAGS = -version-info 1:0:0 -no-undefined libqsecmnxx_la_LDFLAGS = -version-info 1:0:0 -no-undefined
libqsecmnxx_la_LIBADD = libqsecmnxx_la_LIBADD =

View File

@ -150,9 +150,9 @@ libqsecmn_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
$(libqsecmn_la_LDFLAGS) $(LDFLAGS) -o $@ $(libqsecmn_la_LDFLAGS) $(LDFLAGS) -o $@
libqsecmnxx_la_DEPENDENCIES = libqsecmnxx_la_DEPENDENCIES =
am__libqsecmnxx_la_SOURCES_DIST = Mmgr.cpp StdMmgr.cpp ExcMmgr.cpp \ am__libqsecmnxx_la_SOURCES_DIST = Mmgr.cpp StdMmgr.cpp ExcMmgr.cpp \
Mpool.cpp Mmged.cpp Mpool.cpp
@ENABLE_CXX_TRUE@am_libqsecmnxx_la_OBJECTS = Mmgr.lo StdMmgr.lo \ @ENABLE_CXX_TRUE@am_libqsecmnxx_la_OBJECTS = Mmgr.lo StdMmgr.lo \
@ENABLE_CXX_TRUE@ ExcMmgr.lo Mpool.lo @ENABLE_CXX_TRUE@ ExcMmgr.lo Mmged.lo Mpool.lo
libqsecmnxx_la_OBJECTS = $(am_libqsecmnxx_la_OBJECTS) libqsecmnxx_la_OBJECTS = $(am_libqsecmnxx_la_OBJECTS)
libqsecmnxx_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX \ libqsecmnxx_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX \
$(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CXXLD) \ $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CXXLD) \
@ -435,7 +435,7 @@ libqsecmn_la_SOURCES = alg-base64.c alg-rand.c alg-search.c alg-sort.c \
libqsecmn_la_LDFLAGS = -version-info 1:0:0 -no-undefined libqsecmn_la_LDFLAGS = -version-info 1:0:0 -no-undefined
libqsecmn_la_LIBADD = $(SOCKET_LIBS) $(QUADMATH_LIBS) libqsecmn_la_LIBADD = $(SOCKET_LIBS) $(QUADMATH_LIBS)
@ENABLE_CXX_TRUE@libqsecmnxx_la_SOURCES = \ @ENABLE_CXX_TRUE@libqsecmnxx_la_SOURCES = \
@ENABLE_CXX_TRUE@ Mmgr.cpp StdMmgr.cpp ExcMmgr.cpp Mpool.cpp @ENABLE_CXX_TRUE@ Mmgr.cpp StdMmgr.cpp ExcMmgr.cpp Mmged.cpp Mpool.cpp
@ENABLE_CXX_TRUE@libqsecmnxx_la_LDFLAGS = -version-info 1:0:0 -no-undefined @ENABLE_CXX_TRUE@libqsecmnxx_la_LDFLAGS = -version-info 1:0:0 -no-undefined
@ENABLE_CXX_TRUE@libqsecmnxx_la_LIBADD = @ENABLE_CXX_TRUE@libqsecmnxx_la_LIBADD =
@ -517,6 +517,7 @@ distclean-compile:
-rm -f *.tab.c -rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ExcMmgr.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ExcMmgr.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Mmged.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Mmgr.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Mmgr.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Mpool.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Mpool.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/StdMmgr.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/StdMmgr.Plo@am__quote@

41
qse/lib/cmn/Mmged.cpp Normal file
View File

@ -0,0 +1,41 @@
/*
* $Id$
*
Copyright (c) 2006-2014 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <qse/cmn/Mmged.hpp>
#include <qse/cmn/ExcMmgr.hpp>
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
Mmged::Mmged (Mmgr* mmgr)
{
if (!mmgr) mmgr = ExcMmgr::getDFL(); // TODO: use a different manager???? StdMmgr???
this->mmgr = mmgr;
}
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////

View File

@ -48,3 +48,25 @@ void Mmgr::free_mem (mmgr_t* mmgr, void* ptr)
///////////////////////////////// /////////////////////////////////
QSE_END_NAMESPACE(QSE) QSE_END_NAMESPACE(QSE)
///////////////////////////////// /////////////////////////////////
void* operator new (qse_size_t size, QSE::Mmgr* mmgr)
{
return mmgr->allocMem (size);
}
void operator delete (void* ptr, QSE::Mmgr* mmgr)
{
mmgr->freeMem (ptr);
}
#if 0
void* operator new[] (qse_size_t size, QSE::Mmgr* mmgr)
{
return mmgr->allocMem (size);
}
void operator delete[] (void* ptr, QSE::Mmgr* mmgr)
{
mmgr->freeMem (ptr);
}
#endif

View File

@ -31,7 +31,7 @@
QSE_BEGIN_NAMESPACE(QSE) QSE_BEGIN_NAMESPACE(QSE)
///////////////////////////////// /////////////////////////////////
Mpool::Mpool (qse_size_t datum_size, qse_size_t block_size) Mpool::Mpool (Mmgr* mmgr, qse_size_t datum_size, qse_size_t block_size): Mmged(mmgr)
{ {
if (datum_size > 0 && datum_size < QSE_SIZEOF(void*)) if (datum_size > 0 && datum_size < QSE_SIZEOF(void*))
datum_size = QSE_SIZEOF(void*); datum_size = QSE_SIZEOF(void*);
@ -87,7 +87,7 @@ void Mpool::dispose ()
Block* next = block->next; Block* next = block->next;
//::delete[] (qse_uint8_t*)block; //::delete[] (qse_uint8_t*)block;
this->freeMem ((qse_uint8_t*)block); this->mmgr->freeMem ((qse_uint8_t*)block);
block = next; block = next;
} }
@ -107,7 +107,7 @@ Mpool::Block* Mpool::add_block ()
//Block* block = (Block*)::new qse_uint8_t[ //Block* block = (Block*)::new qse_uint8_t[
// QSE_SIZEOF(Block) + this->block_size * this->datum_size]; // QSE_SIZEOF(Block) + this->block_size * this->datum_size];
Block* block = (Block*)this->allocMem (QSE_SIZEOF(Block) + this->block_size * this->datum_size); Block* block = (Block*)this->mmgr->allocMem (QSE_SIZEOF(Block) + this->block_size * this->datum_size);
if (!block) return QSE_NULL; // this line may not be reached if the allocator raises an exception if (!block) return QSE_NULL; // this line may not be reached if the allocator raises an exception
//this->free_list = (Chain*)block->data; //this->free_list = (Chain*)block->data;
@ -135,3 +135,14 @@ Mpool::Block* Mpool::add_block ()
///////////////////////////////// /////////////////////////////////
QSE_END_NAMESPACE(QSE) QSE_END_NAMESPACE(QSE)
///////////////////////////////// /////////////////////////////////
void* operator new (qse_size_t size, QSE::Mpool* mp)
{
return mp->isEnabled()? mp->allocate (): ::operator new (size, mp->getMmgr());
}
void operator delete (void* ptr, QSE::Mpool* mp)
{
if (mp->isEnabled()) mp->dispose (ptr);
else ::operator delete (ptr, mp->getMmgr());
}

View File

@ -43,7 +43,7 @@ void* StdMmgr::reallocMem (void* ptr, qse_size_t n)
} }
void StdMmgr::freeMem (void* ptr) void StdMmgr::freeMem (void* ptr)
{ {
::free (ptr); ::free (ptr);
} }