chromium/third_party/grpc/src/src/core/lib/resource_quota/arena.h

//
//
// Copyright 2017 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//

// \file Arena based allocator
// Allows very fast allocation of memory, but that memory cannot be freed until
// the arena as a whole is freed
// Tracks the total memory allocated against it, so that future arenas can
// pre-allocate the right amount of memory

#ifndef GRPC_SRC_CORE_LIB_RESOURCE_QUOTA_ARENA_H
#define GRPC_SRC_CORE_LIB_RESOURCE_QUOTA_ARENA_H

#include <grpc/support/port_platform.h>

#include <stddef.h>

#include <atomic>
#include <limits>
#include <memory>
#include <new>
#include <utility>

#include "absl/meta/type_traits.h"
#include "absl/utility/utility.h"

#include <grpc/event_engine/memory_allocator.h>

#include "src/core/lib/gpr/alloc.h"
#include "src/core/lib/gprpp/construct_destruct.h"
#include "src/core/lib/promise/context.h"
#include "src/core/lib/resource_quota/memory_quota.h"

namespace grpc_core {

namespace arena_detail {

struct PoolAndSize {};

template <typename Void, size_t kIndex, size_t kObjectSize,
          size_t... kBucketSize>
struct PoolIndexForSize;

PoolIndexForSize<absl::enable_if_t<kObjectSize <= kSmallestRemainingBucket>, kIndex, kObjectSize, kSmallestRemainingBucket, kBucketSizes...>;

PoolIndexForSize<absl::enable_if_t<(kObjectSize > kSmallestRemainingBucket)>, kIndex, kObjectSize, kSmallestRemainingBucket, kBucketSizes...>;

template <size_t kObjectSize, size_t... kBucketSizes>
constexpr size_t PoolFromObjectSize(
    absl::integer_sequence<size_t, kBucketSizes...>) {}

template <size_t kObjectSize, size_t... kBucketSizes>
constexpr size_t AllocationSizeFromObjectSize(
    absl::integer_sequence<size_t, kBucketSizes...>) {}

template <size_t kIndex, size_t... kBucketSizes>
struct ChoosePoolForAllocationSizeImpl;

ChoosePoolForAllocationSizeImpl<kIndex, kBucketSize, kBucketSizes...>;

ChoosePoolForAllocationSizeImpl<kIndex>;

template <size_t... kBucketSizes>
PoolAndSize ChoosePoolForAllocationSize(
    size_t n, absl::integer_sequence<size_t, kBucketSizes...>) {}

}  // namespace arena_detail

class Arena {};

// Smart pointer for arenas when the final size is not required.
struct ScopedArenaDeleter {};
ScopedArenaPtr;
inline ScopedArenaPtr MakeScopedArena(size_t initial_size,
                                      MemoryAllocator* memory_allocator) {}

// Arenas form a context for activities
template <>
struct ContextType<Arena> {};

}  // namespace grpc_core

#endif  // GRPC_SRC_CORE_LIB_RESOURCE_QUOTA_ARENA_H