Opened 14 years ago

Closed 14 years ago

#2520 closed Feature Requests (fixed)

Overloaded new operator are not called from serialization

Reported by: Runar Undheim <R.Undheim@…> Owned by: Robert Ramey
Milestone: Boost 1.38.0 Component: serialization
Version: Boost 1.37.0 Severity: Problem
Keywords: overloading new operator Cc:

Description

Could someone help to update the serialization code in boost to call the overloaded new operator, please? There already exist some code, but the code is not active because of a #if 0 statement. And if I set the code to #if 1 the code is not compiling with Visual C++ 2005. So I have updated the heap_allocator implementation in archive\detail\iserializer.hpp to support overload of new operator, but I'm not sure if it will work on all compilers. I have just tested the code with Visual C++ 2005.

First the code that I used to check if the new operator is overloaded. If there are compilers that don't support the code, the implementation could be updated using defines and only return false. This way we take advantage of the information on supported compilers, an the code will work as before on unsupported compilers. Should the code that check if it has a new operator be part of the Type Traits library?

typedef char True; // sizeof(True) == 1
typedef struct { char a[2]; } False; // sizeof(False) > 1

template <class U, U x> struct test;

template <typename C>
const True *hasNewOperator(C*, 
  test<void *(*)(size_t),&C::operator new>* = NULL) {
    // Function must be implemented on 
    // Visual C++ 2005 to prevent linking error
    return NULL;
  }

template <typename R>
const False *hasNewOperator(...) {
  // Function must be implemented on 
  // Visual C++ 2005 to prevent linking error
  return NULL;
}

/* #define hasNewOperator(f) (sizeof(*hasNewOperator<f> 
  (static_cast<f*>(NULL))) == sizeof(True)) */

The heap_allocator is then updated to use the hasNewOperator:

template<class T>
struct heap_allocator
{
  template<class U>
  static T* new_operator(U);

  static T* new_operator(const True*) {
    return static_cast<T *>((T::operator new)(sizeof(T)));
  }

  static T* new_operator(const False*) {
    return static_cast<T *>(operator new(sizeof(T)));
  }

  static T * invoke() {
    return new_operator(hasNewOperator<T>(
      static_cast<T *>(NULL)));
  }
};

Attachments (1)

NewOperatorTest.cpp (1.3 KB ) - added by Runar Undheim <R.Undheim@…> 14 years ago.

Download all attachments as: .zip

Change History (4)

comment:1 by Robert Ramey, 14 years ago

Status: newassigned

Could you include a test which can be added to the test suite to verify that this works for all platforms?

Robert Ramey

by Runar Undheim <R.Undheim@…>, 14 years ago

Attachment: NewOperatorTest.cpp added

comment:2 by Runar Undheim <R.Undheim@…>, 14 years ago

I'm new to boost and to test code. But I have added a test file that I think should work. You may probably want to change some of the strings.

Runar Undheim

comment:3 by Robert Ramey, 14 years ago

Resolution: fixed
Status: assignedclosed

I've implemented this idea in accordance with boost customs and practice. Testing shows that it fails on borland, sun version < 5.9, IBM and pathscale. So for now, its included only for gcc and recent versions of msvc

Note: See TracTickets for help on using tickets.