changed the position of the mmgr parameter in some constructors.

removed StdMmgr::getInstance() and initialized the default mmgr inside Mmgr::getDFL() to work around an issue related to c++ initialization order across translation units
This commit is contained in:
hyung-hwan 2018-10-31 10:40:25 +00:00
parent b01cf731ca
commit efc34cdd69
26 changed files with 87 additions and 88 deletions

View File

@ -106,12 +106,12 @@ public:
}
public:
Array (qse_size_t capacity = DEFAULT_CAPACITY): Mmged(QSE_NULL)
Array (Mmgr* mmgr = QSE_NULL): Mmged(mmgr)
{
this->init_array (capacity);
this->init_array (DEFAULT_CAPACITY);
}
Array (Mmgr* mmgr, qse_size_t capacity = DEFAULT_CAPACITY): Mmged(mmgr)
Array (qse_size_t capacity, Mmgr* mmgr = QSE_NULL): Mmged(mmgr)
{
this->init_array (capacity);
}

View File

@ -123,14 +123,14 @@ public:
INVALID_INDEX = ParentType::INVALID_INDEX
};
BinaryHeap (qse_size_t capacity = DEFAULT_CAPACITY): ParentType (QSE_NULL, capacity) {}
BinaryHeap (Mmgr* mmgr = QSE_NULL): ParentType(DEFAULT_CAPACITY, mmgr) {}
BinaryHeap (Mmgr* mmgr, qse_size_t capacity = DEFAULT_CAPACITY): ParentType (mmgr, capacity) {}
BinaryHeap (qse_size_t capacity, Mmgr* mmgr = QSE_NULL): ParentType(capacity, mmgr) {}
BinaryHeap (const SelfType& heap): ParentType (heap) {}
BinaryHeap (const SelfType& heap): ParentType(heap) {}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
BinaryHeap (SelfType&& heap): ParentType (QSE_CPP_RVREF(heap)) {}
BinaryHeap (SelfType&& heap): ParentType(QSE_CPP_RVREF(heap)) {}
#endif
~BinaryHeap () {}

View File

@ -137,7 +137,7 @@ private:
this->nodes[i] = QSE_NULL;
}
this->datum_list = new(this->getMmgr()) DatumList (this->getMmgr(), mpb_size);
this->datum_list = new(this->getMmgr()) DatumList(mpb_size, this->getMmgr());
}
catch (...)
{
@ -163,20 +163,14 @@ private:
public:
HashList (
qse_size_t node_capacity = DEFAULT_CAPACITY,
qse_size_t load_factor = DEFAULT_LOAD_FACTOR,
qse_size_t mpb_size = 0): Mmged(QSE_NULL)
Mmgr* mmgr = QSE_NULL): Mmged(mmgr)
{
this->init_list (node_capacity, load_factor, mpb_size);
this->init_list (DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, 0);
}
HashList (
Mmgr* mmgr,
qse_size_t node_capacity = DEFAULT_CAPACITY,
qse_size_t load_factor = DEFAULT_LOAD_FACTOR,
qse_size_t mpb_size = 0): Mmged(mmgr)
HashList (qse_size_t node_capacity, qse_size_t load_factor = DEFAULT_LOAD_FACTOR, qse_size_t mpb_size = 0, Mmgr* mmgr = QSE_NULL): Mmged(mmgr)
{
this->init_list (node_capacity, load_factor, mpb_size);
this->init_list (DEFAULT_CAPACITY, load_factor, mpb_size);
}
HashList (const SelfType& list): Mmged(list)
@ -198,8 +192,7 @@ public:
}
// placement new
this->datum_list = new(list.getMmgr())
DatumList (list.getMmgr(), list.getMpool().getBlockSize());
this->datum_list = new(list.getMmgr()) DatumList(list.getMpool().getBlockSize(), list.getMmgr());
}
catch (...)
{
@ -675,7 +668,7 @@ protected:
// to be inserted are yielded off the original list and inserted
// without new allocation.
//SelfType temp (this->getMmgr(), this->_resizer(this->node_capacity), this->load_factor, mpool.getBlockSize());
SelfType temp (this->getMmgr(), this->_resizer(this->node_capacity, this->getGrowthPolicy()), this->load_factor, 0);
SelfType temp (this->_resizer(this->node_capacity, this->getGrowthPolicy()), this->load_factor, 0, this->getMmgr());
Node* p = this->datum_list->getHeadNode();
while (p)
{

View File

@ -121,18 +121,17 @@ public:
MIN_LOAD_FACTOR = PairList::MIN_LOAD_FACTOR
};
HashTable (qse_size_t capacity = DEFAULT_CAPACITY,
qse_size_t load_factor = DEFAULT_LOAD_FACTOR,
qse_size_t mpb_size = 0):
Mmged(QSE_NULL), pair_list(QSE_NULL, capacity, load_factor, mpb_size)
HashTable (Mmgr* mmgr = QSE_NULL):
Mmged(mmgr), pair_list(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, 0, mmgr)
{
}
HashTable (Mmgr* mmgr,
qse_size_t capacity = DEFAULT_CAPACITY,
HashTable (qse_size_t capacity,
qse_size_t load_factor = DEFAULT_LOAD_FACTOR,
qse_size_t mpb_size = 0):
Mmged(mmgr), pair_list(mmgr, capacity, load_factor, mpb_size)
qse_size_t mpb_size = 0,
Mmgr* mmgr = QSE_NULL):
Mmged(mmgr), pair_list(capacity, load_factor, mpb_size, mmgr)
{
}

View File

@ -40,7 +40,7 @@ QSE_BEGIN_NAMESPACE(QSE)
/// managed by another memory manager.
///
/// \code
/// QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
/// QSE::HeapMmgr heap_mmgr (30000, QSE::Mmgr::getDFL());
/// QSE::LinkedList<int> int_list (&heap_mmgr);
/// int_list.append (10);
/// int_list.append (20);
@ -49,11 +49,9 @@ QSE_BEGIN_NAMESPACE(QSE)
class QSE_EXPORT HeapMmgr: public Mmgr, public Mmged
{
public:
HeapMmgr (qse_size_t heap_size) QSE_CPP_NOEXCEPT;
/// The constructor function accepts an memory manager \a mmgr that
/// is used to create a heap of the size \a heap_size.
HeapMmgr (Mmgr* mmgr, qse_size_t heap_size) QSE_CPP_NOEXCEPT;
HeapMmgr (qse_size_t heap_size, Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT;
/// The destructor function frees the heap. Memory areas returned by
/// allocate(), reallocate(), allocMem(), reallocMem() are invalidated

View File

@ -211,16 +211,14 @@ public:
this->clear (true);
}
LinkedList (qse_size_t mpb_size = 0):
Mmged(QSE_NULL), mp(QSE_NULL, QSE_SIZEOF(Node), mpb_size)
LinkedList (Mmgr* mmgr = QSE_NULL): Mmged(mmgr), mp(QSE_NULL, QSE_SIZEOF(Node), 0)
{
this->node_count = 0;
this->head_node = QSE_NULL;
this->tail_node = QSE_NULL;
}
LinkedList (Mmgr* mmgr, qse_size_t mpb_size = 0):
Mmged(mmgr), mp(mmgr, QSE_SIZEOF(Node), mpb_size)
LinkedList (qse_size_t mpb_size, Mmgr* mmgr = QSE_NULL): Mmged(mmgr), mp(mmgr, QSE_SIZEOF(Node), mpb_size)
{
this->node_count = 0;
this->head_node = QSE_NULL;

View File

@ -98,13 +98,11 @@ public:
typedef typename PairTree::ConstIterator ConstIterator;
RedBlackTable (qse_size_t mpb_size = 0):
Mmged(QSE_NULL), pair_tree(QSE_NULL, mpb_size)
RedBlackTable (Mmgr* mmgr = QSE_NULL): Mmged(mmgr), pair_tree(mmgr, 0)
{
}
RedBlackTable (Mmgr* mmgr, qse_size_t mpb_size = 0):
Mmged(mmgr), pair_tree(mmgr, mpb_size)
RedBlackTable (qse_size_t mpb_size, Mmgr* mmgr = QSE_NULL): Mmged(mmgr), pair_tree(mpb_size, mmgr)
{
}

View File

@ -340,18 +340,17 @@ private:
}
public:
RedBlackTree (qse_size_t mpb_size = 0):
Mmged(QSE_NULL), mp(QSE_NULL, QSE_SIZEOF(Node), mpb_size), node_count(0)
RedBlackTree (Mmgr* mmgr = QSE_NULL): Mmged(mmgr), mp(mmgr, QSE_SIZEOF(Node), 0), node_count(0)
{
this->init_tree ();
}
RedBlackTree (Mmgr* mmgr, qse_size_t mpb_size = 0):
Mmged(mmgr), mp(mmgr, QSE_SIZEOF(Node), mpb_size), node_count(0)
RedBlackTree (qse_size_t mpb_size, Mmgr* mmgr = QSE_NULL): Mmged(mmgr), mp(QSE_NULL, QSE_SIZEOF(Node), mpb_size), node_count(0)
{
this->init_tree ();
}
RedBlackTree (const SelfType& rbt):
Mmged(rbt.getMmgr()),
mp(rbt.getMmgr(), rbt.mp.getDatumSize(), rbt.mp.getBlockSize()),

View File

@ -46,9 +46,11 @@ public:
void* reallocMem (void* ptr, qse_size_t n) QSE_CPP_NOEXCEPT;
void freeMem (void* ptr) QSE_CPP_NOEXCEPT;
#if 0
/// The getInstance() function returns the stock instance of the StdMmgr
/// class.
static StdMmgr* getInstance () QSE_CPP_NOEXCEPT;
#endif
};
/////////////////////////////////

View File

@ -185,8 +185,7 @@ private:
typedef StrBase<qse_wchar_t, QSE_WT('\0'), WcStringOpset> ParentType;
public:
WcString (): ParentType() {}
WcString (Mmgr* mmgr): ParentType(mmgr) {}
WcString (Mmgr* mmgr = QSE_NULL): ParentType(mmgr) {}
WcString (int capacity, Mmgr* mmgr = QSE_NULL): ParentType(capacity, mmgr) {}
WcString (qse_size_t capacity, Mmgr* mmgr = QSE_NULL): ParentType(capacity, mmgr) {}
WcString (const qse_wchar_t* str, Mmgr* mmgr = QSE_NULL): ParentType(str, mmgr) {}
@ -217,8 +216,7 @@ private:
typedef StrBase<qse_mchar_t, QSE_MT('\0'), MbStringOpset> ParentType;
public:
MbString (): ParentType() {}
MbString (Mmgr* mmgr): ParentType(mmgr) {}
MbString (Mmgr* mmgr = QSE_NULL): ParentType(mmgr) {}
MbString (int capacity, Mmgr* mmgr = QSE_NULL): ParentType(capacity, mmgr) {}
MbString (qse_size_t capacity, Mmgr* mmgr = QSE_NULL): ParentType(capacity, mmgr) {}
MbString (const qse_mchar_t* str, Mmgr* mmgr = QSE_NULL): ParentType(str, mmgr) {}

View File

@ -36,10 +36,10 @@ class SttpCmd: public QSE::Array<QSE::String>
{
public:
SttpCmd (const qse_char_t* n = QSE_T(""), QSE::Mmgr* mmgr = QSE_NULL):
QSE::Array<QSE::String>(mmgr, 20), name (n) {}
QSE::Array<QSE::String>(20, mmgr), name (n) {}
SttpCmd (const QSE::String& n, QSE::Mmgr* mmgr = QSE_NULL):
QSE::Array<QSE::String>(mmgr, 20), name (n) {}
QSE::Array<QSE::String>(20, mmgr), name (n) {}
qse_size_t getArgCount () const
{

View File

@ -36,12 +36,7 @@ struct xma_xtn_t
HeapMmgr* heap;
};
HeapMmgr::HeapMmgr (qse_size_t heap_size) QSE_CPP_NOEXCEPT:
Mmgr(), Mmged(QSE_NULL), xma(QSE_NULL), heap_size (heap_size)
{
}
HeapMmgr::HeapMmgr (Mmgr* mmgr, qse_size_t heap_size) QSE_CPP_NOEXCEPT:
HeapMmgr::HeapMmgr (qse_size_t heap_size, Mmgr* mmgr) QSE_CPP_NOEXCEPT:
Mmgr(), Mmged(mmgr), xma(QSE_NULL), heap_size (heap_size)
{
}

View File

@ -54,10 +54,26 @@ void* Mmgr::callocate (qse_size_t n, bool raise_exception) QSE_CPP_THREXCEPT1(Me
return ptr;
}
Mmgr* Mmgr::dfl_mmgr = StdMmgr::getInstance();
#if 0
#if defined(__GNUC__)
static StdMmgr __attribute__((init_priority(101))) std_dfl_mmgr;
#else
static StdMmgr std_dfl_mmgr;
#endif
Mmgr* Mmgr::dfl_mmgr = &std_dfl_mmgr;
//Mmgr* Mmgr::dfl_mmgr = StdMmgr::getInstance(); <--- has an issue with initialization order
#else
// C++ initialization order across translation units are not defined.
// so it's tricky to work around this... the init_priority attribute
// can solve this problem. but it's compiler dependent.
// Mmgr::getDFL() resets dfl_mmgr to &std_dfl_mmgr if it's NULL.
Mmgr* Mmgr::dfl_mmgr = QSE_NULL;
#endif
Mmgr* Mmgr::getDFL () QSE_CPP_NOEXCEPT
{
static StdMmgr std_dfl_mmgr;
if (!Mmgr::dfl_mmgr) Mmgr::dfl_mmgr = &std_dfl_mmgr;
return Mmgr::dfl_mmgr;
}

View File

@ -47,11 +47,14 @@ void StdMmgr::freeMem (void* ptr) QSE_CPP_NOEXCEPT
::free (ptr);
}
#if 0
StdMmgr* StdMmgr::getInstance () QSE_CPP_NOEXCEPT
{
static StdMmgr DFL;
return &DFL;
}
#endif
/////////////////////////////////
QSE_END_NAMESPACE(QSE)

View File

@ -73,8 +73,8 @@ typedef QSE::Array<PosStr> StrArray;
int main ()
{
//StrArray h (QSE_NULL, 100);
StrArray h (QSE_NULL, 15);
//StrArray h (100);
StrArray h (15);
char buf[20];
for (int i = 0; i < 20; i++)

View File

@ -93,8 +93,8 @@ printf ("================\n");
printf ("----------------------\n");
#endif
//QSE::HashList<T> h (mmgr, 1000, 75, 1000);
QSE::HashList<T> h (mmgr, 1000, 75, 500);
//QSE::HashList<T> h (1000, 75, 1000, mmgr);
QSE::HashList<T> h (1000, 75, 500, mmgr);
for (int i = 0; i < 1000; i++)
{
T x(i);
@ -102,7 +102,7 @@ for (int i = 0; i < 1000; i++)
}
printf ("h.getSize() => %d\n", (int)h.getSize());
QSE::HashList<T> h2 (mmgr, 1000, 75, 700);
QSE::HashList<T> h2 (1000, 75, 700, mmgr);
h2 = h;
for (QSE::HashList<T>::Iterator it = h2.getIterator(); it.isLegit(); it++)

View File

@ -71,10 +71,10 @@ int main ()
{
// qse_open_stdsios ();
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 3000000);
QSE::HeapMmgr heap_mmgr (3000000, QSE::Mmgr::getDFL());
//QSE::HashTable<int,int,IntHasher> int_table (&heap_mmgr, 1000);
IntTable int_table (NULL, IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 1000);
//IntTable int_table (NULL, IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 0);
IntTable int_table (IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 1000);
//IntTable int_table (IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 0);
printf ("----------\n");
for (int i = 0; i < 100; i++)
{
@ -112,8 +112,8 @@ printf ("----------\n");
printf ("%d\n", int_table[9990]);
*/
//Str1Table s1 (NULL, IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 1000);
Str1Table s1 (NULL, IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 0);
//Str1Table s1 (IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 1000);
Str1Table s1 (IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 0);
//Str1Table s1;
s1.insert ("hello", 20);
s1.insert ("hello", 20);
@ -150,8 +150,8 @@ printf ("----------\n");
printf ("------------------\n");
{
Str1Table s11 (&heap_mmgr, IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 0);
//Str1Table s11 (NULL, IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 200);
Str1Table s11 (IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 0, &heap_mmgr);
//Str1Table s11 (IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 200);
//Str1Table s11;
for (int i = 0; i < 100; i++)

View File

@ -22,7 +22,7 @@ typedef QSE::RedBlackTree<IntPair> IntTree;
int main ()
{
IntTree t (0, 10000);
IntTree t (10000, QSE_NULL);
//IntTree t (0);
for (int i = 0; i < 20 ; i++)
@ -54,7 +54,7 @@ int main ()
printf ("------------------\n");
t.insert (IntPair(99, 999));
t.insert (IntPair(88, 888));
//IntTree y (QSE_NULL, 5);
//IntTree y (5, QSE_NULL);
x = t;
for (it = x.getConstIterator(); it.isLegit(); ++it)
{

View File

@ -39,10 +39,10 @@ int main ()
{
// qse_open_stdsios ();
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 3000000);
//QSE::RedBlackTable<int,int,IntHasher> int_table (&heap_mmgr, 1000);
IntTable int_table (NULL, 1000);
//IntTable int_table (NULL, 0);
QSE::HeapMmgr heap_mmgr (3000000, QSE::Mmgr::getDFL());
//QSE::RedBlackTable<int,int,IntHasher> int_table (1000, &heap_mmgr);
IntTable int_table (1000, QSE_NULL);
//IntTable int_table (0, QSE_NULL);
printf ("----------\n");
for (int i = 0; i < 100; i++)
{
@ -80,8 +80,8 @@ printf ("----------\n");
printf ("%d\n", int_table[9990]);
*/
//Str1Table s1 (NULL, 1000);
Str1Table s1 (NULL, 0);
//Str1Table s1 (1000, QSE_NULL);
Str1Table s1 (0, QSE_NULL);
//Str1Table s1;
s1.insert ("hello", 20);
s1.insert ("hello", 20);
@ -118,8 +118,8 @@ printf ("----------\n");
printf ("------------------\n");
{
Str1Table s11 (&heap_mmgr, 0);
//Str1Table s11 (NULL, 200);
Str1Table s11 (0, &heap_mmgr);
//Str1Table s11 (200, QSE_NULL);
//Str1Table s11;
for (int i = 0; i < 100; i++)

View File

@ -28,7 +28,7 @@ struct destroy_x_in_mmgr
int main ()
{
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
QSE::HeapMmgr heap_mmgr (30000, QSE::Mmgr::getDFL());
{
QSE::ScopedPtr<X> x1 (new X);

View File

@ -45,8 +45,8 @@ struct destroy_x_in_mmgr
int f2 ()
{
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
QSE::HeapMmgr heap_mmgr_2 (QSE::Mmgr::getDFL(), 30000);
QSE::HeapMmgr heap_mmgr (30000, QSE::Mmgr::getDFL());
QSE::HeapMmgr heap_mmgr_2 (30000, QSE::Mmgr::getDFL());
QSE::SharedPtr<X> y (new X(1));
QSE::SharedPtr<X,QSE::SharedPtrMmgrDeleter<X> > k (&heap_mmgr);

View File

@ -6,7 +6,7 @@
void t1 ()
{
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
QSE::HeapMmgr heap_mmgr (30000, QSE::Mmgr::getDFL());
QSE::String* z = new QSE::String();
{

View File

@ -99,7 +99,7 @@ protected:
static int test1()
{
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
QSE::HeapMmgr heap_mmgr (30000, QSE::Mmgr::getDFL());
MyApp app (&heap_mmgr);
MyApp app2 (&heap_mmgr);

View File

@ -62,7 +62,7 @@ static QSE::TcpServerF<ClientHandler>* g_server;
static int test1 (void)
{
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
QSE::HeapMmgr heap_mmgr (30000, QSE::Mmgr::getDFL());
#if defined(QSE_LANG_CPP11)
QSE::TcpServerL<int(QSE::TcpServer::Worker*)> server (

View File

@ -18,7 +18,7 @@ static int g_stopreq = 0;
static qse_ntime_t sleep_interval = { 1, 0 };
QSE::HeapMmgr g_heap_mmgr (QSE::Mmgr::getDFL(), 30000);
QSE::HeapMmgr g_heap_mmgr (30000, QSE::Mmgr::getDFL());
class MyThread: public QSE::Thread
{

View File

@ -19,7 +19,7 @@
static int g_stopreq = 0;
static qse_ntime_t sleep_interval = { 1, 0 };
QSE::HeapMmgr g_heap_mmgr (QSE::Mmgr::getDFL(), 30000);
QSE::HeapMmgr g_heap_mmgr (30000, QSE::Mmgr::getDFL());
struct rq_data_t
{