MOS Source Code
Loading...
Searching...
No Matches
buddy_core.c File Reference
+ Include dependency graph for buddy_core.c:

Go to the source code of this file.

Macros

#define log2(x)
 
#define log2_ceil(x)
 

Functions

static void add_to_freelist (size_t order, phyframe_t *frame)
 
static pfn_t get_buddy_pfn (size_t page_pfn, size_t order)
 
static void dump_list (size_t order)
 
static void populate_freelist (const size_t start_pfn, const size_t nframes, const size_t order)
 Add [start_pfn, start_pfn + nframes - 1] to the freelist of order order
 
static void break_this_pfn (pfn_t this_pfn, size_t this_order)
 
static void extract_exact_range (pfn_t start, size_t nframes, enum phyframe_state state)
 
static void break_the_order (const size_t order)
 
static bool try_merge (const pfn_t pfn, u8 order)
 Try finding a buddy frame and merging it with the given frame.
 
void buddy_dump_all ()
 Dump the state of the buddy allocator.
 
void buddy_init (size_t max_nframes)
 Initialize the buddy allocator with the given maximum number of frames.
 
void buddy_reserve_n (pfn_t pfn, size_t nframes)
 Reserve several frames at the given physical frame number.
 
phyframe_tbuddy_alloc_n_exact (size_t nframes)
 Allocate nframes of contiguous physical memory.
 
void buddy_free_n (pfn_t pfn, size_t nframes)
 Free nframes of contiguous physical memory.
 

Variables

static const size_t orders [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 }
 
static const size_t max_order = MOS_ARRAY_SIZE(orders) - 1
 
struct { 
 
   list_head   freelists [MOS_ARRAY_SIZE(orders)] 
 
buddy = { 0 } 
 
static spinlock_t buddy_lock = SPINLOCK_INIT
 

Macro Definition Documentation

◆ log2

#define log2 ( x)
Value:
__extension__({ \
__extension__ __auto_type _x = (x); \
_x ? (sizeof(_x) * 8 - 1 - __builtin_clzl(_x)) : 0; \
})

Definition at line 11 of file buddy_core.c.

Referenced by buddy_init().

◆ log2_ceil

#define log2_ceil ( x)
Value:
(log2(x) + (pow2(log2(x)) < x))
#define log2(x)
Definition buddy_core.c:11
#define pow2(x)
Definition mos_stdlib.h:32

Definition at line 16 of file buddy_core.c.

Referenced by buddy_alloc_n_exact(), and buddy_free_n().

Function Documentation

◆ add_to_freelist()

static void add_to_freelist ( size_t order,
phyframe_t * frame )
static

Definition at line 28 of file buddy_core.c.

Referenced by break_the_order(), break_this_pfn(), buddy_free_n(), populate_freelist(), and try_merge().

+ Here is the call graph for this function:

◆ get_buddy_pfn()

static pfn_t get_buddy_pfn ( size_t page_pfn,
size_t order )
static

Definition at line 46 of file buddy_core.c.

Referenced by try_merge().

◆ dump_list()

static void dump_list ( size_t order)
static

Definition at line 52 of file buddy_core.c.

Referenced by buddy_dump_all().

◆ populate_freelist()

static void populate_freelist ( const size_t start_pfn,
const size_t nframes,
const size_t order )
static

Add [start_pfn, start_pfn + nframes - 1] to the freelist of order order

Parameters
start_pfnphysical frame number of the first frame in the block
nframesnumber of frames in the block
orderorder of the block, must be in [0, MAX_ORDER]

Definition at line 74 of file buddy_core.c.

Referenced by buddy_init(), and populate_freelist().

+ Here is the call graph for this function:

◆ break_this_pfn()

static void break_this_pfn ( pfn_t this_pfn,
size_t this_order )
static

Definition at line 102 of file buddy_core.c.

Referenced by extract_exact_range().

+ Here is the call graph for this function:

◆ extract_exact_range()

static void extract_exact_range ( pfn_t start,
size_t nframes,
enum phyframe_state state )
static

Definition at line 123 of file buddy_core.c.

Referenced by buddy_alloc_n_exact(), and buddy_reserve_n().

+ Here is the call graph for this function:

◆ break_the_order()

static void break_the_order ( const size_t order)
static

Definition at line 216 of file buddy_core.c.

Referenced by break_the_order(), and buddy_alloc_n_exact().

+ Here is the call graph for this function:

◆ try_merge()

static bool try_merge ( const pfn_t pfn,
u8 order )
staticnodiscard

Try finding a buddy frame and merging it with the given frame.

Parameters
pfnphysical frame number
orderorder of the frame, given by log2(nframes)
Returns
true if a buddy was found and merged to a higher order freelist
false if...

Definition at line 257 of file buddy_core.c.

Referenced by buddy_free_n(), and try_merge().

+ Here is the call graph for this function:

◆ buddy_dump_all()

void buddy_dump_all ( )

Dump the state of the buddy allocator.

Definition at line 301 of file buddy_core.c.

Referenced by pmm_dump_lists().

+ Here is the call graph for this function:

◆ buddy_init()

void buddy_init ( size_t max_nframes)

Initialize the buddy allocator with the given maximum number of frames.

Parameters
max_nframesThe maximum number of frames that are addressable on the system.

Definition at line 309 of file buddy_core.c.

Referenced by pmm_init().

+ Here is the call graph for this function:

◆ buddy_reserve_n()

void buddy_reserve_n ( pfn_t pfn,
size_t nframes )

Reserve several frames at the given physical frame number.

Parameters
pfnThe physical frame number to reserve.
nframesThe number of frames to reserve.

Definition at line 323 of file buddy_core.c.

Referenced by pmm_reserve_frames().

+ Here is the call graph for this function:

◆ buddy_alloc_n_exact()

phyframe_t * buddy_alloc_n_exact ( size_t nframes)

Allocate nframes of contiguous physical memory.

Parameters
nframesThe number of frames to allocate.
Returns
phyframe_t * The first frame in the contiguous block.
Note
nframes will be allocated exactly, and each frame can be freed individually. (unlike buddy_alloc_n_compound)

Definition at line 331 of file buddy_core.c.

Referenced by pmm_allocate_frames().

+ Here is the call graph for this function:

◆ buddy_free_n()

void buddy_free_n ( pfn_t pfn,
size_t nframes )

Free nframes of contiguous physical memory.

Parameters
pfnThe physical frame number of the first frame in the contiguous block.
nframesThe number of frames to free.
Note
// !! The number of frames freed must be the same as the number of frames allocated. // TODO

Definition at line 370 of file buddy_core.c.

Referenced by pmm_free_frames().

+ Here is the call graph for this function:

Variable Documentation

◆ orders

const size_t orders[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 }
static

Definition at line 18 of file buddy_core.c.

Referenced by break_the_order(), buddy_alloc_n_exact(), buddy_init(), and try_merge().

◆ max_order

const size_t max_order = MOS_ARRAY_SIZE(orders) - 1
static

Definition at line 19 of file buddy_core.c.

Referenced by extract_exact_range().

◆ freelists

Definition at line 23 of file buddy_core.c.

◆ [struct]

◆ buddy_lock