chromium/base/metrics/persistent_memory_allocator.cc

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif

#include "base/metrics/persistent_memory_allocator.h"

#include <assert.h>

#include <algorithm>
#include <atomic>
#include <optional>
#include <string_view>

#include "base/bits.h"
#include "base/containers/contains.h"
#include "base/debug/alias.h"
#include "base/debug/crash_logging.h"
#include "base/debug/dump_without_crashing.h"
#include "base/files/memory_mapped_file.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/persistent_histogram_allocator.h"
#include "base/metrics/sparse_histogram.h"
#include "base/notreached.h"
#include "base/numerics/checked_math.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/strcat.h"
#include "base/system/sys_info.h"
#include "base/threading/scoped_blocking_call.h"
#include "build/build_config.h"

#if BUILDFLAG(IS_WIN)
#include <windows.h>
// Must be after <windows.h>
#include <winbase.h>
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#include <sys/mman.h>
#if BUILDFLAG(IS_ANDROID)
#include <sys/prctl.h>
#endif
#endif

namespace {

// Limit of memory segment size. It has to fit in an unsigned 32-bit number
// and should be a power of 2 in order to accommodate almost any page size.
constexpr uint32_t kSegmentMaxSize =;  // 1 GiB

// A constant (random) value placed in the shared metadata to identify
// an already initialized memory segment.
constexpr uint32_t kGlobalCookie =;

// The current version of the metadata. If updates are made that change
// the metadata, the version number can be queried to operate in a backward-
// compatible manner until the memory segment is completely re-initalized.
// Note: If you update the metadata in a non-backwards compatible way, reset
// |kCompatibleVersions|. Otherwise, add the previous version.
constexpr uint32_t kGlobalVersion =;
static constexpr uint32_t kOldCompatibleVersions[] =;

// Constant values placed in the block headers to indicate its state.
constexpr uint32_t kBlockCookieFree =;
constexpr uint32_t kBlockCookieQueue =;
constexpr uint32_t kBlockCookieWasted =;
constexpr uint32_t kBlockCookieAllocated =;

// TODO(bcwhite): When acceptable, consider moving flags to std::atomic<char>
// types rather than combined bitfield.

// Flags stored in the flags_ field of the SharedMetadata structure below.
constexpr uint32_t kFlagCorrupt =;
constexpr uint32_t kFlagFull =;

// Errors that are logged in "errors" histogram.
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum AllocatorError : int {};

bool CheckFlag(const volatile std::atomic<uint32_t>* flags, uint32_t flag) {}

void SetFlag(volatile std::atomic<uint32_t>* flags, uint32_t flag) {}

}  // namespace

base  // namespace base