GC fixes/improvements

- Have a real finalizer stage for destroying objects instead of mixing them
  in to the sweep stage.
- Base GC rate on a running average of the allocation rate instead of basing
  it on the amount of time taken since the last sweep started.
- More GC stats for better tweaking.
This commit is contained in:
Marisa Heit 2022-11-06 11:19:26 -06:00 committed by Christoph Oelckers
commit 7f899bd412
4 changed files with 379 additions and 171 deletions

View file

@ -3,7 +3,7 @@
** Wrappers for the malloc family of functions that count used bytes.
**
**---------------------------------------------------------------------------
** Copyright 1998-2008 Randy Heit
** Copyright 1998-2008 Marisa Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
@ -45,7 +45,7 @@
#endif
#include "engineerrors.h"
#include "dobject.h"
#include "dobjgc.h"
#ifndef _MSC_VER
#define _NORMAL_BLOCK 0
@ -59,25 +59,22 @@ void *M_Malloc(size_t size)
{
void *block = malloc(size);
if (block == NULL)
if (block == nullptr)
I_FatalError("Could not malloc %zu bytes", size);
GC::AllocBytes += _msize(block);
GC::ReportAlloc(_msize(block));
return block;
}
void *M_Realloc(void *memblock, size_t size)
{
if (memblock != NULL)
{
GC::AllocBytes -= _msize(memblock);
}
size_t oldsize = memblock ? _msize(memblock) : 0;
void *block = realloc(memblock, size);
if (block == NULL)
if (block == nullptr)
{
I_FatalError("Could not realloc %zu bytes", size);
}
GC::AllocBytes += _msize(block);
GC::ReportRealloc(oldsize, _msize(block));
return block;
}
#else
@ -85,28 +82,25 @@ void *M_Malloc(size_t size)
{
void *block = malloc(size+sizeof(size_t));
if (block == NULL)
if (block == nullptr)
I_FatalError("Could not malloc %zu bytes", size);
size_t *sizeStore = (size_t *) block;
*sizeStore = size;
block = sizeStore+1;
GC::AllocBytes += _msize(block);
GC::ReportAlloc(_msize(block));
return block;
}
void *M_Realloc(void *memblock, size_t size)
{
if(memblock == NULL)
if (memblock == nullptr)
return M_Malloc(size);
if (memblock != NULL)
{
GC::AllocBytes -= _msize(memblock);
}
size_t oldsize = _msize(memblock);
void *block = realloc(((size_t*) memblock)-1, size+sizeof(size_t));
if (block == NULL)
if (block == nullptr)
{
I_FatalError("Could not realloc %zu bytes", size);
}
@ -115,7 +109,7 @@ void *M_Realloc(void *memblock, size_t size)
*sizeStore = size;
block = sizeStore+1;
GC::AllocBytes += _msize(block);
GC::ReportRealloc(oldsize, _msize(block));
return block;
}
#endif
@ -129,25 +123,22 @@ void *M_Malloc_Dbg(size_t size, const char *file, int lineno)
{
void *block = _malloc_dbg(size, _NORMAL_BLOCK, file, lineno);
if (block == NULL)
if (block == nullptr)
I_FatalError("Could not malloc %zu bytes in %s, line %d", size, file, lineno);
GC::AllocBytes += _msize(block);
GC::ReportAlloc(_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);
}
size_t oldsize = memblock ? _msize(memblock) : 0;
void *block = _realloc_dbg(memblock, size, _NORMAL_BLOCK, file, lineno);
if (block == NULL)
if (block == nullptr)
{
I_FatalError("Could not realloc %zu bytes in %s, line %d", size, file, lineno);
}
GC::AllocBytes += _msize(block);
GC::ReportRealloc(oldsize, _msize(block));
return block;
}
#else
@ -155,29 +146,26 @@ void *M_Malloc_Dbg(size_t size, const char *file, int lineno)
{
void *block = _malloc_dbg(size+sizeof(size_t), _NORMAL_BLOCK, file, lineno);
if (block == NULL)
if (block == nullptr)
I_FatalError("Could not malloc %zu bytes in %s, line %d", size, file, lineno);
size_t *sizeStore = (size_t *) block;
*sizeStore = size;
block = sizeStore+1;
GC::AllocBytes += _msize(block);
GC::ReportAlloc(_msize(block));
return block;
}
void *M_Realloc_Dbg(void *memblock, size_t size, const char *file, int lineno)
{
if(memblock == NULL)
if (memblock == nullptr)
return M_Malloc_Dbg(size, file, lineno);
if (memblock != NULL)
{
GC::AllocBytes -= _msize(memblock);
}
size_t oldsize = _msize(memblock);
void *block = _realloc_dbg(((size_t*) memblock)-1, size+sizeof(size_t), _NORMAL_BLOCK, file, lineno);
if (block == NULL)
if (block == nullptr)
{
I_FatalError("Could not realloc %zu bytes in %s, line %d", size, file, lineno);
}
@ -186,29 +174,22 @@ void *M_Realloc_Dbg(void *memblock, size_t size, const char *file, int lineno)
*sizeStore = size;
block = sizeStore+1;
GC::AllocBytes += _msize(block);
GC::ReportRealloc(oldsize, _msize(block));
return block;
}
#endif
#endif
void M_Free (void *block)
{
if (block != nullptr)
{
GC::ReportDealloc(_msize(block));
#if !defined(__solaris__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
void M_Free (void *block)
{
if (block != NULL)
{
GC::AllocBytes -= _msize(block);
free(block);
}
}
#else
void M_Free (void *block)
{
if(block != NULL)
{
GC::AllocBytes -= _msize(block);
free(((size_t*) block)-1);
#endif
}
}
#endif