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 may be useful for the shared library to use a user defined operator new() and operator delete() in the main application executable. You may 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:

  1. Write the code that defines your own versions of operator new() or operator delete(). For example, this program shows operator 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;
    }
  2. Create an export list that contains the mangled name symbols for the operator you are defining. For new() and delete(), there are a limited number of name mangling possibilities when compiling 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 the compiler is using in your object; then refer to Table 1 to find the matching symbols. For example:
    1. Compile without linking:
      xlC -c my_app.C
      Creates my_app.o.
    2. Use the nm command to display the symbols in new.o
      nm -epC my_app.o
      The nm command displays a listing similar to this:
      .__nw__FUl           T           0
      TOC                  d          56
      __nw__FUl            D          60          12
      __nw__FUl            d          56           4
      __nw__FUl is a valid symbol listed in Table 1. Add this symbol to your export list.
  3. 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:
    xlC my_app.o -bE:my_app.exp -brtl
    Where my_app.exp is the export file that you created in step 2.
Table 1. Mangled names for operator new(), operator delete(), vector new, and vector delete
  Mangled names
Operator new and vector new names when compiling with -qlanglvl=nonewexcp
  • __nw__FUl
  • __nw__FUlPv
  • __nw__FUlRCQ2_3std9nothrow_t
  • __vn__FUl
  • __vn__FUlPv
  • __vn__FUlRCQ2_3std9nothrow_t
Operator new and vector new names when compiling with -qlanglvl=newexcp
  • __snw__FUl
  • __svn__FUl
Operator delete names
  • __dl__FPv
  • __dl__FPvRCQ2_3std9nothrow_t
  • __dl__FPvT1
  • __dl__FPvUl
  • __vd__FPv
  • __vd__FPvRCQ2_3std9nothrow_t
  • __vd__FPvUl