| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "courgette/memory_allocator.h" | 5 #include "courgette/memory_allocator.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <algorithm> |
| 11 | 11 |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 | 14 |
| 15 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // The file is created in the %TEMP% folder. | 19 // The file is created in the %TEMP% folder. |
| 20 // NOTE: Since the file will be used as backing for a memory allocation, | 20 // NOTE: Since the file will be used as backing for a memory allocation, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 bool FileMapping::valid() const { | 76 bool FileMapping::valid() const { |
| 77 return view_ != NULL; | 77 return view_ != NULL; |
| 78 } | 78 } |
| 79 | 79 |
| 80 void* FileMapping::view() const { | 80 void* FileMapping::view() const { |
| 81 return view_; | 81 return view_; |
| 82 } | 82 } |
| 83 | 83 |
| 84 // TempMapping | 84 // TempMapping |
| 85 | 85 |
| 86 long long TempMapping::alloc_cur = 0; |
| 87 long long TempMapping::alloc_sum = 0; |
| 88 long long TempMapping::alloc_max = 0; |
| 89 |
| 86 TempMapping::TempMapping() { | 90 TempMapping::TempMapping() { |
| 87 } | 91 } |
| 88 | 92 |
| 89 TempMapping::~TempMapping() { | 93 TempMapping::~TempMapping() { |
| 94 alloc_cur -= size_; |
| 90 } | 95 } |
| 91 | 96 |
| 92 bool TempMapping::Initialize(size_t size) { | 97 bool TempMapping::Initialize(size_t size) { |
| 93 file_ = CreateTempFile(); | 98 file_ = CreateTempFile(); |
| 94 if (!file_.IsValid()) | 99 if (!file_.IsValid()) |
| 95 return false; | 100 return false; |
| 96 | 101 |
| 97 // TODO(tommi): The assumption here is that the alignment of pointers (this) | 102 // TODO(tommi): The assumption here is that the alignment of pointers (this) |
| 98 // is as strict or stricter than the alignment of the element type. This is | 103 // is as strict or stricter than the alignment of the element type. This is |
| 99 // not always true, e.g. __m128 has 16-byte alignment. | 104 // not always true, e.g. __m128 has 16-byte alignment. |
| 100 size += sizeof(this); | 105 size += sizeof(this); |
| 101 if (!file_.SetLength(size) || | 106 if (!file_.SetLength(size) || |
| 102 !mapping_.Create(file_.GetPlatformFile(), size)) { | 107 !mapping_.Create(file_.GetPlatformFile(), size)) { |
| 103 file_.Close(); | 108 file_.Close(); |
| 104 return false; | 109 return false; |
| 105 } | 110 } |
| 106 | 111 |
| 107 TempMapping** write = reinterpret_cast<TempMapping**>(mapping_.view()); | 112 TempMapping** write = reinterpret_cast<TempMapping**>(mapping_.view()); |
| 108 write[0] = this; | 113 write[0] = this; |
| 109 | 114 |
| 115 size_ = size; |
| 116 alloc_cur += size_; |
| 117 alloc_sum += size_; |
| 118 alloc_max = std::max(alloc_max, alloc_cur); |
| 119 |
| 110 return true; | 120 return true; |
| 111 } | 121 } |
| 112 | 122 |
| 113 void* TempMapping::memory() const { | 123 void* TempMapping::memory() const { |
| 114 uint8_t* mem = reinterpret_cast<uint8_t*>(mapping_.view()); | 124 uint8_t* mem = reinterpret_cast<uint8_t*>(mapping_.view()); |
| 115 // The 'this' pointer is written at the start of mapping_.view(), so | 125 // The 'this' pointer is written at the start of mapping_.view(), so |
| 116 // go past it. (See Initialize()). | 126 // go past it. (See Initialize()). |
| 117 if (mem) | 127 if (mem) |
| 118 mem += sizeof(this); | 128 mem += sizeof(this); |
| 119 DCHECK(mem); | 129 DCHECK(mem); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 130 if (mem) { | 140 if (mem) { |
| 131 ret = reinterpret_cast<TempMapping**>(mem)[-1]; | 141 ret = reinterpret_cast<TempMapping**>(mem)[-1]; |
| 132 } | 142 } |
| 133 DCHECK(ret); | 143 DCHECK(ret); |
| 134 return ret; | 144 return ret; |
| 135 } | 145 } |
| 136 | 146 |
| 137 } // namespace courgette | 147 } // namespace courgette |
| 138 | 148 |
| 139 #endif // OS_WIN | 149 #endif // OS_WIN |
| OLD | NEW |