Fix thread safety of mem_alloc and mem_free

This works by removing everything from these functions except for the
`malloc` and `free`.
This commit is contained in:
heinrich5991 2018-11-26 20:27:00 +01:00
parent eb32a492c4
commit a5a5fe1cc8
5 changed files with 3 additions and 113 deletions

View file

@ -60,7 +60,6 @@ static DBG_LOGGER loggers[16];
static int num_loggers = 0;
static NETSTATS network_stats = {0};
static MEMSTATS memory_stats = {0};
static NETSOCKET invalid_socket = {NETTYPE_INVALID, -1, -1};
@ -260,80 +259,14 @@ static const int MEM_GUARD_VAL = 0xbaadc0de;
void *mem_alloc_debug(const char *filename, int line, unsigned size, unsigned alignment)
{
/* TODO: fix alignment */
/* TODO: add debugging */
MEMTAIL *tail;
MEMHEADER *header = (struct MEMHEADER *)malloc(size+sizeof(MEMHEADER)+sizeof(MEMTAIL));
dbg_assert(header != 0, "mem_alloc failure");
if(!header)
return NULL;
tail = (struct MEMTAIL *)(((char*)(header+1))+size);
header->size = size;
header->filename = filename;
header->line = line;
memory_stats.allocated += header->size;
memory_stats.total_allocations++;
memory_stats.active_allocations++;
tail->guard = MEM_GUARD_VAL;
header->prev = (MEMHEADER *)0;
header->next = first;
if(first)
first->prev = header;
first = header;
/*dbg_msg("mem", "++ %p", header+1); */
return header+1;
return malloc(size);
}
void mem_free(void *p)
{
if(p)
{
MEMHEADER *header = (MEMHEADER *)p - 1;
MEMTAIL *tail = (MEMTAIL *)(((char*)(header+1))+header->size);
if(tail->guard != MEM_GUARD_VAL)
dbg_msg("mem", "!! %p", p);
/* dbg_msg("mem", "-- %p", p); */
memory_stats.allocated -= header->size;
memory_stats.active_allocations--;
if(header->prev)
header->prev->next = header->next;
else
first = header->next;
if(header->next)
header->next->prev = header->prev;
free(header);
}
free(p);
}
void mem_debug_dump(IOHANDLE file)
{
char buf[1024];
MEMHEADER *header = first;
if(!file)
file = io_open("memory.txt", IOFLAG_WRITE);
if(file)
{
while(header)
{
str_format(buf, sizeof(buf), "%s(%d): %d", header->filename, header->line, header->size);
io_write(file, buf, strlen(buf));
io_write_newline(file);
header = header->next;
}
io_close(file);
}
}
void mem_copy(void *dest, const void *source, unsigned size)
{
memcpy(dest, source, size);
@ -2023,11 +1956,6 @@ int mem_comp(const void *a, const void *b, int size)
return memcmp(a,b,size);
}
const MEMSTATS *mem_stats()
{
return &memory_stats;
}
void net_stats(NETSTATS *stats_inout)
{
*stats_inout = network_stats;

View file

@ -170,14 +170,6 @@ void mem_zero(void *block, unsigned size);
*/
int mem_comp(const void *a, const void *b, int size);
/*
Function: mem_check
Validates the heap
Will trigger a assert if memory has failed.
*/
int mem_check_imp();
#define mem_check() dbg_assert_imp(__FILE__, __LINE__, mem_check_imp(), "Memory check failed")
/* Group: File IO */
enum {
IOFLAG_READ = 1,
@ -1203,8 +1195,6 @@ int net_would_block();
int net_socket_read_wait(NETSOCKET sock, int time);
void mem_debug_dump(IOHANDLE file);
void swap_endian(void *data, unsigned elem_size, unsigned num);
@ -1215,15 +1205,6 @@ void dbg_logger_stdout();
void dbg_logger_debugger();
void dbg_logger_file(const char *filename);
typedef struct
{
int allocated;
int active_allocations;
int total_allocations;
} MEMSTATS;
const MEMSTATS *mem_stats();
typedef struct
{
int sent_packets;

View file

@ -672,10 +672,8 @@ void CClient::DebugRender()
total = 42
*/
FrameTimeAvg = FrameTimeAvg*0.9f + m_RenderFrameTime*0.1f;
str_format(aBuffer, sizeof(aBuffer), "ticks: %8d %8d mem %dk %d gfxmem: %dk fps: %3d",
str_format(aBuffer, sizeof(aBuffer), "ticks: %8d %8d gfxmem: %dk fps: %3d",
m_CurGameTick, m_PredTick,
mem_stats()->allocated/1024,
mem_stats()->total_allocations,
Graphics()->MemoryUsage()/1024,
(int)(1.0f/FrameTimeAvg + 0.5f));
Graphics()->QuadsText(2, 2, 16, aBuffer);

View file

@ -1290,12 +1290,6 @@ int CServer::Run()
m_Lastheartbeat = 0;
m_GameStartTime = time_get();
if(g_Config.m_Debug)
{
str_format(aBuf, sizeof(aBuf), "baseline memory usage %dk", mem_stats()->allocated/1024);
Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "server", aBuf);
}
while(m_RunServer)
{
int64 t = time_get();

View file

@ -24,16 +24,6 @@ public:
IStorage *m_pStorage;
bool m_Logging;
static void Con_DbgDumpmem(IConsole::IResult *pResult, void *pUserData)
{
CEngine *pEngine = static_cast<CEngine *>(pUserData);
char aBuf[32];
str_timestamp(aBuf, sizeof(aBuf));
char aFilename[128];
str_format(aFilename, sizeof(aFilename), "dumps/memory_%s.txt", aBuf);
mem_debug_dump(pEngine->m_pStorage->OpenFile(aFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE));
}
static void Con_DbgLognetwork(IConsole::IResult *pResult, void *pUserData)
{
CEngine *pEngine = static_cast<CEngine *>(pUserData);
@ -89,7 +79,6 @@ public:
if(!m_pConsole || !m_pStorage)
return;
m_pConsole->Register("dbg_dumpmem", "", CFGFLAG_SERVER|CFGFLAG_CLIENT, Con_DbgDumpmem, this, "Dump the memory");
m_pConsole->Register("dbg_lognetwork", "", CFGFLAG_SERVER|CFGFLAG_CLIENT, Con_DbgLognetwork, this, "Log the network");
}