changed the memory allocation scheme for some c++ classes
This commit is contained in:
		| @ -141,7 +141,7 @@ if ENABLE_CXX | ||||
|  | ||||
| lib_LTLIBRARIES += libqsecmnxx.la | ||||
| 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_LIBADD =  | ||||
|  | ||||
|  | ||||
| @ -150,9 +150,9 @@ libqsecmn_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ | ||||
| 	$(libqsecmn_la_LDFLAGS) $(LDFLAGS) -o $@ | ||||
| libqsecmnxx_la_DEPENDENCIES = | ||||
| 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@	ExcMmgr.lo Mpool.lo | ||||
| @ENABLE_CXX_TRUE@	ExcMmgr.lo Mmged.lo Mpool.lo | ||||
| libqsecmnxx_la_OBJECTS = $(am_libqsecmnxx_la_OBJECTS) | ||||
| libqsecmnxx_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX \ | ||||
| 	$(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_LIBADD = $(SOCKET_LIBS) $(QUADMATH_LIBS) | ||||
| @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_LIBADD =  | ||||
| @ -517,6 +517,7 @@ distclean-compile: | ||||
| 	-rm -f *.tab.c | ||||
|  | ||||
| @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)/Mpool.Plo@am__quote@ | ||||
| @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/StdMmgr.Plo@am__quote@ | ||||
|  | ||||
							
								
								
									
										41
									
								
								qse/lib/cmn/Mmged.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								qse/lib/cmn/Mmged.cpp
									
									
									
									
									
										Normal 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) | ||||
| ///////////////////////////////// | ||||
| @ -48,3 +48,25 @@ void Mmgr::free_mem (mmgr_t* mmgr, void* ptr) | ||||
| ///////////////////////////////// | ||||
| 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 | ||||
|  | ||||
| @ -31,7 +31,7 @@ | ||||
| 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*)) | ||||
| 		datum_size = QSE_SIZEOF(void*); | ||||
| @ -87,7 +87,7 @@ void Mpool::dispose () | ||||
| 		Block* next = block->next; | ||||
|  | ||||
| 		//::delete[] (qse_uint8_t*)block; | ||||
| 		this->freeMem ((qse_uint8_t*)block); | ||||
| 		this->mmgr->freeMem ((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->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 | ||||
|  | ||||
| 	//this->free_list = (Chain*)block->data; | ||||
| @ -135,3 +135,14 @@ Mpool::Block* Mpool::add_block () | ||||
| ///////////////////////////////// | ||||
| 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()); | ||||
| } | ||||
|  | ||||
| @ -43,7 +43,7 @@ void* StdMmgr::reallocMem (void* ptr, qse_size_t n) | ||||
| } | ||||
|  | ||||
| void StdMmgr::freeMem (void* ptr)  | ||||
| {  | ||||
| { | ||||
| 	::free (ptr);  | ||||
| } | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user