Buddy-system memory allocator implementation.
#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. | |
| 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_t * | create_buddy_table (unsigned int power_of_two) | 
| Variables | |
| static void * | buddy_base_address = 0 | 
| #define BUDDY_BLOCK_ORDER 6 | 
Definition at line 13 of file buddy.c.
Referenced by create_buddy_table(), and next_power2().
| 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.
| size | The 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().
| void buddy_free | ( | void * | ptr | ) | 
Free the given pointer (and coalesce it with its buddy if possible).
| ptr | The 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().
| 
 | static | 
This function assumes that a block of the specified order exists.
| bucket | The 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().
| 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().
| 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.
| power_of_two | The 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().
| int dump_buddy_table | ( | buddy_list_bucket_t * | buddy_master | ) | 
Definition at line 41 of file buddy.c.
References FREE, INVALID, next_power2(), buddy_list::size, buddy_list::use, and VALID.
| 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
| v | Find 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().
| 
 | static | 
Definition at line 15 of file buddy.c.
Referenced by buddy_try_merge(), and create_buddy_table().