OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ | 5 #ifndef BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ |
6 #define BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ | 6 #define BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <atomic> | 10 #include <atomic> |
11 #include <memory> | 11 #include <memory> |
12 | 12 |
13 #include "base/atomicops.h" | 13 #include "base/atomicops.h" |
14 #include "base/base_export.h" | 14 #include "base/base_export.h" |
| 15 #include "base/files/file_path.h" |
15 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/strings/string_piece.h" | 18 #include "base/strings/string_piece.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 | 21 |
21 class HistogramBase; | 22 class HistogramBase; |
22 class MemoryMappedFile; | 23 class MemoryMappedFile; |
23 class SharedMemory; | 24 class SharedMemory; |
24 | 25 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 }; | 120 }; |
120 | 121 |
121 enum : Reference { | 122 enum : Reference { |
122 kReferenceNull = 0 // A common "null" reference value. | 123 kReferenceNull = 0 // A common "null" reference value. |
123 }; | 124 }; |
124 | 125 |
125 enum : uint32_t { | 126 enum : uint32_t { |
126 kTypeIdAny = 0 // Match any type-id inside GetAsObject(). | 127 kTypeIdAny = 0 // Match any type-id inside GetAsObject(). |
127 }; | 128 }; |
128 | 129 |
| 130 // This is the standard file extension (suitable for being passed to the |
| 131 // AddExtension() method of base::FilePath) for dumps of persistent memory. |
| 132 static const base::FilePath::CharType kFileExtension[]; |
| 133 |
129 // The allocator operates on any arbitrary block of memory. Creation and | 134 // The allocator operates on any arbitrary block of memory. Creation and |
130 // persisting or sharing of that block with another process is the | 135 // persisting or sharing of that block with another process is the |
131 // responsibility of the caller. The allocator needs to know only the | 136 // responsibility of the caller. The allocator needs to know only the |
132 // block's |base| address, the total |size| of the block, and any internal | 137 // block's |base| address, the total |size| of the block, and any internal |
133 // |page| size (zero if not paged) across which allocations should not span. | 138 // |page| size (zero if not paged) across which allocations should not span. |
134 // The |id| is an arbitrary value the caller can use to identify a | 139 // The |id| is an arbitrary value the caller can use to identify a |
135 // particular memory segment. It will only be loaded during the initial | 140 // particular memory segment. It will only be loaded during the initial |
136 // creation of the segment and can be checked by the caller for consistency. | 141 // creation of the segment and can be checked by the caller for consistency. |
137 // The |name|, if provided, is used to distinguish histograms for this | 142 // The |name|, if provided, is used to distinguish histograms for this |
138 // allocator. Only the primary owner of the segment should define this value; | 143 // allocator. Only the primary owner of the segment should define this value; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 // with the following histograms: | 189 // with the following histograms: |
185 // UMA.PersistentAllocator.name.Allocs | 190 // UMA.PersistentAllocator.name.Allocs |
186 // UMA.PersistentAllocator.name.UsedPct | 191 // UMA.PersistentAllocator.name.UsedPct |
187 void CreateTrackingHistograms(base::StringPiece name); | 192 void CreateTrackingHistograms(base::StringPiece name); |
188 | 193 |
189 // Direct access to underlying memory segment. If the segment is shared | 194 // Direct access to underlying memory segment. If the segment is shared |
190 // across threads or processes, reading data through these values does | 195 // across threads or processes, reading data through these values does |
191 // not guarantee consistency. Use with care. Do not write. | 196 // not guarantee consistency. Use with care. Do not write. |
192 const void* data() const { return const_cast<const char*>(mem_base_); } | 197 const void* data() const { return const_cast<const char*>(mem_base_); } |
193 size_t length() const { return mem_size_; } | 198 size_t length() const { return mem_size_; } |
| 199 size_t size() const { return mem_size_; } |
194 size_t used() const; | 200 size_t used() const; |
195 | 201 |
196 // Get an object referenced by a |ref|. For safety reasons, the |type_id| | 202 // Get an object referenced by a |ref|. For safety reasons, the |type_id| |
197 // code and size-of(|T|) are compared to ensure the reference is valid | 203 // code and size-of(|T|) are compared to ensure the reference is valid |
198 // and cannot return an object outside of the memory segment. A |type_id| of | 204 // and cannot return an object outside of the memory segment. A |type_id| of |
199 // kTypeIdAny (zero) will match any though the size is still checked. NULL is | 205 // kTypeIdAny (zero) will match any though the size is still checked. NULL is |
200 // returned if any problem is detected, such as corrupted storage or incorrect | 206 // returned if any problem is detected, such as corrupted storage or incorrect |
201 // parameters. Callers MUST check that the returned value is not-null EVERY | 207 // parameters. Callers MUST check that the returned value is not-null EVERY |
202 // TIME before accessing it or risk crashing! Once dereferenced, the pointer | 208 // TIME before accessing it or risk crashing! Once dereferenced, the pointer |
203 // is safe to reuse forever. | 209 // is safe to reuse forever. |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 | 404 |
399 private: | 405 private: |
400 std::unique_ptr<MemoryMappedFile> mapped_file_; | 406 std::unique_ptr<MemoryMappedFile> mapped_file_; |
401 | 407 |
402 DISALLOW_COPY_AND_ASSIGN(FilePersistentMemoryAllocator); | 408 DISALLOW_COPY_AND_ASSIGN(FilePersistentMemoryAllocator); |
403 }; | 409 }; |
404 | 410 |
405 } // namespace base | 411 } // namespace base |
406 | 412 |
407 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ | 413 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ |
OLD | NEW |