• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2015 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 // Note: ported from Chromium commit head: 60f9667
5 
6 #ifndef SHARED_MEMORY_REGION_H_
7 #define SHARED_MEMORY_REGION_H_
8 
9 #include "base/memory/shared_memory.h"
10 #include "bitstream_buffer.h"
11 
12 namespace media {
13 
14 // Helper class to access a region of a SharedMemory. Different from
15 // SharedMemory, in which the |offset| of function MapAt() must be aligned to
16 // the value of |SysInfo::VMAllocationGranularity()|, the |offset| of a
17 // SharedMemoryRegion needs not to be aligned, this class hides the details
18 // and returns the mapped address of the given offset.
19 class SharedMemoryRegion {
20  public:
21   // Creates a SharedMemoryRegion.
22   // The mapped memory region begins at |offset| bytes from the start of the
23   // shared memory and the length is |size|. It will take the ownership of
24   // the |handle| and release the resource when being destroyed. Different
25   // from SharedMemory, the |offset| needs not to be aligned to the value of
26   // |SysInfo::VMAllocationGranularity()|.
27   SharedMemoryRegion(const base::SharedMemoryHandle& handle,
28                      off_t offset,
29                      size_t size,
30                      bool read_only);
31 
32   // Creates a SharedMemoryRegion from the given |bistream_buffer|.
33   SharedMemoryRegion(const BitstreamBuffer& bitstream_buffer, bool read_only);
34 
35   // Maps the shared memory into the caller's address space.
36   // Return true on success, false otherwise.
37   bool Map();
38 
39   // Gets a pointer to the mapped region if it has been mapped via Map().
40   // Returns |nullptr| if it is not mapped. The returned pointer points
41   // to the memory at the offset previously passed to the constructor.
42   void* memory();
43 
size()44   size_t size() const { return size_; }
45 
46  private:
47   base::SharedMemory shm_;
48   off_t offset_;
49   size_t size_;
50   size_t alignment_size_;
51 
52   DISALLOW_COPY_AND_ASSIGN(SharedMemoryRegion);
53 };
54 
55 }  // namespace media
56 
57 #endif  // SHARED_MEMORY_REGION_H_
58