ROSS
Macros | Functions | Variables
buddy.c File Reference

Buddy-system memory allocator implementation. More...

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <ross.h>
#include "buddy.h"

Go to the source code of this file.

Macros

#define BUDDY_BLOCK_ORDER   6
 Minimum block order. More...
 

Functions

unsigned int next_power2 (unsigned int v)
 
int dump_buddy_table (buddy_list_bucket_t *buddy_master)
 
int buddy_try_merge (buddy_list_t *blt)
 
void buddy_free (void *ptr)
 
static void buddy_split (buddy_list_bucket_t *bucket)
 
void * buddy_alloc (unsigned size)
 
buddy_list_bucket_tcreate_buddy_table (unsigned int power_of_two)
 

Variables

static void * buddy_base_address = 0
 

Macro Definition Documentation

#define BUDDY_BLOCK_ORDER   6

Definition at line 13 of file buddy.c.

Referenced by create_buddy_table(), and next_power2().

Function Documentation

void* buddy_alloc ( unsigned  size)

Find the smallest block that will contain size and return it. Note this returns the memory allocated and usable, not the entire buffer. This may involve breaking up larger blocks.

Parameters
sizeThe size of the data this allocation must be able to hold.

Definition at line 234 of file buddy.c.

References buddy_split(), buddy_list_bucket::count, g_tw_buddy_master, INVALID, buddy_list_bucket::is_valid, next_power2(), buddy_list_bucket::order, tw_error(), TW_LOC, buddy_list::use, and USED.

Referenced by tw_snapshot_delta().

Here is the call graph for this function:

Here is the caller graph for this function:

void buddy_free ( void *  ptr)

Free the given pointer (and coalesce it with its buddy if possible).

Parameters
ptrThe pointer to free.

Definition at line 137 of file buddy.c.

References buddy_try_merge(), buddy_list_bucket::count, FREE, g_tw_buddy_master, buddy_list_bucket::order, buddy_list::size, TW_LOC, tw_printf(), buddy_list::use, and USED.

Referenced by tw_event_free(), tw_event_rollback(), and tw_eventq_push_list().

Here is the call graph for this function:

Here is the caller graph for this function:

static void buddy_split ( buddy_list_bucket_t bucket)
static

This function assumes that a block of the specified order exists.

Parameters
bucketThe bucket containing a block we intend to split.

Definition at line 197 of file buddy.c.

References buddy_list_bucket::count, FREE, buddy_list_bucket::order, buddy_list::size, and buddy_list::use.

Referenced by buddy_alloc().

Here is the caller graph for this function:

int buddy_try_merge ( buddy_list_t blt)

See if we can merge. If we can, see if we can merge again.

Definition at line 80 of file buddy.c.

References buddy_base_address, buddy_list_bucket::count, FREE, g_tw_buddy_master, buddy_list_bucket::order, buddy_list::size, and buddy_list::use.

Referenced by buddy_free().

Here is the caller graph for this function:

buddy_list_bucket_t* create_buddy_table ( unsigned int  power_of_two)

Pass in the power of two e.g., passing 5 will yield 2^5 = 32.

Parameters
power_of_twoThe largest "order" this table will support.

<Minimum block order

<Minimum block order

<Minimum block order

<Minimum block order

Definition at line 288 of file buddy.c.

References buddy_base_address, BUDDY_BLOCK_ORDER, buddy_list_bucket::count, FREE, INVALID, buddy_list_bucket::is_valid, buddy_list_bucket::order, buddy_list::size, tw_calloc(), TW_LOC, buddy_list::use, and VALID.

Referenced by setup_pes().

Here is the call graph for this function:

Here is the caller graph for this function:

int dump_buddy_table ( buddy_list_bucket_t buddy_master)

Definition at line 41 of file buddy.c.

References buddy_list_bucket::count, FREE, INVALID, buddy_list_bucket::is_valid, next_power2(), buddy_list_bucket::order, buddy_list::size, buddy_list::use, and VALID.

Here is the call graph for this function:

unsigned int next_power2 ( unsigned int  v)

Finds the next power of 2 or, if v is a power of 2, return that. From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2

Parameters
vFind a power of 2 >= v.

<Minimum block order

<Minimum block order

Definition at line 23 of file buddy.c.

References BUDDY_BLOCK_ORDER.

Referenced by buddy_alloc(), and dump_buddy_table().

Here is the caller graph for this function:

Variable Documentation

void* buddy_base_address = 0
static

Definition at line 15 of file buddy.c.

Referenced by buddy_try_merge(), and create_buddy_table().