Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Unified Diff: native_client_sdk/src/libraries/nacl_io/memfs/mem_fs_node.cc

Issue 443693002: [NaCl SDK] nacl_io: Remove use of new/delete for data buffers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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;
}

Powered by Google App Engine
This is Rietveld 408576698