Skip to content

Commit

Permalink
memory: let the debug allocator mimic the standard allocator more clo…
Browse files Browse the repository at this point in the history
…sely

The standard allocator returns page-aligned addresses for large allocations.
Some osv code incorrectly relies on this.

While we should fix the incorrect code, for now, adjust the debug allocator
to return aligned addresses.

The debug allocator now uses the following layout:

  [header page][guard page][user data][pattern tail][guard page]
  • Loading branch information
avikivity committed Jul 11, 2013
1 parent 79aa5d2 commit 1ea5672
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions core/mempool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ struct header {
char fence[16];
size_t size2;
};
static const size_t pad_before = mmu::page_size;
static const size_t pad_before = 2 * mmu::page_size;
static const size_t pad_after = mmu::page_size;

void* malloc(size_t size)
Expand All @@ -679,30 +679,30 @@ void* malloc(size_t size)
return std_malloc(size);
}

auto hsize = size + sizeof(header);
auto asize = align_up(hsize, mmu::page_size);
auto asize = align_up(size, mmu::page_size);
auto padded_size = pad_before + asize + pad_after;
void* v = free_area.fetch_add(padded_size, std::memory_order_relaxed);
mmu::vpopulate(v, mmu::page_size);
new (v) header(size);
v += pad_before;
mmu::vpopulate(v, asize);
auto h = new (v) header(size);
memset(v + hsize, '$', asize - hsize);
return h + 1;
memset(v + size, '$', asize - size);
return v;
}

void free(void* v)
{
if (v < debug_base) {
return std_free(v);
}
auto h = static_cast<header*>(v) - 1;
auto h = static_cast<header*>(v - pad_before);
auto size = h->size;
auto hsize = size + sizeof(header);
auto asize = align_up(hsize, mmu::page_size);
char* vv = reinterpret_cast<char*>(h);
assert(std::all_of(vv + hsize, vv + asize, [=](char c) { return c == '$'; }));
auto asize = align_up(size, mmu::page_size);
char* vv = reinterpret_cast<char*>(v);
assert(std::all_of(vv + size, vv + asize, [=](char c) { return c == '$'; }));
h->~header();
mmu::vdepopulate(h, asize);
mmu::vdepopulate(h, mmu::page_size);
mmu::vdepopulate(v, asize);
}

void* realloc(void* v, size_t size)
Expand Down

0 comments on commit 1ea5672

Please sign in to comment.