Code cleanup, started to add definitions for MinGW/VC++
git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@5834 30fe4595-0a0c-4342-8851-515496e4dcbd Former-commit-id: 2632e23089655b7d321d40034a0aa3827318d431 Former-commit-id: 8e0999dd31d96a3e4d99d8d1cdafa8d66f922745
This commit is contained in:
parent
c3a8169944
commit
3d99d4edf3
5 changed files with 89 additions and 41 deletions
|
@ -26,10 +26,14 @@
|
||||||
#include "memmanager.h"
|
#include "memmanager.h"
|
||||||
#ifdef __DEBUG_MEMORYMANAGER__
|
#ifdef __DEBUG_MEMORYMANAGER__
|
||||||
|
|
||||||
|
#if defined(__MINGW32__)
|
||||||
|
|
||||||
|
#else
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <intrin.h>
|
#include <intrin.h>
|
||||||
|
|
||||||
#pragma intrinsic(_ReturnAddress)
|
#pragma intrinsic(_ReturnAddress)
|
||||||
|
#endif
|
||||||
|
|
||||||
//
|
//
|
||||||
// Configuration (depending on the compiler)
|
// Configuration (depending on the compiler)
|
||||||
|
@ -49,10 +53,10 @@ static unsigned int GfMM_Counter = 0; // Counter of memory blocks
|
||||||
//
|
//
|
||||||
void* operator new (size_t size)
|
void* operator new (size_t size)
|
||||||
{
|
{
|
||||||
#if defined(WIN32) // Has to be replaced by a definition of VC++ versus gcc
|
#if defined(__MINGW32__)
|
||||||
void* RetAddr = _ReturnAddress(); // VC++
|
void* RetAddr = __builtin_return_address (0); // gcc
|
||||||
#else
|
#else
|
||||||
void* RetAddr = __builtin_return_address (0); // gcc
|
void* RetAddr = _ReturnAddress(); // VC++
|
||||||
#endif
|
#endif
|
||||||
return GfMemoryManagerAlloc(size, GF_MM_ALLOCTYPE_NEW,RetAddr);
|
return GfMemoryManagerAlloc(size, GF_MM_ALLOCTYPE_NEW,RetAddr);
|
||||||
}
|
}
|
||||||
|
@ -63,7 +67,15 @@ void* operator new (size_t size)
|
||||||
//
|
//
|
||||||
void operator delete (void *b)
|
void operator delete (void *b)
|
||||||
{
|
{
|
||||||
GfMemoryManagerFree(b, GF_MM_ALLOCTYPE_NEW);
|
if (GfMemoryManagerRunning())
|
||||||
|
{
|
||||||
|
if (GfMM->DoNotFree)
|
||||||
|
GfMemoryManagerAccept(b, GF_MM_ALLOCTYPE_NEW);
|
||||||
|
else
|
||||||
|
GfMemoryManagerFree(b, GF_MM_ALLOCTYPE_NEW);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
GfMemoryManagerFree(b, GF_MM_ALLOCTYPE_NEW);
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
|
|
||||||
|
@ -74,17 +86,25 @@ void operator delete (void *b)
|
||||||
|
|
||||||
void * _tgf_win_malloc(size_t size)
|
void * _tgf_win_malloc(size_t size)
|
||||||
{
|
{
|
||||||
#if defined(WIN32) // Has to be replaced by a definition of VC++ versus gcc
|
#if defined(__MINGW32__)
|
||||||
void* RetAddr = _ReturnAddress(); // VC++
|
|
||||||
#else
|
|
||||||
void* RetAddr = __builtin_return_address (0); // gcc
|
void* RetAddr = __builtin_return_address (0); // gcc
|
||||||
|
#else
|
||||||
|
void* RetAddr = _ReturnAddress(); // VC++
|
||||||
#endif
|
#endif
|
||||||
return GfMemoryManagerAlloc(size, GF_MM_ALLOCTYPE_MALLOC,RetAddr);
|
return GfMemoryManagerAlloc(size, GF_MM_ALLOCTYPE_MALLOC,RetAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _tgf_win_free(void * b)
|
void _tgf_win_free(void * b)
|
||||||
{
|
{
|
||||||
GfMemoryManagerFree(b, GF_MM_ALLOCTYPE_MALLOC);
|
if (GfMemoryManagerRunning())
|
||||||
|
{
|
||||||
|
if (GfMM->DoNotFree)
|
||||||
|
GfMemoryManagerAccept(b, GF_MM_ALLOCTYPE_MALLOC);
|
||||||
|
else
|
||||||
|
GfMemoryManagerFree(b, GF_MM_ALLOCTYPE_MALLOC);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
GfMemoryManagerFree(b, GF_MM_ALLOCTYPE_MALLOC);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _tgf_win_accept(void * b)
|
void _tgf_win_accept(void * b)
|
||||||
|
@ -152,6 +172,7 @@ tMemoryManager* GfMemoryManager()
|
||||||
MemoryManager->Size = sizeof(tMemoryManager);
|
MemoryManager->Size = sizeof(tMemoryManager);
|
||||||
MemoryManager->State = GF_MM_STATE_NULL;
|
MemoryManager->State = GF_MM_STATE_NULL;
|
||||||
MemoryManager->AddedSpace = 0;
|
MemoryManager->AddedSpace = 0;
|
||||||
|
MemoryManager->DoNotFree = false;
|
||||||
|
|
||||||
MemoryManager->RootOfList.Mark = MM_MARKER;
|
MemoryManager->RootOfList.Mark = MM_MARKER;
|
||||||
MemoryManager->RootOfList.Type = GF_MM_ALLOCTYPE_MEMMAN;
|
MemoryManager->RootOfList.Type = GF_MM_ALLOCTYPE_MEMMAN;
|
||||||
|
@ -471,6 +492,24 @@ bool GfMemoryManagerRunning()
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// Set DoNotFree flag for debugging
|
||||||
|
//
|
||||||
|
void GfMemoryManagerDoAccept()
|
||||||
|
{
|
||||||
|
GfMM->DoNotFree = true;
|
||||||
|
}
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// Set DoNotFree flag for debugging
|
||||||
|
//
|
||||||
|
void GfMemoryManagerDoFree()
|
||||||
|
{
|
||||||
|
GfMM->DoNotFree = false;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
// ... WDB test
|
// ... WDB test
|
||||||
|
|
|
@ -32,6 +32,8 @@ TGF_API bool GfMemoryManagerAllocate(); // Initialize memory manager
|
||||||
TGF_API void GfMemoryManagerRelease(); // Release memory manager at Shutdown
|
TGF_API void GfMemoryManagerRelease(); // Release memory manager at Shutdown
|
||||||
TGF_API bool GfMemoryManagerRunning(); // Is the memory manager running?
|
TGF_API bool GfMemoryManagerRunning(); // Is the memory manager running?
|
||||||
TGF_API void GfMemoryManagerSetup(int AddedSpace);
|
TGF_API void GfMemoryManagerSetup(int AddedSpace);
|
||||||
|
TGF_API void GfMemoryManagerDoAccept();
|
||||||
|
TGF_API void GfMemoryManagerDoFree();
|
||||||
//
|
//
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -82,6 +84,7 @@ typedef struct
|
||||||
size_t Size; // Size of memory manager
|
size_t Size; // Size of memory manager
|
||||||
int State; // State of memory manager
|
int State; // State of memory manager
|
||||||
int AddedSpace; // Number of bytes added to each block
|
int AddedSpace; // Number of bytes added to each block
|
||||||
|
bool DoNotFree; // Do not free the blocks if flag is set
|
||||||
|
|
||||||
} tMemoryManager;
|
} tMemoryManager;
|
||||||
//
|
//
|
||||||
|
|
|
@ -216,7 +216,7 @@ TGF_API void GfPoolMove(tMemoryPool* oldPool, tMemoryPool* newPool);
|
||||||
/*********************************
|
/*********************************
|
||||||
* New memory debug tools *
|
* New memory debug tools *
|
||||||
*********************************/
|
*********************************/
|
||||||
// To enble the hunting for memory leaks uncomment the following line
|
// To enable the hunting for memory leaks uncomment the following line
|
||||||
//#define __DEBUG_MEMORYMANAGER__
|
//#define __DEBUG_MEMORYMANAGER__
|
||||||
#if (defined(WIN32) && defined(__DEBUG_MEMORYMANAGER__))
|
#if (defined(WIN32) && defined(__DEBUG_MEMORYMANAGER__))
|
||||||
|
|
||||||
|
@ -225,6 +225,8 @@ TGF_API void GfPoolMove(tMemoryPool* oldPool, tMemoryPool* newPool);
|
||||||
#define realloc _tgf_win_realloc
|
#define realloc _tgf_win_realloc
|
||||||
#define free _tgf_win_free
|
#define free _tgf_win_free
|
||||||
#define accept _tgf_win_accept
|
#define accept _tgf_win_accept
|
||||||
|
#define doaccept GfMemoryManagerDoAccept
|
||||||
|
#define dofree GfMemoryManagerDoFree
|
||||||
#ifdef strdup
|
#ifdef strdup
|
||||||
#undef strdup
|
#undef strdup
|
||||||
#endif
|
#endif
|
||||||
|
@ -236,6 +238,8 @@ TGF_API void * _tgf_win_realloc(void * memblock, size_t size);
|
||||||
TGF_API void _tgf_win_free(void * memblock);
|
TGF_API void _tgf_win_free(void * memblock);
|
||||||
TGF_API void _tgf_win_accept(void * memblock);
|
TGF_API void _tgf_win_accept(void * memblock);
|
||||||
TGF_API char * _tgf_win_strdup(const char * str);
|
TGF_API char * _tgf_win_strdup(const char * str);
|
||||||
|
TGF_API void GfMemoryManagerDoAccept();
|
||||||
|
TGF_API void GfMemoryManagerDoFree();
|
||||||
|
|
||||||
#endif // WIN32
|
#endif // WIN32
|
||||||
// </esppat>
|
// </esppat>
|
||||||
|
|
|
@ -18,56 +18,53 @@
|
||||||
|
|
||||||
#include "gui.h"
|
#include "gui.h"
|
||||||
|
|
||||||
|
// WDB test ...
|
||||||
|
#ifdef __DEBUG_MEMORYMANAGER__
|
||||||
|
|
||||||
// Avoid memory leaks ...
|
// Avoid memory leaks ...
|
||||||
int NumberOfScreens = -1;
|
int NumberOfScreens = 0;
|
||||||
tGfuiScreen* OwnerOfScreens[MAXSCREENS];
|
tGfuiScreen* OwnerOfScreens[MAXSCREENS];
|
||||||
|
|
||||||
void RegisterScreens(void* screen)
|
void RegisterScreens(void* screen)
|
||||||
{
|
{
|
||||||
if (++NumberOfScreens < MAXSCREENS)
|
tGfuiScreen* _screen = (tGfuiScreen*) screen;
|
||||||
OwnerOfScreens[NumberOfScreens] = (tGfuiScreen*) screen;
|
if (NumberOfScreens < MAXSCREENS)
|
||||||
|
OwnerOfScreens[NumberOfScreens++] = _screen;
|
||||||
else
|
else
|
||||||
GfLogInfo("NumberOfScreens: %d > MAXSCREENS\n", NumberOfScreens);
|
GfLogInfo("NumberOfScreens: %d > MAXSCREENS\n", NumberOfScreens);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FreeScreens()
|
void FreeScreens()
|
||||||
{
|
{
|
||||||
|
// For debugging purposes:
|
||||||
|
//doaccept(); // Do not free the blocks, just take it out of the list
|
||||||
|
|
||||||
for (int I = 0; I <= NumberOfScreens; I++)
|
for (int I = 0; I <= NumberOfScreens; I++)
|
||||||
{
|
{
|
||||||
|
// This screen is corrupted!
|
||||||
|
// TODO: Find out why
|
||||||
|
if (I == 2)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Get the screen from the owner
|
||||||
tGfuiScreen* screen = OwnerOfScreens[I];
|
tGfuiScreen* screen = OwnerOfScreens[I];
|
||||||
if (screen)
|
if (screen)
|
||||||
{
|
{
|
||||||
tGfuiObject* object = screen->objects;
|
GfuiScreenRelease(screen); // Free all resources
|
||||||
while (object)
|
|
||||||
{
|
|
||||||
tGfuiObject* release = object;
|
|
||||||
object = object->next;
|
|
||||||
if (object == release)
|
|
||||||
object = NULL;
|
|
||||||
if (object == screen->objects)
|
|
||||||
object = NULL;
|
|
||||||
gfuiReleaseObject(release);
|
|
||||||
}
|
|
||||||
|
|
||||||
tGfuiKey* key = screen->userKeys;
|
|
||||||
while (key)
|
|
||||||
{
|
|
||||||
tGfuiKey* relkey = key;
|
|
||||||
key = key->next;
|
|
||||||
if (key == relkey)
|
|
||||||
key = NULL;
|
|
||||||
if (key == screen->userKeys)
|
|
||||||
key = NULL;
|
|
||||||
free(relkey->name);
|
|
||||||
free(relkey->descr);
|
|
||||||
free(relkey);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(screen);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Back to normal mode
|
||||||
|
dofree(); // Free the blocks
|
||||||
|
|
||||||
}
|
}
|
||||||
// ... Avoid memory leaks
|
// ... Avoid memory leaks
|
||||||
|
#else
|
||||||
|
void RegisterScreens(void* screen){};
|
||||||
|
void FreeScreens(){};
|
||||||
|
#endif
|
||||||
|
// ... WDB test
|
||||||
|
|
||||||
|
|
||||||
void GfuiInit(void)
|
void GfuiInit(void)
|
||||||
{
|
{
|
||||||
|
|
|
@ -75,8 +75,13 @@ main(int argc, char *argv[])
|
||||||
// THIS HAS TO BE THE FIRST LINE OF CODE!!!
|
// THIS HAS TO BE THE FIRST LINE OF CODE!!!
|
||||||
GfMemoryManagerAllocate();
|
GfMemoryManagerAllocate();
|
||||||
|
|
||||||
|
// Because there are some memory blocks that are allocated too small
|
||||||
|
// we get corrupted the following memory blocks.
|
||||||
|
// To avoid it, we can use an additional size (4 Bytes per block)
|
||||||
|
// while allocation!
|
||||||
|
|
||||||
// For hunting of corrupted memory blocks comment the following line
|
// For hunting of corrupted memory blocks comment the following line
|
||||||
//GfMemoryManagerSetup(4); // Add 4 bytes per block
|
GfMemoryManagerSetup(4); // Add 4 bytes per block
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
// ... WDB test
|
// ... WDB test
|
||||||
|
|
Loading…
Reference in a new issue