I've written an application which supports plugins. Now I am wondering: which is the easiest way(from the perspective of the plugin programmer) to track how much heap memory a plugin has reserved, so that it can be displayed in the main application.
The restrictions I am currently imposing on the plugin programmer are:
- They must inherit an interface that looks like:
class plugin
{
void init();
void process();
void reset();
};
class plugin
{
void init();
void process();
void reset();
};
To copy to clipboard, switch view to plain text mode
- They aren't allowed to keep member or stack variables, but should allocate memory in init() and release in reset().
- They must include a certain header file of mine.
Now, the perfect solution would be completely transparent and impose no further restrictions on the programmer. I was hoping I could overload the new operator for the plugin class:
class plugin
{
...
void *operator new( size_t n);
void operator delete( void *p, size_t n);
void *operator new[]( size_t n );
void operator delete[]( void *p, size_t n);
};
class plugin
{
...
void *operator new( size_t n);
void operator delete( void *p, size_t n);
void *operator new[]( size_t n );
void operator delete[]( void *p, size_t n);
};
To copy to clipboard, switch view to plain text mode
but that isn't enough if the constructors of the objects created also call new. I could catch some cases by reimplementing the global new the same way:
void *operator new( size_t n);
void operator delete( void *p, size_t n);
void *operator new[]( size_t n );
void operator delete[]( void *p, size_t n);
void *operator new( size_t n);
void operator delete( void *p, size_t n);
void *operator new[]( size_t n );
void operator delete[]( void *p, size_t n);
To copy to clipboard, switch view to plain text mode
and I guess I should do the same with malloc and free for good measure.
But what if the plugin programmer uses classes defined in a dynamic library. Is there a way to "insert" my new operator into other libraries?
I am not looking for a perfect solution that tracks everything. Typically an init() function will reserve a couple of images and lookup maps, etc.
Here is an example:
void init()
{
// Handled by my global new operator:
lum = new unsigned char[256][256][256];
// OK, as long as they use my image class for which I can reimplement new
img = new Image(800,600, rgb, s8bit);
// What about the stl, it relies heavily on placement new, will my new get called?
// And it seems most classes has an "allocator" template argument.
// The following seems to work, but is that always the case?
data = new std::vector<int>;
data->resize(128*128);
}
void init()
{
// Handled by my global new operator:
lum = new unsigned char[256][256][256];
// OK, as long as they use my image class for which I can reimplement new
img = new Image(800,600, rgb, s8bit);
// What about the stl, it relies heavily on placement new, will my new get called?
// And it seems most classes has an "allocator" template argument.
// The following seems to work, but is that always the case?
data = new std::vector<int>;
data->resize(128*128);
}
To copy to clipboard, switch view to plain text mode
There must be other ways as well, but it seems they all mean more restrictions(or tedious work) for the plugin programmer.
I'm thankful for any ideas how to go about this.
Bookmarks