diff --git a/qse/include/qse/Hashable.hpp b/qse/include/qse/Hashable.hpp index 928f588c..a25c5085 100644 --- a/qse/include/qse/Hashable.hpp +++ b/qse/include/qse/Hashable.hpp @@ -57,7 +57,7 @@ public: static qse_size_t getHashCode (const qse_char_t* str) { - return this->getHashCode (0, str); + return Hashable::getHashCode (0, str); } static qse_size_t getHashCode (qse_size_t init, const void* data, qse_size_t size) @@ -88,7 +88,7 @@ public: static qse_size_t getHashCode (const void* data, qse_size_t size) { - return this->getHashCode (0, data, size); + return Hashable::getHashCode (0, data, size); } }; diff --git a/qse/include/qse/Makefile.am b/qse/include/qse/Makefile.am index 897a9aeb..38ce8d39 100644 --- a/qse/include/qse/Makefile.am +++ b/qse/include/qse/Makefile.am @@ -9,7 +9,7 @@ pkginclude_HEADERS = \ if ENABLE_CXX pkginclude_HEADERS += \ Types.hpp Hashable.hpp Uncopyable.hpp RefCounted.hpp \ - ScopedPtr.hpp Exception.hpp + Exception.hpp endif install-data-hook: diff --git a/qse/include/qse/Makefile.in b/qse/include/qse/Makefile.in index 279625c7..02b641c6 100644 --- a/qse/include/qse/Makefile.in +++ b/qse/include/qse/Makefile.in @@ -52,7 +52,7 @@ build_triplet = @build@ host_triplet = @host@ @ENABLE_CXX_TRUE@am__append_1 = \ @ENABLE_CXX_TRUE@ Types.hpp Hashable.hpp Uncopyable.hpp RefCounted.hpp \ -@ENABLE_CXX_TRUE@ ScopedPtr.hpp Exception.hpp +@ENABLE_CXX_TRUE@ Exception.hpp subdir = include/qse DIST_COMMON = $(am__pkginclude_HEADERS_DIST) $(srcdir)/Makefile.am \ @@ -95,7 +95,7 @@ am__can_run_installinfo = \ am__pkginclude_HEADERS_DIST = conf-msw.h conf-os2.h conf-dos.h \ conf-vms.h conf-mac.h conf-inf.h types.h macros.h pack1.h \ unpack.h Types.hpp Hashable.hpp Uncopyable.hpp RefCounted.hpp \ - ScopedPtr.hpp Exception.hpp + Exception.hpp am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; am__vpath_adj = case $$p in \ $(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \ diff --git a/qse/include/qse/cmn/Array.hpp b/qse/include/qse/cmn/Array.hpp index 49c52500..60aaf652 100644 --- a/qse/include/qse/cmn/Array.hpp +++ b/qse/include/qse/cmn/Array.hpp @@ -75,7 +75,8 @@ public: INVALID_INDEX = ~(qse_size_t)0 }; - Array (Mmgr* mmgr = QSE_NULL, qse_size_t capacity = DEFAULT_CAPACITY): Mmged (mmgr) +private: + void init_array (int capacity) { if (capacity <= 0) { @@ -92,6 +93,17 @@ public: this->count = 0; } +public: + Array (qse_size_t capacity = DEFAULT_CAPACITY): Mmged (QSE_NULL) + { + this->init_array (capacity); + } + + Array (Mmgr* mmgr, qse_size_t capacity = DEFAULT_CAPACITY): Mmged (mmgr) + { + this->init_array (capacity); + } + Array (const SelfType& array): Mmged (array.getMmgr()), count (0), capacity (0), buffer (QSE_NULL) diff --git a/qse/include/qse/cmn/BinaryHeap.hpp b/qse/include/qse/cmn/BinaryHeap.hpp index 6cddd667..38fc3f0a 100644 --- a/qse/include/qse/cmn/BinaryHeap.hpp +++ b/qse/include/qse/cmn/BinaryHeap.hpp @@ -117,7 +117,12 @@ public: INVALID_INDEX = ParentType::INVALID_INDEX }; - BinaryHeap (Mmgr* mmgr = QSE_NULL, qse_size_t capacity = DEFAULT_CAPACITY): + BinaryHeap (qse_size_t capacity = DEFAULT_CAPACITY): + ParentType (QSE_NULL, capacity) + { + } + + BinaryHeap (Mmgr* mmgr, qse_size_t capacity = DEFAULT_CAPACITY): ParentType (mmgr, capacity) { } diff --git a/qse/include/qse/cmn/HashList.hpp b/qse/include/qse/cmn/HashList.hpp index 0c5ffa13..fd66316f 100644 --- a/qse/include/qse/cmn/HashList.hpp +++ b/qse/include/qse/cmn/HashList.hpp @@ -99,11 +99,8 @@ public: MIN_LOAD_FACTOR = 20 }; - HashList ( - Mmgr* mmgr = QSE_NULL, - qse_size_t node_capacity = DEFAULT_CAPACITY, - qse_size_t load_factor = DEFAULT_LOAD_FACTOR, - qse_size_t mpb_size = 0): Mmged(mmgr) +private: + void init_list (qse_size_t node_capacity, qse_size_t load_factor, qse_size_t mpb_size) { if (node_capacity < MIN_CAPACITY) node_capacity = MIN_CAPACITY; if (load_factor < MIN_LOAD_FACTOR) load_factor = MIN_LOAD_FACTOR; @@ -151,7 +148,25 @@ public: this->threshold = node_capacity * load_factor / 100; } - HashList (const SelfType& list): Mmged (list) +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) + { + this->init_list (node_capacity, load_factor, mpb_size); + } + + 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) + { + this->init_list (node_capacity, load_factor, mpb_size); + } + + HashList (const SelfType& list): Mmged(list) { this->nodes = QSE_NULL; this->node_capacity = 0; diff --git a/qse/include/qse/cmn/HashTable.hpp b/qse/include/qse/cmn/HashTable.hpp index 2ebe3575..310d5c94 100644 --- a/qse/include/qse/cmn/HashTable.hpp +++ b/qse/include/qse/cmn/HashTable.hpp @@ -117,15 +117,22 @@ public: MIN_LOAD_FACTOR = PairList::MIN_LOAD_FACTOR }; - HashTable (Mmgr* mmgr = QSE_NULL, - qse_size_t capacity = DEFAULT_CAPACITY, + HashTable (qse_size_t capacity = DEFAULT_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) + Mmged(QSE_NULL), pair_list(QSE_NULL, capacity, load_factor, mpb_size) { } - HashTable (const SelfType& table): Mmged (table), pair_list (table.pair_list) + HashTable (Mmgr* mmgr, + qse_size_t capacity = DEFAULT_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) + { + } + + HashTable (const SelfType& table): Mmged(table), pair_list(table.pair_list) { } diff --git a/qse/include/qse/cmn/LinkedList.hpp b/qse/include/qse/cmn/LinkedList.hpp index 2655dd26..a9499290 100644 --- a/qse/include/qse/cmn/LinkedList.hpp +++ b/qse/include/qse/cmn/LinkedList.hpp @@ -211,8 +211,16 @@ public: this->clear (true); } - LinkedList (Mmgr* mmgr = QSE_NULL, qse_size_t mpb_size = 0): - Mmged(mmgr), mp (mmgr, QSE_SIZEOF(Node), mpb_size) + LinkedList (qse_size_t mpb_size = 0): + Mmged(QSE_NULL), mp(QSE_NULL, QSE_SIZEOF(Node), mpb_size) + { + 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) { this->node_count = 0; this->head_node = QSE_NULL; diff --git a/qse/include/qse/cmn/Makefile.am b/qse/include/qse/cmn/Makefile.am index 9534f993..8a5f58e1 100644 --- a/qse/include/qse/cmn/Makefile.am +++ b/qse/include/qse/cmn/Makefile.am @@ -51,7 +51,8 @@ pkginclude_HEADERS = \ if ENABLE_CXX pkginclude_HEADERS += \ - Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp SharedPtr.hpp \ + Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp \ + ScopedPtr.hpp SharedPtr.hpp \ Mpool.hpp Association.hpp LinkedList.hpp HashList.hpp HashTable.hpp \ RedBlackTree.hpp RedBlackTable.hpp \ Array.hpp BinaryHeap.hpp diff --git a/qse/include/qse/cmn/Makefile.in b/qse/include/qse/cmn/Makefile.in index f42622a6..2dac188d 100644 --- a/qse/include/qse/cmn/Makefile.in +++ b/qse/include/qse/cmn/Makefile.in @@ -51,7 +51,8 @@ POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ @ENABLE_CXX_TRUE@am__append_1 = \ -@ENABLE_CXX_TRUE@ Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp SharedPtr.hpp \ +@ENABLE_CXX_TRUE@ Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp \ +@ENABLE_CXX_TRUE@ ScopedPtr.hpp SharedPtr.hpp \ @ENABLE_CXX_TRUE@ Mpool.hpp Association.hpp LinkedList.hpp HashList.hpp HashTable.hpp \ @ENABLE_CXX_TRUE@ RedBlackTree.hpp RedBlackTable.hpp \ @ENABLE_CXX_TRUE@ Array.hpp BinaryHeap.hpp @@ -93,9 +94,9 @@ am__pkginclude_HEADERS_DIST = alg.h chr.h cp949.h cp950.h dir.h dll.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 HeapMmgr.hpp Mmged.hpp \ - SharedPtr.hpp Mpool.hpp Association.hpp LinkedList.hpp \ - HashList.hpp HashTable.hpp RedBlackTree.hpp RedBlackTable.hpp \ - Array.hpp BinaryHeap.hpp + ScopedPtr.hpp SharedPtr.hpp Mpool.hpp Association.hpp \ + LinkedList.hpp HashList.hpp HashTable.hpp RedBlackTree.hpp \ + RedBlackTable.hpp Array.hpp BinaryHeap.hpp am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; am__vpath_adj = case $$p in \ $(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \ diff --git a/qse/include/qse/cmn/Mmged.hpp b/qse/include/qse/cmn/Mmged.hpp index b7dbd7d7..120d678c 100644 --- a/qse/include/qse/cmn/Mmged.hpp +++ b/qse/include/qse/cmn/Mmged.hpp @@ -46,10 +46,19 @@ public: /// /// The getMmgr() function returns the memory manager associated. /// - Mmgr* getMmgr () const { return this->mmgr; } + Mmgr* getMmgr () const { return this->_mmgr; } protected: - Mmgr* mmgr; + /// + /// The setMmgr() function changes the memory manager. + /// Changing memory manager requires extra care to be taken + /// especially when you have some data allocated with the previous + /// manager. for this reason, i put this as a protected function. + /// + void setMmgr(Mmgr* mmgr); + +private: + Mmgr* _mmgr; }; ///////////////////////////////// diff --git a/qse/include/qse/cmn/Mmgr.hpp b/qse/include/qse/cmn/Mmgr.hpp index 7950c236..dab33d23 100644 --- a/qse/include/qse/cmn/Mmgr.hpp +++ b/qse/include/qse/cmn/Mmgr.hpp @@ -193,24 +193,4 @@ void* operator new[] (qse_size_t size, QSE::Mmgr* mmgr); void operator delete[] (void* ptr, QSE::Mmgr* mmgr); #endif - -///////////////////////////////// -QSE_BEGIN_NAMESPACE(QSE) -///////////////////////////////// - -// Customized deleter for ScopedPtr -template -struct ScopedPtrMmgrDeleter -{ - void operator() (T* ptr, void* arg) - { - ptr->~T (); - ::operator delete (ptr, (QSE::Mmgr*)arg); - } -}; - -///////////////////////////////// -QSE_END_NAMESPACE(QSE) -///////////////////////////////// - #endif diff --git a/qse/include/qse/cmn/RedBlackTable.hpp b/qse/include/qse/cmn/RedBlackTable.hpp index 35f09b8b..2d249e3a 100644 --- a/qse/include/qse/cmn/RedBlackTable.hpp +++ b/qse/include/qse/cmn/RedBlackTable.hpp @@ -91,12 +91,17 @@ public: typedef typename PairTree::ConstIterator ConstIterator; - RedBlackTable (Mmgr* mmgr = QSE_NULL, qse_size_t mpb_size = 0): - Mmged(mmgr), pair_tree (mmgr, mpb_size) + RedBlackTable (qse_size_t mpb_size = 0): + Mmged(QSE_NULL), pair_tree(QSE_NULL, mpb_size) { } - RedBlackTable (const SelfType& table): Mmged (table), pair_tree (table.pair_tree) + RedBlackTable (Mmgr* mmgr, qse_size_t mpb_size = 0): + Mmged(mmgr), pair_tree(mmgr, mpb_size) + { + } + + RedBlackTable (const SelfType& table): Mmged(table), pair_tree(table.pair_tree) { } diff --git a/qse/include/qse/cmn/RedBlackTree.hpp b/qse/include/qse/cmn/RedBlackTree.hpp index dcaa46e3..eb5690f3 100644 --- a/qse/include/qse/cmn/RedBlackTree.hpp +++ b/qse/include/qse/cmn/RedBlackTree.hpp @@ -319,10 +319,8 @@ public: typedef RedBlackTreeComparator DefaultComparator; - RedBlackTree (Mmgr* mmgr = QSE_NULL, qse_size_t mpb_size = 0): - Mmged (mmgr), - mp (mmgr, QSE_SIZEOF(Node), mpb_size), - node_count (0) +private: + void init_tree (qse_size_t mpb_size) { #if defined(QSE_REDBLACKTREE_ALLOCATE_NIL) // create a nil object. note it doesn't go into the memory pool. @@ -338,6 +336,19 @@ public: this->root = this->nil; } +public: + RedBlackTree (qse_size_t mpb_size = 0): + Mmged(QSE_NULL), mp(QSE_NULL, QSE_SIZEOF(Node), mpb_size), node_count(0) + { + this->init_tree (mpb_size); + } + + RedBlackTree (Mmgr* mmgr, qse_size_t mpb_size = 0): + Mmged (mmgr), mp (mmgr, QSE_SIZEOF(Node), mpb_size), node_count (0) + { + this->init_tree (mpb_size); + } + RedBlackTree (const SelfType& rbt): Mmged (rbt.getMmgr()), mp (rbt.getMmgr(), rbt.mp.getDatumSize(), rbt.mp.getBlockSize()), diff --git a/qse/include/qse/ScopedPtr.hpp b/qse/include/qse/cmn/ScopedPtr.hpp similarity index 90% rename from qse/include/qse/ScopedPtr.hpp rename to qse/include/qse/cmn/ScopedPtr.hpp index 785e0502..f300ea4a 100644 --- a/qse/include/qse/ScopedPtr.hpp +++ b/qse/include/qse/cmn/ScopedPtr.hpp @@ -24,10 +24,11 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _QSE_SCOPEDPTR_HPP_ -#define _QSE_SCOPEDPTR_HPP_ +#ifndef _QSE_CMN_SCOPEDPTR_HPP_ +#define _QSE_CMN_SCOPEDPTR_HPP_ #include +#include ///////////////////////////////// QSE_BEGIN_NAMESPACE(QSE) @@ -51,6 +52,16 @@ struct ScopedPtrArrayDeleter } }; +template +struct ScopedPtrMmgrDeleter +{ + void operator() (T* ptr, void* arg) + { + ptr->~T (); + ::operator delete (ptr, (QSE::Mmgr*)arg); + } +}; + /// The ScopedPtr class is a template class that destroys the object the /// pointer points to when its destructor is called. You can use this class /// to free a certain resource associated to the pointer when it goes out @@ -58,7 +69,7 @@ struct ScopedPtrArrayDeleter /// /// \code /// #include -/// #include +/// #include /// #include /// /// @@ -69,14 +80,6 @@ struct ScopedPtrArrayDeleter /// ~X() { printf ("X destructed\n"); } /// }; /// -/// struct destroy_x_in_mmgr -/// { -/// void operator() (X* x, void* arg) -/// { -/// x->~X(); -/// ::operator delete (x, (QSE::Mmgr*)arg); -/// } -/// }; /// /// int main () /// { @@ -85,7 +88,7 @@ struct ScopedPtrArrayDeleter /// { /// QSE::ScopedPtr x1 (new X); /// QSE::ScopedPtr > x3 (new X[10]); -/// QSE::ScopedPtr x2 (new(&heap_mmgr) X, &heap_mmgr); +/// QSE::ScopedPtr > x2 (new(&heap_mmgr) X, &heap_mmgr); /// } /// /// return 0; @@ -97,7 +100,7 @@ template > class QSE_EXPORT ScopedPtr: public Uncopyable { public: - typedef SharedPtr SelfType; + typedef ScopedPtr SelfType; typedef ScopedPtrDeleter DefaultDeleter; diff --git a/qse/include/qse/cmn/SharedPtr.hpp b/qse/include/qse/cmn/SharedPtr.hpp index e990dd4a..73f706e5 100644 --- a/qse/include/qse/cmn/SharedPtr.hpp +++ b/qse/include/qse/cmn/SharedPtr.hpp @@ -119,7 +119,7 @@ public: // must copy the memory manager pointer as the item // to be copied is allocated using the memory manager of sp. - this->mmgr = sp.getMmgr(); + this->setMmgr (sp.getMmgr()); this->item = sp.item; this->item->ref++; diff --git a/qse/lib/cmn/Mmged.cpp b/qse/lib/cmn/Mmged.cpp index 0133d881..c1fd8172 100644 --- a/qse/lib/cmn/Mmged.cpp +++ b/qse/lib/cmn/Mmged.cpp @@ -33,8 +33,15 @@ QSE_BEGIN_NAMESPACE(QSE) Mmged::Mmged (Mmgr* mmgr) { if (!mmgr) mmgr = Mmgr::getDFL(); - this->mmgr = mmgr; + this->_mmgr = mmgr; } + +void Mmged::setMmgr (Mmgr* mmgr) +{ + if (!mmgr) mmgr = Mmgr::getDFL(); + this->_mmgr = mmgr; +} + ///////////////////////////////// QSE_END_NAMESPACE(QSE) ///////////////////////////////// diff --git a/qse/lib/cmn/Mpool.cpp b/qse/lib/cmn/Mpool.cpp index 31fe7b1e..f62375ca 100644 --- a/qse/lib/cmn/Mpool.cpp +++ b/qse/lib/cmn/Mpool.cpp @@ -87,7 +87,7 @@ void Mpool::dispose () Block* next = block->next; //::delete[] (qse_uint8_t*)block; - this->mmgr->dispose ((qse_uint8_t*)block); + this->getMmgr()->dispose ((qse_uint8_t*)block); block = next; } @@ -107,7 +107,7 @@ Mpool::Block* Mpool::add_block () //Block* block = (Block*)::new qse_uint8_t[ // QSE_SIZEOF(Block) + this->block_size * this->datum_size]; - Block* block = (Block*)this->mmgr->allocate (QSE_SIZEOF(Block) + this->block_size * this->datum_size); + Block* block = (Block*)this->getMmgr()->allocate (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 //this->free_list = (Chain*)block->data; diff --git a/qse/lib/sed/Sed.cpp b/qse/lib/sed/Sed.cpp index 39bc5cc2..d068debc 100644 --- a/qse/lib/sed/Sed.cpp +++ b/qse/lib/sed/Sed.cpp @@ -35,7 +35,7 @@ QSE_BEGIN_NAMESPACE(QSE) int Sed::open () { - sed = qse_sed_open (this->mmgr, QSE_SIZEOF(Sed*)); + sed = qse_sed_open (this->getMmgr(), QSE_SIZEOF(Sed*)); if (!sed) return -1; *(Sed**)QSE_XTN(sed) = this;