changed the memory allocation scheme for some c++ classes

This commit is contained in:
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_
#define _QSE_EXCEPTION_CLASS_
#ifndef _QSE_EXCEPTION_HPP_
#define _QSE_EXCEPTION_HPP_
#include <qse/types.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> >
class HashList
class HashList: public Mmged
{
public:
typedef LinkedList<T,MPOOL> DatumList;
@ -63,9 +61,10 @@ public:
typedef HashList<T,MPOOL,HASHER,COMPARATOR> SelfType;
HashList (
Mmgr* mmgr = QSE_NULL,
qse_size_t node_capacity = 10,
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->node_capacity = 0;
@ -73,28 +72,31 @@ public:
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;
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->datum_list = new DatumList (mpb_size);
this->datum_list = new(this->getMmgr()) DatumList (this->getMmgr(), mpb_size);
}
catch (...)
{
if (this->nodes)
{
delete[] this->nodes;
//delete[] this->nodes;
this->getMmgr()->freeMem (this->nodes);
this->node_capacity = 0;
this->nodes = QSE_NULL;
}
if (this->datum_list)
{
delete this->datum_list;
this->delete_datum_list ();
this->datum_list = QSE_NULL;
}
@ -105,7 +107,7 @@ public:
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->node_capacity = 0;
@ -113,26 +115,30 @@ public:
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;
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->datum_list = new DatumList (list.datum_list->getMPBlockSize());
this->datum_list = new(list.getMmgr()) DatumList (list.getMmgr(), list.datum_list->getMPBlockSize());
}
catch (...)
{
if (this->nodes)
{
delete[] this->nodes;
//delete[] this->nodes;
this->getMmgr()->freeMem (this->nodes);
this->node_capacity = 0;
this->nodes = QSE_NULL;
}
if (this->datum_list)
{
delete this->datum_list;
this->delete_datum_list ();
this->datum_list = QSE_NULL;
}
@ -163,8 +169,8 @@ public:
~HashList ()
{
this->clear ();
if (this->nodes) delete[] this->nodes;
if (this->datum_list) delete this->datum_list;
if (this->nodes) this->getMmgr()->freeMem (this->nodes); //delete[] this->nodes;
if (this->datum_list) this->delete_datum_list ();
}
SelfType& operator= (const SelfType& list)
@ -416,7 +422,7 @@ protected:
// Move nodes around instead of values to prevent
// existing values from being copied over and destroyed.
// 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();
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/cmn/Mpool.hpp>
#include <qse/cmn/Mpoolable.hpp>
/////////////////////////////////
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 LinkedListNode: protected Mpoolable
class LinkedListNode
{
public:
friend class LinkedList<T,MPOOL>;
@ -84,7 +82,7 @@ protected:
///
/// 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:
typedef LinkedList<T,MPOOL> SelfType;
@ -100,14 +98,14 @@ public:
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->head_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->head_node = QSE_NULL;
@ -119,6 +117,7 @@ public:
SelfType& operator= (const SelfType& ll)
{
this->clear ();
// note that the memory pool itself is not copied.
for (Node* p = ll.head_node; p != QSE_NULL; p = p->next)
this->append (p->value);
return *this;
@ -172,7 +171,7 @@ public:
// create a new node to hold the value and insert it.
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);
}
@ -307,13 +306,8 @@ public:
//call the destructor
node->~Node ();
// cal the deallocator
#if defined(_MSC_VER)
node->operator delete (node, &this->mp);
#else
node->dispose (node, &this->mp);
#endif
// free the memory
::operator delete (node, &this->mp);
}
void remove (qse_size_t index)
@ -499,16 +493,9 @@ public:
{
saved = p->next;
if (this->mp.isDisabled()) delete p;
else
{
p->~Node ();
#if defined(_MSC_VER)
p->operator delete (p, &this->mp);
#else
p->dispose (p, &this->mp);
#endif
}
// placement new/delete handling
p->~Node (); // call the destructor
::operator delete (p, &this->mp); // free the memory
this->node_count--;
p = saved;
@ -545,7 +532,6 @@ public:
}
protected:
//Mpool mp;
MPOOL mp;
Node* head_node;
Node* tail_node;

View File

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

View File

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

View File

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