29#define RESERVED_MAX 64
42static inline void *
alloc(
size_t size,
size_t align)
90 if (idx == (
size_t) -1)
return -1;
93 size_t child_idx = idx << 1;
119 if (!bitmap_check(&
buddies[zone][order].
bitmap, idx))
return;
152 panic(
"Cannot allocate buddy");
169 if (idx == (
size_t) -1)
186 panic(
"trying to free from kernel code");
216 printk(
"buddy: set unusable: %p-%p\n", addr, addr+size-1);
219 panic(
"buddy: memory set unusable after the first allocation");
222 panic(
"buddy: too many unusable ranges");
239 if (end <= zone_start || addr >= zone_end)
244 uintptr_t zone_addr =
MAX(addr, zone_start) - zone_start;
245 uintptr_t zone_size =
MIN(end, zone_end) - zone_start - zone_addr;
249 size_t end_idx = (zone_addr + zone_size - 1) /
BUDDY_MAX_BS;
258 for (
size_t idx = start_idx; idx <= end_idx; ++idx) {
268 if (ffidx >= start_idx && ffidx <= end_idx)
298 for (
size_t idx = 0; idx <
zone_blocks[zone]; ++idx) {
299 if (!bitmap_check(&top->
bitmap, idx)) {
330 printk(
"buddy: Setting up buddy allocator (total memory %p)\n", total_mem);
341 if (zone_end > zone_start)
381 extern char kernel_start;
static void bitmap_set_range(struct bitmap *bitmap, size_t findex, size_t lindex)
Sets the bits from findex to lindex, inclusive.
static size_t bitmap_size(size_t n)
Returns the size in bytes of a bitmap of n bits.
static void bitmap_clear_range(struct bitmap *bitmap, size_t findex, size_t lindex)
Clears the bits from findex to lindex, inclusive.
static size_t zone_blocks[BUDDY_ZONE_NR]
static void buddy_reserved_done(void)
Frees the pages of the blocks set unusable at boot that no reserved range covers, at the first alloca...
static size_t buddy_recursive_alloc(int zone, size_t order)
Takes a free block of order order from zone, splitting a bigger block if there is none.
static int reserved_page(paddr_t page)
Returns whether a range set unusable at boot covers page.
static void buddy_recursive_free(int zone, size_t order, size_t idx)
Frees the block at index idx of order order in zone, merging it with its buddy, and that block with i...
static struct @152050200367053112031232350014135337374054232355 reserved[RESERVED_MAX]
struct buddy buddies[BUDDY_ZONE_NR][BUDDY_MAX_ORDER+1]
static void * alloc(size_t size, size_t align)
Allocates memory for the bitmaps at boot, from what mm_setup() set aside: their size depends on how m...
struct elf32_shdr elf_shdr_t
void buddy_free(int zone, paddr_t paddr, size_t size)
Frees a block that buddy_alloc() gave.
void buddy_set_unusable(paddr_t paddr, size_t size)
Marks a range of physical memory as not free, at boot, before the first allocation: the kernel,...
paddr_t buddy_alloc(int zone, size_t size)
Allocates a block of physical memory from a zone, rounded up to a power of two pages.
uintptr_t buddy_zone_offset[]
#define BUDDY_ZONE_NORMAL
int buddy_setup(size_t total_mem)
Sets up the allocator at boot: a bitmap for each zone and order, every top-order block of RAM free,...
void * mm_boot_alloc(size_t size, size_t align)
Allocates boot memory, zeroed, before the buddy allocator is set up.
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
A bitmap: its blocks, and its last bit's index.
One order of a buddy allocator's zone (mm/buddy.c): a bitmap of its blocks, a set bit for a block tha...