- fixed calculation of allocated memory for garbage collection

https://forum.zdoom.org/viewtopic.php?t=68897
This commit is contained in:
alexey.lysiuk 2020-06-09 16:48:51 +03:00
parent 3a23cc69d6
commit 18371fb27a
2 changed files with 27 additions and 3 deletions

View file

@ -266,14 +266,12 @@ private:
void *operator new(size_t len, nonew&) void *operator new(size_t len, nonew&)
{ {
GC::AllocBytes += len;
return M_Malloc(len); return M_Malloc(len);
} }
public: public:
void operator delete (void *mem, nonew&) void operator delete (void *mem, nonew&)
{ {
GC::AllocBytes -= _msize(mem);
M_Free(mem); M_Free(mem);
} }

View file

@ -45,7 +45,7 @@
#endif #endif
#include "engineerrors.h" #include "engineerrors.h"
#include "m_alloc.h" #include "dobject.h"
#ifndef _MSC_VER #ifndef _MSC_VER
#define _NORMAL_BLOCK 0 #define _NORMAL_BLOCK 0
@ -62,16 +62,22 @@ void *M_Malloc(size_t size)
if (block == NULL) if (block == NULL)
I_FatalError("Could not malloc %zu bytes", size); I_FatalError("Could not malloc %zu bytes", size);
GC::AllocBytes += _msize(block);
return block; return block;
} }
void *M_Realloc(void *memblock, size_t size) void *M_Realloc(void *memblock, size_t size)
{ {
if (memblock != NULL)
{
GC::AllocBytes -= _msize(memblock);
}
void *block = realloc(memblock, size); void *block = realloc(memblock, size);
if (block == NULL) if (block == NULL)
{ {
I_FatalError("Could not realloc %zu bytes", size); I_FatalError("Could not realloc %zu bytes", size);
} }
GC::AllocBytes += _msize(block);
return block; return block;
} }
#else #else
@ -86,6 +92,7 @@ void *M_Malloc(size_t size)
*sizeStore = size; *sizeStore = size;
block = sizeStore+1; block = sizeStore+1;
GC::AllocBytes += _msize(block);
return block; return block;
} }
@ -94,6 +101,10 @@ void *M_Realloc(void *memblock, size_t size)
if(memblock == NULL) if(memblock == NULL)
return M_Malloc(size); return M_Malloc(size);
if (memblock != NULL)
{
GC::AllocBytes -= _msize(memblock);
}
void *block = realloc(((size_t*) memblock)-1, size+sizeof(size_t)); void *block = realloc(((size_t*) memblock)-1, size+sizeof(size_t));
if (block == NULL) if (block == NULL)
{ {
@ -104,6 +115,7 @@ void *M_Realloc(void *memblock, size_t size)
*sizeStore = size; *sizeStore = size;
block = sizeStore+1; block = sizeStore+1;
GC::AllocBytes += _msize(block);
return block; return block;
} }
#endif #endif
@ -120,16 +132,22 @@ void *M_Malloc_Dbg(size_t size, const char *file, int lineno)
if (block == NULL) if (block == NULL)
I_FatalError("Could not malloc %zu bytes in %s, line %d", size, file, lineno); I_FatalError("Could not malloc %zu bytes in %s, line %d", size, file, lineno);
GC::AllocBytes += _msize(block);
return block; return block;
} }
void *M_Realloc_Dbg(void *memblock, size_t size, const char *file, int lineno) void *M_Realloc_Dbg(void *memblock, size_t size, const char *file, int lineno)
{ {
if (memblock != NULL)
{
GC::AllocBytes -= _msize(memblock);
}
void *block = _realloc_dbg(memblock, size, _NORMAL_BLOCK, file, lineno); void *block = _realloc_dbg(memblock, size, _NORMAL_BLOCK, file, lineno);
if (block == NULL) if (block == NULL)
{ {
I_FatalError("Could not realloc %zu bytes in %s, line %d", size, file, lineno); I_FatalError("Could not realloc %zu bytes in %s, line %d", size, file, lineno);
} }
GC::AllocBytes += _msize(block);
return block; return block;
} }
#else #else
@ -144,6 +162,7 @@ void *M_Malloc_Dbg(size_t size, const char *file, int lineno)
*sizeStore = size; *sizeStore = size;
block = sizeStore+1; block = sizeStore+1;
GC::AllocBytes += _msize(block);
return block; return block;
} }
@ -152,6 +171,10 @@ void *M_Realloc_Dbg(void *memblock, size_t size, const char *file, int lineno)
if(memblock == NULL) if(memblock == NULL)
return M_Malloc_Dbg(size, file, lineno); return M_Malloc_Dbg(size, file, lineno);
if (memblock != NULL)
{
GC::AllocBytes -= _msize(memblock);
}
void *block = _realloc_dbg(((size_t*) memblock)-1, size+sizeof(size_t), _NORMAL_BLOCK, file, lineno); void *block = _realloc_dbg(((size_t*) memblock)-1, size+sizeof(size_t), _NORMAL_BLOCK, file, lineno);
if (block == NULL) if (block == NULL)
@ -163,6 +186,7 @@ void *M_Realloc_Dbg(void *memblock, size_t size, const char *file, int lineno)
*sizeStore = size; *sizeStore = size;
block = sizeStore+1; block = sizeStore+1;
GC::AllocBytes += _msize(block);
return block; return block;
} }
#endif #endif
@ -173,6 +197,7 @@ void M_Free (void *block)
{ {
if (block != NULL) if (block != NULL)
{ {
GC::AllocBytes -= _msize(block);
free(block); free(block);
} }
} }
@ -181,6 +206,7 @@ void M_Free (void *block)
{ {
if(block != NULL) if(block != NULL)
{ {
GC::AllocBytes -= _msize(block);
free(((size_t*) block)-1); free(((size_t*) block)-1);
} }
} }