#ifdef UNSAFE_BUFFERS_BUILD
#pragma check_unsafe_buffers
#endif
#include "components/discardable_memory/common/discardable_shared_memory_heap.h"
#include <bit>
#include <memory>
#include <string>
#include <utility>
#include "base/containers/contains.h"
#include "base/format_macros.h"
#include "base/memory/aligned_memory.h"
#include "base/memory/discardable_shared_memory.h"
#include "base/memory/page_size.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/ranges/algorithm.h"
#include "base/strings/stringprintf.h"
#include "base/trace_event/memory_dump_manager.h"
namespace discardable_memory {
namespace {
bool IsInFreeList(DiscardableSharedMemoryHeap::Span* span) { … }
}
DiscardableSharedMemoryHeap::Span::Span(
base::DiscardableSharedMemory* shared_memory,
size_t first_block,
size_t num_blocks,
DiscardableSharedMemoryHeap::ScopedMemorySegment* memory_segment)
: … { … }
DiscardableSharedMemoryHeap::ScopedMemorySegment::ScopedMemorySegment(
DiscardableSharedMemoryHeap* heap,
std::unique_ptr<base::DiscardableSharedMemory> shared_memory,
size_t size,
int32_t id,
base::OnceClosure deleted_callback)
: … { … }
base::span<uint8_t> DiscardableSharedMemoryHeap::Span::memory() const { … }
DiscardableSharedMemoryHeap::ScopedMemorySegment*
DiscardableSharedMemoryHeap::Span::GetScopedMemorySegmentForTesting() const { … }
DiscardableSharedMemoryHeap::ScopedMemorySegment::~ScopedMemorySegment() { … }
bool DiscardableSharedMemoryHeap::ScopedMemorySegment::IsUsed() const { … }
bool DiscardableSharedMemoryHeap::ScopedMemorySegment::IsResident() const { … }
bool DiscardableSharedMemoryHeap::ScopedMemorySegment::ContainsSpan(
Span* span) const { … }
base::trace_event::MemoryAllocatorDump*
DiscardableSharedMemoryHeap::ScopedMemorySegment::CreateMemoryAllocatorDump(
Span* span,
size_t block_size,
const char* name,
base::trace_event::ProcessMemoryDump* pmd) const { … }
void DiscardableSharedMemoryHeap::ScopedMemorySegment::OnMemoryDump(
base::trace_event::ProcessMemoryDump* pmd) const { … }
DiscardableSharedMemoryHeap::DiscardableSharedMemoryHeap()
: … { … }
DiscardableSharedMemoryHeap::~DiscardableSharedMemoryHeap() { … }
std::unique_ptr<DiscardableSharedMemoryHeap::Span>
DiscardableSharedMemoryHeap::Grow(
std::unique_ptr<base::DiscardableSharedMemory> shared_memory,
size_t size,
int32_t id,
base::OnceClosure deleted_callback) { … }
void DiscardableSharedMemoryHeap::MergeIntoFreeLists(
std::unique_ptr<Span> span) { … }
void DiscardableSharedMemoryHeap::MergeIntoFreeListsClean(
std::unique_ptr<Span> span) { … }
std::unique_ptr<DiscardableSharedMemoryHeap::Span>
DiscardableSharedMemoryHeap::Split(Span* span, size_t blocks) { … }
std::unique_ptr<DiscardableSharedMemoryHeap::Span>
DiscardableSharedMemoryHeap::SearchFreeLists(size_t blocks, size_t slack) { … }
void DiscardableSharedMemoryHeap::ReleaseFreeMemory() { … }
void DiscardableSharedMemoryHeap::ReleasePurgedMemory() { … }
size_t DiscardableSharedMemoryHeap::GetSize() const { … }
size_t DiscardableSharedMemoryHeap::GetFreelistSize() const { … }
std::optional<size_t> DiscardableSharedMemoryHeap::GetResidentSize() const { … }
bool DiscardableSharedMemoryHeap::OnMemoryDump(
const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) { … }
void DiscardableSharedMemoryHeap::InsertIntoFreeList(
std::unique_ptr<DiscardableSharedMemoryHeap::Span> span) { … }
std::unique_ptr<DiscardableSharedMemoryHeap::Span>
DiscardableSharedMemoryHeap::RemoveFromFreeList(Span* span) { … }
std::unique_ptr<DiscardableSharedMemoryHeap::Span>
DiscardableSharedMemoryHeap::Carve(Span* span, size_t blocks) { … }
void DiscardableSharedMemoryHeap::RegisterSpan(Span* span) { … }
void DiscardableSharedMemoryHeap::UnregisterSpan(Span* span) { … }
bool DiscardableSharedMemoryHeap::IsMemoryUsed(
const base::DiscardableSharedMemory* shared_memory,
size_t size) { … }
bool DiscardableSharedMemoryHeap::IsMemoryResident(
const base::DiscardableSharedMemory* shared_memory) { … }
void DiscardableSharedMemoryHeap::ReleaseMemory(
const base::DiscardableSharedMemory* shared_memory,
size_t size) { … }
void DiscardableSharedMemoryHeap::OnMemoryDump(
const base::DiscardableSharedMemory* shared_memory,
size_t size,
int32_t segment_id,
base::trace_event::ProcessMemoryDump* pmd) { … }
base::trace_event::MemoryAllocatorDump*
DiscardableSharedMemoryHeap::CreateMemoryAllocatorDump(
Span* span,
const char* name,
base::trace_event::ProcessMemoryDump* pmd) const { … }
std::pair<const base::DiscardableSharedMemory*, size_t>
DiscardableSharedMemoryHeap::SpanBeginKey(const Span& span) { … }
std::pair<const base::DiscardableSharedMemory*, size_t>
DiscardableSharedMemoryHeap::SpanEndKey(const Span& span) { … }
}