added ExcMmgr

simplified LinkedList
This commit is contained in:
2015-01-05 04:15:32 +00:00
parent 3b672857aa
commit 8d88ef3f40
17 changed files with 274 additions and 117 deletions

63
qse/lib/cmn/ExcMmgr.cpp Normal file
View File

@ -0,0 +1,63 @@
/*
* $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/ExcMmgr.hpp>
#include <stdlib.h>
#include <stdio.h>
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
void* ExcMmgr::allocMem (qse_size_t n)
{
void* ptr = ::malloc (n);
if (!ptr) throw 1; // TODO: change 1 to a proper exception object
return ptr;
}
void* ExcMmgr::reallocMem (void* ptr, qse_size_t n)
{
void* xptr = ::realloc (ptr, n);
if (!xptr) throw 1;
return xptr;
}
void ExcMmgr::freeMem (void* ptr)
{
::free (ptr);
}
ExcMmgr* ExcMmgr::getDFL ()
{
static ExcMmgr DFL;
return &DFL;
}
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////

View File

@ -141,7 +141,7 @@ if ENABLE_CXX
lib_LTLIBRARIES += libqsecmnxx.la
libqsecmnxx_la_SOURCES = \
Mmgr.cpp StdMmgr.cpp Mpool.cpp
Mmgr.cpp StdMmgr.cpp ExcMmgr.cpp Mpool.cpp
libqsecmnxx_la_LDFLAGS = -version-info 1:0:0 -no-undefined
libqsecmnxx_la_LIBADD =

View File

@ -149,9 +149,10 @@ libqsecmn_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
$(libqsecmn_la_LDFLAGS) $(LDFLAGS) -o $@
libqsecmnxx_la_DEPENDENCIES =
am__libqsecmnxx_la_SOURCES_DIST = Mmgr.cpp StdMmgr.cpp Mpool.cpp
am__libqsecmnxx_la_SOURCES_DIST = Mmgr.cpp StdMmgr.cpp ExcMmgr.cpp \
Mpool.cpp
@ENABLE_CXX_TRUE@am_libqsecmnxx_la_OBJECTS = Mmgr.lo StdMmgr.lo \
@ENABLE_CXX_TRUE@ Mpool.lo
@ENABLE_CXX_TRUE@ ExcMmgr.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) \
@ -434,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 Mpool.cpp
@ENABLE_CXX_TRUE@ Mmgr.cpp StdMmgr.cpp ExcMmgr.cpp Mpool.cpp
@ENABLE_CXX_TRUE@libqsecmnxx_la_LDFLAGS = -version-info 1:0:0 -no-undefined
@ENABLE_CXX_TRUE@libqsecmnxx_la_LIBADD =
@ -515,6 +516,7 @@ mostlyclean-compile:
distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ExcMmgr.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@

View File

@ -30,12 +30,12 @@
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
void* Mmgr::alloc_mem (mmgr_t* mmgr, size_t n)
void* Mmgr::alloc_mem (mmgr_t* mmgr, qse_size_t n)
{
return ((Mmgr*)mmgr->ctx)->allocMem (n);
}
void* Mmgr::realloc_mem (mmgr_t* mmgr, void* ptr, size_t n)
void* Mmgr::realloc_mem (mmgr_t* mmgr, void* ptr, qse_size_t n)
{
return ((Mmgr*)mmgr->ctx)->reallocMem (ptr, n);
}

View File

@ -25,9 +25,7 @@
*/
#include <qse/cmn/Mpool.hpp>
// TODO: can i use QSE_MMGR_XXXXX instead of ::new and ::delete???
#include <qse/cmn/mem.h>
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
@ -47,6 +45,7 @@ Mpool::Mpool (qse_size_t datum_size, qse_size_t block_size)
this->nalloc = 0;
#endif
}
Mpool::~Mpool ()
{
this->dispose ();
@ -59,7 +58,9 @@ void* Mpool::allocate ()
void* ptr = this->free_list;
if (!ptr)
{
this->add_block ();
// NOTE: 'return QSE_NULL' can't be reached if add_block()
// raises an exception.
if (!this->add_block ()) return QSE_NULL;
ptr = this->free_list;
}
this->free_list = this->free_list->next;
@ -84,24 +85,30 @@ void Mpool::dispose ()
while (block)
{
Block* next = block->next;
::delete[] block;
//::delete[] (qse_uint8_t*)block;
this->freeMem ((qse_uint8_t*)block);
block = next;
}
this->free_list = QSE_NULL;
this->mp_blocks = QSE_NULL;
#if defined(QSE_DEBUG_MPOOL)
this->navail = 0;
this->nalloc = 0;
#endif
}
void Mpool::add_block ()
Mpool::Block* Mpool::add_block ()
{
QSE_ASSERT (this->datum_size > 0 && this->block_size > 0);
Block* block = (Block*)::new qse_uint8_t[
QSE_SIZEOF(Block) + this->block_size * this->datum_size];
//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);
if (!block) return QSE_NULL; // this line may not be reached if the allocator raises an exception
//this->free_list = (Chain*)block->data;
this->free_list = (Chain*)(block + 1);
@ -114,12 +121,15 @@ void Mpool::add_block ()
}
ptr->next = QSE_NULL;
block->next = this->mp_blocks;
block->next = this->mp_blocks;
this->mp_blocks = block;
#if defined(QSE_DEBUG_MPOOL)
this->navail += this->block_size;
this->nalloc += this->block_size;
#endif
return block;
}
/////////////////////////////////

View File

@ -32,12 +32,12 @@ QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
void* StdMmgr::allocMem (size_t n)
void* StdMmgr::allocMem (qse_size_t n)
{
return ::malloc (n);
return ::malloc (n);
}
void* StdMmgr::reallocMem (void* ptr, size_t n)
void* StdMmgr::reallocMem (void* ptr, qse_size_t n)
{
return ::realloc (ptr, n);
}