OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef __STDC_LIMIT_MACROS | 5 #ifndef __STDC_LIMIT_MACROS |
6 #define __STDC_LIMIT_MACROS | 6 #define __STDC_LIMIT_MACROS |
7 #endif | 7 #endif |
8 | 8 |
9 #include "nacl_io/memfs/mem_fs_node.h" | 9 #include "nacl_io/memfs/mem_fs_node.h" |
10 | 10 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 Error MemFsNode::FTruncate(off_t new_size) { | 88 Error MemFsNode::FTruncate(off_t new_size) { |
89 AUTO_LOCK(node_lock_); | 89 AUTO_LOCK(node_lock_); |
90 return Resize(new_size); | 90 return Resize(new_size); |
91 } | 91 } |
92 | 92 |
93 Error MemFsNode::Resize(off_t new_length) { | 93 Error MemFsNode::Resize(off_t new_length) { |
94 if (new_length < 0) | 94 if (new_length < 0) |
95 return EINVAL; | 95 return EINVAL; |
96 size_t new_size = static_cast<size_t>(new_length); | 96 size_t new_size = static_cast<size_t>(new_length); |
97 | 97 |
| 98 size_t new_capacity = data_capacity_; |
98 if (new_size > data_capacity_) { | 99 if (new_size > data_capacity_) { |
99 // While the node size is small, grow exponentially. When it starts to get | 100 // While the node size is small, grow exponentially. When it starts to get |
100 // larger, grow linearly. | 101 // larger, grow linearly. |
101 size_t extra = std::min(new_size, kMaxResizeIncrement); | 102 size_t extra = std::min(new_size, kMaxResizeIncrement); |
102 data_capacity_ = new_size + extra; | 103 new_capacity = new_size + extra; |
103 } else { | 104 } else if (new_length < stat_.st_size) { |
104 data_capacity_ = new_size; | 105 // Shrinking capacity |
| 106 new_capacity = new_size; |
105 } | 107 } |
106 | 108 |
107 data_ = (char*)realloc(data_, data_capacity_); | 109 if (new_capacity != data_capacity_) { |
108 if (data_capacity_ != 0) { | 110 data_ = (char*)realloc(data_, new_capacity); |
109 assert(data_ != NULL); | 111 if (new_capacity != 0) { |
110 if (data_ == NULL) | 112 assert(data_ != NULL); |
111 return ENOMEM; | 113 if (data_ == NULL) |
112 if (new_length > stat_.st_size) | 114 return ENOMEM; |
113 memset(data_ + stat_.st_size, 0, new_length - stat_.st_size); | 115 } |
| 116 data_capacity_ = new_capacity; |
114 } | 117 } |
115 | 118 |
| 119 if (new_length > stat_.st_size) |
| 120 memset(data_ + stat_.st_size, 0, new_length - stat_.st_size); |
116 stat_.st_size = new_length; | 121 stat_.st_size = new_length; |
117 return 0; | 122 return 0; |
118 } | 123 } |
119 | 124 |
120 } // namespace nacl_io | 125 } // namespace nacl_io |
OLD | NEW |