Skip to content
Snippets Groups Projects
Commit 1ea5672f authored by Avi Kivity's avatar Avi Kivity
Browse files

memory: let the debug allocator mimic the standard allocator more closely

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]
parent 79aa5d28
No related branches found
No related tags found
No related merge requests found
...@@ -670,7 +670,7 @@ struct header { ...@@ -670,7 +670,7 @@ struct header {
char fence[16]; char fence[16];
size_t size2; 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; static const size_t pad_after = mmu::page_size;
void* malloc(size_t size) void* malloc(size_t size)
...@@ -679,15 +679,15 @@ void* malloc(size_t size) ...@@ -679,15 +679,15 @@ void* malloc(size_t size)
return std_malloc(size); return std_malloc(size);
} }
auto hsize = size + sizeof(header); auto asize = align_up(size, mmu::page_size);
auto asize = align_up(hsize, mmu::page_size);
auto padded_size = pad_before + asize + pad_after; auto padded_size = pad_before + asize + pad_after;
void* v = free_area.fetch_add(padded_size, std::memory_order_relaxed); 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; v += pad_before;
mmu::vpopulate(v, asize); mmu::vpopulate(v, asize);
auto h = new (v) header(size); memset(v + size, '$', asize - size);
memset(v + hsize, '$', asize - hsize); return v;
return h + 1;
} }
void free(void* v) void free(void* v)
...@@ -695,14 +695,14 @@ void free(void* v) ...@@ -695,14 +695,14 @@ void free(void* v)
if (v < debug_base) { if (v < debug_base) {
return std_free(v); return std_free(v);
} }
auto h = static_cast<header*>(v) - 1; auto h = static_cast<header*>(v - pad_before);
auto size = h->size; auto size = h->size;
auto hsize = size + sizeof(header); auto asize = align_up(size, mmu::page_size);
auto asize = align_up(hsize, mmu::page_size); char* vv = reinterpret_cast<char*>(v);
char* vv = reinterpret_cast<char*>(h); assert(std::all_of(vv + size, vv + asize, [=](char c) { return c == '$'; }));
assert(std::all_of(vv + hsize, vv + asize, [=](char c) { return c == '$'; }));
h->~header(); h->~header();
mmu::vdepopulate(h, asize); mmu::vdepopulate(h, mmu::page_size);
mmu::vdepopulate(v, asize);
} }
void* realloc(void* v, size_t size) void* realloc(void* v, size_t size)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment