Index: native_client_sdk/src/libraries/nacl_io/memfs/mem_fs_node.cc |
diff --git a/native_client_sdk/src/libraries/nacl_io/memfs/mem_fs_node.cc b/native_client_sdk/src/libraries/nacl_io/memfs/mem_fs_node.cc |
index 1e41591ca0a3b971d8aad42bd1a3bac36ab90c74..0040df3faeb8bb7203d737870fdc853e7798bd40 100644 |
--- a/native_client_sdk/src/libraries/nacl_io/memfs/mem_fs_node.cc |
+++ b/native_client_sdk/src/libraries/nacl_io/memfs/mem_fs_node.cc |
@@ -2,14 +2,20 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#ifndef __STDC_LIMIT_MACROS |
+#define __STDC_LIMIT_MACROS |
+#endif |
+ |
#include "nacl_io/memfs/mem_fs_node.h" |
+#include <assert.h> |
#include <errno.h> |
#include <string.h> |
#include <algorithm> |
#include "nacl_io/kernel_handle.h" |
+#include "nacl_io/osinttypes.h" |
#include "nacl_io/osstat.h" |
#include "sdk_util/auto_lock.h" |
@@ -23,7 +29,10 @@ const size_t kMaxResizeIncrement = 16 * 1024 * 1024; |
} // namespace |
-MemFsNode::MemFsNode(Filesystem* filesystem) : Node(filesystem) { |
+MemFsNode::MemFsNode(Filesystem* filesystem) |
+ : Node(filesystem), |
+ data_(NULL), |
+ data_capacity_(0) { |
SetType(S_IFREG); |
} |
@@ -46,7 +55,7 @@ Error MemFsNode::Read(const HandleAttr& attr, |
count = size - attr.offs; |
} |
- memcpy(buf, &data_[attr.offs], count); |
+ memcpy(buf, data_ + attr.offs, count); |
*out_bytes = static_cast<int>(count); |
return 0; |
} |
@@ -56,40 +65,56 @@ Error MemFsNode::Write(const HandleAttr& attr, |
size_t count, |
int* out_bytes) { |
*out_bytes = 0; |
- AUTO_LOCK(node_lock_); |
if (count == 0) |
return 0; |
- if (count + attr.offs > static_cast<size_t>(stat_.st_size)) { |
- Resize(count + attr.offs); |
- count = stat_.st_size - attr.offs; |
+ AUTO_LOCK(node_lock_); |
+ off_t new_size = attr.offs + count; |
+ if (new_size > stat_.st_size) { |
+ Error error = Resize(new_size); |
+ if (error) { |
+ LOG_ERROR("memfs: resize (%" PRIoff ") failed: %s", new_size, |
+ strerror(error)); |
+ return error; |
+ } |
} |
- memcpy(&data_[attr.offs], buf, count); |
+ memcpy(data_ + attr.offs, buf, count); |
*out_bytes = static_cast<int>(count); |
return 0; |
} |
Error MemFsNode::FTruncate(off_t new_size) { |
AUTO_LOCK(node_lock_); |
- Resize(new_size); |
- return 0; |
+ return Resize(new_size); |
} |
-void MemFsNode::Resize(off_t new_size) { |
- if (new_size > static_cast<off_t>(data_.capacity())) { |
+Error MemFsNode::Resize(off_t new_length) { |
+ if (new_length < 0) |
+ return EINVAL; |
+ size_t new_size = static_cast<size_t>(new_length); |
+ |
+ if (new_size > data_capacity_) { |
// While the node size is small, grow exponentially. When it starts to get |
// larger, grow linearly. |
- size_t extra = std::min<size_t>(new_size, kMaxResizeIncrement); |
- data_.reserve(new_size + extra); |
- } else if (new_size < stat_.st_size) { |
- // Shrink to fit. std::vector usually doesn't reduce allocation size, so |
- // use the swap trick. |
- std::vector<char>(data_).swap(data_); |
+ size_t extra = std::min(new_size, kMaxResizeIncrement); |
+ data_capacity_ = new_size + extra; |
+ } else { |
+ data_capacity_ = new_size; |
} |
- data_.resize(new_size); |
- stat_.st_size = new_size; |
+ |
+ data_ = (char*)realloc(data_, data_capacity_); |
+ if (data_capacity_ != 0) { |
+ assert(data_ != NULL); |
+ if (data_ == NULL) |
+ return ENOMEM; |
+ if (new_length > stat_.st_size) |
+ memset(data_ + stat_.st_size, 0, new_length - stat_.st_size); |
+ } |
+ |
+ stat_.st_size = new_length; |
+ return 0; |
} |
} // namespace nacl_io |