aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library.js4
-rw-r--r--src/preamble.js7
-rw-r--r--tests/runner.py34
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: