| 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 716 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 // Dereference a block |ref| and ensure that it's valid for the desired | 727 // Dereference a block |ref| and ensure that it's valid for the desired |
| 728 // |type_id| and |size|. |special| indicates that we may try to access block | 728 // |type_id| and |size|. |special| indicates that we may try to access block |
| 729 // headers not available to callers but still accessed by this module. By | 729 // headers not available to callers but still accessed by this module. By |
| 730 // having internal dereferences go through this same function, the allocator | 730 // having internal dereferences go through this same function, the allocator |
| 731 // is hardened against corruption. | 731 // is hardened against corruption. |
| 732 const volatile PersistentMemoryAllocator::BlockHeader* | 732 const volatile PersistentMemoryAllocator::BlockHeader* |
| 733 PersistentMemoryAllocator::GetBlock(Reference ref, uint32_t type_id, | 733 PersistentMemoryAllocator::GetBlock(Reference ref, uint32_t type_id, |
| 734 uint32_t size, bool queue_ok, | 734 uint32_t size, bool queue_ok, |
| 735 bool free_ok) const { | 735 bool free_ok) const { |
| 736 // Validation of parameters. | 736 // Validation of parameters. |
| 737 if (ref < (queue_ok ? kReferenceQueue : sizeof(SharedMetadata))) |
| 738 return nullptr; |
| 737 if (ref % kAllocAlignment != 0) | 739 if (ref % kAllocAlignment != 0) |
| 738 return nullptr; | 740 return nullptr; |
| 739 if (ref < (queue_ok ? kReferenceQueue : sizeof(SharedMetadata))) | |
| 740 return nullptr; | |
| 741 size += sizeof(BlockHeader); | 741 size += sizeof(BlockHeader); |
| 742 if (ref + size > mem_size_) | 742 if (ref + size > mem_size_) |
| 743 return nullptr; | 743 return nullptr; |
| 744 | 744 |
| 745 // Validation of referenced block-header. | 745 // Validation of referenced block-header. |
| 746 if (!free_ok) { | 746 if (!free_ok) { |
| 747 uint32_t freeptr = std::min( | 747 uint32_t freeptr = std::min( |
| 748 shared_meta()->freeptr.load(std::memory_order_relaxed), mem_size_); | 748 shared_meta()->freeptr.load(std::memory_order_relaxed), mem_size_); |
| 749 if (ref + size > freeptr) | 749 if (ref + size > freeptr) |
| 750 return nullptr; | 750 return nullptr; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 907 | 907 |
| 908 // static | 908 // static |
| 909 bool FilePersistentMemoryAllocator::IsFileAcceptable( | 909 bool FilePersistentMemoryAllocator::IsFileAcceptable( |
| 910 const MemoryMappedFile& file, | 910 const MemoryMappedFile& file, |
| 911 bool read_only) { | 911 bool read_only) { |
| 912 return IsMemoryAcceptable(file.data(), file.length(), 0, read_only); | 912 return IsMemoryAcceptable(file.data(), file.length(), 0, read_only); |
| 913 } | 913 } |
| 914 #endif // !defined(OS_NACL) | 914 #endif // !defined(OS_NACL) |
| 915 | 915 |
| 916 } // namespace base | 916 } // namespace base |
| OLD | NEW |