blob: c5ebeee6f64e6b5daa3a3ce3c8a7cbcf75b0a439 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
brettw@google.come3c034a2008-08-08 03:31:40 +09005#ifndef BASE_SHARED_MEMORY_H_
6#define BASE_SHARED_MEMORY_H_
initial.commit3f4a7322008-07-27 06:49:38 +09007
brettw@google.come3c034a2008-08-08 03:31:40 +09008#include "base/basictypes.h"
initial.commit3f4a7322008-07-27 06:49:38 +09009#include "base/process_util.h"
10
11// SharedMemoryHandle is a platform specific type which represents
12// the underlying OS handle to a shared memory segment.
brettw@google.come3c034a2008-08-08 03:31:40 +090013#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090014typedef HANDLE SharedMemoryHandle;
15typedef HANDLE SharedMemoryLock;
brettw@google.come3c034a2008-08-08 03:31:40 +090016#elif defined(OS_POSIX)
avi@google.com3a208982008-08-13 06:06:40 +090017#include <semaphore.h>
initial.commit3f4a7322008-07-27 06:49:38 +090018typedef int SharedMemoryHandle;
avi@google.com3a208982008-08-13 06:06:40 +090019typedef sem_t* SharedMemoryLock;
initial.commit3f4a7322008-07-27 06:49:38 +090020#endif
21
22// Platform abstraction for shared memory. Provides a C++ wrapper
23// around the OS primitive for a memory mapped file.
24class SharedMemory {
25 public:
26 // Create a new SharedMemory object.
27 SharedMemory();
28
29 // Create a new SharedMemory object from an existing, open
30 // shared memory file.
31 SharedMemory(SharedMemoryHandle handle, bool read_only);
32
33 // Create a new SharedMemory object from an existing, open
34 // shared memory file that was created by a remote process and not shared
35 // to the current process.
36 SharedMemory(SharedMemoryHandle handle, bool read_only,
37 ProcessHandle process);
38
39 // Destructor. Will close any open files.
40 ~SharedMemory();
41
42 // Creates or opens a shared memory segment based on a name.
43 // If read_only is true, opens the memory as read-only.
44 // If open_existing is true, and the shared memory already exists,
45 // opens the existing shared memory and ignores the size parameter.
46 // Returns true on success, false on failure.
47 bool Create(const std::wstring &name, bool read_only, bool open_existing,
48 size_t size);
49
50 // Opens a shared memory segment based on a name.
51 // If read_only is true, opens for read-only access.
52 // Returns true on success, false on failure.
53 bool Open(const std::wstring &name, bool read_only);
54
55 // Maps the shared memory into the caller's address space.
56 // Returns true on success, false otherwise. The memory address
57 // is accessed via the memory() accessor.
58 bool Map(size_t bytes);
59
60 // Unmaps the shared memory from the caller's address space.
61 // Returns true if successful; returns false on error or if the
62 // memory is not mapped.
63 bool Unmap();
64
65 // Get the size of the opened shared memory backing file.
66 // Note: This size is only available to the creator of the
67 // shared memory, and not to those that opened shared memory
68 // created externally.
69 // Returns 0 if not opened or unknown.
70 size_t max_size() const { return max_size_; }
71
72 // Gets a pointer to the opened memory space if it has been
73 // Mapped via Map(). Returns NULL if it is not mapped.
74 void *memory() const { return memory_; }
75
76 // Get access to the underlying OS handle for this segment.
77 // Use of this handle for anything other than an opaque
78 // identifier is not portable.
79 SharedMemoryHandle handle() const { return mapped_file_; }
80
81 // Closes the open shared memory segment.
82 // It is safe to call Close repeatedly.
83 void Close();
84
85 // Share the shared memory to another process. Attempts
86 // to create a platform-specific new_handle which can be
87 // used in a remote process to access the shared memory
88 // file. new_handle is an ouput parameter to receive
89 // the handle for use in the remote process.
90 // Returns true on success, false otherwise.
91 bool ShareToProcess(ProcessHandle process,
92 SharedMemoryHandle *new_handle) {
93 return ShareToProcessCommon(process, new_handle, false);
94 }
95
96 // Logically equivalent to:
97 // bool ok = ShareToProcess(process, new_handle);
98 // Close();
99 // return ok;
100 bool GiveToProcess(ProcessHandle process,
101 SharedMemoryHandle *new_handle) {
102 return ShareToProcessCommon(process, new_handle, true);
103 }
104
105 // Lock the shared memory.
106 // This is a cross-process lock which may be recursively
107 // locked by the same thread.
108 void Lock();
109
110 // Release the shared memory lock.
111 void Unlock();
112
113 private:
avi@google.com9e0cc152008-08-14 04:59:07 +0900114#if defined(OS_POSIX)
115 bool CreateOrOpen(const std::wstring &name, int posix_flags);
116#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900117 bool ShareToProcessCommon(ProcessHandle process,
118 SharedMemoryHandle *new_handle, bool close_self);
119
120 std::wstring name_;
121 SharedMemoryHandle mapped_file_;
122 void* memory_;
123 bool read_only_;
124 size_t max_size_;
125 SharedMemoryLock lock_;
126
127 DISALLOW_EVIL_CONSTRUCTORS(SharedMemory);
128};
129
130// A helper class that acquires the shared memory lock while
131// the SharedMemoryAutoLock is in scope.
132class SharedMemoryAutoLock {
133 public:
134 explicit SharedMemoryAutoLock(SharedMemory* shared_memory)
135 : shared_memory_(shared_memory) {
136 shared_memory_->Lock();
137 }
138
139 ~SharedMemoryAutoLock() {
140 shared_memory_->Unlock();
141 }
142
143 private:
144 SharedMemory* shared_memory_;
145 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock);
146};
147
148
brettw@google.come3c034a2008-08-08 03:31:40 +0900149#endif // BASE_SHARED_MEMORY_H_
license.botf003cfe2008-08-24 09:55:55 +0900150