Skip to content

Commit

Permalink
- fixed calculation of allocated memory for garbage collection
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-lysiuk committed Jun 9, 2020
1 parent 3a23cc6 commit 18371fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/common/objects/dobject.h
Expand Up @@ -266,14 +266,12 @@ class DObject

void *operator new(size_t len, nonew&)
{
GC::AllocBytes += len;
return M_Malloc(len);
}
public:

void operator delete (void *mem, nonew&)
{
GC::AllocBytes -= _msize(mem);
M_Free(mem);
}

Expand Down
28 changes: 27 additions & 1 deletion src/common/utility/m_alloc.cpp
Expand Up @@ -45,7 +45,7 @@
#endif

#include "engineerrors.h"
#include "m_alloc.h"
#include "dobject.h"

#ifndef _MSC_VER
#define _NORMAL_BLOCK 0
Expand All @@ -62,16 +62,22 @@ void *M_Malloc(size_t size)
if (block == NULL)
I_FatalError("Could not malloc %zu bytes", size);

GC::AllocBytes += _msize(block);
return block;
}

void *M_Realloc(void *memblock, size_t size)
{
if (memblock != NULL)
{
GC::AllocBytes -= _msize(memblock);
}
void *block = realloc(memblock, size);
if (block == NULL)
{
I_FatalError("Could not realloc %zu bytes", size);
}
GC::AllocBytes += _msize(block);
return block;
}
#else
Expand All @@ -86,6 +92,7 @@ void *M_Malloc(size_t size)
*sizeStore = size;
block = sizeStore+1;

GC::AllocBytes += _msize(block);
return block;
}

Expand All @@ -94,6 +101,10 @@ void *M_Realloc(void *memblock, size_t size)
if(memblock == NULL)
return M_Malloc(size);

if (memblock != NULL)
{
GC::AllocBytes -= _msize(memblock);
}
void *block = realloc(((size_t*) memblock)-1, size+sizeof(size_t));
if (block == NULL)
{
Expand All @@ -104,6 +115,7 @@ void *M_Realloc(void *memblock, size_t size)
*sizeStore = size;
block = sizeStore+1;

GC::AllocBytes += _msize(block);
return block;
}
#endif
Expand All @@ -120,16 +132,22 @@ void *M_Malloc_Dbg(size_t size, const char *file, int lineno)
if (block == NULL)
I_FatalError("Could not malloc %zu bytes in %s, line %d", size, file, lineno);

GC::AllocBytes += _msize(block);
return block;
}

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);
if (block == NULL)
{
I_FatalError("Could not realloc %zu bytes in %s, line %d", size, file, lineno);
}
GC::AllocBytes += _msize(block);
return block;
}
#else
Expand All @@ -144,6 +162,7 @@ void *M_Malloc_Dbg(size_t size, const char *file, int lineno)
*sizeStore = size;
block = sizeStore+1;

GC::AllocBytes += _msize(block);
return block;
}

Expand All @@ -152,6 +171,10 @@ void *M_Realloc_Dbg(void *memblock, size_t size, const char *file, int lineno)
if(memblock == NULL)
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);

if (block == NULL)
Expand All @@ -163,6 +186,7 @@ void *M_Realloc_Dbg(void *memblock, size_t size, const char *file, int lineno)
*sizeStore = size;
block = sizeStore+1;

GC::AllocBytes += _msize(block);
return block;
}
#endif
Expand All @@ -173,6 +197,7 @@ void M_Free (void *block)
{
if (block != NULL)
{
GC::AllocBytes -= _msize(block);
free(block);
}
}
Expand All @@ -181,6 +206,7 @@ void M_Free (void *block)
{
if(block != NULL)
{
GC::AllocBytes -= _msize(block);
free(((size_t*) block)-1);
}
}
Expand Down

0 comments on commit 18371fb

Please sign in to comment.