#include "base/memory/discardable_shared_memory.h"
#include <stdint.h>
#include <algorithm>
#include "base/atomicops.h"
#include "base/bits.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/memory/discardable_memory.h"
#include "base/memory/discardable_memory_internal.h"
#include "base/memory/page_size.h"
#include "base/memory/shared_memory_tracker.h"
#include "base/numerics/safe_math.h"
#include "base/tracing_buildflags.h"
#include "build/build_config.h"
#include "partition_alloc/page_allocator.h"
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_NACL)
#include <sys/mman.h>
#endif
#if BUILDFLAG(IS_ANDROID)
#include "third_party/ashmem/ashmem.h"
#endif
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include "base/win/windows_version.h"
#endif
#if BUILDFLAG(IS_FUCHSIA)
#include <lib/zx/vmar.h>
#include <zircon/types.h>
#include "base/fuchsia/fuchsia_logging.h"
#endif
#if BUILDFLAG(ENABLE_BASE_TRACING)
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/process_memory_dump.h"
#endif
namespace base {
namespace {
AtomicType;
UAtomicType;
template <int>
Time TimeFromWireFormat(int64_t value);
template <int>
int64_t TimeToWireFormat(Time time);
template <>
[[maybe_unused]] Time TimeFromWireFormat<4>(int64_t value) { … }
template <>
[[maybe_unused]] int64_t TimeToWireFormat<4>(Time time) { … }
template <>
[[maybe_unused]] Time TimeFromWireFormat<8>(int64_t value) { … }
template <>
[[maybe_unused]] int64_t TimeToWireFormat<8>(Time time) { … }
struct SharedState { … };
SharedState* SharedStateFromSharedMemory(
const WritableSharedMemoryMapping& shared_memory) { … }
size_t AlignToPageSize(size_t size) { … }
#if BUILDFLAG(IS_ANDROID)
bool UseAshmemUnpinningForDiscardableMemory() {
if (!ashmem_device_is_supported())
return false;
if (base::DiscardableMemoryBackingFieldTrialIsEnabled()) {
return base::GetDiscardableMemoryBackingFieldTrialGroup() ==
base::DiscardableMemoryTrialGroup::kAshmem;
}
return true;
}
#endif
}
DiscardableSharedMemory::DiscardableSharedMemory() = default;
DiscardableSharedMemory::DiscardableSharedMemory(
UnsafeSharedMemoryRegion shared_memory_region)
: … { … }
DiscardableSharedMemory::~DiscardableSharedMemory() = default;
bool DiscardableSharedMemory::CreateAndMap(size_t size) { … }
bool DiscardableSharedMemory::Map(size_t size) { … }
bool DiscardableSharedMemory::Unmap() { … }
DiscardableSharedMemory::LockResult DiscardableSharedMemory::Lock(
size_t offset, size_t length) { … }
void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { … }
span<uint8_t> DiscardableSharedMemory::memory() const { … }
span<uint8_t> DiscardableSharedMemory::mapped_memory() const { … }
bool DiscardableSharedMemory::Purge(Time current_time) { … }
bool DiscardableSharedMemory::IsMemoryResident() const { … }
bool DiscardableSharedMemory::IsMemoryLocked() const { … }
void DiscardableSharedMemory::Close() { … }
void DiscardableSharedMemory::CreateSharedMemoryOwnershipEdge(
trace_event::MemoryAllocatorDump* local_segment_dump,
trace_event::ProcessMemoryDump* pmd,
bool is_owned) const { … }
DiscardableSharedMemory::LockResult DiscardableSharedMemory::LockPages(
const UnsafeSharedMemoryRegion& region,
size_t offset,
size_t length) { … }
void DiscardableSharedMemory::UnlockPages(
const UnsafeSharedMemoryRegion& region,
size_t offset,
size_t length) { … }
Time DiscardableSharedMemory::Now() const { … }
#if BUILDFLAG(IS_ANDROID)
bool DiscardableSharedMemory::IsAshmemDeviceSupportedForTesting() {
return UseAshmemUnpinningForDiscardableMemory();
}
#endif
}