OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/mount_node_mem.h" | 5 #include "nacl_io/mount_node_mem.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 24 matching lines...) Expand all Loading... | |
35 size_t count, | 35 size_t count, |
36 int* out_bytes) { | 36 int* out_bytes) { |
37 *out_bytes = 0; | 37 *out_bytes = 0; |
38 | 38 |
39 AUTO_LOCK(node_lock_); | 39 AUTO_LOCK(node_lock_); |
40 if (count == 0) | 40 if (count == 0) |
41 return 0; | 41 return 0; |
42 | 42 |
43 size_t size = stat_.st_size; | 43 size_t size = stat_.st_size; |
44 | 44 |
45 if (attr.offs + count > size) { | 45 if (attr.offs + count > static_cast<size_t>(size)) { |
binji
2013/11/15 17:26:23
Why is this necessary? size is already a size_t.
Sam Clegg
2013/11/15 18:13:34
Done.
| |
46 count = size - attr.offs; | 46 count = size - attr.offs; |
47 } | 47 } |
48 | 48 |
49 memcpy(buf, &data_[attr.offs], count); | 49 memcpy(buf, &data_[attr.offs], count); |
50 *out_bytes = static_cast<int>(count); | 50 *out_bytes = static_cast<int>(count); |
51 return 0; | 51 return 0; |
52 } | 52 } |
53 | 53 |
54 Error MountNodeMem::Write(const HandleAttr& attr, | 54 Error MountNodeMem::Write(const HandleAttr& attr, |
55 const void* buf, | 55 const void* buf, |
56 size_t count, | 56 size_t count, |
57 int* out_bytes) { | 57 int* out_bytes) { |
58 *out_bytes = 0; | 58 *out_bytes = 0; |
59 AUTO_LOCK(node_lock_); | 59 AUTO_LOCK(node_lock_); |
60 | 60 |
61 if (count == 0) | 61 if (count == 0) |
62 return 0; | 62 return 0; |
63 | 63 |
64 if (count + attr.offs > stat_.st_size) { | 64 if (count + attr.offs > static_cast<size_t>(stat_.st_size)) { |
65 Resize(count + attr.offs); | 65 Resize(count + attr.offs); |
66 count = stat_.st_size - attr.offs; | 66 count = stat_.st_size - attr.offs; |
67 } | 67 } |
68 | 68 |
69 memcpy(&data_[attr.offs], buf, count); | 69 memcpy(&data_[attr.offs], buf, count); |
70 *out_bytes = static_cast<int>(count); | 70 *out_bytes = static_cast<int>(count); |
71 return 0; | 71 return 0; |
72 } | 72 } |
73 | 73 |
74 Error MountNodeMem::FTruncate(off_t new_size) { | 74 Error MountNodeMem::FTruncate(off_t new_size) { |
75 AUTO_LOCK(node_lock_); | 75 AUTO_LOCK(node_lock_); |
76 Resize(new_size); | 76 Resize(new_size); |
77 return 0; | 77 return 0; |
78 } | 78 } |
79 | 79 |
80 void MountNodeMem::Resize(off_t new_size) { | 80 void MountNodeMem::Resize(off_t new_size) { |
81 if (new_size > data_.capacity()) { | 81 if (new_size > static_cast<off_t>(data_.capacity())) { |
82 // While the node size is small, grow exponentially. When it starts to get | 82 // While the node size is small, grow exponentially. When it starts to get |
83 // larger, grow linearly. | 83 // larger, grow linearly. |
84 size_t extra = std::min<size_t>(new_size, kMaxResizeIncrement); | 84 size_t extra = std::min<size_t>(new_size, kMaxResizeIncrement); |
85 data_.reserve(new_size + extra); | 85 data_.reserve(new_size + extra); |
86 } else if (new_size < stat_.st_size) { | 86 } else if (new_size < stat_.st_size) { |
87 // Shrink to fit. std::vector usually doesn't reduce allocation size, so | 87 // Shrink to fit. std::vector usually doesn't reduce allocation size, so |
88 // use the swap trick. | 88 // use the swap trick. |
89 std::vector<char>(data_).swap(data_); | 89 std::vector<char>(data_).swap(data_); |
90 } | 90 } |
91 data_.resize(new_size); | 91 data_.resize(new_size); |
92 stat_.st_size = new_size; | 92 stat_.st_size = new_size; |
93 } | 93 } |
94 | 94 |
95 } // namespace nacl_io | 95 } // namespace nacl_io |
OLD | NEW |