2009-12-19 06:34:42 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2014-11-19 14:42:24 +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.
|
2009-12-19 06:34:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <qse/cmn/Mmgr.hpp>
|
2015-02-02 10:45:57 +00:00
|
|
|
#include <qse/cmn/StdMmgr.hpp>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "mem-prv.h"
|
2009-12-19 06:34:42 +00:00
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
QSE_BEGIN_NAMESPACE(QSE)
|
|
|
|
/////////////////////////////////
|
|
|
|
|
2018-07-01 02:11:33 +00:00
|
|
|
void* Mmgr::alloc_mem (mmgr_t* mmgr, qse_size_t n) QSE_CPP_NOEXCEPT
|
2009-12-19 06:34:42 +00:00
|
|
|
{
|
2014-07-09 15:01:16 +00:00
|
|
|
return ((Mmgr*)mmgr->ctx)->allocMem (n);
|
2009-12-19 06:34:42 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 02:11:33 +00:00
|
|
|
void* Mmgr::realloc_mem (mmgr_t* mmgr, void* ptr, qse_size_t n) QSE_CPP_NOEXCEPT
|
2009-12-19 06:34:42 +00:00
|
|
|
{
|
2014-07-09 15:01:16 +00:00
|
|
|
return ((Mmgr*)mmgr->ctx)->reallocMem (ptr, n);
|
2009-12-19 06:34:42 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 02:11:33 +00:00
|
|
|
void Mmgr::free_mem (mmgr_t* mmgr, void* ptr) QSE_CPP_NOEXCEPT
|
2009-12-19 06:34:42 +00:00
|
|
|
{
|
2014-07-09 15:01:16 +00:00
|
|
|
((Mmgr*)mmgr->ctx)->freeMem (ptr);
|
2009-12-19 06:34:42 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 12:11:40 +00:00
|
|
|
void* Mmgr::callocate (qse_size_t n, bool raise_exception) /*QSE_CPP_THREXCEPT1(MemoryError)*/
|
2015-02-05 15:51:50 +00:00
|
|
|
{
|
2018-07-01 07:33:56 +00:00
|
|
|
void* ptr = this->allocate(n, raise_exception);
|
2020-08-06 11:42:39 +00:00
|
|
|
if (ptr) QSE_MEMSET (ptr, 0, n);
|
2015-02-05 15:51:50 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:40:25 +00:00
|
|
|
#if 0
|
|
|
|
#if defined(__GNUC__)
|
2019-09-03 16:46:57 +00:00
|
|
|
static StdMmgr __attribute__((init_priority(101))) std_dfl_mmgr; //<- this solved the problem
|
2018-10-31 10:40:25 +00:00
|
|
|
#else
|
2019-09-03 16:46:57 +00:00
|
|
|
static StdMmgr std_dfl_mmgr; //<-- has an issue for undefined initialization order
|
2018-10-31 10:40:25 +00:00
|
|
|
#endif
|
2018-11-01 04:24:32 +00:00
|
|
|
Mmgr* Mmgr::dfl_mmgr = &std_dfl_mmgr;
|
2019-09-03 16:46:57 +00:00
|
|
|
//Mmgr* Mmgr::dfl_mmgr = StdMmgr::getInstance(); //<--- has an issue as well
|
2018-11-01 04:24:32 +00:00
|
|
|
Mmgr* Mmgr::getDFL () QSE_CPP_NOEXCEPT
|
|
|
|
{
|
|
|
|
return Mmgr::dfl_mmgr;
|
|
|
|
}
|
2018-10-31 10:40:25 +00:00
|
|
|
#else
|
|
|
|
// C++ initialization order across translation units are not defined.
|
|
|
|
// so it's tricky to work around this... the init_priority attribute
|
|
|
|
// can solve this problem. but it's compiler dependent.
|
|
|
|
// Mmgr::getDFL() resets dfl_mmgr to &std_dfl_mmgr if it's NULL.
|
|
|
|
Mmgr* Mmgr::dfl_mmgr = QSE_NULL;
|
2015-02-02 10:45:57 +00:00
|
|
|
|
2018-07-01 02:11:33 +00:00
|
|
|
Mmgr* Mmgr::getDFL () QSE_CPP_NOEXCEPT
|
2015-02-02 10:45:57 +00:00
|
|
|
{
|
2018-10-31 10:40:25 +00:00
|
|
|
static StdMmgr std_dfl_mmgr;
|
2018-10-31 12:52:19 +00:00
|
|
|
Mmgr* mmgr = Mmgr::dfl_mmgr;
|
|
|
|
return mmgr? mmgr: &std_dfl_mmgr;
|
2015-02-02 10:45:57 +00:00
|
|
|
}
|
2018-11-01 04:24:32 +00:00
|
|
|
#endif
|
2015-02-02 10:45:57 +00:00
|
|
|
|
2018-07-01 02:11:33 +00:00
|
|
|
void Mmgr::setDFL (Mmgr* mmgr) QSE_CPP_NOEXCEPT
|
2015-02-02 10:45:57 +00:00
|
|
|
{
|
|
|
|
Mmgr::dfl_mmgr = mmgr;
|
|
|
|
}
|
|
|
|
|
2009-12-19 06:34:42 +00:00
|
|
|
/////////////////////////////////
|
|
|
|
QSE_END_NAMESPACE(QSE)
|
|
|
|
/////////////////////////////////
|
2015-02-01 16:07:26 +00:00
|
|
|
|
2019-06-06 12:11:40 +00:00
|
|
|
void* operator new (qse_size_t size, QSE::Mmgr* mmgr) /*QSE_CPP_THREXCEPT1(QSE::Mmgr::MemoryError)*/
|
2015-02-01 16:07:26 +00:00
|
|
|
{
|
2015-02-02 13:30:33 +00:00
|
|
|
return mmgr->allocate (size);
|
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::Mmgr* mmgr)
|
|
|
|
#else
|
2015-02-01 16:07:26 +00:00
|
|
|
void operator delete (void* ptr, QSE::Mmgr* mmgr)
|
2015-03-24 09:34:57 +00:00
|
|
|
#endif
|
2015-02-01 16:07:26 +00:00
|
|
|
{
|
2015-02-02 13:30:33 +00:00
|
|
|
mmgr->dispose (ptr);
|
2015-02-01 16:07:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 12:11:40 +00:00
|
|
|
void* operator new (qse_size_t size, QSE::Mmgr* mmgr, void* existing_ptr) /*QSE_CPP_THREXCEPT1(QSE::Mmgr::MemoryError)*/
|
2015-03-04 16:30:09 +00:00
|
|
|
{
|
|
|
|
// mmgr unused. i put it in the parameter list to make this function
|
|
|
|
// less conflicting with the stock ::operator new() that doesn't allocate.
|
|
|
|
return existing_ptr;
|
|
|
|
}
|
|
|
|
|
2015-02-01 16:07:26 +00:00
|
|
|
#if 0
|
|
|
|
void* operator new[] (qse_size_t size, QSE::Mmgr* mmgr)
|
|
|
|
{
|
2015-02-02 13:30:33 +00:00
|
|
|
return mmgr->allocate (size);
|
2015-02-01 16:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete[] (void* ptr, QSE::Mmgr* mmgr)
|
|
|
|
{
|
2015-02-02 13:30:33 +00:00
|
|
|
mmgr->dispose (ptr);
|
2015-02-01 16:07:26 +00:00
|
|
|
}
|
|
|
|
#endif
|