This source file includes following definitions.
- InitNoLock
- MapImplNoLock
- Unmap
#include "mojo/system/raw_shared_buffer.h"
#include <stdint.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <limits>
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_file.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/posix/eintr_wrapper.h"
#include "base/sys_info.h"
#include "base/threading/thread_restrictions.h"
#include "mojo/embedder/platform_handle.h"
COMPILE_ASSERT(sizeof(size_t) <= sizeof(uint64_t), size_t_too_big);
COMPILE_ASSERT(sizeof(off_t) <= sizeof(uint64_t), off_t_too_big);
namespace mojo {
namespace system {
bool RawSharedBuffer::InitNoLock() {
DCHECK(!handle_.is_valid());
base::ThreadRestrictions::ScopedAllowIO allow_io;
if (static_cast<uint64_t>(num_bytes_) >
static_cast<uint64_t>(std::numeric_limits<off_t>::max())) {
return false;
}
base::FilePath shared_buffer_dir;
if (!base::GetShmemTempDir(false, &shared_buffer_dir)) {
LOG(ERROR) << "Failed to get temporary directory for shared memory";
return false;
}
base::FilePath shared_buffer_file;
base::ScopedFILE fp(base::CreateAndOpenTemporaryFileInDir(
shared_buffer_dir, &shared_buffer_file));
if (!fp) {
LOG(ERROR) << "Failed to create/open temporary file for shared memory";
return false;
}
if (unlink(shared_buffer_file.value().c_str()) != 0) {
PLOG(WARNING) << "unlink";
}
base::ScopedFD fd(dup(fileno(fp.get())));
if (!fd.is_valid()) {
PLOG(ERROR) << "dup";
return false;
}
if (HANDLE_EINTR(ftruncate(fd.get(), static_cast<off_t>(num_bytes_))) != 0) {
PLOG(ERROR) << "ftruncate";
return false;
}
handle_.reset(embedder::PlatformHandle(fd.release()));
return true;
}
scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapImplNoLock(
size_t offset,
size_t length) {
lock_.AssertAcquired();
size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity();
size_t real_offset = offset - offset_rounding;
size_t real_length = length + offset_rounding;
DCHECK_LE(static_cast<uint64_t>(real_offset),
static_cast<uint64_t>(std::numeric_limits<off_t>::max()));
void* real_base = mmap(NULL, real_length, PROT_READ | PROT_WRITE, MAP_SHARED,
handle_.get().fd, static_cast<off_t>(real_offset));
if (real_base == MAP_FAILED || !real_base) {
PLOG(ERROR) << "mmap";
return scoped_ptr<RawSharedBufferMapping>();
}
void* base = static_cast<char*>(real_base) + offset_rounding;
return make_scoped_ptr(
new RawSharedBufferMapping(base, length, real_base, real_length));
}
void RawSharedBufferMapping::Unmap() {
int result = munmap(real_base_, real_length_);
PLOG_IF(ERROR, result != 0) << "munmap";
}
}
}