Carrying metadata with fancy pointers

Consider the following high-level design for a memory resource, which should remind you very much of std::pmr::monotonic_buffer_resource:

  • Keep a list of chunks of memory we've gotten from the system. For each chunk, also store an index of how many bytes we've allocated from the beginning of the chunk; and store a count freed of how many bytes we've deallocated from this specific chunk.
  • When someone calls allocate(n), increment any one of our chunks' index by the appropriate number of bytes if possible, or get a new chunk from the upstream resource if absolutely necessary.
  • When someone calls deallocate(p, n), figure out which of our chunks p came from and increment its freed += n. If freed == index, then ...

Get Mastering the C++17 STL now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.