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

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..cf80e4def125b0f14a086e82879cb79b073822d8 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
@@ -4,6 +4,7 @@
#include "nacl_io/memfs/mem_fs_node.h"
+#include <assert.h>
#include <errno.h>
#include <string.h>
@@ -23,7 +24,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 +50,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;
}
@@ -62,34 +66,47 @@ Error MemFsNode::Write(const HandleAttr& attr,
return 0;
if (count + attr.offs > static_cast<size_t>(stat_.st_size)) {
- Resize(count + attr.offs);
+ Error rtn = Resize(count + attr.offs);
+ if (rtn != 0)
+ return rtn;
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;
}
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;
+ if (new_length > SIZE_MAX)
+ return EFBIG;
+ 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_ = (char*)realloc(data_, data_capacity_);
+ if (data_capacity_ != 0) {
+ assert(data_ != NULL);
binji 2014/08/06 17:18:00 could return error here?
Sam Clegg 2014/08/07 11:49:52 Done.
+ if (new_length > stat_.st_size)
+ memset(data_ + stat_.st_size, 0, new_length - stat_.st_size);
}
- data_.resize(new_size);
- stat_.st_size = new_size;
+
+ stat_.st_size = new_length;
+ return 0;
}
} // namespace nacl_io

Powered by Google App Engine
This is Rietveld 408576698