56 _Safenew(
const char *
file,
int line,
int flag)
60 std::cerr << std::endl
61 <<
"SAFENEW fault: NULL return pointer in file "
62 << file <<
" at line " << line << std::endl;
63 }
else if (flag == 1) {
64 std::cerr << std::endl
65 <<
"SAFENEWARR fault: NULL return pointer in file "
66 << file <<
" at line " << line << std::endl;
72 _Safenewfill(
void *pv,
size_t size,
char fill)
83 #ifdef DEBUG_MEMMANAGER
89 clMemMan::pstFindElem(
const void* pvToFind)
const
93 stList *pstL = pstRoot;
96 while (pstL->pstNext && (pstL->pstNext->stMB.pv <= pvToFind)) {
98 if (pstL->stMB.pv == pvToFind) {
99 if (pstL->stMB.eSt == ALLOCATED) {
104 CERR << std::endl <<
"clMemMan " << sName <<
" error: pointer "
105 << (
void*)pvToFind <<
" not found in pstFindElem()" << std::endl;
110 clMemMan::pstFindPrev(
const void *pvToFindPrev)
const
114 stList *pstL = pstRoot;
118 while (pstL->pstNext && (pstL->pstNext->stMB.pv <= pvToFindPrev)) {
119 pstN = pstL->pstNext;
120 if (pstN->stMB.pv == pvToFindPrev) {
121 if (pstN->stMB.eSt == ALLOCATED) {
128 CERR << std::endl <<
"clMemMan " << sName <<
" error: pointer "
129 << (
void*)pvToFindPrev <<
" not found in pstFindPrev()"
136 clMemMan::_remove(
const void *pvToRemove, clMemMan::eRemoveMode eMode, flag fArr, flag fFill)
140 stList *pstL = pstFindPrev(pvToRemove);
144 CERR << std::endl <<
"clMemMan " << sName <<
" warning: pointer "
145 << (
void*)pvToRemove;
147 std::cerr <<
" to array";
149 std::cerr <<
" not found in _remove()" << std::endl;
150 throw clMemMan::ErrNotFound();
153 stList *pstN = pstL->pstNext;
157 ASSERT(fArr && pstN->stMB.fArr);
158 _Safenewfill(pstN->stMB.pv, pstN->stMB.size, cDebugFree);
164 pstL->pstNext = pstN->pstNext;
170 pstN->stMB.eSt = FREED;
173 case DELBUTNOTRELEASE:
175 pstN->stMB.eSt = FREEDBUTNOTRELEASED;
181 clMemMan::clMemMan(
char *sNameIn)
182 : pstRoot(NULL), sName(NULL)
185 sName =
new char[strlen(sNameIn)+1];
186 strcpy(sName, sNameIn);
189 pstRoot =
new stList(stMemBlock());
192 clMemMan::~clMemMan(
void)
194 stList* pstL = pstRoot;
200 pstL = pstL->pstNext;
210 clMemMan::fIsBlock(
const void *pvBlock,
size_t sizeBlock)
const
215 stList *pstL = pstFindElem(pvBlock);
218 if (pstL && (pstL->stMB.size == sizeBlock)) {
219 ASSERT(pstL->stMB.eSt != UNKNOWN);
220 if (pstL->stMB.eSt == ALLOCATED) {
229 clMemMan::fIsPointerToBlock(
const void *pvBlock)
const
233 stList *pstL = pstFindElem(pvBlock);
237 ASSERT(pstL->stMB.eSt != UNKNOWN);
238 if (pstL->stMB.eSt == ALLOCATED) {
247 clMemMan::fIsValid(
const void *pvValid,
size_t sizeValid)
const
252 stList *pstL = pstRoot;
255 while (pstL->pstNext) {
256 pstL = pstL->pstNext;
257 flag fCond1 = (pstL->stMB.pv <= pvValid);
258 flag fCond2 = (((
void*)pstL->stMB.pv+pstL->stMB.size)
259 >= ((
void*)pvValid+sizeValid));
261 if (fCond1 && fCond2) {
262 ASSERT(pstL->stMB.eSt != UNKNOWN);
263 if (pstL->stMB.eSt == ALLOCATED) {
273 clMemMan::sizeOfBlock(
const void* pvSizeOf)
const
276 stList *pstL = pstFindElem(pvSizeOf);
280 return pstL->stMB.size;
288 clMemMan::fIsArray(
const void *pvIsArray)
const
291 stList *pstL = pstFindElem(pvIsArray);
295 return pstL->stMB.fArr;
301 clMemMan::eBlockStatus(
const void *pvBStatus)
const
304 stList *pstL = pstFindElem(pvBStatus);
308 return pstL->stMB.eSt;
314 clMemMan::ClearRefs(
void)
316 stList *pstL = pstRoot;
319 while(pstL->pstNext) {
320 pstL = pstL->pstNext;
326 clMemMan::PutRef(
const void *pvRef)
330 stList *pstL = pstFindElem(pvRef);
339 clMemMan::fIsRefd(
const void *pvIsRefd)
const
343 stList *pstL = pstFindElem(pvIsRefd);
346 if (pstL && (pstL->stMB.eSt == ALLOCATED) && (pstL->stMB.fRef == 1)) {
355 clMemMan::DumpRef(std::ostream& rout)
const
357 rout <<
"Memory Manager 1.0";
359 rout <<
": " << sName;
363 rout <<
"Ref'd blocks:" << std::endl;
365 stList *pstL = pstRoot;
369 while (pstL->pstNext) {
370 pstL = pstL->pstNext;
371 ASSERT(pstL->stMB.eSt != UNKNOWN);
374 if (pstL->stMB.fRef) {
375 rout <<
"Block " << setw(4) << iCount <<
':' << std::endl
376 <<
"pointer " << pstL->stMB.pv
377 <<
", size " << pstL->stMB.size
380 if (pstL->stMB.eSt == ALLOCATED) {
381 rout <<
"ALLOCATED" << std::endl;
382 }
else if (pstL->stMB.eSt == FREED) {
383 rout <<
"FREED" << std::endl;
384 }
else if (pstL->stMB.eSt == FREEDBUTNOTRELEASED) {
385 rout <<
"FREEDBUTNOTRELEASED" << std::endl;
390 rout <<
"Unref'd blocks:" << std::endl;
394 while (pstL->pstNext) {
395 pstL = pstL->pstNext;
396 ASSERT(pstL->stMB.eSt != UNKNOWN);
399 if (!pstL->stMB.fRef) {
400 rout <<
"Block " << setw(4) << iCount <<
':' << std::endl
401 <<
"pointer " << pstL->stMB.pv
402 <<
", size " << pstL->stMB.size
405 if (pstL->stMB.eSt == ALLOCATED) {
406 rout <<
"ALLOCATED" << std::endl;
407 }
else if (pstL->stMB.eSt == FREED) {
408 rout <<
"FREED" << std::endl;
409 }
else if (pstL->stMB.eSt == FREEDBUTNOTRELEASED) {
410 rout <<
"FREEDBUTNOTRELEASED" << std::endl;
419 clMemMan::add(
const void *pvIn,
size_t sizeIn, flag fArr)
423 ASSERT(fArr == 0 || fArr == 1);
425 stList *pstL = pstRoot;
429 while (pstL->pstNext) {
430 pstN = pstL->pstNext;
431 if ((pstN->stMB.pv == pvIn) && (pstN->stMB.eSt == ALLOCATED)) {
432 CERR << std::endl <<
"clMemMan" << sName
433 <<
" error: block pointed by "
434 << (
void*)pvIn <<
", size " << sizeIn << std::endl
435 <<
"is already defined. Previous size is "
436 << pstN->stMB.size << std::endl;
440 if (pstN->stMB.pv >= pvIn) {
446 pstN =
new stList(stMemBlock((
void*)pvIn, sizeIn, ALLOCATED, fArr));
450 CERR << std::endl <<
"clMemMan " << sName
451 <<
": error in allocation in add()" << std::endl;
455 pstN->pstNext = pstL->pstNext;
456 pstL->pstNext = pstN;
461 operator << (std::ostream& rout,
const clMemMan& rm)
463 rout <<
"Memory Manager 1.0";
465 rout <<
": " << rm.sName;
469 clMemMan::stList *pstL = rm.pstRoot;
472 while (pstL->pstNext) {
473 pstL = pstL->pstNext;
474 ASSERT(pstL->stMB.eSt != UNKNOWN);
476 rout <<
"Block " << setw(4) << (++iCount) <<
':' << std::endl
477 <<
"pointer " << pstL->stMB.pv
478 <<
", size " << pstL->stMB.size
481 if (pstL->stMB.eSt == ALLOCATED) {
482 rout <<
"ALLOCATED" << std::endl;
483 }
else if (pstL->stMB.eSt == FREED) {
484 rout <<
"FREED" << std::endl;
485 }
else if (pstL->stMB.eSt == FREEDBUTNOTRELEASED) {
486 rout <<
"FREEDBUTNOTRELEASED" << std::endl;
#define MBDYN_EXCEPT_ARGS
std::ostream & operator<<(std::ostream &out, const FullMatrixHandler &m)
#define ASSERT(expression)
#define defaultMemoryManager