Chromium Code Reviews| 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 #include "nacl_io/memfs/mem_fs_node.h" | 5 #include "nacl_io/memfs/mem_fs_node.h" |
| 6 | 6 |
| 7 #include <assert.h> | |
| 7 #include <errno.h> | 8 #include <errno.h> |
| 8 #include <string.h> | 9 #include <string.h> |
| 9 | 10 |
| 10 #include <algorithm> | 11 #include <algorithm> |
| 11 | 12 |
| 12 #include "nacl_io/kernel_handle.h" | 13 #include "nacl_io/kernel_handle.h" |
| 13 #include "nacl_io/osstat.h" | 14 #include "nacl_io/osstat.h" |
| 14 #include "sdk_util/auto_lock.h" | 15 #include "sdk_util/auto_lock.h" |
| 15 | 16 |
| 16 namespace nacl_io { | 17 namespace nacl_io { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // The maximum size to reserve in addition to the requested size. Resize() will | 21 // The maximum size to reserve in addition to the requested size. Resize() will |
| 21 // allocate twice as much as requested, up to this value. | 22 // allocate twice as much as requested, up to this value. |
| 22 const size_t kMaxResizeIncrement = 16 * 1024 * 1024; | 23 const size_t kMaxResizeIncrement = 16 * 1024 * 1024; |
| 23 | 24 |
| 24 } // namespace | 25 } // namespace |
| 25 | 26 |
| 26 MemFsNode::MemFsNode(Filesystem* filesystem) : Node(filesystem) { | 27 MemFsNode::MemFsNode(Filesystem* filesystem) |
| 28 : Node(filesystem), | |
| 29 data_(NULL), | |
| 30 data_capacity_(0) { | |
| 27 SetType(S_IFREG); | 31 SetType(S_IFREG); |
| 28 } | 32 } |
| 29 | 33 |
| 30 MemFsNode::~MemFsNode() { | 34 MemFsNode::~MemFsNode() { |
| 31 } | 35 } |
| 32 | 36 |
| 33 Error MemFsNode::Read(const HandleAttr& attr, | 37 Error MemFsNode::Read(const HandleAttr& attr, |
| 34 void* buf, | 38 void* buf, |
| 35 size_t count, | 39 size_t count, |
| 36 int* out_bytes) { | 40 int* out_bytes) { |
| 37 *out_bytes = 0; | 41 *out_bytes = 0; |
| 38 | 42 |
| 39 AUTO_LOCK(node_lock_); | 43 AUTO_LOCK(node_lock_); |
| 40 if (count == 0) | 44 if (count == 0) |
| 41 return 0; | 45 return 0; |
| 42 | 46 |
| 43 size_t size = stat_.st_size; | 47 size_t size = stat_.st_size; |
| 44 | 48 |
| 45 if (attr.offs + count > size) { | 49 if (attr.offs + count > size) { |
| 46 count = size - attr.offs; | 50 count = size - attr.offs; |
| 47 } | 51 } |
| 48 | 52 |
| 49 memcpy(buf, &data_[attr.offs], count); | 53 memcpy(buf, data_ + attr.offs, count); |
| 50 *out_bytes = static_cast<int>(count); | 54 *out_bytes = static_cast<int>(count); |
| 51 return 0; | 55 return 0; |
| 52 } | 56 } |
| 53 | 57 |
| 54 Error MemFsNode::Write(const HandleAttr& attr, | 58 Error MemFsNode::Write(const HandleAttr& attr, |
| 55 const void* buf, | 59 const void* buf, |
| 56 size_t count, | 60 size_t count, |
| 57 int* out_bytes) { | 61 int* out_bytes) { |
| 58 *out_bytes = 0; | 62 *out_bytes = 0; |
| 59 AUTO_LOCK(node_lock_); | 63 AUTO_LOCK(node_lock_); |
| 60 | 64 |
| 61 if (count == 0) | 65 if (count == 0) |
| 62 return 0; | 66 return 0; |
| 63 | 67 |
| 64 if (count + attr.offs > static_cast<size_t>(stat_.st_size)) { | 68 if (count + attr.offs > static_cast<size_t>(stat_.st_size)) { |
| 65 Resize(count + attr.offs); | 69 Error rtn = Resize(count + attr.offs); |
| 70 if (rtn != 0) | |
| 71 return rtn; | |
| 66 count = stat_.st_size - attr.offs; | 72 count = stat_.st_size - attr.offs; |
| 67 } | 73 } |
| 68 | 74 |
| 69 memcpy(&data_[attr.offs], buf, count); | 75 memcpy(data_ + attr.offs, buf, count); |
| 70 *out_bytes = static_cast<int>(count); | 76 *out_bytes = static_cast<int>(count); |
| 71 return 0; | 77 return 0; |
| 72 } | 78 } |
| 73 | 79 |
| 74 Error MemFsNode::FTruncate(off_t new_size) { | 80 Error MemFsNode::FTruncate(off_t new_size) { |
| 75 AUTO_LOCK(node_lock_); | 81 AUTO_LOCK(node_lock_); |
| 76 Resize(new_size); | 82 return Resize(new_size); |
| 83 } | |
| 84 | |
| 85 Error MemFsNode::Resize(off_t new_length) { | |
| 86 if (new_length < 0) | |
| 87 return EINVAL; | |
| 88 if (new_length > SIZE_MAX) | |
| 89 return EFBIG; | |
| 90 size_t new_size = static_cast<size_t>(new_length); | |
| 91 | |
| 92 if (new_size > data_capacity_) { | |
| 93 // While the node size is small, grow exponentially. When it starts to get | |
| 94 // larger, grow linearly. | |
| 95 size_t extra = std::min(new_size, kMaxResizeIncrement); | |
| 96 data_capacity_ = new_size + extra; | |
| 97 } else { | |
| 98 data_capacity_ = new_size; | |
| 99 } | |
| 100 | |
| 101 data_ = (char*)realloc(data_, data_capacity_); | |
| 102 if (data_capacity_ != 0) { | |
| 103 assert(data_ != NULL); | |
|
binji
2014/08/06 17:18:00
could return error here?
Sam Clegg
2014/08/07 11:49:52
Done.
| |
| 104 if (new_length > stat_.st_size) | |
| 105 memset(data_ + stat_.st_size, 0, new_length - stat_.st_size); | |
| 106 } | |
| 107 | |
| 108 stat_.st_size = new_length; | |
| 77 return 0; | 109 return 0; |
| 78 } | 110 } |
| 79 | 111 |
| 80 void MemFsNode::Resize(off_t new_size) { | |
| 81 if (new_size > static_cast<off_t>(data_.capacity())) { | |
| 82 // While the node size is small, grow exponentially. When it starts to get | |
| 83 // larger, grow linearly. | |
| 84 size_t extra = std::min<size_t>(new_size, kMaxResizeIncrement); | |
| 85 data_.reserve(new_size + extra); | |
| 86 } else if (new_size < stat_.st_size) { | |
| 87 // Shrink to fit. std::vector usually doesn't reduce allocation size, so | |
| 88 // use the swap trick. | |
| 89 std::vector<char>(data_).swap(data_); | |
| 90 } | |
| 91 data_.resize(new_size); | |
| 92 stat_.st_size = new_size; | |
| 93 } | |
| 94 | |
| 95 } // namespace nacl_io | 112 } // namespace nacl_io |
| OLD | NEW |