I don't want to use a normal wrapper because I'm actually using new operators in my code and not mallocs. Also, I would like to keep track of the memory my sound library uses (3rd party lib).
I know there is a way to overload the new operator (although I've never done it), but from my understanding that is only on a per class basis. If I could 'globally' overload new, I guess that would be good enough.
Thanks.
Rock
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
: Ideally, I would like to wrap malloc to add this functionality, but would like to do this with the linker.
: I looked into the ALIAS linker option (maybe Watcom specific), and although it says it will replace a symbol with another symbol (malloc with _dbgmalloc for me),
: this really isn't quite good enough since in my wrapper function _dbgmalloc, I need to call the real malloc. In this situation, the linker tries to replace the real malloc call with _dbgmalloc too, which is obviously very bad.
: I don't want to use a normal wrapper because I'm actually using new operators in my code and not mallocs. Also, I would like to keep track of the memory my sound library uses (3rd party lib).
: I know there is a way to overload the new operator (although I've never done it), but from my understanding that is only on a per class basis. If I could 'globally' overload new, I guess that would be good enough.
: Thanks.
: Rock
I don't know if this will work for you, but...
There is a function called coreleft() (and some other related ones like farcoreleft(), etc)
it returns the free memory. If you get the empth memory at the beginning, you can simply call coreleft and subtract
If you are using extended memory, this might not work.
Also, using your substitution scheme, maybe you can create ANOTHER wrapper for malloc (like anothermalloc()) which will simply call malloc(). That gets around the substitution
Hope this helps. If not, ignore my rambling.
MLINK
You could have new call malloc to get its memory (I've done this too), but then you must overload delete to call free. This is because malloc/free and new/delete are NOT guaranteed to use the same heap mechanism.
Keep in mind that some compilers treat operator new and operator new[] differently (and correspondingly operator delete and operator delete[]). If you notice that performing an array-new doesn't end up in operator new directly or indirectly, then you may have to overload operator new[] (and for simplicity you can just call the original operator new unless you're keen on having a seperate routine).
Don't mix malloc/free and new/delete unless you really want trouble, though.
Watcom seems to call malloc for all new operators by default. I realize that that is compiler specific, but I just wanted it for a temporary feature while I'm building my program (easy memory checks).
How do I globally override new? I still want it to call ctrs/dtrs, and if needed I can call malloc/free here (since Watcom does it anyways). At most I only want to write four wrappers (new/delete, new[]/delete[]). Any more than that and it isn't worth the time (for my case).
Thanks.
Rock
If you want to override the global operator new, new[], delete, delete[]... follow the prototype road... follow the prototype road....:
void *operator new(size_t Size);
void *operator new[](size_t Size);
void operator delete(void* pvData);
void operator delete[](void* pvData);
Just define/declare these outside of class scope.