Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(448)

Side by Side Diff: courgette/memory_allocator.cc

Issue 6597038: Implementation of an STL compatible allocator for Courgette on Windows.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « courgette/memory_allocator.h ('k') | courgette/streams.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 <map>
8
9 #include "base/file_util.h"
10
11 #ifdef OS_WIN
12
13 namespace courgette {
14
15 // TempFile
16
17 TempFile::TempFile() : file_(base::kInvalidPlatformFileValue), size_(0) {
18 }
19
20 TempFile::~TempFile() {
21 Close();
22 }
23
24 void TempFile::Close() {
25 if (valid()) {
26 base::ClosePlatformFile(file_);
27 file_ = base::kInvalidPlatformFileValue;
28 size_ = 0;
29 }
30 }
31
32 bool TempFile::Create() {
33 DCHECK(file_ == base::kInvalidPlatformFileValue);
34 FilePath path;
35 if (file_util::CreateTemporaryFile(&path)) {
36 bool created = false;
37 base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
38 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ |
39 base::PLATFORM_FILE_WRITE |
40 base::PLATFORM_FILE_DELETE_ON_CLOSE |
41 base::PLATFORM_FILE_TEMPORARY;
42 file_ = base::CreatePlatformFile(path, flags, &created, &error_code);
43 PLOG_IF(ERROR, file_ == base::kInvalidPlatformFileValue)
44 << "CreatePlatformFile";
45 }
46 return valid();
47 }
48
49 bool TempFile::valid() const {
50 return file_ != base::kInvalidPlatformFileValue;
51 }
52
53 base::PlatformFile TempFile::handle() const {
54 return file_;
55 }
56
57 size_t TempFile::size() const {
58 return size_;
59 }
60
61 bool TempFile::SetSize(size_t size) {
62 bool ret = base::TruncatePlatformFile(file_, size);
63 if (ret)
64 size_ = size;
65 return ret;
66 }
67
68 // FileMapping
69
70 FileMapping::FileMapping() : mapping_(NULL), view_(NULL) {
71 }
72
73 FileMapping::~FileMapping() {
74 Close();
75 }
76
77 bool FileMapping::Create(HANDLE file, size_t size) {
78 DCHECK(file != INVALID_HANDLE_VALUE);
79 DCHECK(!valid());
80 mapping_ = ::CreateFileMapping(file, NULL, PAGE_READWRITE, 0, 0, NULL);
81 if (mapping_)
82 view_ = ::MapViewOfFile(mapping_, FILE_MAP_WRITE, 0, 0, size);
83
84 if (!valid()) {
85 PLOG(ERROR) << "Failed to map file";
86 Close();
87 }
88
89 return valid();
90 }
91
92 void FileMapping::Close() {
93 if (view_)
94 ::UnmapViewOfFile(view_);
95 if (mapping_)
96 ::CloseHandle(mapping_);
97 mapping_ = NULL;
98 view_ = NULL;
99 }
100
101 bool FileMapping::valid() const {
102 return view_ != NULL;
103 }
104
105 void* FileMapping::view() const {
106 return view_;
107 }
108
109 // TempMapping
110
111 TempMapping::TempMapping() {
112 }
113
114 TempMapping::~TempMapping() {
115 }
116
117 bool TempMapping::Initialize(size_t size) {
118 // TODO(tommi): The assumption here is that the alignment of pointers (this)
119 // is as strict or stricter than the alignment of the element type. This is
120 // not always true, e.g. __m128 has 16-byte alignment.
121 size += sizeof(this);
122 bool ret = file_.Create() && file_.SetSize(size) &&
123 mapping_.Create(file_.handle(), size);
124 if (ret) {
125 TempMapping** write = reinterpret_cast<TempMapping**>(mapping_.view());
126 write[0] = this;
127 }
128
129 return ret;
130 }
131
132 void* TempMapping::memory() const {
133 uint8* mem = reinterpret_cast<uint8*>(mapping_.view());
134 if (mem)
135 mem += sizeof(this);
136 DCHECK(mem);
137 return mem;
138 }
139
140 // static
141 TempMapping* TempMapping::GetMappingFromPtr(void* mem) {
142 TempMapping* ret = NULL;
143 if (mem) {
144 ret = reinterpret_cast<TempMapping**>(mem)[-1];
145 }
146 DCHECK(ret);
147 return ret;
148 }
149
150 } // namespace courgette
151
152 #endif // OS_WIN
OLDNEW
« no previous file with comments | « courgette/memory_allocator.h ('k') | courgette/streams.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698