1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82

base / allocator / partition_allocator / src / partition_alloc / internal_allocator_forward.h [blame]

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

#ifndef PARTITION_ALLOC_INTERNAL_ALLOCATOR_FORWARD_H_
#define PARTITION_ALLOC_INTERNAL_ALLOCATOR_FORWARD_H_

#include <new>
#include <type_traits>

#include "partition_alloc/partition_alloc_base/component_export.h"
#include "partition_alloc/partition_alloc_forward.h"

// Internal Allocator can be used to get heap allocations required to
// implement PartitionAlloc's feature.
// As Internal Allocator being PartitionAlloc with minimal configuration,
// it is not allowed to use this allocator for PA's core implementation to avoid
// reentrancy issues. Also don't use this when satisfying the very first PA-E
// allocation of the process.

namespace partition_alloc::internal {

PA_COMPONENT_EXPORT(PARTITION_ALLOC)
PartitionRoot& InternalAllocatorRoot();

// A class that meets C++ named requirements, Allocator.
template <typename T>
class InternalAllocator {
 public:
  using value_type = T;
  using is_always_equal = std::true_type;

  InternalAllocator() = default;

  template <typename U>
  InternalAllocator(const InternalAllocator<U>&) {}  // NOLINT

  template <typename U>
  InternalAllocator& operator=(const InternalAllocator<U>&) {
    return *this;
  }

  template <typename U>
  bool operator==(const InternalAllocator<U>&) {
    // InternalAllocator<T> can free allocations made by InternalAllocator<U>.
    return true;
  }

  value_type* allocate(std::size_t count);

  void deallocate(value_type* ptr, std::size_t);
};

// Inherit this to make a class allocated on the internal partition.
struct PA_COMPONENT_EXPORT(PARTITION_ALLOC) InternalPartitionAllocated {
  static void* operator new(size_t count);
  static void* operator new(size_t count, std::align_val_t alignment);
  // Though we do not forward placement new, we need to define this explicitly
  // to allow it.
  static void* operator new(std::size_t, void* ptr) { return ptr; }
  static void operator delete(void* ptr);
  static void operator delete(void* ptr, std::align_val_t);
};

// Create an object on heap in the internal partition.
template <typename T, typename... Args>
T* ConstructAtInternalPartition(Args&&... args);

// Destroy an object on heap in the internal partition.
// TODO(crbug.com/40274826) This is an unused function. Start using it in tests
// and/or in production code.
template <typename T>
void DestroyAtInternalPartition(T* ptr);

// A deleter for `std::unique_ptr<T>`.
struct PA_COMPONENT_EXPORT(PARTITION_ALLOC) InternalPartitionDeleter final {
  void operator()(void* ptr) const;
};

}  // namespace partition_alloc::internal

#endif  // PARTITION_ALLOC_INTERNAL_ALLOCATOR_FORWARD_H_