1 // Copyright 2019 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_MEMORY_MADV_FREE_DISCARDABLE_MEMORY_ALLOCATOR_POSIX_H_ 6 #define BASE_MEMORY_MADV_FREE_DISCARDABLE_MEMORY_ALLOCATOR_POSIX_H_ 7 8 #include <stddef.h> 9 10 #include <atomic> 11 #include <memory> 12 13 #include "base/base_export.h" 14 #include "base/functional/bind.h" 15 #include "base/functional/callback.h" 16 #include "base/memory/discardable_memory.h" 17 #include "base/memory/discardable_memory_allocator.h" 18 #include "base/memory/madv_free_discardable_memory_posix.h" 19 #include "base/trace_event/base_tracing.h" 20 #include "build/build_config.h" 21 22 namespace base { 23 class BASE_EXPORT MadvFreeDiscardableMemoryAllocatorPosix 24 : public DiscardableMemoryAllocator, 25 public base::trace_event::MemoryDumpProvider { 26 public: 27 MadvFreeDiscardableMemoryAllocatorPosix(); 28 29 MadvFreeDiscardableMemoryAllocatorPosix( 30 const MadvFreeDiscardableMemoryAllocatorPosix&) = delete; 31 MadvFreeDiscardableMemoryAllocatorPosix& operator=( 32 const MadvFreeDiscardableMemoryAllocatorPosix&) = delete; 33 34 ~MadvFreeDiscardableMemoryAllocatorPosix() override; 35 36 std::unique_ptr<DiscardableMemory> AllocateLockedDiscardableMemory( 37 size_t size) override; 38 39 size_t GetBytesAllocated() const override; 40 ReleaseFreeMemory()41 void ReleaseFreeMemory() override { 42 // Do nothing, since MADV_FREE discardable memory does not keep any memory 43 // overhead that can be released. 44 } 45 46 bool OnMemoryDump(const trace_event::MemoryDumpArgs& args, 47 trace_event::ProcessMemoryDump* pmd) override; 48 49 private: 50 std::atomic<size_t> bytes_allocated_{0}; 51 }; 52 } // namespace base 53 54 #endif // BASE_MEMORY_MADV_FREE_DISCARDABLE_MEMORY_ALLOCATOR_POSIX_H_ 55