diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-08-22 16:59:31 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-08-23 10:31:15 -0700 |
commit | ecfcb187e32f87338e55e1e4a2323c5917abe8fd (patch) | |
tree | 630bb22c999e0c2329dc2897ee4a74ec3df60220 | |
parent | c03324859da71a65ccc3d1807836d6ea1264714c (diff) |
keep MEMFS files as typed arrays until we need them to be flexible
-rw-r--r-- | src/library_memfs.js | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/src/library_memfs.js b/src/library_memfs.js index 0fa6cdfb..0ed0a61a 100644 --- a/src/library_memfs.js +++ b/src/library_memfs.js @@ -1,6 +1,16 @@ mergeInto(LibraryManager.library, { $MEMFS__deps: ['$FS'], $MEMFS: { + // content modes + CONTENT_ORIGINAL: 1, // contains the original content written into it (in a typed array most likely) + CONTENT_FLEXIBLE: 2, // has been modified or never set to anything, and is a flexible js array that can grow/shrink + ensureFlexible: function(node) { + if (node.contentMode !== MEMFS.CONTENT_FLEXIBLE) { + node.contents = Array.prototype.slice.call(node.contents); + node.contentMode = MEMFS.CONTENT_FLEXIBLE; + } + }, + mount: function(mount) { return MEMFS.create_node(null, '/', {{{ cDefine('S_IFDIR') }}} | 0777, 0); }, @@ -40,6 +50,7 @@ mergeInto(LibraryManager.library, { mmap: MEMFS.stream_ops.mmap }; node.contents = []; + node.contentMode = MEMFS.CONTENT_FLEXIBLE; } else if (FS.isLink(node.mode)) { node.node_ops = { getattr: MEMFS.node_ops.getattr, @@ -98,6 +109,7 @@ mergeInto(LibraryManager.library, { node.timestamp = attr.timestamp; } if (attr.size !== undefined) { + MEMFS.ensureFlexible(node); var contents = node.contents; if (attr.size < contents.length) contents.length = attr.size; else while (attr.size > contents.length) contents.push(0); @@ -165,7 +177,7 @@ mergeInto(LibraryManager.library, { var contents = stream.node.contents; var size = Math.min(contents.length - position, length); #if USE_TYPED_ARRAYS == 2 - if (contents.subarray) { // typed array + if (size > 8 && contents.subarray) { // non-trivial, and typed array buffer.set(contents.subarray(position, position + size), offset); } else #endif @@ -177,12 +189,22 @@ mergeInto(LibraryManager.library, { return size; }, write: function(stream, buffer, offset, length, position) { - var contents = stream.node.contents; + var node = stream.node; + node.timestamp = Date.now(); + var contents = node.contents; + if (length && contents.length === 0 && position === 0 && buffer.subarray) { + // just replace it with the new data + assert(buffer.length); + node.contents = new Uint8Array(buffer.subarray(offset, offset+length)); // TODO: do not copy when unnecessary + node.contentMode = MEMFS.CONTENT_ORIGINAL; + return length; + } + MEMFS.ensureFlexible(node); + var contents = node.contents; while (contents.length < position) contents.push(0); for (var i = 0; i < length; i++) { contents[position + i] = buffer[offset + i]; } - stream.node.timestamp = Date.now(); return length; }, llseek: function(stream, offset, whence) { @@ -202,6 +224,7 @@ mergeInto(LibraryManager.library, { return position; }, allocate: function(stream, offset, length) { + MEMFS.ensureFlexible(stream.node); var contents = stream.node.contents; var limit = offset + length; while (limit > contents.length) contents.push(0); @@ -240,4 +263,5 @@ mergeInto(LibraryManager.library, { }, } } -});
\ No newline at end of file +}); + |