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

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

Powered by Google App Engine
This is Rietveld 408576698