discardable_memory_allocator.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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
  3. // found in the LICENSE file.
  4. #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATOR_H_
  5. #define BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATOR_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include "base/base_export.h"
  9. #include "base/callback.h"
  10. #include "base/memory/discardable_memory.h"
  11. namespace base {
  12. class DiscardableMemory;
  13. // An allocator which creates and manages DiscardableMemory. The allocator
  14. // itself should be created via CreateDiscardableMemoryAllocator, which
  15. // selects an appropriate implementation depending on platform support.
  16. class BASE_EXPORT DiscardableMemoryAllocator {
  17. public:
  18. DiscardableMemoryAllocator() = default;
  19. DiscardableMemoryAllocator(const DiscardableMemoryAllocator&) = delete;
  20. DiscardableMemoryAllocator& operator=(const DiscardableMemoryAllocator&) =
  21. delete;
  22. virtual ~DiscardableMemoryAllocator() = default;
  23. // Returns the allocator instance.
  24. static DiscardableMemoryAllocator* GetInstance();
  25. // Sets the allocator instance. Can only be called once, e.g. on startup.
  26. // Ownership of |instance| remains with the caller.
  27. static void SetInstance(DiscardableMemoryAllocator* allocator);
  28. // Creates an initially-locked instance of discardable memory.
  29. // If the platform supports Android ashmem or madvise(MADV_FREE),
  30. // platform-specific techniques will be used to discard memory under pressure.
  31. // Otherwise, discardable memory is emulated and manually discarded
  32. // heuristicly (via memory pressure notifications).
  33. virtual std::unique_ptr<DiscardableMemory> AllocateLockedDiscardableMemory(
  34. size_t size) = 0;
  35. // Allocates discardable memory the same way |AllocateLockedDiscardableMemory|
  36. // does. In case of failure, calls |on_no_memory| and retries once. As a
  37. // consequence, |on_no_memory| should free some memory, and importantly,
  38. // address space as well.
  39. //
  40. // In case of allocation failure after retry, terminates the process with
  41. // an Out Of Memory status (for triage in crash reports).
  42. //
  43. // As a consequence, does *not* return nullptr.
  44. std::unique_ptr<DiscardableMemory>
  45. AllocateLockedDiscardableMemoryWithRetryOrDie(size_t size,
  46. OnceClosure on_no_memory);
  47. // Gets the total number of bytes allocated by this allocator which have not
  48. // been discarded.
  49. virtual size_t GetBytesAllocated() const = 0;
  50. // Release any memory used in the implementation of discardable memory that is
  51. // not immediately being used.
  52. virtual void ReleaseFreeMemory() = 0;
  53. };
  54. } // namespace base
  55. #endif // BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATOR_H_