chromium/base/allocator/miracle_parameter.h

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

#ifndef BASE_ALLOCATOR_MIRACLE_PARAMETER_H_
#define BASE_ALLOCATOR_MIRACLE_PARAMETER_H_

#include "base/base_export.h"
#include "base/containers/span.h"
#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"

// This is a mirror copy of the //components/miracle_parameter/ to resolve the
// dependency cycle of (base->miracle_parameter->base).
// Eventually the miracle_parameter component will have a public interface in
// //base/ and this could be removed.
// TODO(crbug.com/40279826): remove miracle_parameter from
// //base/allocator/.

namespace base {

namespace miracle_parameter {

namespace {

template <typename Enum>
Enum GetFieldTrialParamByFeatureAsEnum(
    const base::Feature& feature,
    const std::string& param_name,
    const Enum default_value,
    const base::span<const typename base::FeatureParam<Enum>::Option>&
        options) {}

}  // namespace

constexpr int kMiracleParameterMemory512MB =;
constexpr int kMiracleParameterMemory1GB =;
constexpr int kMiracleParameterMemory2GB =;
constexpr int kMiracleParameterMemory4GB =;
constexpr int kMiracleParameterMemory8GB =;
constexpr int kMiracleParameterMemory16GB =;

// GetParamNameWithSuffix put a parameter name suffix based on
// the amount of physical memory.
//
// - "ForLessThan512MB" for less than 512MB memory devices.
// - "For512MBTo1GB" for 512MB to 1GB memory devices.
// - "For1GBTo2GB" for 1GB to 2GB memory devices.
// - "For2GBTo4GB" for 2GB to 4GB memory devices.
// - "For4GBTo8GB" for 4GB to 8GB memory devices.
// - "For8GBTo16GB" for 8GB to 16GB memory devices.
// - "For16GBAndAbove" for 16GB memory and above devices.
BASE_EXPORT
std::string GetParamNameWithSuffix(const std::string& param_name);

// Provides a similar behavior with FeatureParam<std::string> except the return
// value is determined by the amount of physical memory.
BASE_EXPORT
std::string GetMiracleParameterAsString(const base::Feature& feature,
                                        const std::string& param_name,
                                        const std::string& default_value);

// Provides a similar behavior with FeatureParam<double> except the return value
// is determined by the amount of physical memory.
BASE_EXPORT
double GetMiracleParameterAsDouble(const base::Feature& feature,
                                   const std::string& param_name,
                                   double default_value);

// Provides a similar behavior with FeatureParam<int> except the return value is
// determined by the amount of physical memory.
BASE_EXPORT
int GetMiracleParameterAsInt(const base::Feature& feature,
                             const std::string& param_name,
                             int default_value);

// Provides a similar behavior with FeatureParam<bool> except the return value
// is determined by the amount of physical memory.
BASE_EXPORT
bool GetMiracleParameterAsBool(const base::Feature& feature,
                               const std::string& param_name,
                               bool default_value);

// Provides a similar behavior with FeatureParam<base::TimeDelta> except the
// return value is determined by the amount of physical memory.
BASE_EXPORT
base::TimeDelta GetMiracleParameterAsTimeDelta(const base::Feature& feature,
                                               const std::string& param_name,
                                               base::TimeDelta default_value);

// Provides a similar behavior with FeatureParam<Enum> except the return value
// is determined by the amount of physical memory.
template <typename Enum>
Enum GetMiracleParameterAsEnum(
    const base::Feature& feature,
    const std::string& param_name,
    const Enum default_value,
    const base::span<const typename base::FeatureParam<Enum>::Option> options) {}

#define MIRACLE_PARAMETER_FOR_STRING(function_name, feature, param_name,    \
                                     default_value)

#define MIRACLE_PARAMETER_FOR_DOUBLE(function_name, feature, param_name,    \
                                     default_value)

#define MIRACLE_PARAMETER_FOR_INT(function_name, feature, param_name,     \
                                  default_value)

#define MIRACLE_PARAMETER_FOR_BOOL(function_name, feature, param_name,      \
                                   default_value)

#define MIRACLE_PARAMETER_FOR_TIME_DELTA(function_name, feature, param_name,   \
                                         default_value)

#define MIRACLE_PARAMETER_FOR_ENUM(function_name, feature, param_name,      \
                                   default_value, type, options)

}  // namespace miracle_parameter

}  // namespace base

#endif  // BASE_ALLOCATOR_MIRACLE_PARAMETER_H_