2008-01-11 14:42:14 +01:00
|
|
|
/*
|
2017-05-16 14:11:15 +02:00
|
|
|
* Copyright (C) 2007-2017 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
|
|
|
|
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-03-24 23:15:25 +01:00
|
|
|
/*void
|
2017-05-16 16:30:27 +02:00
|
|
|
Mem_Mngr::Print_heap()
|
|
|
|
{
|
2010-07-23 11:20:24 +02:00
|
|
|
unsigned int i;
|
2007-11-21 00:27:30 +01:00
|
|
|
mexPrintf("i :");
|
2009-12-16 18:18:38 +01:00
|
|
|
for (i = 0; i < CHUNK_SIZE; i++)
|
2017-05-16 16:30:27 +02:00
|
|
|
mexPrintf("%3d ", i);
|
2007-11-21 00:27:30 +01:00
|
|
|
mexPrintf("\n");
|
2017-05-16 16:30:27 +02:00
|
|
|
}
|
2017-03-24 23:15:25 +01:00
|
|
|
*/
|
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;
|
|
|
|
NZE_Mem = NULL;
|
|
|
|
NZE_Mem_add = NULL;
|
|
|
|
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
|
|
|
{
|
2017-03-24 23:15:25 +01:00
|
|
|
filename_mem = 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
|
|
|
}
|
|
|
|
|
2009-12-16 18:18:38 +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;
|
2008-12-19 11:24:31 +01:00
|
|
|
if (!Chunk_Stack.empty()) /*An unused block of memory available inside the heap*/
|
2007-11-21 00:27:30 +01:00
|
|
|
{
|
2009-12-16 18:18:38 +01:00
|
|
|
NonZeroElem *p1 = Chunk_Stack.back();
|
2007-11-21 00:27:30 +01:00
|
|
|
Chunk_Stack.pop_back();
|
2009-12-16 18:18:38 +01:00
|
|
|
return (p1);
|
2007-11-21 00:27:30 +01:00
|
|
|
}
|
2009-12-16 18:18:38 +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++;
|
|
|
|
return (NZE_Mem_add[i]);
|
2007-11-21 00:27:30 +01:00
|
|
|
}
|
2008-12-19 11:24:31 +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++;
|
2017-05-16 16:30:27 +02:00
|
|
|
NZE_Mem = (NonZeroElem *) mxMalloc(CHUNK_BLCK_SIZE*sizeof(NonZeroElem)); /*The block of memory allocated*/
|
2017-03-24 23:15:25 +01:00
|
|
|
error_msg.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)
|
|
|
|
{
|
|
|
|
NZE_Mem_add = (NonZeroElem **) mxRealloc(NZE_Mem_add, CHUNK_SIZE*sizeof(NonZeroElem *)); /*We have to redefine the size of pointer on the memory*/
|
|
|
|
error_msg.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
|
|
|
|
{
|
|
|
|
NZE_Mem_add = (NonZeroElem **) mxMalloc(CHUNK_SIZE*sizeof(NonZeroElem *)); /*We have to define the size of pointer on the memory*/
|
|
|
|
error_msg.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++)
|
2017-03-24 23:15:25 +01:00
|
|
|
NZE_Mem_add[i] = (NonZeroElem *) (NZE_Mem+(i-CHUNK_heap_pos));
|
2009-12-16 18:18:38 +01:00
|
|
|
i = CHUNK_heap_pos++;
|
|
|
|
return (NZE_Mem_add[i]);
|
2007-11-21 00:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-12-16 18:18:38 +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;
|
2009-08-29 18:04:06 +02:00
|
|
|
size_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
|
|
|
{
|
2011-02-04 16:53:12 +01:00
|
|
|
gap = ((size_t) (pos)-(size_t) (NZE_Mem_add[i*CHUNK_BLCK_SIZE]))/sizeof(NonZeroElem);
|
2009-12-16 18:18:38 +01:00
|
|
|
if ((gap < CHUNK_BLCK_SIZE) && (gap >= 0))
|
2007-11-21 00:27:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-12-16 18:18:38 +01:00
|
|
|
Chunk_Stack.push_back((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();
|
|
|
|
}
|