OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef COURGETTE_MEMORY_ALLOCATOR_H_ | 5 #ifndef COURGETTE_MEMORY_ALLOCATOR_H_ |
6 #define COURGETTE_MEMORY_ALLOCATOR_H_ | 6 #define COURGETTE_MEMORY_ALLOCATOR_H_ |
7 | 7 |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
8 #include <stdlib.h> | 10 #include <stdlib.h> |
9 | 11 |
10 #include "base/basictypes.h" | |
11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
12 #include "base/files/file.h" | 13 #include "base/files/file.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/process/memory.h" | 16 #include "base/process/memory.h" |
16 | 17 |
17 #ifndef NDEBUG | 18 #ifndef NDEBUG |
18 | 19 |
19 // A helper class to track down call sites that are not handling error cases. | 20 // A helper class to track down call sites that are not handling error cases. |
20 template<class T> | 21 template<class T> |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 } | 188 } |
188 | 189 |
189 template<class OtherT> | 190 template<class OtherT> |
190 MemoryAllocator(const MemoryAllocator<OtherT>& other) _THROW0() { // NOLINT | 191 MemoryAllocator(const MemoryAllocator<OtherT>& other) _THROW0() { // NOLINT |
191 } | 192 } |
192 | 193 |
193 ~MemoryAllocator() { | 194 ~MemoryAllocator() { |
194 } | 195 } |
195 | 196 |
196 void deallocate(pointer ptr, size_type size) { | 197 void deallocate(pointer ptr, size_type size) { |
197 uint8* mem = reinterpret_cast<uint8*>(ptr); | 198 uint8_t* mem = reinterpret_cast<uint8_t*>(ptr); |
198 mem -= sizeof(T); | 199 mem -= sizeof(T); |
199 if (mem[0] == HEAP_ALLOCATION) { | 200 if (mem[0] == HEAP_ALLOCATION) { |
200 free(mem); | 201 free(mem); |
201 } else { | 202 } else { |
202 DCHECK_EQ(static_cast<uint8>(FILE_ALLOCATION), mem[0]); | 203 DCHECK_EQ(static_cast<uint8_t>(FILE_ALLOCATION), mem[0]); |
203 UncheckedDelete(TempMapping::GetMappingFromPtr(mem)); | 204 UncheckedDelete(TempMapping::GetMappingFromPtr(mem)); |
204 } | 205 } |
205 } | 206 } |
206 | 207 |
207 pointer allocate(size_type count) { | 208 pointer allocate(size_type count) { |
208 // We use the first byte of each allocation to mark the allocation type. | 209 // We use the first byte of each allocation to mark the allocation type. |
209 // However, so that the allocation is properly aligned, we allocate an | 210 // However, so that the allocation is properly aligned, we allocate an |
210 // extra element and then use the first byte of the first element | 211 // extra element and then use the first byte of the first element |
211 // to mark the allocation type. | 212 // to mark the allocation type. |
212 count++; | 213 count++; |
213 | 214 |
214 if (count > max_size()) | 215 if (count > max_size()) |
215 return NULL; | 216 return NULL; |
216 | 217 |
217 size_type bytes = count * sizeof(T); | 218 size_type bytes = count * sizeof(T); |
218 uint8* mem = NULL; | 219 uint8_t* mem = NULL; |
219 | 220 |
220 // First see if we can do this allocation on the heap. | 221 // First see if we can do this allocation on the heap. |
221 if (count < kMaxHeapAllocationSize && | 222 if (count < kMaxHeapAllocationSize && |
222 base::UncheckedMalloc(bytes, reinterpret_cast<void**>(&mem))) { | 223 base::UncheckedMalloc(bytes, reinterpret_cast<void**>(&mem))) { |
223 mem[0] = static_cast<uint8>(HEAP_ALLOCATION); | 224 mem[0] = static_cast<uint8_t>(HEAP_ALLOCATION); |
224 } else { | 225 } else { |
225 // Back the allocation with a temp file if either the request exceeds the | 226 // Back the allocation with a temp file if either the request exceeds the |
226 // max heap allocation threshold or the heap allocation failed. | 227 // max heap allocation threshold or the heap allocation failed. |
227 TempMapping* mapping = UncheckedNew<TempMapping>(); | 228 TempMapping* mapping = UncheckedNew<TempMapping>(); |
228 if (mapping) { | 229 if (mapping) { |
229 if (mapping->Initialize(bytes)) { | 230 if (mapping->Initialize(bytes)) { |
230 mem = reinterpret_cast<uint8*>(mapping->memory()); | 231 mem = reinterpret_cast<uint8_t*>(mapping->memory()); |
231 mem[0] = static_cast<uint8>(FILE_ALLOCATION); | 232 mem[0] = static_cast<uint8_t>(FILE_ALLOCATION); |
232 } else { | 233 } else { |
233 UncheckedDelete(mapping); | 234 UncheckedDelete(mapping); |
234 } | 235 } |
235 } | 236 } |
236 } | 237 } |
237 return mem ? reinterpret_cast<pointer>(mem + sizeof(T)) : NULL; | 238 return mem ? reinterpret_cast<pointer>(mem + sizeof(T)) : NULL; |
238 } | 239 } |
239 | 240 |
240 pointer allocate(size_type count, const void* hint) { | 241 pointer allocate(size_type count, const void* hint) { |
241 return allocate(count); | 242 return allocate(count); |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 protected: | 496 protected: |
496 T* buffer_; | 497 T* buffer_; |
497 size_t size_; // how much of the buffer we're using. | 498 size_t size_; // how much of the buffer we're using. |
498 size_t alloc_size_; // how much space we have allocated. | 499 size_t alloc_size_; // how much space we have allocated. |
499 Allocator alloc_; | 500 Allocator alloc_; |
500 }; | 501 }; |
501 | 502 |
502 } // namespace courgette | 503 } // namespace courgette |
503 | 504 |
504 #endif // COURGETTE_MEMORY_ALLOCATOR_H_ | 505 #endif // COURGETTE_MEMORY_ALLOCATOR_H_ |
OLD | NEW |