Hasty Briefsbeta

Bilingual

Speculations on arenas and non-trivial destructors

7 months ago
  • #Memory Management
  • #C++
  • #Arena Allocator
  • The article discusses the use of destructors in C++ with arena allocators, highlighting that destructors do not complicate arena usage.
  • A small change allows resource-managing objects (like file handles, sockets, threads) to be safely placed in arenas, maintaining exception safety.
  • The article introduces an updated arena allocator with an additional pointer member for tracking destructors, adding minimal overhead for objects with non-trivial destructors.
  • The author emphasizes that while these techniques are theoretically sound, they have not yet been tested in real-world C++ applications, acknowledging C++'s complexity.
  • A code example demonstrates the implementation of a linked list of destructors (Dtor) to manage object destruction in reverse order of instantiation.
  • The updated allocator function (alloc) checks for trivial destructors and handles non-trivial cases by registering objects for cleanup via the Dtor list.
  • The article concludes by showing how the updated allocator can be used to manage arrays of resource-managing objects (e.g., TcpSocket) within arenas, ensuring proper cleanup.
  • The interface for using arenas remains unchanged, allowing seamless integration of the new destructor management feature.