Region-based memory management: Difference between revisions

Content deleted Content added
Line 12:
 
<syntaxhighlight lang="cpp">
class Region;Arena {
public:
void* allocate(size_t size) {
return ::operator new(size);
}
 
void deallocate(void* ptr) {
::operator delete(ptr);
}
};
 
struct ListNode {
Line 20 ⟶ 29:
};
 
Arena arena;
Region* region = new Region();
 
ListNode* head = nullptr;
for (int i = 1; i <= 1000; ++i) {
ListNode* newNode = new static_cast<ListNode*>(regionarena.allocate(sizeof(ListNode)));
newNode->next = head;
head = newNode;
Line 30 ⟶ 40:
// (use list here)
// ...
arena.deallocate(head);
delete region;
</syntaxhighlight>
Although it required many operations to construct the linked list, it can be quickly deallocated in a single operation by destroying the region (arena) in which the nodes were allocated. There is no need to traverse the list.
 
== Implementation ==