| 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 #include "base/metrics/persistent_memory_allocator.h" | 5 #include "base/metrics/persistent_memory_allocator.h" |
| 6 | 6 |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 | 9 |
| 10 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
| (...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 allocs_histogram_ = Histogram::FactoryGet( | 446 allocs_histogram_ = Histogram::FactoryGet( |
| 447 "UMA.PersistentAllocator." + name_string + ".Allocs", 1, 10000, 50, | 447 "UMA.PersistentAllocator." + name_string + ".Allocs", 1, 10000, 50, |
| 448 HistogramBase::kUmaTargetedHistogramFlag); | 448 HistogramBase::kUmaTargetedHistogramFlag); |
| 449 } | 449 } |
| 450 | 450 |
| 451 size_t PersistentMemoryAllocator::used() const { | 451 size_t PersistentMemoryAllocator::used() const { |
| 452 return std::min(shared_meta()->freeptr.load(std::memory_order_relaxed), | 452 return std::min(shared_meta()->freeptr.load(std::memory_order_relaxed), |
| 453 mem_size_); | 453 mem_size_); |
| 454 } | 454 } |
| 455 | 455 |
| 456 PersistentMemoryAllocator::Reference PersistentMemoryAllocator::GetAsReference( |
| 457 const void* memory, |
| 458 uint32_t type_id) const { |
| 459 uintptr_t address = reinterpret_cast<uintptr_t>(memory); |
| 460 if (address < reinterpret_cast<uintptr_t>(mem_base_)) |
| 461 return kReferenceNull; |
| 462 |
| 463 uintptr_t offset = address - reinterpret_cast<uintptr_t>(mem_base_); |
| 464 if (offset >= mem_size_ || offset < sizeof(BlockHeader)) |
| 465 return kReferenceNull; |
| 466 |
| 467 Reference ref = static_cast<Reference>(offset) - sizeof(BlockHeader); |
| 468 if (!GetBlockData(ref, type_id, kSizeAny)) |
| 469 return kReferenceNull; |
| 470 |
| 471 return ref; |
| 472 } |
| 473 |
| 456 size_t PersistentMemoryAllocator::GetAllocSize(Reference ref) const { | 474 size_t PersistentMemoryAllocator::GetAllocSize(Reference ref) const { |
| 457 const volatile BlockHeader* const block = GetBlock(ref, 0, 0, false, false); | 475 const volatile BlockHeader* const block = GetBlock(ref, 0, 0, false, false); |
| 458 if (!block) | 476 if (!block) |
| 459 return 0; | 477 return 0; |
| 460 uint32_t size = block->size; | 478 uint32_t size = block->size; |
| 461 // Header was verified by GetBlock() but a malicious actor could change | 479 // Header was verified by GetBlock() but a malicious actor could change |
| 462 // the value between there and here. Check it again. | 480 // the value between there and here. Check it again. |
| 463 if (size <= sizeof(BlockHeader) || ref + size > mem_size_) { | 481 if (size <= sizeof(BlockHeader) || ref + size > mem_size_) { |
| 464 SetCorrupt(); | 482 SetCorrupt(); |
| 465 return 0; | 483 return 0; |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 889 | 907 |
| 890 // static | 908 // static |
| 891 bool FilePersistentMemoryAllocator::IsFileAcceptable( | 909 bool FilePersistentMemoryAllocator::IsFileAcceptable( |
| 892 const MemoryMappedFile& file, | 910 const MemoryMappedFile& file, |
| 893 bool read_only) { | 911 bool read_only) { |
| 894 return IsMemoryAcceptable(file.data(), file.length(), 0, read_only); | 912 return IsMemoryAcceptable(file.data(), file.length(), 0, read_only); |
| 895 } | 913 } |
| 896 #endif // !defined(OS_NACL) | 914 #endif // !defined(OS_NACL) |
| 897 | 915 |
| 898 } // namespace base | 916 } // namespace base |
| OLD | NEW |