MentOS  0.8.0
The Mentoring Operating System
Macros | Enumerations | Functions
buddysystem.c File Reference

Buddy System. More...

Macros

#define __DEBUG_HEADER__   "[BUDDY ]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_NOTICE
 Set log level.
 
#define LOW_WATERMARK_LEVEL   10
 Cache level low limit after which allocation starts.
 
#define HIGH_WATERMARK_LEVEL   70
 Cache level high limit, above it deallocation happens.
 
#define MID_WATERMARK_LEVEL   ((LOW_WATERMARK_LEVEL + HIGH_WATERMARK_LEVEL) / 2)
 Cache level midway limit.
 

Enumerations

enum  bb_flag { FREE_PAGE = 0 , ROOT_PAGE = 1 }
 Bitwise flags for identifying page types and statuses. More...
 

Functions

static void __bb_set_flag (bb_page_t *page, int flag)
 Sets the given flag in the page. More...
 
static void __bb_clear_flag (bb_page_t *page, int flag)
 Clears the given flag from the page. More...
 
static int __bb_test_flag (bb_page_t *page, int flag)
 Gets the given flag from the page. More...
 
static bb_page_t__get_page_from_base (bb_instance_t *instance, bb_page_t *base, unsigned int index)
 Returns the page at the given index, starting from the given base. More...
 
static bb_page_t__get_page_at_index (bb_instance_t *instance, unsigned int index)
 Returns the page at the given index, starting from the first page of the BB system. More...
 
static unsigned int __get_page_range (bb_instance_t *instance, bb_page_t *begin, bb_page_t *end)
 Computes the number of pages separating the two pages (begin, end). More...
 
static unsigned long __get_buddy_at_index (unsigned long page_idx, unsigned int order)
 Get the buddy index of a page. More...
 
static bb_free_area_t__get_area_of_order (bb_instance_t *instance, unsigned int order)
 Returns the pointer to the free-area manager for the given order. More...
 
static int __page_is_buddy (bb_page_t *page, unsigned int order)
 Checks if the page is FREE and has the same order. More...
 
bb_page_tbb_alloc_pages (bb_instance_t *instance, unsigned int order)
 Allocate a block of page frames of size 2^order. More...
 
void bb_free_pages (bb_instance_t *instance, bb_page_t *page)
 Free a block of page frames of size 2^order. More...
 
void buddy_system_init (bb_instance_t *instance, const char *name, void *pages_start, uint32_t bbpage_offset, uint32_t pages_stride, uint32_t pages_count)
 Initialize Buddy System. More...
 
void buddy_system_dump (bb_instance_t *instance)
 Print the size of free_list of each free_area. More...
 
unsigned long buddy_system_get_total_space (bb_instance_t *instance)
 Returns the total space for the given instance. More...
 
unsigned long buddy_system_get_free_space (bb_instance_t *instance)
 Returns the free space for the given instance. More...
 
unsigned long buddy_system_get_cached_space (bb_instance_t *instance)
 Returns the cached space for the given instance. More...
 
static void __cache_extend (bb_instance_t *instance, int count)
 Extenmds the cache of the given amount. More...
 
static void __cache_shrink (bb_instance_t *instance, int count)
 Shrinks the cache. More...
 
static bb_page_t__cached_alloc (bb_instance_t *instance)
 Allocate memory using the given cache. More...
 
static void __cached_free (bb_instance_t *instance, bb_page_t *page)
 Frees the memory of the allocated page. More...
 
bb_page_tbb_alloc_page_cached (bb_instance_t *instance)
 Alloc a page using bb cache. More...
 
void bb_free_page_cached (bb_instance_t *instance, bb_page_t *page)
 Free a page allocated with bb_alloc_page_cached. More...
 

Detailed Description

Buddy System.

Enumeration Type Documentation

◆ bb_flag

enum bb_flag

Bitwise flags for identifying page types and statuses.

Enumerator
FREE_PAGE 

Bit position that identifies when a page is free or not.

ROOT_PAGE 

Bit position that identifies when a page is the root page.

Function Documentation

◆ __bb_clear_flag()

static void __bb_clear_flag ( bb_page_t page,
int  flag 
)
inlinestatic

Clears the given flag from the page.

Parameters
pageThe page of which we want to modify the flag.
flagThe flag we want to clear.

◆ __bb_set_flag()

static void __bb_set_flag ( bb_page_t page,
int  flag 
)
inlinestatic

Sets the given flag in the page.

Parameters
pageThe page of which we want to modify the flag.
flagThe flag we want to set.

◆ __bb_test_flag()

static int __bb_test_flag ( bb_page_t page,
int  flag 
)
inlinestatic

Gets the given flag from the page.

Parameters
pageThe page of which we want to modify the flag.
flagThe flag we want to test.
Returns
1 if the bit is set, 0 otherwise.

◆ __cache_extend()

static void __cache_extend ( bb_instance_t instance,
int  count 
)
static

Extenmds the cache of the given amount.

Parameters
instancethe cache instance.
countthe amount to extend to.

◆ __cache_shrink()

static void __cache_shrink ( bb_instance_t instance,
int  count 
)
static

Shrinks the cache.

Parameters
instancethe cache instance.
countthe amount to shrink.

◆ __cached_alloc()

static bb_page_t* __cached_alloc ( bb_instance_t instance)
static

Allocate memory using the given cache.

Parameters
instancethe cache instance.
Returns
a pointer to the allocated page.

◆ __cached_free()

static void __cached_free ( bb_instance_t instance,
bb_page_t page 
)
static

Frees the memory of the allocated page.

Parameters
instancethe cache instance.
pagea pointer to the allocated page.

◆ __get_area_of_order()

static bb_free_area_t* __get_area_of_order ( bb_instance_t instance,
unsigned int  order 
)
inlinestatic

Returns the pointer to the free-area manager for the given order.

Parameters
instancethe buddysystem instance.
orderthe desired order.
Returns
pointer to the free-area manager.

◆ __get_buddy_at_index()

static unsigned long __get_buddy_at_index ( unsigned long  page_idx,
unsigned int  order 
)
inlinestatic

Get the buddy index of a page.

--------------------— xor --------------------— | page_idx ^ (1UL << order) = buddy_idx | | 1 1 0 |

| 0 1 1 |

If the bit of page_idx that corresponds to the block size, is 1, then we have to take the block on the left (0), otherwise we have to take the block on the right (1).

Parameters
page_idxthe page index.
orderthe logarithm of the size of the block.
Returns
the page index of the buddy of page.

◆ __get_page_at_index()

static bb_page_t* __get_page_at_index ( bb_instance_t instance,
unsigned int  index 
)
inlinestatic

Returns the page at the given index, starting from the first page of the BB system.

Parameters
instanceThe buddy system instance we are working with.
indexThe number of pages we want to move from the first page.
Returns
The page we found.

◆ __get_page_from_base()

static bb_page_t* __get_page_from_base ( bb_instance_t instance,
bb_page_t base,
unsigned int  index 
)
inlinestatic

Returns the page at the given index, starting from the given base.

Parameters
instanceThe buddy system instance we are working with.
baseThe base page from which we move at the given index.
indexThe number of pages we want to move from the base.
Returns
The page we found.

◆ __get_page_range()

static unsigned int __get_page_range ( bb_instance_t instance,
bb_page_t begin,
bb_page_t end 
)
inlinestatic

Computes the number of pages separating the two pages (begin, end).

Parameters
instancethe buddy system instance we are working with.
beginthe first page.
endthe second page.
Returns
The number of pages between begin and end.

◆ __page_is_buddy()

static int __page_is_buddy ( bb_page_t page,
unsigned int  order 
)
inlinestatic

Checks if the page is FREE and has the same order.

Parameters
pagethe page to check.
orderthe oder to check.
Returns
1 if the page is buddy, 0 otherwise.

◆ bb_alloc_page_cached()

bb_page_t* bb_alloc_page_cached ( bb_instance_t instance)

Alloc a page using bb cache.

Parameters
instanceBuddy system instance.
Returns
An allocated page.

◆ bb_alloc_pages()

bb_page_t* bb_alloc_pages ( bb_instance_t instance,
unsigned int  order 
)

Allocate a block of page frames of size 2^order.

Parameters
instanceA buddy system instance.
orderThe logarithm of the size of the block.
Returns
The address of the first page descriptor of the block, or NULL.

◆ bb_free_page_cached()

void bb_free_page_cached ( bb_instance_t instance,
bb_page_t page 
)

Free a page allocated with bb_alloc_page_cached.

Parameters
instanceBuddy system instance.
pageThe address of the first page descriptor of the block.

◆ bb_free_pages()

void bb_free_pages ( bb_instance_t instance,
bb_page_t page 
)

Free a block of page frames of size 2^order.

Parameters
instanceA buddy system instance.
pageThe address of the first page descriptor of the block.

◆ buddy_system_dump()

void buddy_system_dump ( bb_instance_t instance)

Print the size of free_list of each free_area.

Parameters
instanceA buddy system instance.

◆ buddy_system_get_cached_space()

unsigned long buddy_system_get_cached_space ( bb_instance_t instance)

Returns the cached space for the given instance.

Parameters
instanceA buddy system instance.
Returns
The requested total sapce.

◆ buddy_system_get_free_space()

unsigned long buddy_system_get_free_space ( bb_instance_t instance)

Returns the free space for the given instance.

Parameters
instanceA buddy system instance.
Returns
The requested total sapce.

◆ buddy_system_get_total_space()

unsigned long buddy_system_get_total_space ( bb_instance_t instance)

Returns the total space for the given instance.

Parameters
instanceA buddy system instance.
Returns
The requested total sapce.

◆ buddy_system_init()

void buddy_system_init ( bb_instance_t instance,
const char *  name,
void *  pages_start,
uint32_t  bbpage_offset,
uint32_t  pages_stride,
uint32_t  pages_count 
)

Initialize Buddy System.

Parameters
instanceA buddysystem instance.
nameThe name of the current instance (for debug purposes)
pages_startThe start address of the page structures
bbpage_offsetThe offset from the start of the whole page of the bb_page_t struct.
pages_strideThe (padded) size of the whole page structure
pages_countThe number of pages in this region