Contur 2
Educational OS kernel simulator
Loading...
Searching...
No Matches
block_allocator.h
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <memory>
7
8#include "contur/core/error.h"
9
10namespace contur {
11
16 class BlockAllocator final
17 {
18 public:
21 explicit BlockAllocator(std::size_t totalBlocks);
22
25
27 BlockAllocator(const BlockAllocator &) = delete;
28
33
35 BlockAllocator &operator=(BlockAllocator &&) noexcept;
36
39 [[nodiscard]] Result<std::size_t> allocate();
40
44 [[nodiscard]] Result<void> free(std::size_t blockIndex);
45
49 [[nodiscard]] bool isFree(std::size_t blockIndex) const noexcept;
50
53 [[nodiscard]] std::size_t totalBlocks() const noexcept;
54
57 [[nodiscard]] std::size_t freeBlocks() const noexcept;
58
60 void reset();
61
62 private:
63 struct Impl;
64 std::unique_ptr<Impl> impl_;
65 };
66
67} // namespace contur
std::unique_ptr< Impl > impl_
Result< void > free(std::size_t blockIndex)
Frees a previously allocated block.
~BlockAllocator()
Destroys block allocator.
bool isFree(std::size_t blockIndex) const noexcept
Returns whether block is free.
BlockAllocator(std::size_t totalBlocks)
Creates a block allocator over a fixed number of blocks.
void reset()
Resets allocator state, marking every block as free.
Result< std::size_t > allocate()
Allocates one free block.
BlockAllocator(BlockAllocator &&) noexcept
Move-constructs allocator state.
std::size_t totalBlocks() const noexcept
Total number of blocks managed by allocator.
BlockAllocator(const BlockAllocator &)=delete
Copy construction is disabled.
std::size_t freeBlocks() const noexcept
Number of currently free blocks.
BlockAllocator & operator=(const BlockAllocator &)=delete
Copy assignment is disabled.
A result type that holds either a success value of type T or an ErrorCode.
Definition error.h:104
Error codes and Result<T> type for fallible operations.
Definition block.h:15