2008-01-11 14:42:14 +01:00
|
|
|
/*
|
2022-06-07 17:25:43 +02:00
|
|
|
* Copyright © 2007-2022 Dynare Team
|
2008-01-11 14:42:14 +01:00
|
|
|
*
|
|
|
|
* This file is part of Dynare.
|
|
|
|
*
|
|
|
|
* Dynare is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Dynare is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-06-09 17:33:48 +02:00
|
|
|
* along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2008-01-11 14:42:14 +01:00
|
|
|
*/
|
2009-12-16 14:21:31 +01:00
|
|
|
|
2008-01-30 17:18:06 +01:00
|
|
|
#include "Mem_Mngr.hh"
|
2007-11-21 00:27:30 +01:00
|
|
|
|
2021-02-09 15:55:36 +01:00
|
|
|
#include "dynmex.h"
|
|
|
|
|
2007-11-21 00:27:30 +01:00
|
|
|
Mem_Mngr::Mem_Mngr()
|
|
|
|
{
|
2009-12-16 18:18:38 +01:00
|
|
|
swp_f = false;
|
|
|
|
swp_f_b = 0;
|
2007-11-21 00:27:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Mem_Mngr::init_Mem()
|
|
|
|
{
|
|
|
|
Chunk_Stack.clear();
|
2009-12-16 18:18:38 +01:00
|
|
|
CHUNK_SIZE = 0;
|
|
|
|
Nb_CHUNK = 0;
|
2021-02-01 13:49:27 +01:00
|
|
|
NZE_Mem = nullptr;
|
|
|
|
NZE_Mem_add = nullptr;
|
2009-12-16 18:18:38 +01:00
|
|
|
CHUNK_heap_pos = 0;
|
2009-07-21 17:50:12 +02:00
|
|
|
NZE_Mem_Allocated.clear();
|
2007-11-21 00:27:30 +01:00
|
|
|
}
|
|
|
|
|
2009-12-16 18:18:38 +01:00
|
|
|
void
|
|
|
|
Mem_Mngr::fixe_file_name(string filename_arg)
|
2007-11-21 00:27:30 +01:00
|
|
|
{
|
2021-02-03 18:10:01 +01:00
|
|
|
filename_mem = move(filename_arg);
|
2007-11-21 00:27:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Mem_Mngr::init_CHUNK_BLCK_SIZE(int u_count)
|
|
|
|
{
|
2009-12-16 18:18:38 +01:00
|
|
|
CHUNK_BLCK_SIZE = u_count;
|
2007-11-21 00:27:30 +01:00
|
|
|
}
|
|
|
|
|
2023-11-29 19:00:21 +01:00
|
|
|
NonZeroElem*
|
2007-11-21 00:27:30 +01:00
|
|
|
Mem_Mngr::mxMalloc_NZE()
|
|
|
|
{
|
2010-07-23 11:20:24 +02:00
|
|
|
long unsigned int i;
|
2019-12-20 14:50:19 +01:00
|
|
|
if (!Chunk_Stack.empty()) /*An unused block of memory available inside the heap*/
|
2007-11-21 00:27:30 +01:00
|
|
|
{
|
2023-11-29 19:00:21 +01:00
|
|
|
NonZeroElem* p1 = Chunk_Stack.back();
|
2007-11-21 00:27:30 +01:00
|
|
|
Chunk_Stack.pop_back();
|
2021-02-03 18:10:01 +01:00
|
|
|
return p1;
|
2007-11-21 00:27:30 +01:00
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
else if (CHUNK_heap_pos < CHUNK_SIZE) /*there is enough allocated memory space available we keep
|
|
|
|
it at the top of the heap*/
|
2007-11-21 00:27:30 +01:00
|
|
|
{
|
2009-12-16 18:18:38 +01:00
|
|
|
i = CHUNK_heap_pos++;
|
2021-02-03 18:10:01 +01:00
|
|
|
return NZE_Mem_add[i];
|
2007-11-21 00:27:30 +01:00
|
|
|
}
|
2019-12-20 14:50:19 +01:00
|
|
|
else /*We have to allocate extra memory space*/
|
2007-11-21 00:27:30 +01:00
|
|
|
{
|
2009-12-16 18:18:38 +01:00
|
|
|
CHUNK_SIZE += CHUNK_BLCK_SIZE;
|
2007-11-21 00:27:30 +01:00
|
|
|
Nb_CHUNK++;
|
2023-11-29 19:00:21 +01:00
|
|
|
NZE_Mem = static_cast<NonZeroElem*>(
|
|
|
|
mxMalloc(CHUNK_BLCK_SIZE * sizeof(NonZeroElem))); /*The block of memory allocated*/
|
|
|
|
test_mxMalloc(NZE_Mem, __LINE__, __FILE__, __func__, CHUNK_BLCK_SIZE * sizeof(NonZeroElem));
|
2009-07-21 17:50:12 +02:00
|
|
|
NZE_Mem_Allocated.push_back(NZE_Mem);
|
2009-12-16 18:18:38 +01:00
|
|
|
if (!NZE_Mem)
|
2017-03-24 23:15:25 +01:00
|
|
|
mexPrintf("Not enough memory available\n");
|
2017-05-16 16:30:27 +02:00
|
|
|
if (NZE_Mem_add)
|
|
|
|
{
|
2023-11-29 19:00:21 +01:00
|
|
|
NZE_Mem_add = static_cast<NonZeroElem**>(mxRealloc(
|
|
|
|
NZE_Mem_add,
|
|
|
|
CHUNK_SIZE
|
|
|
|
* sizeof(
|
|
|
|
NonZeroElem*))); /*We have to redefine the size of pointer on the memory*/
|
|
|
|
test_mxMalloc(NZE_Mem_add, __LINE__, __FILE__, __func__,
|
|
|
|
CHUNK_SIZE * sizeof(NonZeroElem*));
|
2008-12-19 11:24:31 +01:00
|
|
|
}
|
2017-05-16 16:30:27 +02:00
|
|
|
else
|
|
|
|
{
|
2023-11-29 19:00:21 +01:00
|
|
|
NZE_Mem_add = static_cast<NonZeroElem**>(mxMalloc(
|
|
|
|
CHUNK_SIZE
|
|
|
|
* sizeof(NonZeroElem*))); /*We have to define the size of pointer on the memory*/
|
|
|
|
test_mxMalloc(NZE_Mem_add, __LINE__, __FILE__, __func__,
|
|
|
|
CHUNK_SIZE * sizeof(NonZeroElem*));
|
2009-07-21 17:50:12 +02:00
|
|
|
}
|
2017-03-24 23:15:25 +01:00
|
|
|
|
|
|
|
if (!NZE_Mem_add)
|
|
|
|
mexPrintf("Not enough memory available\n");
|
2009-12-16 18:18:38 +01:00
|
|
|
for (i = CHUNK_heap_pos; i < CHUNK_SIZE; i++)
|
2023-11-29 19:00:21 +01:00
|
|
|
NZE_Mem_add[i] = const_cast<NonZeroElem*>(NZE_Mem + (i - CHUNK_heap_pos));
|
2009-12-16 18:18:38 +01:00
|
|
|
i = CHUNK_heap_pos++;
|
2021-02-03 18:10:01 +01:00
|
|
|
return NZE_Mem_add[i];
|
2007-11-21 00:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-11-29 19:00:21 +01:00
|
|
|
Mem_Mngr::mxFree_NZE(void* pos)
|
2007-11-21 00:27:30 +01:00
|
|
|
{
|
2010-07-23 11:20:24 +02:00
|
|
|
unsigned int i;
|
2022-06-07 17:25:43 +02:00
|
|
|
ptrdiff_t gap;
|
2009-12-16 18:18:38 +01:00
|
|
|
for (i = 0; i < Nb_CHUNK; i++)
|
2007-11-21 00:27:30 +01:00
|
|
|
{
|
2023-11-29 19:00:21 +01:00
|
|
|
gap = (reinterpret_cast<ptrdiff_t>(pos)
|
|
|
|
- reinterpret_cast<ptrdiff_t>(NZE_Mem_add[i * CHUNK_BLCK_SIZE]))
|
|
|
|
/ sizeof(NonZeroElem);
|
2021-02-03 18:10:01 +01:00
|
|
|
if (gap < CHUNK_BLCK_SIZE && gap >= 0)
|
2007-11-21 00:27:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
Chunk_Stack.push_back(static_cast<NonZeroElem*>(pos));
|
2007-11-21 00:27:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Mem_Mngr::Free_All()
|
|
|
|
{
|
2009-12-16 18:18:38 +01:00
|
|
|
while (NZE_Mem_Allocated.size())
|
|
|
|
{
|
|
|
|
mxFree(NZE_Mem_Allocated.back());
|
|
|
|
NZE_Mem_Allocated.pop_back();
|
|
|
|
}
|
2010-08-18 13:51:57 +02:00
|
|
|
if (NZE_Mem_add)
|
|
|
|
mxFree(NZE_Mem_add);
|
2007-11-21 00:27:30 +01:00
|
|
|
init_Mem();
|
|
|
|
}
|