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