Hi everyone,

After recently changing to a threadsafe malloc, I'd like to use it for all C++ allocations in WebCore/JavaScriptCore. Darin suggested a neat trick for doing this, where we can do a private declaration of operator new, so it won't affect outside C++ code:

#if __GNUC__
#define PRIVATE_INLINE __private_extern__ inline __attribute__ ((always_inline))
#else
#define PRIVATE_INLINE inline
#endif

PRIVATE_INLINE void* operator new(size_t s) { return fastMalloc(s); }
PRIVATE_INLINE void operator delete(void* p) { fastFree(p); }
PRIVATE_INLINE void* operator new[](size_t s) { return fastMalloc(s); }
PRIVATE_INLINE void operator delete[](void* p) { fastFree(p); }


Note that the operator declarations are inline, so the header that contains this must be included in every C++ implementation file (or you could get new/delete mismatches which would lead to crashes or random memory trashing).

On Mac OS X, the easiest way to do this is to make sure these definitions are included in the per-project prefix header. The Xcode build system will tell gcc to make a precompiled header out of this, and then automatically pass it as a prefix header to all gcc invocations. I don't know if other build systems have anything analogous. Another possible alternative is to make sure these declarations go in config.h, and then force every source file to include it. But that's a little ugly and furthermore hard to maintain, since forgetting a config.h include will not lead to a build failure but will cause incorrect code to be generated.


So does anyone have any experience here? I'd like the solution to work at least on Linux, Windows and Symbian Series 60.

Regards,
Maciej

_______________________________________________
webkit-dev mailing list
[email protected]
http://www.opendarwin.org/mailman/listinfo/webkit-dev

Reply via email to