blob: 7b144d162df630fbd42c45594625e60a090fa191 [file] [log] [blame]
[email protected]8c197e502012-11-20 20:32:301// 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#ifndef REMOTING_BASE_SHARED_BUFFER_H_
6#define REMOTING_BASE_SHARED_BUFFER_H_
7
8#include "base/basictypes.h"
9#include "base/memory/ref_counted.h"
10#include "base/process.h"
11#include "base/shared_memory.h"
12
13namespace remoting {
14
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
17// ref-counted lifetime management and unique buffer identifiers.
18class SharedBuffer
19 : public base::RefCountedThreadSafe<SharedBuffer> {
20 public:
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,
23 // ptr() method will return NULL. This constructor set the identifier of this
24 // buffer to 0.
25 explicit SharedBuffer(uint32 size);
26
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
29 // reason, ptr() method will return NULL.
[email protected]ff72dd12012-11-27 05:18:2530 SharedBuffer(int id, base::SharedMemoryHandle handle, uint32 size);
[email protected]8c197e502012-11-20 20:32:3031
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
34 // the operation fails for any reason, ptr() method will return NULL.
[email protected]ff72dd12012-11-27 05:18:2535 SharedBuffer(int id, base::SharedMemoryHandle handle,
[email protected]8c197e502012-11-20 20:32:3036 base::ProcessHandle process, uint32 size);
37
38 // Returns pointer to the beginning of the allocated data buffer. Returns NULL
39 // if the object initialization failed for any reason.
40 void* ptr() const { return shared_memory_.memory(); }
41
42 // Returns handle of the shared memory section containing the allocated
43 // data buffer.
44 base::SharedMemoryHandle handle() const { return shared_memory_.handle(); }
45
[email protected]ff72dd12012-11-27 05:18:2546 int id() const { return id_; }
[email protected]8c197e502012-11-20 20:32:3047 uint32 size() const { return size_; }
48
[email protected]ff72dd12012-11-27 05:18:2549 void set_id(int id) { id_ = id; }
[email protected]8c197e502012-11-20 20:32:3050
51 private:
52 friend class base::RefCountedThreadSafe<SharedBuffer>;
53 virtual ~SharedBuffer();
54
55 // Unique identifier of the buffer or 0 if ID hasn't been set.
[email protected]ff72dd12012-11-27 05:18:2556 int id_;
[email protected]8c197e502012-11-20 20:32:3057
58 // Shared memory section backing up the buffer.
59 base::SharedMemory shared_memory_;
60
61 // Size of the buffer in bytes.
62 uint32 size_;
63
64 DISALLOW_COPY_AND_ASSIGN(SharedBuffer);
65};
66
67} // namespace remoting
68
69#endif // REMOTING_BASE_SHARED_BUFFER_H_