refactor memory allocation in places that might happen before malloc is ready

This commit is contained in:
Alon Zakai 2015-05-15 12:04:08 -07:00
Родитель efe2a0bf1e
Коммит da616fdacf
4 изменённых файлов: 11 добавлений и 4 удалений

Просмотреть файл

@ -271,7 +271,7 @@ function JSify(data, functionsOnly) {
print('STATIC_BASE = ' + Runtime.GLOBAL_BASE + ';\n');
print('STATICTOP = STATIC_BASE + ' + Runtime.alignMemory(Variables.nextIndexedOffset) + ';\n');
} else {
print('gb = parentModule["_malloc"]({{{ STATIC_BUMP }}});\n');
print('gb = getMemory({{{ STATIC_BUMP }}});\n');
print('// STATICTOP = STATIC_BASE + ' + Runtime.alignMemory(Variables.nextIndexedOffset) + ';\n'); // comment as metadata only
}
}

Просмотреть файл

@ -433,6 +433,13 @@ function allocate(slab, types, allocator, ptr) {
}
Module['allocate'] = allocate;
// Allocate memory during any stage of startup - static memory early on, dynamic memory later, malloc when ready
function getMemory(size) {
if (!staticSealed) return Runtime.staticAlloc(size);
if (typeof _sbrk !== 'undefined' && !_sbrk.called) return Runtime.dynamicAlloc(size);
return _malloc(size);
}
function Pointer_stringify(ptr, /* optional */ length) {
if (length === 0 || !ptr) return '';
// TODO: use TextDecoder

Просмотреть файл

@ -10,7 +10,7 @@
var gb = 0;
// Each module has its own stack
var STACKTOP = parentModule['_malloc'](TOTAL_STACK);
var STACKTOP = getMemory(TOTAL_STACK);
assert(STACKTOP % 8 == 0);
var STACK_MAX = STACKTOP + TOTAL_STACK;
Module.cleanups.push(function() {

Просмотреть файл

@ -1025,13 +1025,13 @@ if __name__ == '__main__':
# set up emterpreter stack top (note we must use malloc if in a shared lib, or other enviroment where static memory is sealed)
js = ['''
var EMTSTACKTOP = (staticSealed ? _malloc : Runtime.staticAlloc)(%s);
var EMTSTACKTOP = getMemory(%s);
var EMT_STACK_MAX = EMTSTACKTOP + %d;
''' % (EMT_STACK_MAX, EMT_STACK_MAX)]
# write out our bytecode, and runtime relocation logic
js += ['''
var eb = (staticSealed ? _malloc : Runtime.staticAlloc)(%s);
var eb = getMemory(%s);
assert(eb %% 8 === 0);
__ATPRERUN__.push(function() {
''' % len(all_code)]