diff options
author | alon@honor <none@none> | 2010-10-06 20:02:30 -0700 |
---|---|---|
committer | alon@honor <none@none> | 2010-10-06 20:02:30 -0700 |
commit | 99f96653a706fc61d4be31ce8b45cd6d74c06c6a (patch) | |
tree | 8a997d03444ba0ee55d6ec8e68dbeb799be68915 /src/preamble.js | |
parent | 0390eea0b6d157be7d7e2ae0e388e16a3db92581 (diff) |
Runtime system, and inlining of stackEnter/Exit
Diffstat (limited to 'src/preamble.js')
-rw-r--r-- | src/preamble.js | 46 |
1 files changed, 9 insertions, 37 deletions
diff --git a/src/preamble.js b/src/preamble.js index 4f7fef93..33c9515d 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -52,6 +52,11 @@ function Pointer_niceify(ptr) { // If just a slab is given, will allocate room for it and copy it there. In // other words, do whatever is necessary in order to return a pointer, that // points to the slab (and possibly position) we are given. + +ALLOC_NORMAL = 0; // Tries to use _malloc() +ALLOC_STACK = 1; // Lives for the duration of the current function call +ALLOC_STATIC = 2; // Cannot be freed + function Pointer_make(slab, pos, allocator) { pos = pos ? pos : 0; if (slab === HEAP) return pos; @@ -92,42 +97,11 @@ function Pointer_stringify(ptr) { // Memory management -ALLOC_NORMAL = 0; // Tries to use _malloc() -ALLOC_STACK = 1; // Lives for the duration of the current function call -ALLOC_STATIC = 2; // Cannot be freed - -function alignMemory(x) { - return Math.ceil(x/QUANTUM_SIZE)*QUANTUM_SIZE; // Allocate blocks of proper minimum size - // Also keeps STACKTOP/etc. aligned -} - PAGE_SIZE = 4096; function alignMemoryPage(x) { return Math.ceil(x/PAGE_SIZE)*PAGE_SIZE; } -function stackEnter() { - STACK_STACK.push(STACKTOP); -} -function stackExit() { - STACKTOP = STACK_STACK.pop(); -} -function stackAlloc(size) { - size = alignMemory(size); - assert(STACKTOP + size - STACKROOT < TOTAL_STACK, "No room on stack!"); - var ret = STACKTOP; - STACKTOP += size; - return ret; -} - -function staticAlloc(size) { - size = alignMemory(size); - assert(STATICTOP + size - STATICROOT < TOTAL_STATIC, "No room for static allocation!"); - var ret = STATICTOP; - STATICTOP += size; - return ret; -} - // If we don't have malloc/free implemented, use a simple implementation. if (!this._malloc) { _malloc = staticAlloc; @@ -147,14 +121,12 @@ function __initializeRuntime__() { HEAP = intArrayFromString('(null)'); // So printing %s of NULL gives '(null)' // Also this ensures we leave 0 as an invalid address, 'NULL' - if (!this['TOTAL_STATIC']) TOTAL_STATIC = 64*1024*100; // Reserved room for static allocation - STATICROOT = STATICTOP = alignMemoryPage(HEAP.length); - - if (!this['TOTAL_STACK']) TOTAL_STACK = 64*1024*10; // Reserved room for stack STACK_STACK = []; - STACKROOT = STACKTOP = alignMemoryPage(STATICROOT + TOTAL_STATIC); + STACK_ROOT = STACKTOP = alignMemoryPage(10); + if (!this['TOTAL_STACK']) TOTAL_STACK = 64*1024*100; // Reserved room for stack + STACK_MAX = STACK_ROOT + TOTAL_STACK; - HEAPTOP = alignMemoryPage(STACKROOT + TOTAL_STACK); // Start of the "processes' data segment" + STATICTOP = alignMemoryPage(STACK_MAX); } // stdio.h |