This source file includes following definitions.
- CreateTempFile
- view_
- InitializeView
- Create
- Close
- valid
- view
- Initialize
- memory
- valid
- GetMappingFromPtr
#include "courgette/memory_allocator.h"
#include <map>
#include "base/file_util.h"
#include "base/strings/stringprintf.h"
#if defined(OS_WIN)
namespace {
base::File CreateTempFile() {
base::FilePath path;
if (!base::CreateTemporaryFile(&path))
return base::File();
int flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ |
base::File::FLAG_WRITE | base::File::FLAG_DELETE_ON_CLOSE |
base::File::FLAG_TEMPORARY;
return base::File(path, flags);
}
}
namespace courgette {
FileMapping::FileMapping() : mapping_(NULL), view_(NULL) {
}
FileMapping::~FileMapping() {
Close();
}
bool FileMapping::InitializeView(size_t size) {
DCHECK(view_ == NULL);
DCHECK(mapping_ != NULL);
view_ = ::MapViewOfFile(mapping_, FILE_MAP_WRITE, 0, 0, size);
if (!view_) {
Close();
return false;
}
return true;
}
bool FileMapping::Create(HANDLE file, size_t size) {
DCHECK(file != INVALID_HANDLE_VALUE);
DCHECK(!valid());
mapping_ = ::CreateFileMapping(file, NULL, PAGE_READWRITE, 0, 0, NULL);
if (!mapping_)
return false;
return InitializeView(size);
}
void FileMapping::Close() {
if (view_)
::UnmapViewOfFile(view_);
if (mapping_)
::CloseHandle(mapping_);
mapping_ = NULL;
view_ = NULL;
}
bool FileMapping::valid() const {
return view_ != NULL;
}
void* FileMapping::view() const {
return view_;
}
TempMapping::TempMapping() {
}
TempMapping::~TempMapping() {
}
bool TempMapping::Initialize(size_t size) {
file_ = CreateTempFile();
if (!file_.IsValid())
return false;
size += sizeof(this);
if (!file_.SetLength(size) ||
!mapping_.Create(file_.GetPlatformFile(), size)) {
file_.Close();
return false;
}
TempMapping** write = reinterpret_cast<TempMapping**>(mapping_.view());
write[0] = this;
return true;
}
void* TempMapping::memory() const {
uint8* mem = reinterpret_cast<uint8*>(mapping_.view());
if (mem)
mem += sizeof(this);
DCHECK(mem);
return mem;
}
bool TempMapping::valid() const {
return mapping_.valid();
}
TempMapping* TempMapping::GetMappingFromPtr(void* mem) {
TempMapping* ret = NULL;
if (mem) {
ret = reinterpret_cast<TempMapping**>(mem)[-1];
}
DCHECK(ret);
return ret;
}
}
#endif