Skip to content

Commit

Permalink
memory: try harder to allocate large blocks
Browse files Browse the repository at this point in the history
We use index_of_conservative() so that the search for a free block is
guaranteed to run quickly (it ensures that the lists we search have free
blocks at least as large as the requested block), but that can leave
us with unsatisfied allocations even though enough a free block of the
required size is available, but simply in a list of smaller blocks.

Fix by detecting the situation, and falling back to a slower search if we
reach it.
  • Loading branch information
avikivity committed Aug 23, 2015
1 parent 63d0473 commit ebc46f1
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions core/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ class page_list {
}
_front = ary[_front].link._next;
}
page* find(uint32_t n_pages, page* ary) {
auto n = _front;
while (n && ary[n].span_size < n_pages) {
n = ary[n].link._next;
}
if (!n) {
return nullptr;
}
return &ary[n];
}
};

class small_pool {
Expand Down Expand Up @@ -390,6 +400,7 @@ template <typename Trimmer>
void*
cpu_pages::allocate_large_and_trim(unsigned n_pages, Trimmer trimmer) {
auto idx = index_of_conservative(n_pages);
auto orig_idx = idx;
if (n_pages >= (2u << idx)) {
throw std::bad_alloc();
}
Expand All @@ -400,11 +411,19 @@ cpu_pages::allocate_large_and_trim(unsigned n_pages, Trimmer trimmer) {
if (initialize()) {
return allocate_large_and_trim(n_pages, trimmer);
}
// Can smaller list possibly hold object?
idx = index_of(n_pages);
if (idx == orig_idx) { // was exact power of two
// FIXME: request application to free memory
throw std::bad_alloc();
}
}
auto& list = fsu.free_spans[idx];
page* span = list.find(n_pages, pages);
if (!span) {
// FIXME: request application to free memory
throw std::bad_alloc();
}
auto& list = fsu.free_spans[idx];
page* span = &list.front(pages);
auto span_size = span->span_size;
auto span_idx = span - pages;
unlink(list, span);
Expand Down

0 comments on commit ebc46f1

Please sign in to comment.