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 #include "base/sys_info.h"
7 #include "shared_memory_region.h"
8
9 namespace media {
10
SharedMemoryRegion(const base::SharedMemoryHandle & handle,off_t offset,size_t size,bool read_only)11 SharedMemoryRegion::SharedMemoryRegion(const base::SharedMemoryHandle& handle,
12 off_t offset,
13 size_t size,
14 bool read_only)
15 : shm_(handle, read_only),
16 offset_(offset),
17 size_(size),
18 alignment_size_(offset % base::SysInfo::VMAllocationGranularity()) {
19 DCHECK_GE(offset_, 0) << "Invalid offset: " << offset_;
20 }
21
SharedMemoryRegion(const BitstreamBuffer & bitstream_buffer,bool read_only)22 SharedMemoryRegion::SharedMemoryRegion(const BitstreamBuffer& bitstream_buffer,
23 bool read_only)
24 : SharedMemoryRegion(bitstream_buffer.handle(),
25 bitstream_buffer.offset(),
26 bitstream_buffer.size(),
27 read_only) {}
28
Map()29 bool SharedMemoryRegion::Map() {
30 if (offset_ < 0) {
31 DVLOG(1) << "Invalid offset: " << offset_;
32 return false;
33 }
34 return shm_.MapAt(offset_ - alignment_size_, size_ + alignment_size_);
35 }
36
memory()37 void* SharedMemoryRegion::memory() {
38 int8_t* addr = reinterpret_cast<int8_t*>(shm_.memory());
39 return addr ? addr + alignment_size_ : nullptr;
40 }
41
42 } // namespace media
43