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

Side by Side Diff: native_client_sdk/src/libraries/nacl_mounts/mount_node_mem.cc

Issue 12194030: Rename mount (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix whitespace Created 7 years, 10 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
(Empty)
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
3 * found in the LICENSE file.
4 */
5 #include "nacl_mounts/mount_node_mem.h"
6
7 #include <errno.h>
8 #include <string.h>
9
10 #include "nacl_mounts/osstat.h"
11 #include "utils/auto_lock.h"
12
13 #define BLOCK_SIZE (1 << 16)
14 #define BLOCK_MASK (BLOCK_SIZE - 1)
15
16 MountNodeMem::MountNodeMem(Mount *mount)
17 : MountNode(mount),
18 data_(NULL),
19 capacity_(0) {
20 stat_.st_mode |= S_IFREG;
21 }
22
23 MountNodeMem::~MountNodeMem() {
24 free(data_);
25 }
26
27 int MountNodeMem::Read(size_t offs, void *buf, size_t count) {
28 AutoLock lock(&lock_);
29 if (count == 0) return 0;
30 if (offs + count > GetSize()) {
31 count = GetSize() - offs;
32 }
33
34 memcpy(buf, &data_[offs], count);
35 return static_cast<int>(count);
36 }
37
38 int MountNodeMem::Write(size_t offs, const void *buf, size_t count) {
39 AutoLock lock(&lock_);
40
41 if (count == 0) return 0;
42
43 if (count + offs > GetSize()) {
44 Truncate(count + offs);
45 count = GetSize() - offs;
46 }
47
48 memcpy(&data_[offs], buf, count);
49 return static_cast<int>(count);
50 }
51
52 int MountNodeMem::Truncate(size_t size) {
53 size_t need = (size + BLOCK_MASK) & ~BLOCK_MASK;
54
55 // If the current capacity is correct, just adjust and return
56 if (need == capacity_) {
57 stat_.st_size = static_cast<off_t>(size);
58 return 0;
59 }
60
61 // Attempt to realloc the block
62 char *newdata = static_cast<char *>(realloc(data_, need));
63 if (newdata != NULL) {
64 // Zero out new space.
65 if (size > GetSize())
66 memset(newdata + GetSize(), 0, size - GetSize());
67
68 data_ = newdata;
69 capacity_ = need;
70 stat_.st_size = static_cast<off_t>(size);
71 return 0;
72 }
73
74 // If we failed, then adjust size according to what we keep
75 if (size > capacity_) size = capacity_;
76
77 // Update the size and return the new size
78 stat_.st_size = static_cast<off_t>(size);
79 errno = EIO;
80 return -1;
81 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698