2015-01-02 06:40:41 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2015-01-02 06:40:41 +00:00
|
|
|
|
|
|
|
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/Mpool.hpp>
|
2015-01-05 04:15:32 +00:00
|
|
|
#include <qse/cmn/mem.h>
|
2015-01-02 06:40:41 +00:00
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
QSE_BEGIN_NAMESPACE(QSE)
|
|
|
|
/////////////////////////////////
|
|
|
|
|
2015-02-01 16:07:26 +00:00
|
|
|
Mpool::Mpool (Mmgr* mmgr, qse_size_t datum_size, qse_size_t block_size): Mmged(mmgr)
|
2015-01-02 06:40:41 +00:00
|
|
|
{
|
|
|
|
if (datum_size > 0 && datum_size < QSE_SIZEOF(void*))
|
|
|
|
datum_size = QSE_SIZEOF(void*);
|
|
|
|
|
|
|
|
this->mp_blocks = QSE_NULL;
|
|
|
|
this->free_list = QSE_NULL;
|
|
|
|
this->datum_size = datum_size;
|
|
|
|
this->block_size = block_size;
|
|
|
|
#if defined(QSE_DEBUG_MPOOL)
|
|
|
|
this->navail = 0;
|
|
|
|
this->nalloc = 0;
|
|
|
|
#endif
|
|
|
|
}
|
2015-01-05 04:15:32 +00:00
|
|
|
|
2015-01-02 06:40:41 +00:00
|
|
|
Mpool::~Mpool ()
|
|
|
|
{
|
|
|
|
this->dispose ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void* Mpool::allocate ()
|
|
|
|
{
|
|
|
|
if (this->datum_size <= 0 || this->block_size <= 0) return QSE_NULL;
|
|
|
|
|
|
|
|
void* ptr = this->free_list;
|
|
|
|
if (!ptr)
|
|
|
|
{
|
2015-01-05 04:15:32 +00:00
|
|
|
// NOTE: 'return QSE_NULL' can't be reached if add_block()
|
|
|
|
// raises an exception.
|
|
|
|
if (!this->add_block ()) return QSE_NULL;
|
2015-01-02 06:40:41 +00:00
|
|
|
ptr = this->free_list;
|
|
|
|
}
|
|
|
|
this->free_list = this->free_list->next;
|
|
|
|
#if defined(QSE_DEBUG_MPOOL)
|
|
|
|
this->navail--;
|
|
|
|
#endif
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mpool::dispose (void* ptr)
|
|
|
|
{
|
|
|
|
((Chain*)ptr)->next = this->free_list;
|
|
|
|
this->free_list = (Chain*)ptr;
|
|
|
|
#if defined(QSE_DEBUG_MPOOL)
|
|
|
|
this->navail++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mpool::dispose ()
|
|
|
|
{
|
|
|
|
Block* block = this->mp_blocks;
|
|
|
|
while (block)
|
|
|
|
{
|
|
|
|
Block* next = block->next;
|
2015-01-05 04:15:32 +00:00
|
|
|
|
|
|
|
//::delete[] (qse_uint8_t*)block;
|
2015-03-11 01:47:26 +00:00
|
|
|
this->getMmgr()->dispose ((qse_uint8_t*)block);
|
2015-01-05 04:15:32 +00:00
|
|
|
|
2015-01-02 06:40:41 +00:00
|
|
|
block = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->free_list = QSE_NULL;
|
|
|
|
this->mp_blocks = QSE_NULL;
|
2015-01-05 04:15:32 +00:00
|
|
|
|
2015-01-02 06:40:41 +00:00
|
|
|
#if defined(QSE_DEBUG_MPOOL)
|
|
|
|
this->navail = 0;
|
|
|
|
this->nalloc = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-01-05 04:15:32 +00:00
|
|
|
Mpool::Block* Mpool::add_block ()
|
2015-01-02 06:40:41 +00:00
|
|
|
{
|
|
|
|
QSE_ASSERT (this->datum_size > 0 && this->block_size > 0);
|
|
|
|
|
2015-01-05 04:15:32 +00:00
|
|
|
//Block* block = (Block*)::new qse_uint8_t[
|
|
|
|
// QSE_SIZEOF(Block) + this->block_size * this->datum_size];
|
2015-03-11 01:47:26 +00:00
|
|
|
Block* block = (Block*)this->getMmgr()->allocate (QSE_SIZEOF(Block) + this->block_size * this->datum_size);
|
2015-01-05 04:15:32 +00:00
|
|
|
if (!block) return QSE_NULL; // this line may not be reached if the allocator raises an exception
|
2015-01-02 06:40:41 +00:00
|
|
|
|
|
|
|
//this->free_list = (Chain*)block->data;
|
|
|
|
this->free_list = (Chain*)(block + 1);
|
|
|
|
Chain* ptr = this->free_list;
|
|
|
|
for (qse_size_t i = 0; i < this->block_size - 1; i++)
|
|
|
|
{
|
|
|
|
Chain* next = (Chain*)((qse_uint8_t*)ptr + this->datum_size);
|
|
|
|
ptr->next = next;
|
|
|
|
ptr = next;
|
|
|
|
}
|
|
|
|
ptr->next = QSE_NULL;
|
|
|
|
|
2015-01-05 04:15:32 +00:00
|
|
|
block->next = this->mp_blocks;
|
2015-01-02 06:40:41 +00:00
|
|
|
this->mp_blocks = block;
|
2015-01-05 04:15:32 +00:00
|
|
|
|
2015-01-02 06:40:41 +00:00
|
|
|
#if defined(QSE_DEBUG_MPOOL)
|
|
|
|
this->navail += this->block_size;
|
|
|
|
this->nalloc += this->block_size;
|
|
|
|
#endif
|
2015-01-05 04:15:32 +00:00
|
|
|
|
|
|
|
return block;
|
2015-01-02 06:40:41 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 15:42:45 +00:00
|
|
|
int Mpool::swap (Mpool& mpool)
|
|
|
|
{
|
|
|
|
// this function is sensitive to member variables changes.
|
|
|
|
// whenever you add new member variables, this function require
|
|
|
|
// relevant changes.
|
|
|
|
|
|
|
|
if (this->getMmgr() != mpool.getMmgr())
|
|
|
|
{
|
|
|
|
// disallow to swap contents if memory managers are different.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this != &mpool)
|
|
|
|
{
|
|
|
|
#if defined(QSE_DEBUG_MPOOL)
|
|
|
|
qse_size_t org_nalloc = this->nalloc;
|
|
|
|
qse_size_t org_navail = this->navail;
|
|
|
|
|
|
|
|
this->nalloc = mpool.nalloc;
|
|
|
|
this->navail = mpool.navail;
|
|
|
|
|
|
|
|
mpool.nalloc = org_nalloc;
|
|
|
|
mpool.navail = org_navail;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Block* org_mp_blocks = this->mp_blocks;
|
|
|
|
Chain* org_free_list = this->free_list;
|
|
|
|
qse_size_t org_datum_size = this->datum_size;
|
|
|
|
qse_size_t org_block_size = this->block_size;
|
|
|
|
|
|
|
|
this->mp_blocks = mpool.mp_blocks;
|
|
|
|
this->free_list = mpool.free_list;
|
|
|
|
this->datum_size = mpool.datum_size;
|
|
|
|
this->block_size = mpool.block_size;
|
|
|
|
|
|
|
|
mpool.mp_blocks = org_mp_blocks;
|
|
|
|
mpool.free_list = org_free_list;
|
|
|
|
mpool.datum_size = org_datum_size;
|
|
|
|
mpool.block_size = org_block_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-02 06:40:41 +00:00
|
|
|
/////////////////////////////////
|
|
|
|
QSE_END_NAMESPACE(QSE)
|
|
|
|
/////////////////////////////////
|
2015-02-01 16:07:26 +00:00
|
|
|
|
|
|
|
void* operator new (qse_size_t size, QSE::Mpool* mp)
|
|
|
|
{
|
2015-03-06 18:24:49 +00:00
|
|
|
if (mp->isEnabled())
|
|
|
|
{
|
2015-03-07 03:55:40 +00:00
|
|
|
QSE_ASSERTX (size == mp->getDatumSize(),
|
|
|
|
"Pass the right allocation size for the given memory pool");
|
2015-03-06 18:24:49 +00:00
|
|
|
// the size argument is not really used. you must make sure that
|
|
|
|
// the given size matches the datum size of the memory pool.
|
|
|
|
if (size != mp->getDatumSize())
|
|
|
|
{
|
2015-03-07 03:55:40 +00:00
|
|
|
// when the assertion above is excluded during the build,
|
|
|
|
// bypass the memory pool if a wrong size is passed in.
|
|
|
|
// debug your application properly so that this block
|
|
|
|
// is never reached.
|
|
|
|
goto use_mmgr;
|
2015-03-06 18:24:49 +00:00
|
|
|
}
|
2015-03-24 09:34:57 +00:00
|
|
|
return mp->allocate();
|
2015-03-06 18:24:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-07 03:55:40 +00:00
|
|
|
use_mmgr:
|
|
|
|
// when the memory pool is not enabled, it goes through
|
2015-03-06 18:24:49 +00:00
|
|
|
// the memory manager directly and it honors the size argument.
|
2015-03-24 09:34:57 +00:00
|
|
|
//return ::operator new(size, mp->getMmgr());
|
|
|
|
return mp->getMmgr()->allocate(size);
|
2015-03-06 18:24:49 +00:00
|
|
|
}
|
2015-02-01 16:07:26 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 09:34:57 +00:00
|
|
|
#if defined(QSE_CPP_NO_OPERATOR_DELETE_OVERLOADING)
|
|
|
|
void qse_operator_delete (void* ptr, QSE::Mpool* mp)
|
|
|
|
#else
|
2015-02-01 16:07:26 +00:00
|
|
|
void operator delete (void* ptr, QSE::Mpool* mp)
|
2015-03-24 09:34:57 +00:00
|
|
|
#endif
|
2015-02-01 16:07:26 +00:00
|
|
|
{
|
2015-03-24 09:34:57 +00:00
|
|
|
if (mp->isEnabled()) mp->dispose(ptr);
|
|
|
|
else mp->getMmgr()->dispose(ptr);
|
2015-02-01 16:07:26 +00:00
|
|
|
}
|
2015-03-04 16:30:09 +00:00
|
|
|
|
|
|
|
void* operator new (qse_size_t size, QSE::Mpool* mp, void* existing_ptr)
|
|
|
|
{
|
|
|
|
return existing_ptr;
|
|
|
|
}
|