Re: Memory Management

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Klaus Hartmann wrote:
Hi everyone,

I have implemented a memory manager for some SDK I'm working on (a
game engine). The primary goal of this memory manager is to reduce
fragmentation, to perform fast allocation of small blocks of memory,
and to give more detailed information about memory leaks. This
manager works fine, and without problems.

Now, as you might have guessed already, my problems are the operators
new and delete. I wish to overload them in a *safe* way, such that
the memory manager is automatically used. I do not want to use
class-based new/delete operators. It also has to work properly with
other libraries, including the STL. I did overwrite the global
operators, and that woks, but it just doesn't feel safe.

I realize that there's a good chance, that you'll tell me, that
there's just no good and safe way to do what I want. But maybe I'm
wrong, and someone here's got this great idea :-) There's just got to
be some way to write a cusom memory manager.

IF you're using VC++ (which presumably you are, since you're posting here),
and IF your program is monolithic (not a collection of DLLs), then you can
simply provide your own definition of the malloc family of fine functions
and all C and C++ memory allocations will go through your code, including
new, new[], std::allocator, etc.

If you're building a program out of a bunch of DLLs, then it's a bit harder,
but there's still a reasonably clean solution: put your rmemory manager in
a DLL of it's own, supplying malloc (free, etc) and global operator
new/new[]/delete/delete[] as exports from your DLL. Link the import library
for your memory manager with the main executable and all the other DLLs,
making sure that the linker sees your import library before the C/C++
runtime libraries.

-cd


.



Relevant Pages

  • Re: Memory Management
    ... The program is a bunch of DLLs. ... I'm still using the CRT's malloc and free to allocate memory. ... If the application makes a memory allocation request, ... I have implemented a memory manager for some SDK I'm working on (a ...
    (microsoft.public.vc.language)
  • FastMM4 for extended stored procedures DLLs under SQL Server
    ... the default Borland memory manager. ... we do not experience no problems after replacement. ... But we have another part of the project which is a set DLLs which implement ... When using the DLLs now we experience a crash - SQL Server process crashes, ...
    (borland.public.delphi.language.basm)
  • Re: quick question
    ... > dlls and the main program, ... application and all the libraries. ... Look in the FAQ for tips on how to share the FastMM memory manager between ...
    (borland.public.delphi.language.basm)
  • Re: overriding global operator new/delete
    ... I have implemented a memory manager and I would like to override the ... global operators new and delete. ... before msvcrt by placing the memsub.lib before any of standard libraries ... So I think you'll have to override new and delete separately for each ...
    (microsoft.public.vc.language)
  • Re: overriding global operator new/delete
    ... I have implemented a memory manager and I would like to override the ... global operators new and delete. ... My memory manager organized in a dll. ... standard libraries in the linker command line. ...
    (microsoft.public.vc.language)