Chromium Code Reviews| 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..ffd0cb5ffbb93f5a28021aebd2d30d576b622c0c 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 |
| @@ -23,7 +23,10 @@ const size_t kMaxResizeIncrement = 16 * 1024 * 1024; |
| } // namespace |
| -MemFsNode::MemFsNode(Filesystem* filesystem) : Node(filesystem) { |
| +MemFsNode::MemFsNode(Filesystem* filesystem) |
| + : Node(filesystem), |
| + data_(NULL), |
| + data_len_(0) { |
| SetType(S_IFREG); |
| } |
| @@ -46,7 +49,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; |
| } |
| @@ -66,7 +69,7 @@ Error MemFsNode::Write(const HandleAttr& attr, |
| count = stat_.st_size - attr.offs; |
| } |
| - memcpy(&data_[attr.offs], buf, count); |
| + memcpy(data_ + attr.offs, buf, count); |
| *out_bytes = static_cast<int>(count); |
| return 0; |
| } |
| @@ -78,17 +81,14 @@ Error MemFsNode::FTruncate(off_t new_size) { |
| } |
| void MemFsNode::Resize(off_t new_size) { |
| - if (new_size > static_cast<off_t>(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_); |
| - } |
| - data_.resize(new_size); |
| + // 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_len_ = new_size + extra; |
|
binji
2014/08/05 19:30:29
We don't want to always grow. What if new_size < d
Sam Clegg
2014/08/06 09:10:32
Done.
|
| + |
| + data_ = (char*)realloc(data_, data_len_); |
| + if (data_len_ > new_size) |
| + memset(data_ + new_size, 0, data_len_ - new_size); |
|
binji
2014/08/05 19:30:29
Why? I think it makes more sense to zero from the
Sam Clegg
2014/08/06 09:10:32
Done.
|
| stat_.st_size = new_size; |
| } |