This source file includes following definitions.
- InitNoLock
- MapImplNoLock
- Unmap
#include "mojo/system/raw_shared_buffer.h"
#include <windows.h>
#include <limits>
#include "base/logging.h"
#include "base/sys_info.h"
#include "mojo/embedder/platform_handle.h"
#include "mojo/embedder/scoped_platform_handle.h"
namespace mojo {
namespace system {
bool RawSharedBuffer::InitNoLock() {
DCHECK(!handle_.is_valid());
if (static_cast<uint64_t>(num_bytes_) >
static_cast<uint64_t>(std::numeric_limits<DWORD>::max())) {
return false;
}
handle_.reset(embedder::PlatformHandle(CreateFileMapping(
INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
static_cast<DWORD>(num_bytes_), NULL)));
if (!handle_.is_valid()) {
PLOG(ERROR) << "CreateFileMapping";
return false;
}
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<DWORD>::max()));
void* real_base = MapViewOfFile(
handle_.get().handle, FILE_MAP_READ | FILE_MAP_WRITE, 0,
static_cast<DWORD>(real_offset), real_length);
if (!real_base) {
PLOG(ERROR) << "MapViewOfFile";
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() {
BOOL result = UnmapViewOfFile(real_base_);
PLOG_IF(ERROR, !result) << "UnmapViewOfFile";
}
}
}