diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-07-15 20:04:44 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-07-15 20:04:44 -0700 |
commit | 7cfc95807c12a3351c640ad5dde954cba1d3ea4d (patch) | |
tree | 3bcd70f4ab0fbb9db6df5f16ac4da86530b4101f | |
parent | 414f1dc84930595fa804023435267762e4f66be2 (diff) |
refactor Pointer_make into allocate
-rw-r--r-- | src/library.js | 34 | ||||
-rw-r--r-- | src/library_gl.js | 4 | ||||
-rw-r--r-- | src/library_sdl.js | 2 | ||||
-rw-r--r-- | src/parseTools.js | 4 | ||||
-rw-r--r-- | src/postamble.js | 6 | ||||
-rw-r--r-- | src/preamble.js | 17 |
6 files changed, 29 insertions, 38 deletions
diff --git a/src/library.js b/src/library.js index 326ab987..21078c3c 100644 --- a/src/library.js +++ b/src/library.js @@ -427,9 +427,9 @@ var Library = { textIndex += 1; } } - return Pointer_make(ret.concat(0), 0, ALLOC_STACK, 'i8'); // NB: Stored on the stack + return allocate(ret.concat(0), 'i8', ALLOC_STACK); // NB: Stored on the stack //var len = ret.length+1; - //var ret = Pointer_make(ret.concat(0), 0, ALLOC_STACK); // NB: Stored on the stack + //var ret = allocate(ret.concat(0), 0, ALLOC_STACK); // NB: Stored on the stack //STACKTOP -= len; // XXX horrible hack. we rewind the stack, to 'undo' the alloc we just did. // // the point is that this works if nothing else allocs on the stack before // // the string is read, which should be true - it is very transient, see the *printf* functions below. @@ -523,7 +523,7 @@ var Library = { SEEK_CUR: 1, /* Current position. */ SEEK_END: 2, /* End of file. */ init: function() { - _stdin = Pointer_make([0], null, ALLOC_STATIC, 'void*'); + _stdin = allocate([0], 'void*', ALLOC_STATIC); {{{ makeSetValue('_stdin', '0', "STDIO.prepare('<<stdin>>', null, null, true)", 'i32') }}}; if (Module.stdin) { // Make sure stdin returns a newline @@ -539,10 +539,10 @@ var Library = { }; } - _stdout = Pointer_make([0], null, ALLOC_STATIC, 'void*'); + _stdout = allocate([0], 'void*', ALLOC_STATIC); {{{ makeSetValue('_stdout', '0', "STDIO.prepare('<<stdout>>', null, true)", 'i32') }}}; - _stderr = Pointer_make([0], null, ALLOC_STATIC, 'void*'); + _stderr = allocate([0], 'void*', ALLOC_STATIC); {{{ makeSetValue('_stderr', '0', "STDIO.prepare('<<stderr>>', null, true)", 'i32') }}}; }, cleanFilename: function(filename) { @@ -776,7 +776,7 @@ var Library = { var f = STDIO.streams[stream]; if (!f) return -1; // EOF - arguments[0] = Pointer_make(f.data.slice(f.position).concat(0), 0, ALLOC_STACK, 'i8'); + arguments[0] = allocate(f.data.slice(f.position).concat(0), 'i8', ALLOC_STACK); var ret = __scanString.apply(null, arguments); f.position += ret.bytes; return ret.fields; @@ -846,7 +846,7 @@ var Library = { // Leaky and non-shared... FIXME var info = STDIO.streams[stream]; if (!info) return -1; - return Pointer_make(info.data.slice(offset, offset+num), null, ALLOC_NORMAL, 'i8'); + return allocate(info.data.slice(offset, offset+num), 'i8', ALLOC_NORMAL); }, munmap: function(start, num) { @@ -1205,7 +1205,7 @@ var Library = { }, strdup: function(ptr) { - return Pointer_make(String_copy(ptr, true), 0, ALLOC_NORMAL, 'i8'); + return allocate(String_copy(ptr, true), 'i8', ALLOC_NORMAL); }, strpbrk: function(ptr1, ptr2) { @@ -1322,7 +1322,7 @@ var Library = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0 ]; - me.ret = Pointer_make([Pointer_make(values, 0, ALLOC_STATIC, 'i16')+256], 0, ALLOC_STATIC, 'void*'); + me.ret = allocate([allocate(values, 'i16', ALLOC_STATIC)+256], 'void*', ALLOC_STATIC); #if USE_TYPED_ARRAYS == 0 assert(HEAP[HEAP[me.ret]] == 2); assert(HEAP[HEAP[me.ret]-2] == 0); @@ -1692,7 +1692,7 @@ var Library = { if (DLFCN_DATA.error === null) { var msg = 'An error occurred while loading dynamic library.'; var arr = Module.intArrayFromString(msg) - DLFCN_DATA.error = Pointer_make(arr, 0, 2, 'i8'); + DLFCN_DATA.error = allocate(arr, 'i8', 2); } DLFCN_DATA.isError = false; return DLFCN_DATA.error; @@ -1864,7 +1864,7 @@ var Library = { var timezone = "GMT"; if (!(timezone in ___tm_timezones)) { - ___tm_timezones[timezone] = Pointer_make(intArrayFromString(timezone), null, ALLOC_NORMAL, 'i8'); + ___tm_timezones[timezone] = allocate(intArrayFromString(timezone), 'i8', ALLOC_NORMAL); } {{{ makeSetValue('tmPtr', '___tm_struct_layout.tm_zone', '___tm_timezones[timezone]', 'i32') }}} @@ -1909,7 +1909,7 @@ var Library = { var timezone = date.toString().match(/\(([A-Z]+)\)/)[1]; if (!(timezone in ___tm_timezones)) { - ___tm_timezones[timezone] = Pointer_make(intArrayFromString(timezone), null, ALLOC_NORMAL, 'i8'); + ___tm_timezones[timezone] = allocate(intArrayFromString(timezone), 'i8', ALLOC_NORMAL); } {{{ makeSetValue('tmPtr', '___tm_struct_layout.tm_zone', '___tm_timezones[timezone]', 'i32') }}} @@ -1970,8 +1970,8 @@ var Library = { var winterName = winter.toString().match(/\(([A-Z]+)\)/)[1]; var summerName = summer.toString().match(/\(([A-Z]+)\)/)[1]; - var winterNamePtr = Pointer_make(intArrayFromString(winterName), null, ALLOC_NORMAL, 'i8'); - var summerNamePtr = Pointer_make(intArrayFromString(summerName), null, ALLOC_NORMAL, 'i8'); + var winterNamePtr = allocate(intArrayFromString(winterName), 'i8', ALLOC_NORMAL); + var summerNamePtr = allocate(intArrayFromString(summerName), 'i8', ALLOC_NORMAL); _tzname = _malloc(2 * QUANTUM_SIZE); {{{ makeSetValue('_tzname', '0', 'winterNamePtr', 'i32') }}} {{{ makeSetValue('_tzname', QUANTUM_SIZE, 'summerNamePtr', 'i32') }}} @@ -2048,7 +2048,7 @@ var Library = { // var indexes = Runtime.calculateStructAlignment({ fields: ['i32', 'i32'] }); var me = _localeconv; if (!me.ret) { - me.ret = Pointer_make([Pointer_make(intArrayFromString('.'), null, ALLOC_NORMAL, 'i8')], null, ALLOC_NORMAL, 'i8'); // just decimal point, for now + me.ret = allocate([allocate(intArrayFromString('.'), 'i8', ALLOC_NORMAL)], 'i8', ALLOC_NORMAL); // just decimal point, for now } return me.ret; }, @@ -2060,7 +2060,7 @@ var Library = { nl_langinfo: function(item) { var me = _nl_langinfo; if (!me.ret) { - me.ret = Pointer_make(intArrayFromString("eh?"), null, ALLOC_NORMAL, 'i8'); + me.ret = allocate(intArrayFromString("eh?"), 'i8', ALLOC_NORMAL); } return me.ret; }, @@ -2234,7 +2234,7 @@ var Library = { __setErrNo: function(value) { // For convenient setting and returning of errno. var me = ___setErrNo; - if (!me.ptr) me.ptr = Pointer_make([0], 0, ALLOC_STATIC, 'i32'); + if (!me.ptr) me.ptr = allocate([0], 'i32', ALLOC_STATIC); {{{ makeSetValue('me.ptr', '0', 'value', 'i32') }}} return value; }, diff --git a/src/library_gl.js b/src/library_gl.js index 185494c6..9d5f14fa 100644 --- a/src/library_gl.js +++ b/src/library_gl.js @@ -11,9 +11,9 @@ var LibraryGL = { case Module.ctxGL.VENDOR: case Module.ctxGL.RENDERER: case Module.ctxGL.VERSION: - return Pointer_make(intArrayFromString(Module.ctxGL.getParameter(name_)), null, ALLOC_NORMAL); + return allocate(intArrayFromString(Module.ctxGL.getParameter(name_)), 'i8', ALLOC_NORMAL); case 0x1F03: // Extensions - return Pointer_make(intArrayFromString(Module.ctxGL.getSupportedExtensions().join(' ')), null, ALLOC_NORMAL); + return allocate(intArrayFromString(Module.ctxGL.getSupportedExtensions().join(' ')), 'i8', ALLOC_NORMAL); default: throw 'Failure: Invalid glGetString value: ' + name_; } diff --git a/src/library_sdl.js b/src/library_sdl.js index b7e25e95..6c70f9f6 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -357,7 +357,7 @@ mergeInto(Library, { }, SDL_GetError: function() { - return Pointer_make(intArrayFromString("SDL is cool"), null); + return allocate(intArrayFromString("SDL is cool"), 'i8'); }, SDL_CreateRGBSurface: function(flags, width, height, depth, rmask, gmask, bmask, amask) { diff --git a/src/parseTools.js b/src/parseTools.js index afd57543..4a3867ef 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -895,9 +895,7 @@ function makePointer(slab, pos, allocator, type) { } } - return 'Pointer_make(' + slab + ', ' + (pos ? pos : 0) + (allocator ? ', ' + allocator : '') + ', ' + - JSON.stringify(types) + - ')'; + return 'allocate(' + slab + ', ' + JSON.stringify(types) + (allocator ? ', ' + allocator : '') + ')'; } function makeGetSlabs(ptr, type, allowMultiple, unsigned) { diff --git a/src/postamble.js b/src/postamble.js index bd00f937..f1d48ead 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -8,14 +8,14 @@ Module.callMain = function callMain(args) { argv.push(0); } } - var argv = [Pointer_make(intArrayFromString("/bin/this.program"), null, ALLOC_STATIC, 'i8') ]; + var argv = [allocate(intArrayFromString("/bin/this.program"), 'i8', ALLOC_STATIC) ]; pad(); for (var i = 0; i < argc-1; i = i + 1) { - argv.push(Pointer_make(intArrayFromString(args[i]), null, ALLOC_STATIC, 'i8')); + argv.push(allocate(intArrayFromString(args[i]), 'i8', ALLOC_STATIC)); pad(); } argv.push(0); - argv = Pointer_make(argv, null, ALLOC_STATIC, 'i32'); + argv = allocate(argv, 'i32', ALLOC_STATIC); return _main(argc, argv, 0); } diff --git a/src/preamble.js b/src/preamble.js index 151c399d..e60377e6 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -301,19 +301,13 @@ function assert(condition, text) { } } -// Creates a pointer for a certain slab and a certain address in that slab. -// 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. +// Allocates memory for some data and initializes it properly. var ALLOC_NORMAL = 0; // Tries to use _malloc() var ALLOC_STACK = 1; // Lives for the duration of the current function call var ALLOC_STATIC = 2; // Cannot be freed -function Pointer_make(slab, pos, allocator, types) { - pos = pos ? pos : 0; - assert(pos === 0); // TODO: remove 'pos' - if (slab === HEAP) return pos; +function allocate(slab, types, allocator) { var zeroinit, size; if (typeof slab === 'number') { zeroinit = true; @@ -323,7 +317,6 @@ function Pointer_make(slab, pos, allocator, types) { size = slab.length; } - // Finalize var ret = [_malloc, Runtime.stackAlloc, Runtime.staticAlloc][allocator ? allocator : ALLOC_STATIC](Math.max(size, 1)); var singleType = typeof types === 'string' ? types : null; @@ -342,7 +335,7 @@ function Pointer_make(slab, pos, allocator, types) { continue; } #if ASSERTIONS - assert(type, 'Must know what type to store in Pointer_make!'); + assert(type, 'Must know what type to store in allocate!'); #endif if (type === 'i1') { @@ -367,13 +360,13 @@ function Pointer_make(slab, pos, allocator, types) { {{{ makeSetValue(0, 'ret+i', 'curr', 'double') }}} i += {{{ getNativeFieldSize('double', true) }}}; } else { - abort('invalid type for Pointer_make: ' + type); + abort('invalid type for allocate: ' + type); } } return ret; } -Module['Pointer_make'] = Pointer_make; +Module['allocate'] = allocate; function Pointer_stringify(ptr) { var ret = ""; |