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> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <stdlib.h> | 10 #include <stdlib.h> |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 TempMapping* mapping = UncheckedNew<TempMapping>(); | 230 TempMapping* mapping = UncheckedNew<TempMapping>(); |
231 if (mapping) { | 231 if (mapping) { |
232 if (mapping->Initialize(bytes)) { | 232 if (mapping->Initialize(bytes)) { |
233 mem = reinterpret_cast<uint8_t*>(mapping->memory()); | 233 mem = reinterpret_cast<uint8_t*>(mapping->memory()); |
234 mem[0] = static_cast<uint8_t>(FILE_ALLOCATION); | 234 mem[0] = static_cast<uint8_t>(FILE_ALLOCATION); |
235 } else { | 235 } else { |
236 UncheckedDelete(mapping); | 236 UncheckedDelete(mapping); |
237 } | 237 } |
238 } | 238 } |
239 } | 239 } |
| 240 // If the above fails (e.g. because we are in a sandbox), just try the heap. |
| 241 if (!mem && base::UncheckedMalloc(bytes, reinterpret_cast<void**>(&mem))) { |
| 242 mem[0] = static_cast<uint8_t>(HEAP_ALLOCATION); |
| 243 } |
240 return mem ? reinterpret_cast<pointer>(mem + sizeof(T)) : NULL; | 244 return mem ? reinterpret_cast<pointer>(mem + sizeof(T)) : NULL; |
241 } | 245 } |
242 | 246 |
243 pointer allocate(size_type count, const void* hint) { | 247 pointer allocate(size_type count, const void* hint) { |
244 return allocate(count); | 248 return allocate(count); |
245 } | 249 } |
246 | 250 |
247 void construct(pointer ptr, const T& value) { | 251 void construct(pointer ptr, const T& value) { |
248 ::new(ptr) T(value); | 252 ::new(ptr) T(value); |
249 } | 253 } |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 protected: | 502 protected: |
499 T* buffer_; | 503 T* buffer_; |
500 size_t size_; // how much of the buffer we're using. | 504 size_t size_; // how much of the buffer we're using. |
501 size_t alloc_size_; // how much space we have allocated. | 505 size_t alloc_size_; // how much space we have allocated. |
502 Allocator alloc_; | 506 Allocator alloc_; |
503 }; | 507 }; |
504 | 508 |
505 } // namespace courgette | 509 } // namespace courgette |
506 | 510 |
507 #endif // COURGETTE_MEMORY_ALLOCATOR_H_ | 511 #endif // COURGETTE_MEMORY_ALLOCATOR_H_ |
OLD | NEW |