dropped ExcMmgr and enhanced Mmgr to incoporate both exception raising and error returing
This commit is contained in:
@ -1,56 +0,0 @@
|
||||
/*
|
||||
* $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.
|
||||
*/
|
||||
|
||||
#ifndef _QSE_CMN_EXCMMGR_HPP_
|
||||
#define _QSE_CMN_EXCMMGR_HPP_
|
||||
|
||||
#include <qse/cmn/Mmgr.hpp>
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
/// The ExcMmgr class implements the memory manager interface that
|
||||
/// raises an exception upon failure. You can use the StdMmgr class
|
||||
/// if #QSE_NULL should be returned upon failure.
|
||||
|
||||
class QSE_EXPORT ExcMmgr: public Mmgr
|
||||
{
|
||||
public:
|
||||
void* allocMem (qse_size_t n);
|
||||
void* reallocMem (void* ptr, qse_size_t n);
|
||||
void freeMem (void* ptr);
|
||||
|
||||
/// The getInstance() function returns the stock instance of the StdMmgr
|
||||
/// class.
|
||||
static ExcMmgr* getInstance();
|
||||
};
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_END_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
#endif
|
@ -66,6 +66,9 @@ public:
|
||||
qse_size_t load_factor = 75,
|
||||
qse_size_t mpb_size = 0): Mmged(mmgr)
|
||||
{
|
||||
if (node_capacity <= 0) node_capacity = 1;
|
||||
if (load_factor < 20) load_factor = 20;
|
||||
|
||||
this->nodes = QSE_NULL;
|
||||
this->node_capacity = 0;
|
||||
this->datum_list = QSE_NULL;
|
||||
@ -77,7 +80,7 @@ public:
|
||||
// Node* is a plain type that doesn't have any constructors and destructors.
|
||||
// it should be safe to call the memory manager bypassing the new operator.
|
||||
//this->nodes = new Node*[total_count];
|
||||
this->nodes = (Node**)this->getMmgr()->allocMem (QSE_SIZEOF(Node*) * total_count);
|
||||
this->nodes = (Node**)this->getMmgr()->allocate (QSE_SIZEOF(Node*) * total_count);
|
||||
|
||||
// NOTE: something wil go wrong if the memory manager doesn't raise an exception
|
||||
// upon memory allocation failure. Make sure to use a memory allocation
|
||||
@ -95,7 +98,7 @@ public:
|
||||
{
|
||||
if (this->nodes)
|
||||
{
|
||||
this->getMmgr()->freeMem (this->nodes); //delete[] this->nodes;
|
||||
this->getMmgr()->dispose (this->nodes); //delete[] this->nodes;
|
||||
this->nodes = QSE_NULL;
|
||||
this->node_capacity = 0;
|
||||
}
|
||||
@ -123,7 +126,7 @@ public:
|
||||
{
|
||||
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->nodes = (Node**)this->getMmgr()->allocate (QSE_SIZEOF(Node*) * total_count);
|
||||
|
||||
this->node_capacity = list.node_capacity;
|
||||
for (qse_size_t i = 0; i < total_count; i++)
|
||||
@ -139,7 +142,7 @@ public:
|
||||
{
|
||||
if (this->nodes)
|
||||
{
|
||||
this->getMmgr()->freeMem (this->nodes); //delete[] this->nodes;
|
||||
this->getMmgr()->dispose (this->nodes); //delete[] this->nodes;
|
||||
this->nodes = QSE_NULL;
|
||||
this->node_capacity = 0;
|
||||
}
|
||||
@ -176,7 +179,7 @@ public:
|
||||
~HashList ()
|
||||
{
|
||||
this->clear ();
|
||||
if (this->nodes) this->getMmgr()->freeMem (this->nodes); //delete[] this->nodes;
|
||||
if (this->nodes) this->getMmgr()->dispose (this->nodes); //delete[] this->nodes;
|
||||
if (this->datum_list) this->free_datum_list ();
|
||||
}
|
||||
|
||||
|
@ -51,7 +51,7 @@ pkginclude_HEADERS = \
|
||||
|
||||
if ENABLE_CXX
|
||||
pkginclude_HEADERS += \
|
||||
Mmgr.hpp StdMmgr.hpp ExcMmgr.hpp HeapMmgr.hpp Mmged.hpp \
|
||||
Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp \
|
||||
Mpool.hpp Mpoolable.hpp LinkedList.hpp HashList.hpp
|
||||
endif
|
||||
|
||||
|
@ -51,7 +51,7 @@ POST_UNINSTALL = :
|
||||
build_triplet = @build@
|
||||
host_triplet = @host@
|
||||
@ENABLE_CXX_TRUE@am__append_1 = \
|
||||
@ENABLE_CXX_TRUE@ Mmgr.hpp StdMmgr.hpp ExcMmgr.hpp HeapMmgr.hpp Mmged.hpp \
|
||||
@ENABLE_CXX_TRUE@ Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp \
|
||||
@ENABLE_CXX_TRUE@ Mpool.hpp Mpoolable.hpp LinkedList.hpp HashList.hpp
|
||||
|
||||
subdir = include/qse/cmn
|
||||
@ -90,8 +90,8 @@ am__pkginclude_HEADERS_DIST = alg.h chr.h cp949.h cp950.h dir.h dll.h \
|
||||
lda.h main.h map.h mb8.h mbwc.h mem.h mux.h nwad.h nwif.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 ExcMmgr.hpp HeapMmgr.hpp \
|
||||
Mmged.hpp Mpool.hpp Mpoolable.hpp LinkedList.hpp HashList.hpp
|
||||
utf8.h xma.h Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp \
|
||||
Mpool.hpp Mpoolable.hpp LinkedList.hpp HashList.hpp
|
||||
am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
|
||||
am__vpath_adj = case $$p in \
|
||||
$(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \
|
||||
|
@ -41,6 +41,12 @@ QSE_BEGIN_NAMESPACE(QSE)
|
||||
/// #qse_mmgr_t type. Using the class over the primitive type enables you to
|
||||
/// write code in more object-oriented fashion. An inheriting class should
|
||||
/// implement three pure virtual functions.
|
||||
///
|
||||
/// You are free to call allocMem(), reallocMem(), and freeMem() in C++ context
|
||||
/// where no exception raising is desired. If you want an exception to be
|
||||
/// raised upon memory allocation errors, you can call allocate(), reallocate(),
|
||||
/// dispose() instead.
|
||||
///
|
||||
///
|
||||
class QSE_EXPORT Mmgr: public qse_mmgr_t
|
||||
{
|
||||
@ -50,11 +56,15 @@ public:
|
||||
|
||||
QSE_EXCEPTION (MemoryError);
|
||||
|
||||
protected:
|
||||
bool raise_exception;
|
||||
|
||||
public:
|
||||
///
|
||||
/// The Mmgr() function builds a memory manager composed of bridge
|
||||
/// functions connecting itself with it.
|
||||
///
|
||||
Mmgr ()
|
||||
Mmgr (bool raise_exception = true): raise_exception (raise_exception)
|
||||
{
|
||||
this->alloc = alloc_mem;
|
||||
this->realloc = realloc_mem;
|
||||
@ -67,6 +77,38 @@ public:
|
||||
///
|
||||
virtual ~Mmgr () {}
|
||||
|
||||
///
|
||||
/// The allocate() function calls allocMem() for memory
|
||||
/// allocation. if it fails, it raise an exception if it's
|
||||
/// configured to do so.
|
||||
///
|
||||
void* allocate (qse_size_t n)
|
||||
{
|
||||
void* xptr = this->allocMem (n);
|
||||
if (!xptr && this->raise_exception) QSE_THROW (MemoryError);
|
||||
return xptr;
|
||||
}
|
||||
|
||||
///
|
||||
/// The reallocate() function calls reallocMem() for memory
|
||||
/// reallocation. if it fails, it raise an exception if it's
|
||||
/// configured to do so.
|
||||
///
|
||||
void* reallocate (void* ptr, qse_size_t n)
|
||||
{
|
||||
void* xptr = this->reallocMem (ptr, n);
|
||||
if (!xptr && this->raise_exception) QSE_THROW (MemoryError);
|
||||
return xptr;
|
||||
}
|
||||
|
||||
///
|
||||
/// The dispose() function calls freeMem() for memory disposal.
|
||||
///
|
||||
void dispose (void* ptr)
|
||||
{
|
||||
this->freeMem (ptr);
|
||||
}
|
||||
|
||||
//protected:
|
||||
///
|
||||
/// The allocMem() function allocates a chunk of memory of the
|
||||
|
Reference in New Issue
Block a user