The previous frame allocator involved a lot of splitting and merging linked lists and lost all information about frames while they were allocated. The new allocator is based on an array of descriptor structures and a bitmap. Each memory map region of allocatable memory becomes one or more descriptors, each mapping up to 1GiB of physical memory. The descriptors implement two levels of a bitmap tree, and have a pointer into the large contiguous bitmap to track individual pages.
51 lines
1.4 KiB
C++
51 lines
1.4 KiB
C++
#pragma once
|
|
/// \file frame_allocator.h
|
|
/// Allocator for physical memory frames
|
|
|
|
#include <stdint.h>
|
|
|
|
namespace kernel {
|
|
namespace args {
|
|
struct frame_block;
|
|
}}
|
|
|
|
/// Allocator for physical memory frames
|
|
class frame_allocator
|
|
{
|
|
public:
|
|
using frame_block = kernel::args::frame_block;
|
|
|
|
/// Constructor
|
|
/// \arg blocks The bootloader-supplied frame bitmap block list
|
|
/// \arg count Number of entries in the block list
|
|
frame_allocator(frame_block *frames, size_t count);
|
|
|
|
/// Get free frames from the free list. Only frames from the first free block
|
|
/// are returned, so the number may be less than requested, but they will
|
|
/// be contiguous.
|
|
/// \arg count The maximum number of frames to get
|
|
/// \arg address [out] The physical address of the first frame
|
|
/// \returns The number of frames retrieved
|
|
size_t allocate(size_t count, uintptr_t *address);
|
|
|
|
/// Free previously allocated frames.
|
|
/// \arg address The physical address of the first frame to free
|
|
/// \arg count The number of frames to be freed
|
|
void free(uintptr_t address, size_t count);
|
|
|
|
/// Mark frames as used
|
|
/// \arg address The physical address of the first frame to free
|
|
/// \arg count The number of frames to be freed
|
|
void used(uintptr_t address, size_t count);
|
|
|
|
/// Get the global frame allocator
|
|
static frame_allocator & get();
|
|
|
|
private:
|
|
frame_block *m_blocks;
|
|
long m_count;
|
|
|
|
frame_allocator() = delete;
|
|
frame_allocator(const frame_allocator &) = delete;
|
|
};
|