[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / base / shared_memory_nacl.cc
blobfef293719ebc6b4898ac561d16f768b8d5f9366e
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.
5 #include "base/shared_memory.h"
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <sys/mman.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
13 #include "base/logging.h"
15 namespace base {
17 SharedMemory::SharedMemory()
18 : mapped_file_(-1),
19 mapped_size_(0),
20 inode_(0),
21 memory_(NULL),
22 read_only_(false),
23 created_size_(0) {
26 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
27 : mapped_file_(handle.fd),
28 mapped_size_(0),
29 inode_(0),
30 memory_(NULL),
31 read_only_(read_only),
32 created_size_(0) {
35 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
36 ProcessHandle process)
37 : mapped_file_(handle.fd),
38 mapped_size_(0),
39 inode_(0),
40 memory_(NULL),
41 read_only_(read_only),
42 created_size_(0) {
43 NOTREACHED();
46 SharedMemory::~SharedMemory() {
47 Close();
50 // static
51 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
52 return handle.fd >= 0;
55 // static
56 SharedMemoryHandle SharedMemory::NULLHandle() {
57 return SharedMemoryHandle();
60 // static
61 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
62 DCHECK_GE(handle.fd, 0);
63 if (close(handle.fd) < 0)
64 DPLOG(ERROR) << "close";
67 bool SharedMemory::CreateAndMapAnonymous(uint32 size) {
68 // Untrusted code can't create descriptors or handles.
69 return false;
72 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
73 // Untrusted code can't create descriptors or handles.
74 return false;
77 bool SharedMemory::Delete(const std::string& name) {
78 return false;
81 bool SharedMemory::Open(const std::string& name, bool read_only) {
82 return false;
85 bool SharedMemory::Map(uint32 bytes) {
86 if (mapped_file_ == -1)
87 return false;
89 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE),
90 MAP_SHARED, mapped_file_, 0);
92 bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL;
93 if (mmap_succeeded) {
94 mapped_size_ = bytes;
95 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
96 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
97 } else {
98 memory_ = NULL;
101 return mmap_succeeded;
104 bool SharedMemory::Unmap() {
105 if (memory_ == NULL)
106 return false;
108 if (munmap(memory_, mapped_size_) < 0)
109 DPLOG(ERROR) << "munmap";
110 memory_ = NULL;
111 mapped_size_ = 0;
112 return true;
115 SharedMemoryHandle SharedMemory::handle() const {
116 return FileDescriptor(mapped_file_, false);
119 void SharedMemory::Close() {
120 Unmap();
122 if (mapped_file_ > 0) {
123 if (close(mapped_file_) < 0)
124 DPLOG(ERROR) << "close";
125 mapped_file_ = -1;
129 void SharedMemory::Lock() {
130 NOTIMPLEMENTED();
133 void SharedMemory::Unlock() {
134 NOTIMPLEMENTED();
137 bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
138 SharedMemoryHandle *new_handle,
139 bool close_self) {
140 return false;
143 } // namespace base