Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated exception numbering; NFC

...

Code Block
bgColor#ccccff
langcpp
#include <new>

extern "C++" void update_bookkeeping(void *allocated_ptr, std::size_t size, bool alloc);

struct S {
  void *operator new(std::size_t size) noexcept(false) {
    void *ptr = ::operator new(size);
    update_bookkeeping(ptr, size, true);
    return ptr;
  }
 
  void operator delete(void *ptr, std::size_t size) noexcept {
    ::operator delete(ptr);
    update_bookkeeping(ptr, size, false);
  }
};

Exceptions

DCL35-CPP-EX1: A placement deallocation function may be elided for a corresponding placement allocation function, but only in instances where the object placement allocation and object construction are guaranteed to be noexcept(true). Since placement deallocation functions are only called when some part of the object initialization terminates by throwing an exception, it is safe to elide the placement deallocation function when exceptions cannot be thrown. For instance, some vendors implement compiler flags disabling exception support (such as -fno-cxx-exceptions in Clang or /EHs-c- in Microsoft Visual Studio), which has implementation-defined behavior when an exception is thrown, but generally results in program termination similar to calling abort().

...