1 // Copyright (c) 2012 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.
5 #include "courgette/memory_allocator.h"
9 #include "base/files/file_util.h"
10 #include "base/strings/stringprintf.h"
16 // The file is created in the %TEMP% folder.
17 // NOTE: Since the file will be used as backing for a memory allocation,
18 // it will never be so big that size_t cannot represent its size.
19 base::File CreateTempFile() {
21 if (!base::CreateTemporaryFile(&path))
24 int flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ |
25 base::File::FLAG_WRITE | base::File::FLAG_DELETE_ON_CLOSE |
26 base::File::FLAG_TEMPORARY;
27 return base::File(path, flags);
36 FileMapping::FileMapping() : mapping_(NULL), view_(NULL) {
39 FileMapping::~FileMapping() {
43 bool FileMapping::InitializeView(size_t size) {
44 DCHECK(view_ == NULL);
45 DCHECK(mapping_ != NULL);
46 view_ = ::MapViewOfFile(mapping_, FILE_MAP_WRITE, 0, 0, size);
54 bool FileMapping::Create(HANDLE file, size_t size) {
55 DCHECK(file != INVALID_HANDLE_VALUE);
57 mapping_ = ::CreateFileMapping(file, NULL, PAGE_READWRITE, 0, 0, NULL);
61 return InitializeView(size);
64 void FileMapping::Close() {
66 ::UnmapViewOfFile(view_);
68 ::CloseHandle(mapping_);
73 bool FileMapping::valid() const {
77 void* FileMapping::view() const {
83 TempMapping::TempMapping() {
86 TempMapping::~TempMapping() {
89 bool TempMapping::Initialize(size_t size) {
90 file_ = CreateTempFile();
94 // TODO(tommi): The assumption here is that the alignment of pointers (this)
95 // is as strict or stricter than the alignment of the element type. This is
96 // not always true, e.g. __m128 has 16-byte alignment.
98 if (!file_.SetLength(size) ||
99 !mapping_.Create(file_.GetPlatformFile(), size)) {
104 TempMapping** write = reinterpret_cast<TempMapping**>(mapping_.view());
110 void* TempMapping::memory() const {
111 uint8* mem = reinterpret_cast<uint8*>(mapping_.view());
112 // The 'this' pointer is written at the start of mapping_.view(), so
113 // go past it. (See Initialize()).
120 bool TempMapping::valid() const {
121 return mapping_.valid();
125 TempMapping* TempMapping::GetMappingFromPtr(void* mem) {
126 TempMapping* ret = NULL;
128 ret = reinterpret_cast<TempMapping**>(mem)[-1];
134 } // namespace courgette