diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-01-18 13:28:10 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-01-18 13:28:10 -0800 |
commit | 4d2b97a3473c6f7d94fe048558b662011e7d5e56 (patch) | |
tree | 3d7da3d5d310a8d9e0b3409ce370c385a8103632 | |
parent | cf5947187de4390ad0440940f323470b37befbc8 (diff) | |
parent | 9ec2ec222eb0fc140556a325025ae8a07d9005a0 (diff) |
Merge pull request #176 from jterrace/mmap_allocate_opts
mmap and allocate optimizations
-rw-r--r-- | src/library.js | 4 | ||||
-rw-r--r-- | src/preamble.js | 7 | ||||
-rw-r--r-- | tests/runner.py | 34 |
3 files changed, 43 insertions, 2 deletions
diff --git a/src/library.js b/src/library.js index b07ab21d..5bdc64f4 100644 --- a/src/library.js +++ b/src/library.js @@ -3184,7 +3184,9 @@ LibraryManager.library = { * mmap. */ if (stream == -1) { - return allocate(num, 'i8', ALLOC_NORMAL); + var ptr = _malloc(num); + _memset(ptr, 0, num); + return ptr; } var info = FS.streams[stream]; if (!info) return -1; diff --git a/src/preamble.js b/src/preamble.js index 0b9288c4..5c5c21ea 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -462,9 +462,14 @@ function allocate(slab, types, allocator) { var ret = [_malloc, Runtime.stackAlloc, Runtime.staticAlloc][allocator === undefined ? ALLOC_STATIC : allocator](Math.max(size, singleType ? 1 : types.length)); + if (zeroinit) { + _memset(ret, 0, size); + return ret; + } + var i = 0, type; while (i < size) { - var curr = zeroinit ? 0 : slab[i]; + var curr = slab[i]; if (typeof curr === 'function') { curr = Runtime.getFunctionIndex(curr); diff --git a/tests/runner.py b/tests/runner.py index b90eda35..1ea50f79 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -3975,6 +3975,40 @@ def process(filename): } ''' self.do_run(src, 'value:10') + + def test_mmap(self): + src = ''' + #include <stdio.h> + #include <sys/types.h> + #include <sys/mman.h> + #include <assert.h> + + int main(int argc, char *argv[]) { + const int NUM_BYTES = 8 * 1024 * 1024; + const int NUM_INTS = NUM_BYTES / sizeof(int); + + int* map = (int*)mmap(0, NUM_BYTES, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANON, -1, 0); + assert(map != MAP_FAILED); + + int i; + + for (i = 0; i < NUM_INTS; i++) { + map[i] = i; + } + + for (i = 0; i < NUM_INTS; i++) { + assert(map[i] == i); + } + + assert(munmap(map, NUM_BYTES) == 0); + + printf("hello,world"); + return 0; + } + ''' + self.do_run(src, 'hello,world') + self.do_run(src, 'hello,world', force_c=True) def test_cubescript(self): if self.emcc_args is not None and '-O2' in self.emcc_args: |