#include "perfetto/ext/base/paged_memory.h"
#include <algorithm>
#include <cmath>
#include <cstddef>
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
#include <Windows.h>
#else
#include <sys/mman.h>
#endif
#include "perfetto/base/logging.h"
#include "perfetto/ext/base/container_annotations.h"
#include "perfetto/ext/base/utils.h"
namespace perfetto {
namespace base {
namespace {
#if TRACK_COMMITTED_SIZE()
constexpr size_t kCommitChunkSize = 4 * 1024 * 1024;
#endif
size_t RoundUpToSysPageSize(size_t req_size) { … }
size_t GuardSize() { … }
}
PagedMemory PagedMemory::Allocate(size_t req_size, int flags) { … }
PagedMemory::PagedMemory() { … }
PagedMemory::PagedMemory(char* p, size_t size) : … { … }
PagedMemory::PagedMemory(PagedMemory&& other) noexcept { … }
PagedMemory& PagedMemory::operator=(PagedMemory&& other) { … }
PagedMemory::~PagedMemory() { … }
bool PagedMemory::AdviseDontNeed(void* p, size_t size) { … }
#if TRACK_COMMITTED_SIZE()
void PagedMemory::EnsureCommitted(size_t committed_size) {
PERFETTO_DCHECK(committed_size <= size_);
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
if (committed_size_ >= committed_size)
return;
size_t delta = committed_size - committed_size_;
size_t num_additional_chunks =
(delta + kCommitChunkSize - 1) / kCommitChunkSize;
PERFETTO_DCHECK(num_additional_chunks * kCommitChunkSize >= delta);
size_t commit_size = std::min(num_additional_chunks * kCommitChunkSize,
size_ - committed_size_);
void* res = VirtualAlloc(p_ + committed_size_, commit_size, MEM_COMMIT,
PAGE_READWRITE);
PERFETTO_CHECK(res);
ANNOTATE_CHANGE_SIZE(p_, size_, committed_size_,
committed_size_ + commit_size)
committed_size_ += commit_size;
#else
committed_size = std::max(committed_size_, committed_size);
ANNOTATE_CHANGE_SIZE(p_, size_, committed_size_, committed_size)
committed_size_ = committed_size;
#endif
}
#endif
}
}