diff options
author | tingyuan <thuang@mozilla.com> | 2013-03-20 17:18:00 +0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-05-21 17:09:58 -0700 |
commit | 36b4d29ab3e5b1dbf1e44aacff8e1c4178526f05 (patch) | |
tree | 72ad27c2d9570d8a15786065c5a73b509fa7f229 | |
parent | b3420393c13ce5dbb5021fcdea225e12d7816383 (diff) |
1. mmap(): Make use of files that is backed by HEAP.
2. mmap(): Use valloc() directly.
3. mmap(): Try to avoid Array.slice() when possible.
-rw-r--r-- | src/library.js | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/src/library.js b/src/library.js index caf1dd0b..6972a89e 100644 --- a/src/library.js +++ b/src/library.js @@ -3726,30 +3726,42 @@ 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); + var ptr = _valloc(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 the file is not in HEAP or MAP_PRIVATE is specified. + if (contents.buffer === HEAPU8.buffer && flags & MAP_PRIVATE == 0) { + 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 = _valloc(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', @@ -3760,7 +3772,8 @@ 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; }, |