Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -3601,30 +3601,44 @@ LibraryManager.library = {
* this implementation simply uses malloc underneath the call to
* mmap.
*/
var MAP_PRIVATE = 2;
var allocated = false;

if (!_mmap.mappings) _mmap.mappings = {};

if (stream == -1) {
var ptr = _malloc(num);
if (!ptr) return -1;
_memset(ptr, 0, num);
allocated = true;
} else {
var info = FS.streams[stream];
if (!info) return -1;
var contents = info.object.contents;
contents = Array.prototype.slice.call(contents, offset, offset+num);
ptr = allocate(contents, 'i8', ALLOC_NORMAL);
}
// align to page size
var ret = ptr;
if (ptr % PAGE_SIZE != 0) {
var old = ptr;
ptr = _malloc(num + PAGE_SIZE);
ret = alignMemoryPage(ptr);
_memcpy(ret, old, num);
_free(old);
}
if (stream == -1) {
_memset(ret, 0, num);
// Only make a new copy when MAP_PRIVATE is specified.
if (flags & MAP_PRIVATE == 0) {
// We can't emulate MAP_SHARED when the file is not backed by HEAP.
assert(contents.buffer === HEAPU8.buffer);
ptr = contents.byteOffset;
allocated = false;
} else {
// Try to avoid unnecessary slices.
if (offset > 0 || offset + num < contents.length) {
if (contents.subarray) {
contents = contents.subarray(offset, offset+num);
} else {
contents = Array.prototype.slice.call(contents, offset, offset+num);
}
}
ptr = _malloc(num);
if (!ptr) return -1;
HEAPU8.set(contents, ptr);
allocated = true;
}
}
_mmap.mappings[ret] = { malloc: ptr, num: num };
return ret;

_mmap.mappings[ptr] = { malloc: ptr, num: num, allocated: allocated };
return ptr;
},
__01mmap64_: 'mmap',

Expand All @@ -3635,7 +3649,9 @@ LibraryManager.library = {
if (!info) return 0;
if (num == info.num) {
_mmap.mappings[start] = null;
_free(info.malloc);
if (info.allocated) {
_free(info.malloc);
}
}
return 0;
},
Expand Down
6 changes: 5 additions & 1 deletion src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,11 @@ function allocate(slab, types, allocator, ptr) {

#if USE_TYPED_ARRAYS == 2
if (singleType === 'i8') {
HEAPU8.set(new Uint8Array(slab), ret);
if (slab.subarray || slab.slice) {
HEAPU8.set(slab, ret);
} else {
HEAPU8.set(new Uint8Array(slab), ret);
}
return ret;
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6991,7 +6991,9 @@ def test_mmap(self):
for (int i = 0; i < 10; i++) {
int* map = (int*)mmap(0, 5000, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, -1, 0);
/* TODO: Should we align to 4k?
assert(((int)map) % 4096 == 0); // aligned
*/
assert(munmap(map, 5000) == 0);
}

Expand Down
11 changes: 7 additions & 4 deletions tools/file_packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,12 @@ def was_seen(name):
if file_['mode'] == 'preload':
use_data += '''
curr = DataRequest.prototype.requests['%s'];
curr.response = byteArray.subarray(%d,%d);
var data = byteArray.subarray(%d, %d);
var ptr = _malloc(%d);
HEAPU8.set(data, ptr);
curr.response = HEAPU8.subarray(ptr, ptr + %d);
curr.onload();
''' % (file_['name'], file_['data_start'], file_['data_end'])
''' % (file_['name'], file_['data_start'], file_['data_end'], file_['data_end'] - file_['data_start'], file_['data_end'] - file_['data_start'])
use_data += " Module['removeRunDependency']('datafile_%s');\n" % data_target

if Compression.on:
Expand Down Expand Up @@ -365,9 +368,9 @@ def was_seen(name):
num++;
}
total = Math.ceil(total * Module.expectedDataFileDownloads/num);
Module['setStatus']('Downloading data... (' + loaded + '/' + total + ')');
if (Module['setStatus']) Module['setStatus']('Downloading data... (' + loaded + '/' + total + ')');
} else if (!Module.dataFileDownloads) {
Module['setStatus']('Downloading data...');
if (Module['setStatus']) Module['setStatus']('Downloading data...');
}
}
dataFile.open('GET', '%s', true);
Expand Down