1 // Copyright 2019 The Chromium Authors. All rights reserved.
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/bind.h"
15 #include "base/callback.h"
16 #include "base/logging.h"
17 #include "base/macros.h"
18 #include "base/memory/discardable_memory.h"
19 #include "base/memory/discardable_memory_allocator.h"
20 #include "base/memory/madv_free_discardable_memory_posix.h"
21 #include "base/trace_event/memory_dump_provider.h"
22 #include "build/build_config.h"
23 
24 namespace base {
25 class BASE_EXPORT MadvFreeDiscardableMemoryAllocatorPosix
26     : public DiscardableMemoryAllocator,
27       public base::trace_event::MemoryDumpProvider {
28  public:
29   MadvFreeDiscardableMemoryAllocatorPosix();
30   ~MadvFreeDiscardableMemoryAllocatorPosix() override;
31 
32   std::unique_ptr<DiscardableMemory> AllocateLockedDiscardableMemory(
33       size_t size) override;
34 
35   size_t GetBytesAllocated() const override;
36 
ReleaseFreeMemory()37   void ReleaseFreeMemory() override {
38     // Do nothing, since MADV_FREE discardable memory does not keep any memory
39     // overhead that can be released.
40   }
41 
42   bool OnMemoryDump(const trace_event::MemoryDumpArgs& args,
43                     trace_event::ProcessMemoryDump* pmd) override;
44 
45  private:
46   std::atomic<size_t> bytes_allocated_{0};
47 
48   DISALLOW_COPY_AND_ASSIGN(MadvFreeDiscardableMemoryAllocatorPosix);
49 };
50 }  // namespace base
51 
52 #endif  // BASE_MEMORY_MADV_FREE_DISCARDABLE_MEMORY_ALLOCATOR_POSIX_H_
53