I've got a quick question about monitoring memory use in C, before I go hog-wild with my own solutions.
I've written wrappers for memory allocation and deallocation in my code, with the same interface as for the standard C library:
void *dgl_malloc(size_t size);
void dgl_free(void *pointer);
(DGL is the prefix I'm using for my code. I'm not using calloc, so I skipped it)
These are just wrappers for the standard library functions at the moment. I'm more interested in logging memory use than writing my own allocators.
The problem is that there's not a lot I can do with those void pointers. Say for example I want to keep tabs on the amount of memory my functions have allocated. I can increment a counter in dgl_malloc based on the size requested. But when it comes time to release the memory there's no info in that pointer provided to dgl_free to decrement it.
I don't think I can do crazy things with structures, such as making something like:
typedef struct {
void *memPointer;
unsigned long size;
} memStructure;
then creating one of these in dgl_malloc, returning memPointer, then back-casting to memStructure in dgl_free. If I allocate the pointer to another variable, then the extra info is lost.
Is there a simple solution to this that I'm overlooking, or do I have to do hacky? (Note I'm using C, not C++, so C++ only functionality can't be used)