| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 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/test/test_discardable_memory_allocator.h" | 5 #include "base/test/test_discardable_memory_allocator.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <cstdint> |
| 8 #include <cstring> |
| 8 | 9 |
| 9 #include "base/memory/discardable_memory.h" | 10 #include "base/memory/discardable_memory.h" |
| 10 | 11 |
| 11 namespace base { | 12 namespace base { |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 class DiscardableMemoryImpl : public DiscardableMemory { | 15 class DiscardableMemoryImpl : public DiscardableMemory { |
| 15 public: | 16 public: |
| 16 explicit DiscardableMemoryImpl(size_t size) : data_(new uint8_t[size]) {} | 17 explicit DiscardableMemoryImpl(size_t size) |
| 18 : data_(new uint8_t[size]), size_(size) {} |
| 17 | 19 |
| 18 // Overridden from DiscardableMemory: | 20 // Overridden from DiscardableMemory: |
| 19 bool Lock() override { return false; } | 21 bool Lock() override { |
| 20 void Unlock() override {} | 22 DCHECK(!is_locked_); |
| 21 void* data() const override { return data_.get(); } | 23 is_locked_ = true; |
| 24 return false; |
| 25 } |
| 26 |
| 27 void Unlock() override { |
| 28 DCHECK(is_locked_); |
| 29 is_locked_ = false; |
| 30 // Force eviction to catch clients not correctly checking the return value |
| 31 // of Lock(). |
| 32 memset(data_.get(), 0, size_); |
| 33 } |
| 34 |
| 35 void* data() const override { |
| 36 DCHECK(is_locked_); |
| 37 return data_.get(); |
| 38 } |
| 22 | 39 |
| 23 trace_event::MemoryAllocatorDump* CreateMemoryAllocatorDump( | 40 trace_event::MemoryAllocatorDump* CreateMemoryAllocatorDump( |
| 24 const char* name, | 41 const char* name, |
| 25 trace_event::ProcessMemoryDump* pmd) const override { | 42 trace_event::ProcessMemoryDump* pmd) const override { |
| 26 return nullptr; | 43 return nullptr; |
| 27 } | 44 } |
| 28 | 45 |
| 29 private: | 46 private: |
| 47 bool is_locked_ = true; |
| 30 scoped_ptr<uint8_t[]> data_; | 48 scoped_ptr<uint8_t[]> data_; |
| 49 size_t size_; |
| 31 }; | 50 }; |
| 32 | 51 |
| 33 } // namespace | 52 } // namespace |
| 34 | 53 |
| 35 TestDiscardableMemoryAllocator::TestDiscardableMemoryAllocator() { | 54 TestDiscardableMemoryAllocator::TestDiscardableMemoryAllocator() { |
| 36 } | 55 } |
| 37 | 56 |
| 38 TestDiscardableMemoryAllocator::~TestDiscardableMemoryAllocator() { | 57 TestDiscardableMemoryAllocator::~TestDiscardableMemoryAllocator() { |
| 39 } | 58 } |
| 40 | 59 |
| 41 scoped_ptr<DiscardableMemory> | 60 scoped_ptr<DiscardableMemory> |
| 42 TestDiscardableMemoryAllocator::AllocateLockedDiscardableMemory(size_t size) { | 61 TestDiscardableMemoryAllocator::AllocateLockedDiscardableMemory(size_t size) { |
| 43 return make_scoped_ptr(new DiscardableMemoryImpl(size)); | 62 return make_scoped_ptr(new DiscardableMemoryImpl(size)); |
| 44 } | 63 } |
| 45 | 64 |
| 46 } // namespace base | 65 } // namespace base |
| OLD | NEW |