Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 | |
| 5 #include "courgette/memory_allocator.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 | |
| 9 #ifdef OS_WIN | |
| 10 | |
| 11 namespace courgette { | |
| 12 | |
| 13 // TempFile | |
| 14 | |
| 15 TempFile::TempFile() : file_(base::kInvalidPlatformFileValue), size_(0) { | |
| 16 } | |
| 17 | |
| 18 TempFile::~TempFile() { | |
| 19 Close(); | |
| 20 } | |
| 21 | |
| 22 void TempFile::Close() { | |
| 23 if (valid()) { | |
| 24 base::ClosePlatformFile(file_); | |
| 25 file_ = base::kInvalidPlatformFileValue; | |
| 26 size_ = 0; | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 bool TempFile::Create() { | |
| 31 DCHECK(file_ == base::kInvalidPlatformFileValue); | |
| 32 FilePath path; | |
| 33 if (file_util::CreateTemporaryFile(&path)) { | |
| 34 bool created = false; | |
| 35 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | |
| 36 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ | | |
| 37 base::PLATFORM_FILE_WRITE | | |
| 38 base::PLATFORM_FILE_DELETE_ON_CLOSE | | |
| 39 base::PLATFORM_FILE_TEMPORARY; | |
| 40 file_ = base::CreatePlatformFile(path, flags, &created, &error_code); | |
| 41 PLOG_IF(ERROR, file_ == base::kInvalidPlatformFileValue) | |
| 42 << "CreatePlatformFile"; | |
| 43 } | |
| 44 return valid(); | |
| 45 } | |
| 46 | |
| 47 bool TempFile::valid() const { | |
| 48 return file_ != base::kInvalidPlatformFileValue; | |
| 49 } | |
| 50 | |
| 51 base::PlatformFile TempFile::handle() const { | |
| 52 return file_; | |
| 53 } | |
| 54 | |
| 55 size_t TempFile::size() const { | |
| 56 return size_; | |
| 57 } | |
| 58 | |
| 59 bool TempFile::SetSize(size_t size) { | |
| 60 bool ret = base::TruncatePlatformFile(file_, size); | |
| 61 if (ret) | |
| 62 size_ = size; | |
| 63 return ret; | |
| 64 } | |
| 65 | |
| 66 // FileMapping | |
| 67 | |
| 68 FileMapping::FileMapping() : mapping_(NULL), view_(NULL) { | |
| 69 } | |
| 70 | |
| 71 FileMapping::~FileMapping() { | |
| 72 Close(); | |
| 73 } | |
| 74 | |
| 75 bool FileMapping::Create(HANDLE file, size_t size) { | |
| 76 DCHECK(file != INVALID_HANDLE_VALUE); | |
| 77 DCHECK(!valid()); | |
| 78 mapping_ = ::CreateFileMapping(file, NULL, PAGE_READWRITE, 0, 0, NULL); | |
| 79 if (mapping_) | |
| 80 view_ = ::MapViewOfFile(mapping_, FILE_MAP_WRITE, 0, 0, size); | |
| 81 | |
| 82 if (!valid()) { | |
| 83 PLOG(ERROR) << "Failed to map file"; | |
| 84 Close(); | |
| 85 } else { | |
| 86 char* write = reinterpret_cast<char*>(view_); | |
| 87 write[0] = 0; | |
| 88 write[size - 1] = 0; | |
|
sra1
2011/02/28 20:28:38
What does this do?
Does not TempFile::SetSize some
tommi (sloooow) - chröme
2011/02/28 21:17:41
oops, removed, I had this in while debugging.
| |
| 89 } | |
| 90 | |
| 91 return valid(); | |
| 92 } | |
| 93 | |
| 94 void FileMapping::Close() { | |
| 95 if (view_) | |
| 96 ::UnmapViewOfFile(view_); | |
| 97 if (mapping_) | |
| 98 ::CloseHandle(mapping_); | |
| 99 mapping_ = NULL; | |
| 100 view_ = NULL; | |
| 101 } | |
| 102 | |
| 103 bool FileMapping::valid() const { | |
| 104 return view_ != NULL; | |
| 105 } | |
| 106 | |
| 107 void* FileMapping::view() const { | |
| 108 return view_; | |
| 109 } | |
| 110 | |
| 111 // TempMapping | |
| 112 | |
| 113 TempMapping::TempMapping() { | |
| 114 } | |
| 115 | |
| 116 TempMapping::~TempMapping() { | |
| 117 } | |
| 118 | |
| 119 bool TempMapping::Initialize(size_t size) { | |
| 120 size += sizeof(this); | |
|
sra1
2011/02/28 20:28:38
FYI. The assumption here is that the alignment of
tommi (sloooow) - chröme
2011/02/28 21:17:41
Yes, for large objects the alignment will be broke
sra1
2011/02/28 23:18:07
TODO is fine.
| |
| 121 bool ret = file_.Create() && file_.SetSize(size) && | |
| 122 mapping_.Create(file_.handle(), size); | |
| 123 if (ret) { | |
| 124 TempMapping** write = reinterpret_cast<TempMapping**>(mapping_.view()); | |
| 125 *write = this; | |
| 126 } | |
| 127 | |
| 128 return ret; | |
| 129 } | |
| 130 | |
| 131 void* TempMapping::memory() const { | |
| 132 uint8* mem = reinterpret_cast<uint8*>(mapping_.view()); | |
| 133 if (mem) | |
| 134 mem += sizeof(this); | |
| 135 DCHECK(mem); | |
| 136 return mem; | |
| 137 } | |
| 138 | |
| 139 // static | |
| 140 TempMapping* TempMapping::GetMappingFromPtr(void* mem) { | |
| 141 TempMapping* ret = NULL; | |
| 142 if (mem) { | |
| 143 ret = reinterpret_cast<TempMapping**>(mem)[-1]; | |
|
sra1
2011/02/28 20:28:38
It would be nice if the place that sets this was o
tommi (sloooow) - chröme
2011/02/28 21:17:41
I switched Initialize() to also use [].
I don't s
| |
| 144 } | |
| 145 DCHECK(ret); | |
| 146 return ret; | |
| 147 } | |
| 148 | |
| 149 } // namespace courgette | |
| 150 | |
| 151 #endif // OS_WIN | |
| OLD | NEW |