blob: e5140c25ca5b22be219f9597cbabeaeff7999155 [file] [log] [blame]
// Copyright 2024 The Pigweed 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
//
// https://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.
#pragma once
#include <cstdint>
#include "pw_allocator/capability.h"
#include "pw_allocator/layout.h"
#include "pw_allocator/pool.h"
#include "pw_bytes/span.h"
#include "pw_status/status.h"
namespace pw::allocator {
/// Implementation of ``Pool`` that uses a list of free chunks.
///
/// The first ``sizeof(void*)`` bytes of each free chunk is used to store a
/// pointer to the next free chunk, or null for the last free chunk.
class ChunkPool : public Pool {
public:
static constexpr Capabilities kCapabilities =
kImplementsGetRequestedLayout | kImplementsGetUsableLayout |
kImplementsGetAllocatedLayout | kImplementsQuery;
static constexpr size_t kMinSize = sizeof(void*);
static constexpr size_t kMinAlignment = alignof(void*);
/// Construct a `Pool` that allocates from a region of memory.
///
/// @param region The memory to allocate from. Must be large enough to
/// allocate at least one chunk with the given layout.
/// @param layout The size and alignment of the memory to be returned
/// from this pool.
ChunkPool(ByteSpan region, const Layout& layout);
private:
/// @copydoc Pool::Allocate
void* DoAllocate() override;
/// @copydoc Pool::Deallocate
void DoDeallocate(void* ptr) override;
/// @copydoc Pool::Query
Status DoQuery(const void* ptr) const override;
const Layout allocated_layout_;
uintptr_t start_;
uintptr_t end_;
std::byte* next_;
};
} // namespace pw::allocator