| 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 HANDLE mapping_; | 110 HANDLE mapping_; |
| 111 void* view_; | 111 void* view_; |
| 112 }; | 112 }; |
| 113 | 113 |
| 114 // Manages a temporary file and a memory mapping of the temporary file. | 114 // Manages a temporary file and a memory mapping of the temporary file. |
| 115 // The memory that this class manages holds a pointer back to the TempMapping | 115 // The memory that this class manages holds a pointer back to the TempMapping |
| 116 // object itself, so that given a memory pointer allocated by this class, | 116 // object itself, so that given a memory pointer allocated by this class, |
| 117 // you can get a pointer to the TempMapping instance that owns that memory. | 117 // you can get a pointer to the TempMapping instance that owns that memory. |
| 118 class TempMapping { | 118 class TempMapping { |
| 119 public: | 119 public: |
| 120 static long long alloc_cur; |
| 121 static long long alloc_sum; |
| 122 static long long alloc_max; |
| 123 |
| 120 TempMapping(); | 124 TempMapping(); |
| 121 ~TempMapping(); | 125 ~TempMapping(); |
| 122 | 126 |
| 123 // Creates a temporary file of size |size| and maps it into the current | 127 // Creates a temporary file of size |size| and maps it into the current |
| 124 // process's address space. | 128 // process's address space. |
| 125 bool Initialize(size_t size); | 129 bool Initialize(size_t size); |
| 126 | 130 |
| 127 // Returns a writable pointer to the reserved memory. | 131 // Returns a writable pointer to the reserved memory. |
| 128 void* memory() const; | 132 void* memory() const; |
| 129 | 133 |
| 130 // Returns true if the mapping is valid and memory is available. | 134 // Returns true if the mapping is valid and memory is available. |
| 131 bool valid() const; | 135 bool valid() const; |
| 132 | 136 |
| 133 // Returns a pointer to the TempMapping instance that allocated the |mem| | 137 // Returns a pointer to the TempMapping instance that allocated the |mem| |
| 134 // block of memory. It's the callers responsibility to make sure that | 138 // block of memory. It's the callers responsibility to make sure that |
| 135 // the memory block was allocated by the TempMapping class. | 139 // the memory block was allocated by the TempMapping class. |
| 136 static TempMapping* GetMappingFromPtr(void* mem); | 140 static TempMapping* GetMappingFromPtr(void* mem); |
| 137 | 141 |
| 138 protected: | 142 protected: |
| 139 base::File file_; | 143 base::File file_; |
| 140 FileMapping mapping_; | 144 FileMapping mapping_; |
| 145 |
| 146 size_t size_ = 0; |
| 141 }; | 147 }; |
| 142 | 148 |
| 143 // A memory allocator class that allocates memory either from the heap or via a | 149 // A memory allocator class that allocates memory either from the heap or via a |
| 144 // temporary file. The interface is STL inspired but the class does not throw | 150 // temporary file. The interface is STL inspired but the class does not throw |
| 145 // STL exceptions on allocation failure. Instead it returns NULL. | 151 // STL exceptions on allocation failure. Instead it returns NULL. |
| 146 // A file allocation will be made if either the requested memory size exceeds | 152 // A file allocation will be made if either the requested memory size exceeds |
| 147 // |kMaxHeapAllocationSize| or if a heap allocation fails. | 153 // |kMaxHeapAllocationSize| or if a heap allocation fails. |
| 148 // Allocating the memory as a mapping of a temporary file solves the problem | 154 // Allocating the memory as a mapping of a temporary file solves the problem |
| 149 // that there might not be enough physical memory and pagefile to support the | 155 // that there might not be enough physical memory and pagefile to support the |
| 150 // allocation. This can happen because these resources are too small, or | 156 // allocation. This can happen because these resources are too small, or |
| (...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 protected: | 507 protected: |
| 502 T* buffer_; | 508 T* buffer_; |
| 503 size_t size_; // how much of the buffer we're using. | 509 size_t size_; // how much of the buffer we're using. |
| 504 size_t alloc_size_; // how much space we have allocated. | 510 size_t alloc_size_; // how much space we have allocated. |
| 505 Allocator alloc_; | 511 Allocator alloc_; |
| 506 }; | 512 }; |
| 507 | 513 |
| 508 } // namespace courgette | 514 } // namespace courgette |
| 509 | 515 |
| 510 #endif // COURGETTE_MEMORY_ALLOCATOR_H_ | 516 #endif // COURGETTE_MEMORY_ALLOCATOR_H_ |
| OLD | NEW |