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). * MAX_ALLOC (i.e. the whole address space).
*/ */
var BUCKET_START: usize = HEAP_BASE; 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 { function get_bucket(index: usize): List {
assert(index < BUCKET_COUNT); assert(index < BUCKET_COUNT);
return load<List>(BUCKET_START + index * sizeof<usize>()); return changetype<List>(BUCKET_START + index * List.SIZE);
}
function set_bucket(index: usize, list: List): void {
assert(index < BUCKET_COUNT);
store<List>(BUCKET_START + index * sizeof<usize>(), list);
} }
/* /*
@ -330,6 +325,8 @@ function lower_bucket_limit(bucket: usize): u32 {
return 1; return 1;
} }
declare function logi(i: i32): void;
@global @global
function allocate_memory(request: usize): usize { function allocate_memory(request: usize): usize {
var original_bucket: usize, bucket: 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. * possible allocation size. More memory will be reserved later as needed.
*/ */
if (base_ptr == 0) { 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; bucket_limit = BUCKET_COUNT - 1;
update_max_ptr(base_ptr + List.SIZE); update_max_ptr(base_ptr + List.SIZE);
list_init(get_bucket(BUCKET_COUNT - 1)); 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); return String.fromCharCode.apply(String, str);
} }
var ptr = exports.allocate_memory(16); // TODO: create an actual test-runner
exports.free_memory(ptr); var ptr1 = exports.allocate_memory(16);
// runner(exports, 20, 20000); // picked so I/O isn't the bottleneck 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("mem final: " + exports.memory.buffer.byteLength);
console.log(); console.log();
} }