|
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 #include "base/logging.h" | |
9 | |
10 #ifdef OS_WIN | |
11 | |
12 namespace courgette { | |
13 | |
14 // TempFile | |
15 | |
16 TempFile::TempFile() : file_(base::kInvalidPlatformFileValue), size_(0) { | |
17 } | |
18 | |
19 TempFile::~TempFile() { | |
20 Close(); | |
21 } | |
22 | |
23 void TempFile::Close() { | |
24 if (valid()) { | |
25 base::ClosePlatformFile(file_); | |
26 file_ = base::kInvalidPlatformFileValue; | |
27 size_ = 0; | |
28 } | |
29 } | |
30 | |
31 bool TempFile::Create() { | |
32 DCHECK(file_ == base::kInvalidPlatformFileValue); | |
33 FilePath path; | |
34 file_util::CreateTemporaryFile(&path); | |
grt (UTC plus 2)
2011/02/28 15:06:41
what if this returns false?
tommi (sloooow) - chröme
2011/02/28 17:50:00
Done.
| |
35 bool created = false; | |
36 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | |
37 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ | | |
38 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_DELETE_ON_CLOSE | | |
39 base::PLATFORM_FILE_TEMPORARY; | |
40 file_ = base::CreatePlatformFile(path, flags, &created, &error_code); | |
grt (UTC plus 2)
2011/02/28 15:06:41
PLOG_IF(ERROR, file_ == base::kInvalidPlatformFile
tommi (sloooow) - chröme
2011/02/28 17:50:00
Done.
| |
41 return valid(); | |
42 } | |
43 | |
44 bool TempFile::valid() const { | |
45 return file_ != base::kInvalidPlatformFileValue; | |
46 } | |
47 | |
48 base::PlatformFile TempFile::handle() const { | |
49 return file_; | |
50 } | |
51 | |
52 size_t TempFile::size() const { | |
53 return size_; | |
54 } | |
55 | |
56 bool TempFile::SetSize(size_t size) { | |
57 bool ret = base::TruncatePlatformFile(file_, size); | |
58 if (ret) | |
59 size_ = size; | |
60 return ret; | |
61 } | |
62 | |
63 // FileMapping | |
64 | |
65 FileMapping::FileMapping() : mapping_(NULL), view_(NULL) { | |
66 } | |
67 | |
68 FileMapping::~FileMapping() { | |
69 Close(); | |
70 } | |
71 | |
72 bool FileMapping::Create(HANDLE file, size_t size) { | |
73 DCHECK(file != INVALID_HANDLE_VALUE); | |
74 DCHECK(!valid()); | |
75 mapping_ = ::CreateFileMapping(file, NULL, PAGE_READWRITE, 0, 0, NULL); | |
76 if (mapping_) | |
77 view_ = ::MapViewOfFile(mapping_, FILE_MAP_WRITE, 0, 0, size); | |
78 | |
79 if (!valid()) { | |
80 PLOG(ERROR) << "Failed to map file"; | |
81 Close(); | |
82 } else { | |
83 char* write = reinterpret_cast<char*>(view_); | |
84 write[0] = 0; | |
85 write[size - 1] = 0; | |
86 } | |
87 | |
88 return valid(); | |
89 } | |
90 | |
91 void FileMapping::Close() { | |
92 if (view_) | |
93 ::UnmapViewOfFile(view_); | |
94 if (mapping_) | |
95 ::CloseHandle(mapping_); | |
96 mapping_ = NULL; | |
97 view_ = NULL; | |
98 } | |
99 | |
100 bool FileMapping::valid() const { | |
101 return view_ != NULL; | |
102 } | |
103 | |
104 void* FileMapping::view() const { | |
105 return view_; | |
106 } | |
107 | |
108 // TempMapping | |
109 | |
110 TempMapping::TempMapping() { | |
111 } | |
112 | |
113 TempMapping::~TempMapping() { | |
114 } | |
115 | |
116 bool TempMapping::Initialize(size_t size) { | |
117 size += sizeof(TempMapping*); // NOLINT | |
grt (UTC plus 2)
2011/02/28 15:06:41
Style guide says "Use sizeof(varname) instead of s
tommi (sloooow) - chröme
2011/02/28 17:50:00
Done.
| |
118 bool ret = file_.Create() && file_.SetSize(size) && | |
119 mapping_.Create(file_.handle(), size); | |
120 if (ret) { | |
121 TempMapping** write = reinterpret_cast<TempMapping**>(mapping_.view()); | |
122 *write = this; | |
123 } | |
124 | |
125 return ret; | |
126 } | |
127 | |
128 void* TempMapping::memory() const { | |
129 uint8* mem = reinterpret_cast<uint8*>(mapping_.view()); | |
130 if (mem) | |
131 mem += sizeof(TempMapping*); // NOLINT | |
grt (UTC plus 2)
2011/02/28 15:06:41
sizeof(this)?
tommi (sloooow) - chröme
2011/02/28 17:50:00
Done.
| |
132 DCHECK(mem); | |
133 return mem; | |
134 } | |
135 | |
136 // static | |
137 TempMapping* TempMapping::GetMappingFromPtr(void* mem) { | |
138 TempMapping* ret = NULL; | |
139 if (mem) { | |
140 ret = reinterpret_cast<TempMapping**>(mem)[-1]; | |
141 } | |
142 DCHECK(ret); | |
143 return ret; | |
144 } | |
145 | |
146 } // namespace courgette | |
147 | |
148 #endif // OS_WIN | |
OLD | NEW |