Skip to content

Commit

Permalink
Detect incorrect use of pool.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjp41 committed Apr 2, 2020
1 parent 4a3f627 commit 5ac3501
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/mem/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ namespace snmalloc
T* p = stack.pop();

if (p != nullptr)
{
p->set_in_use();
return p;
}

p = memory_provider
.template alloc_chunk<T, bits::next_pow2_const(sizeof(T))>(
Expand All @@ -60,14 +63,21 @@ namespace snmalloc
p->list_next = list;
list = p;

p->set_in_use();
return p;
}

/**
* Return to the pool an object previously retrieved by `acquire`
*
* Do not return objects from `extract`.
*/
void release(T* p)
{
// The object's destructor is not run. If the object is "reallocated", it
// is returned without the constructor being run, so the object is reused
// without re-initialisation.
p->reset_in_use();
stack.push(p);
}

Expand All @@ -80,6 +90,11 @@ namespace snmalloc
return p->next;
}

/**
* Return to the pool a list of object previously retrieved by `extract`
*
* Do not return objects from `acquire`.
*/
void restore(T* first, T* last)
{
// Pushes a linked list of objects onto the stack. Use to put a linked
Expand Down
14 changes: 14 additions & 0 deletions src/mem/pooled.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,19 @@ namespace snmalloc
std::atomic<T*> next = nullptr;
/// Used by the pool to keep the list of all entries ever created.
T* list_next;
std::atomic_flag in_use = ATOMIC_FLAG_INIT;

public:
void set_in_use()
{
if (in_use.test_and_set())
error("Critical error: double use of Pooled Type!");
}

void reset_in_use()
{
in_use.clear();
}

};
} // namespace snmalloc

0 comments on commit 5ac3501

Please sign in to comment.