| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 REMOTING_BASE_SHARED_BUFFER_H_ | 5 #ifndef REMOTING_BASE_SHARED_BUFFER_H_ |
| 6 #define REMOTING_BASE_SHARED_BUFFER_H_ | 6 #define REMOTING_BASE_SHARED_BUFFER_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/process.h" | 10 #include "base/process.h" |
| 11 #include "base/shared_memory.h" | 11 #include "base/shared_memory.h" |
| 12 | 12 |
| 13 namespace remoting { | 13 namespace remoting { |
| 14 | 14 |
| 15 // Represents a memory buffer that can be shared between multiple processes. | 15 // Represents a memory buffer that can be shared between multiple processes. |
| 16 // It is more or less a convenience wrapper around base::SharedMemory providing | 16 // It is more or less a convenience wrapper around base::SharedMemory providing |
| 17 // ref-counted lifetime management and unique buffer identifiers. | 17 // ref-counted lifetime management and unique buffer identifiers. |
| 18 class SharedBuffer | 18 class SharedBuffer |
| 19 : public base::RefCountedThreadSafe<SharedBuffer> { | 19 : public base::RefCountedThreadSafe<SharedBuffer> { |
| 20 public: | 20 public: |
| 21 // Creates a new shared memory buffer of the given size and maps it to | 21 // Creates a new shared memory buffer of the given size and maps it to |
| 22 // the memory of the calling process. If the operation fails for any reason, | 22 // the memory of the calling process. If the operation fails for any reason, |
| 23 // ptr() method will return NULL. This constructor set the identifier of this | 23 // ptr() method will return NULL. This constructor set the identifier of this |
| 24 // buffer to 0. | 24 // buffer to 0. |
| 25 explicit SharedBuffer(uint32 size); | 25 explicit SharedBuffer(uint32 size); |
| 26 | 26 |
| 27 // Opens an existing shared memory buffer and maps it to the memory of | 27 // Opens an existing shared memory buffer and maps it to the memory of |
| 28 // the calling process (in read only mode). If the operation fails for any | 28 // the calling process (in read only mode). If the operation fails for any |
| 29 // reason, ptr() method will return NULL. | 29 // reason, ptr() method will return NULL. |
| 30 SharedBuffer(intptr_t id, base::SharedMemoryHandle handle, uint32 size); | 30 SharedBuffer(int id, base::SharedMemoryHandle handle, uint32 size); |
| 31 | 31 |
| 32 // Opens an existing shared memory buffer created by a different process and | 32 // Opens an existing shared memory buffer created by a different process and |
| 33 // maps it to the memory of the calling process (in read only mode). If | 33 // maps it to the memory of the calling process (in read only mode). If |
| 34 // the operation fails for any reason, ptr() method will return NULL. | 34 // the operation fails for any reason, ptr() method will return NULL. |
| 35 SharedBuffer(intptr_t id, base::SharedMemoryHandle handle, | 35 SharedBuffer(int id, base::SharedMemoryHandle handle, |
| 36 base::ProcessHandle process, uint32 size); | 36 base::ProcessHandle process, uint32 size); |
| 37 | 37 |
| 38 // Returns pointer to the beginning of the allocated data buffer. Returns NULL | 38 // Returns pointer to the beginning of the allocated data buffer. Returns NULL |
| 39 // if the object initialization failed for any reason. | 39 // if the object initialization failed for any reason. |
| 40 void* ptr() const { return shared_memory_.memory(); } | 40 void* ptr() const { return shared_memory_.memory(); } |
| 41 | 41 |
| 42 // Returns handle of the shared memory section containing the allocated | 42 // Returns handle of the shared memory section containing the allocated |
| 43 // data buffer. | 43 // data buffer. |
| 44 base::SharedMemoryHandle handle() const { return shared_memory_.handle(); } | 44 base::SharedMemoryHandle handle() const { return shared_memory_.handle(); } |
| 45 | 45 |
| 46 intptr_t id() const { return id_; } | 46 int id() const { return id_; } |
| 47 uint32 size() const { return size_; } | 47 uint32 size() const { return size_; } |
| 48 | 48 |
| 49 void set_id(intptr_t id) { id_ = id; } | 49 void set_id(int id) { id_ = id; } |
| 50 | 50 |
| 51 private: | 51 private: |
| 52 friend class base::RefCountedThreadSafe<SharedBuffer>; | 52 friend class base::RefCountedThreadSafe<SharedBuffer>; |
| 53 virtual ~SharedBuffer(); | 53 virtual ~SharedBuffer(); |
| 54 | 54 |
| 55 // Unique identifier of the buffer or 0 if ID hasn't been set. | 55 // Unique identifier of the buffer or 0 if ID hasn't been set. |
| 56 intptr_t id_; | 56 int id_; |
| 57 | 57 |
| 58 // Shared memory section backing up the buffer. | 58 // Shared memory section backing up the buffer. |
| 59 base::SharedMemory shared_memory_; | 59 base::SharedMemory shared_memory_; |
| 60 | 60 |
| 61 // Size of the buffer in bytes. | 61 // Size of the buffer in bytes. |
| 62 uint32 size_; | 62 uint32 size_; |
| 63 | 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(SharedBuffer); | 64 DISALLOW_COPY_AND_ASSIGN(SharedBuffer); |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 } // namespace remoting | 67 } // namespace remoting |
| 68 | 68 |
| 69 #endif // REMOTING_BASE_SHARED_BUFFER_H_ | 69 #endif // REMOTING_BASE_SHARED_BUFFER_H_ |
| OLD | NEW |