Archived

This topic is now archived and is closed to further replies.

psykr

memory managers

Recommended Posts

I want to write a nice memory manager, that can allocate memory from its own.. uh.. chunk? and have it do memory leak-tracking as well. I haven''t been able to find a good tutorial to understanding this, so I was hoping someone could explain it to me. 1) What''s a good format for encapsulating this memory manager? Using C++, I was thinking about designing it as a class, with member new/delete operators. 2) Overloading the member new/delete operators didn''t work, because it only works when you''re dealing with the class that the operators are overridden in. So I''d have to make a new instance of class CMemoryChunk every time I need some memory, but I''d like to be able to overload the global operators so that they allocate memory from the memory manager class. 3) Although VC++ 6 already has a built-in memory leak tracer, I want to write my own for educational purposes, but I can''t get "#define new new(__FILE__,__LINE__)" and other tricks used in flipcode''s memory leak tracing article straight. Basically, could you explain the article to me in smaller words?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I have never really written a complete one by myself but I don''t if it will help, I personally use this one. Which is very good.

http://www.flipcode.com/askmid/archives.shtml

And look for "Presenting A Memory Manager"

Share this post


Link to post
Share on other sites
I can''t help you write one yourself, but there is a chapter in the book ''C++ programming for games'', or something like it (white with green title) that has a good chapter on writing a memory manager like that. I was asking around about memory managers, and somebody suggested that. I was pleasantly surprised.

It''s not a bad source for information, in the absence of any other suggestions from anybody else.

Share this post


Link to post
Share on other sites
Here's the code from flipcode all in one file (works on VC++ 6.0)

#include <list>
#include <windows.h>
using namespace std;

#ifdef _DEBUG
inline void * __cdecl operator new(unsigned int size,const char *file, int line);
inline void __cdecl operator delete(void *p);

typedef struct {
DWORD address;
DWORD size;
char file[64];
DWORD line;
} ALLOC_INFO;

typedef list<ALLOC_INFO*> AllocList;

AllocList *allocList;

void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum)
{
ALLOC_INFO *info;

if(!allocList) {
allocList = new(AllocList);
}

info = new(ALLOC_INFO);
info->address = addr;
strncpy(info->file, fname, 63);
info->line = lnum;
info->size = asize;
allocList->insert(allocList->begin(), info);
}

void RemoveTrack(DWORD addr)
{
AllocList::iterator i;

if(!allocList)
return;
for(i = allocList->begin(); i != allocList->end(); i++)
{
if((*i)->address == addr)
{
allocList->remove((*i));
break;
}
}
}

void DumpUnfreed()
{
AllocList::iterator i;
DWORD totalSize = 0;
char buf[1024];

if(!allocList)
return;

for(i = allocList->begin(); i != allocList->end(); i++) {
sprintf(buf, "%-50s:\t\tLINE %d,\t\tADDRESS %d\t%d unfreed\n",
(*i)->file, (*i)->line, (*i)->address, (*i)->size);
OutputDebugString(buf);
totalSize += (*i)->size;
}
sprintf(buf, "-----------------------------------------------------------\n");
OutputDebugString(buf);
sprintf(buf, "Total Unfreed: %d bytes\n", totalSize);
OutputDebugString(buf);
}

inline void* __cdecl operator new(unsigned int size,const char *file, int line)
{
void *ptr = malloc(size);
AddTrack((DWORD)ptr, size, file, line);
return(ptr);
};

inline void __cdecl operator delete(void *p)
{
RemoveTrack((DWORD)p);
free(p);
}
#else
#define DumpUnfreed()
#endif


#ifdef _DEBUG
#define DEBUG_NEW new(__FILE__, __LINE__)
#else
#define DEBUG_NEW new
#endif

#define new DEBUG_NEW

void main()
{
int *p = new int;
DumpUnfreed();
}

The biggest part in getting this to work is to place the preprocessor directives in the correct spot. Notice that the #define DEBUG_NEW new stuff happens just before main. If you place this before the other functions then they get all gooked up. Hope this helps you out.

-Shags

edit - Trying to get the source block to work


[edited by - Shags on October 24, 2003 10:17:58 PM]

Share this post


Link to post
Share on other sites