1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/* $Header: /var/lib/cvs/dynare_cpp/sylv/cc/SylvMemory.cpp,v 1.1.1.1 2004/06/04 13:00:49 kamenik Exp $ */
/* Tag $Name: $ */
#include "SylvMemory.h"
#include "SylvException.h"
#include "KronVector.h"
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
# include <dynmex.h>
#endif
#include <cmath>
#include <cstdio>
#include <cstdlib>
/**********************************************************/
/* SylvMemoryPool */
/**********************************************************/
SylvMemoryPool memory_pool;
SylvMemoryPool::SylvMemoryPool()
: base(0), length(0), allocated(0), stack_mode(false)
{
}
void SylvMemoryPool::init(size_t size)
{
#ifdef USE_MEMORY_POOL
length = size;
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
if (base)
throw SYLV_MES_EXCEPTION("Attempt to use matlab memory pool twice.");
base = (char*) mxMalloc(length);
#else
base = (char*) malloc(length);
#endif
#else
throw SYLV_MES_EXCEPTION("SylvMemoryPool::init() called for non memory pool code.");
#endif
}
void* SylvMemoryPool::allocate(size_t size)
{
#ifdef USE_MEMORY_POOL
if (allocated + size < length) {
char* res = base + allocated;
allocated += size;
return res;
} else {
throw SYLV_MES_EXCEPTION("Run out of memory space");
}
#else
throw SYLV_MES_EXCEPTION("SylvMemoryPool::allocate() called for non memory pool code.");
#endif
}
void SylvMemoryPool::free(void* p)
{
#ifdef USE_MEMORY_POOL
int offset = ((char*)p) - base;
#ifdef DEBUG
if (offset < 0)
throw SYLV_MES_EXCEPTION("SylvMemoryPool::free() frees wrong address < begin.");
if (offset >= (int)length)
throw SYLV_MES_EXCEPTION("SylvMemoryPool::free() frees wrong address > end.");
#endif
if (stack_mode && offset >= 0 && offset < (int)allocated)
allocated = offset;
#else
throw SYLV_MES_EXCEPTION("SylvMemoryPool::free() called for non memory pool code.");
#endif
}
void SylvMemoryPool::setStackMode(bool mode)
{
stack_mode = mode;
}
SylvMemoryPool::~SylvMemoryPool()
{
reset();
}
void SylvMemoryPool::reset()
{
#if !defined(MATLAB_MEX_FILE) && !defined(OCTAVE_MEX_FILE)
delete [] base;
base = 0;
allocated = 0;
length = 0;
stack_mode = false;
#endif
}
/**********************************************************/
/* global new and delete */
/**********************************************************/
#ifdef USE_MEMORY_POOL
void* operator new(size_t size)
{
return memory_pool.allocate(size);
}
void* operator new[](size_t size)
{
return memory_pool.allocate(size);
}
void operator delete(void* p)
{
memory_pool.free(p);
}
void operator delete[](void* p)
{
memory_pool.free(p);
}
#endif
/**********************************************************/
/* saved version of global new and delete */
/**********************************************************/
#ifdef USE_MEMORY_POOL
void* MallocAllocator::operator new(size_t size)
{
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
throw SYLV_MES_EXCEPTION("Attempt to call wrong memory allocator.");
#else
void* res = malloc(size);
if (!res)
throw SYLV_MES_EXCEPTION("Malloc unable to allocate memory.");
return res;
#endif
}
void* MallocAllocator::operator new[](size_t size)
{
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
throw SYLV_MES_EXCEPTION("Attempt to call wrong memory allocator.");
#else
void* res = malloc(size);
if (!res)
throw SYLV_MES_EXCEPTION("Malloc unable allocate memory.");
return res;
#endif
}
void MallocAllocator::operator delete(void* p)
{
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
throw SYLV_MES_EXCEPTION("Attempt to call wrong memory destructor.");
#else
free(p);
#endif
}
void MallocAllocator::operator delete[](void* p)
{
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
throw SYLV_MES_EXCEPTION("Attempt to call wrong memory destructor.");
#else
free(p);
#endif
}
#endif
/**********************************************************/
/* SylvMemoryDriver */
/**********************************************************/
void SylvMemoryDriver::allocate(int num_d, int m, int n, int order)
{
#ifdef USE_MEMORY_POOL
int x_cols = power(m,order);
int total = num_d*x_cols*n; // storage for big matrices
total += x_cols; // storage for one extra row of a big matrix
int dig_vectors = (int)ceil(((double)(power(m,order)-1))/(m-1));
total += 8*n*dig_vectors; // storage for kron vectors instantiated during solv
total += 50*(m*m+n*n); // some storage for small square matrices
total *= sizeof(double); // everything in doubles
memory_pool.init(total);
#endif
}
SylvMemoryDriver::SylvMemoryDriver(int num_d, int m, int n, int order)
{
allocate(num_d, m, n, order);
}
SylvMemoryDriver::SylvMemoryDriver(const SylvParams& pars, int num_d,
int m, int n, int order)
{
if (*(pars.method) == SylvParams::iter)
num_d++;
if (*(pars.want_check))
num_d++;
allocate(num_d, m, n, order);
}
SylvMemoryDriver::~SylvMemoryDriver()
{
memory_pool.reset();
}
void SylvMemoryDriver::setStackMode(bool mode) {
memory_pool.setStackMode(mode);
}
|