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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
6 #define __STDC_LIMIT_MACROS
7 #endif
8
5 #include "nacl_io/memfs/mem_fs_node.h" 9 #include "nacl_io/memfs/mem_fs_node.h"
6 10
11 #include <assert.h>
7 #include <errno.h> 12 #include <errno.h>
8 #include <string.h> 13 #include <string.h>
9 14
10 #include <algorithm> 15 #include <algorithm>
11 16
12 #include "nacl_io/kernel_handle.h" 17 #include "nacl_io/kernel_handle.h"
18 #include "nacl_io/osinttypes.h"
13 #include "nacl_io/osstat.h" 19 #include "nacl_io/osstat.h"
14 #include "sdk_util/auto_lock.h" 20 #include "sdk_util/auto_lock.h"
15 21
16 namespace nacl_io { 22 namespace nacl_io {
17 23
18 namespace { 24 namespace {
19 25
20 // The maximum size to reserve in addition to the requested size. Resize() will 26 // The maximum size to reserve in addition to the requested size. Resize() will
21 // allocate twice as much as requested, up to this value. 27 // allocate twice as much as requested, up to this value.
22 const size_t kMaxResizeIncrement = 16 * 1024 * 1024; 28 const size_t kMaxResizeIncrement = 16 * 1024 * 1024;
23 29
24 } // namespace 30 } // namespace
25 31
26 MemFsNode::MemFsNode(Filesystem* filesystem) : Node(filesystem) { 32 MemFsNode::MemFsNode(Filesystem* filesystem)
33 : Node(filesystem),
34 data_(NULL),
35 data_capacity_(0) {
27 SetType(S_IFREG); 36 SetType(S_IFREG);
28 } 37 }
29 38
30 MemFsNode::~MemFsNode() { 39 MemFsNode::~MemFsNode() {
31 } 40 }
32 41
33 Error MemFsNode::Read(const HandleAttr& attr, 42 Error MemFsNode::Read(const HandleAttr& attr,
34 void* buf, 43 void* buf,
35 size_t count, 44 size_t count,
36 int* out_bytes) { 45 int* out_bytes) {
37 *out_bytes = 0; 46 *out_bytes = 0;
38 47
39 AUTO_LOCK(node_lock_); 48 AUTO_LOCK(node_lock_);
40 if (count == 0) 49 if (count == 0)
41 return 0; 50 return 0;
42 51
43 size_t size = stat_.st_size; 52 size_t size = stat_.st_size;
44 53
45 if (attr.offs + count > size) { 54 if (attr.offs + count > size) {
46 count = size - attr.offs; 55 count = size - attr.offs;
47 } 56 }
48 57
49 memcpy(buf, &data_[attr.offs], count); 58 memcpy(buf, data_ + attr.offs, count);
50 *out_bytes = static_cast<int>(count); 59 *out_bytes = static_cast<int>(count);
51 return 0; 60 return 0;
52 } 61 }
53 62
54 Error MemFsNode::Write(const HandleAttr& attr, 63 Error MemFsNode::Write(const HandleAttr& attr,
55 const void* buf, 64 const void* buf,
56 size_t count, 65 size_t count,
57 int* out_bytes) { 66 int* out_bytes) {
58 *out_bytes = 0; 67 *out_bytes = 0;
59 AUTO_LOCK(node_lock_);
60 68
61 if (count == 0) 69 if (count == 0)
62 return 0; 70 return 0;
63 71
64 if (count + attr.offs > static_cast<size_t>(stat_.st_size)) { 72 AUTO_LOCK(node_lock_);
65 Resize(count + attr.offs); 73 off_t new_size = attr.offs + count;
66 count = stat_.st_size - attr.offs; 74 if (new_size > stat_.st_size) {
75 Error error = Resize(new_size);
76 if (error) {
77 LOG_ERROR("memfs: resize (%" PRIoff ") failed: %s", new_size,
78 strerror(error));
79 return error;
80 }
67 } 81 }
68 82
69 memcpy(&data_[attr.offs], buf, count); 83 memcpy(data_ + attr.offs, buf, count);
70 *out_bytes = static_cast<int>(count); 84 *out_bytes = static_cast<int>(count);
71 return 0; 85 return 0;
72 } 86 }
73 87
74 Error MemFsNode::FTruncate(off_t new_size) { 88 Error MemFsNode::FTruncate(off_t new_size) {
75 AUTO_LOCK(node_lock_); 89 AUTO_LOCK(node_lock_);
76 Resize(new_size); 90 return Resize(new_size);
91 }
92
93 Error MemFsNode::Resize(off_t new_length) {
94 if (new_length < 0)
95 return EINVAL;
96 size_t new_size = static_cast<size_t>(new_length);
97
98 if (new_size > data_capacity_) {
99 // While the node size is small, grow exponentially. When it starts to get
100 // larger, grow linearly.
101 size_t extra = std::min(new_size, kMaxResizeIncrement);
102 data_capacity_ = new_size + extra;
103 } else {
104 data_capacity_ = new_size;
105 }
106
107 data_ = (char*)realloc(data_, data_capacity_);
108 if (data_capacity_ != 0) {
109 assert(data_ != NULL);
110 if (data_ == NULL)
111 return ENOMEM;
112 if (new_length > stat_.st_size)
113 memset(data_ + stat_.st_size, 0, new_length - stat_.st_size);
114 }
115
116 stat_.st_size = new_length;
77 return 0; 117 return 0;
78 } 118 }
79 119
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 120 } // namespace nacl_io
OLDNEW
« no previous file with comments | « native_client_sdk/src/libraries/nacl_io/memfs/mem_fs_node.h ('k') | native_client_sdk/src/libraries/nacl_io/osinttypes.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698