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

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

Issue 574803002: [NaCl SDK] nacl_io: Fix excess realloc'ing in memfs nodes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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
« no previous file with comments | « native_client_sdk/src/libraries/nacl_io/memfs/mem_fs.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 0040df3faeb8bb7203d737870fdc853e7798bd40..6eee9f0721b5e3c3adbcdedae3268e7a62f7422a 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
@@ -95,24 +95,29 @@ Error MemFsNode::Resize(off_t new_length) {
return EINVAL;
size_t new_size = static_cast<size_t>(new_length);
+ size_t new_capacity = data_capacity_;
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(new_size, kMaxResizeIncrement);
- data_capacity_ = new_size + extra;
- } else {
- data_capacity_ = new_size;
+ new_capacity = new_size + extra;
+ } else if (new_length < stat_.st_size) {
+ // Shrinking capacity
+ new_capacity = 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);
+ if (new_capacity != data_capacity_) {
+ data_ = (char*)realloc(data_, new_capacity);
+ if (new_capacity != 0) {
+ assert(data_ != NULL);
+ if (data_ == NULL)
+ return ENOMEM;
+ }
+ data_capacity_ = new_capacity;
}
+ if (new_length > stat_.st_size)
+ memset(data_ + stat_.st_size, 0, new_length - stat_.st_size);
stat_.st_size = new_length;
return 0;
}
« no previous file with comments | « native_client_sdk/src/libraries/nacl_io/memfs/mem_fs.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698