Replacing operator new and operator delete in applications that use shared libraries (C++)
You can define your own versions of operator new() and operator
delete() in C++ applications. In applications that use shared libraries, it might be useful
for the shared library to use a user-defined operator new() and operator
delete() in the main application executable. You might want to do this if you want more
control over memory management than if you use the default calls to these operators in the C++
Runtime Library libC.a. Enabling this facility in your applications requires using the runtime
linking option -brtl, creating an export list with the mangled names for the
operators you are defining, and building your applications with the correct link time option so that
calls to operator new() and operator delete() are replaceable. The
mangled names indicated by the export list are then available to the runtime environment so that
libraries loaded at run time use your versions of operator new() and
operator delete().
Follow these steps:
- Write the code that defines your own versions of
operator new()oroperator delete(). For example, this program showsoperator new()being defined:#include <new> #include <cstdio> #include <cstdlib> void* operator new(unsigned long x) { printf("operator new %ld\n", x); return malloc(x); } int main() { return 5; } - Create an export list that contains the mangled name symbols for the operator
you are defining. For
new()anddelete(), there are a limited number of name mangling possibilities when you compile with xlC.
For example, depending on the exception handling specified with the
-qlanglvl=newexcp option, different mangled names will be used. See Table 1 for the list of possible mangled names.
As an aid to creating an export list, compile without linking the code that has your operator definitions; use the nm command on your object file to display the symbols that the compiler is using in your object; then, refer to Table 1 to find the matching symbols. For example:- Compile without linking:
The command createsxlC -c my_app.Cmy_app.o. - Use the nm command to display the symbols in new.o
The nm command displays a listing similar to this:nm -epC my_app.o
__nw__FUl is a valid symbol that is listed in Table 1. Add this symbol to your export list..__nw__FUl T 0 TOC d 56 __nw__FUl D 60 12 __nw__FUl d 56 4
- Compile without linking:
- Link your application and use the -bE option to specify the export list you
created that contains the mangled names for the operators you are defining. Also, specify the
-brtl option so that the application uses runtime linking. For example:
Where my_app.exp is the export file that you created in step 2.xlC my_app.o -bE:my_app.exp -brtl
| Mangled names | |
|---|---|
| Operator new and vector new names when compiling with -qlanglvl=nonewexcp |
|
| Operator new and vector new names when compiling with -qlanglvl=newexcp |
|
| Operator delete names |
|
