Fix obvious issues in buddy allocator

This commit is contained in:
dcodeIO 2018-02-19 22:45:31 +01:00
parent 1b5fe18c2c
commit 67f6efee1e
4 changed files with 830 additions and 823 deletions

View File

@ -90,16 +90,11 @@ class List {
* MAX_ALLOC (i.e. the whole address space).
*/
var BUCKET_START: usize = HEAP_BASE;
var BUCKET_END: usize = BUCKET_START + BUCKET_COUNT * sizeof<usize>();
var BUCKET_END: usize = BUCKET_START + BUCKET_COUNT * List.SIZE;
function get_bucket(index: usize): List {
assert(index < BUCKET_COUNT);
return load<List>(BUCKET_START + index * sizeof<usize>());
}
function set_bucket(index: usize, list: List): void {
assert(index < BUCKET_COUNT);
store<List>(BUCKET_START + index * sizeof<usize>(), list);
return changetype<List>(BUCKET_START + index * List.SIZE);
}
/*
@ -330,6 +325,8 @@ function lower_bucket_limit(bucket: usize): u32 {
return 1;
}
declare function logi(i: i32): void;
@global
function allocate_memory(request: usize): usize {
var original_bucket: usize, bucket: usize;
@ -349,7 +346,8 @@ function allocate_memory(request: usize): usize {
* possible allocation size. More memory will be reserved later as needed.
*/
if (base_ptr == 0) {
base_ptr = max_ptr = SPLIT_END;
base_ptr = SPLIT_END;
max_ptr = <usize>current_memory() << 16; // differs, must grow first
bucket_limit = BUCKET_COUNT - 1;
update_max_ptr(base_ptr + List.SIZE);
list_init(get_bucket(BUCKET_COUNT - 1));

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -19,9 +19,16 @@ function test(file) {
return String.fromCharCode.apply(String, str);
}
var ptr = exports.allocate_memory(16);
exports.free_memory(ptr);
// runner(exports, 20, 20000); // picked so I/O isn't the bottleneck
// TODO: create an actual test-runner
var ptr1 = exports.allocate_memory(16);
var ptr2 = exports.allocate_memory(16);
if (ptr1 == ptr2) throw Error();
exports.free_memory(ptr1);
exports.free_memory(ptr2);
ptr2 = exports.allocate_memory(16);
if (ptr1 != ptr2) throw Error();
exports.free_memory(ptr2);
console.log("mem final: " + exports.memory.buffer.byteLength);
console.log();
}