PatchworkOS
Loading...
Searching...
No Matches
pmm_bitmap.c
Go to the documentation of this file.
1#include <kernel/log/log.h>
3
4#include <assert.h>
5#include <sys/math.h>
6
8{
9 assert(size >= maxAddr / PAGE_SIZE);
11 bitmap->free = 0;
12 bitmap->total = maxAddr / PAGE_SIZE;
13 bitmap->maxAddr = maxAddr;
14}
15
17{
18 alignment = MAX(ROUND_UP(alignment, PAGE_SIZE), PAGE_SIZE);
19 maxAddr = MIN(maxAddr, bitmap->maxAddr);
20
21 uint64_t index =
23 if (index == bitmap->bitmap.length)
24 {
25 return NULL;
26 }
27
28 bitmap->free -= count;
29 return (void*)(index * PAGE_SIZE + PML_HIGHER_HALF_START);
30}
31
33{
35
37 assert(index < bitmap->maxAddr / PAGE_SIZE);
38
39 bitmap_clear_range(&bitmap->bitmap, index, index + count);
40 bitmap->free += count;
41}
#define assert(expression)
Definition assert.h:29
#define PML_HIGHER_TO_LOWER(addr)
Converts an address from the higher half to the lower half.
#define PML_HIGHER_HALF_START
The start of the higher half of the address space.
void * pmm_bitmap_alloc(pmm_bitmap_t *bitmap, uint64_t count, uintptr_t maxAddr, uint64_t alignment)
Allocates a contiguous region of pages from the bitmap.
Definition pmm_bitmap.c:16
void pmm_bitmap_init(pmm_bitmap_t *bitmap, void *buffer, uint64_t size, uintptr_t maxAddr)
Initializes a PMM bitmap.
Definition pmm_bitmap.c:7
void pmm_bitmap_free(pmm_bitmap_t *bitmap, void *address, uint64_t count)
Frees a region of pages, returning them to the bitmap.
Definition pmm_bitmap.c:32
static void bitmap_init(bitmap_t *map, void *buffer, uint64_t length)
Initialize a bitmap.
Definition bitmap.h:74
static void bitmap_clear_range(bitmap_t *map, uint64_t low, uint64_t high)
Clear a range of bits in the bitmap.
Definition bitmap.h:206
static uint64_t bitmap_find_clear_region_and_set(bitmap_t *map, uint64_t length, uintptr_t maxIdx, uint64_t align)
Find a clear region of specified length and alignment, and set it.
Definition bitmap.h:147
#define MIN(x, y)
Definition math.h:16
#define ROUND_DOWN(number, multiple)
Definition math.h:21
#define ROUND_UP(number, multiple)
Definition math.h:19
#define MAX(x, y)
Definition math.h:15
#define PAGE_SIZE
Memory page size.
Definition proc.h:140
#define NULL
Pointer error value.
Definition NULL.h:23
static uintptr_t address
Definition hpet.c:12
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
static pmm_bitmap_t bitmap
Definition pmm.c:37
static atomic_long count
Definition main.c:9
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:43
uint64_t length
Definition bitmap.h:24
Represents a bitmap allocator's state.
Definition pmm_bitmap.h:22
uint64_t free
The number of free pages in the bitmap.
Definition pmm_bitmap.h:30
bitmap_t bitmap
The underlying bitmap used for tracking page status.
Definition pmm_bitmap.h:26
uint64_t total
The total number of pages managed by the bitmap.
Definition pmm_bitmap.h:34
uintptr_t maxAddr
The maximum address managed by the bitmap.
Definition pmm_bitmap.h:38