Buddy Allocator  0.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
Data Structures | Macros | Functions | Variables
buddy.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include "buddy.h"
#include "list.h"

Go to the source code of this file.

Data Structures

struct  page_t
 

Macros

#define USE_DEBUG   0
 
#define MIN_ORDER   12
 
#define MAX_ORDER   20
 
#define PAGE_SIZE   (1<<MIN_ORDER)
 
#define PAGE_TO_ADDR(page_idx)   (void *)((page_idx*PAGE_SIZE) + g_memory)
 
#define ADDR_TO_PAGE(addr)   ((unsigned long)((void *)addr - (void *)g_memory) / PAGE_SIZE)
 
#define BUDDY_ADDR(addr, o)
 
#define PDEBUG(fmt,...)
 
#define IFDEBUG(x)
 

Functions

void buddy_init ()
 
void * buddy_alloc (int size)
 
void buddy_free (void *addr)
 
void buddy_dump ()
 

Variables

struct list_head free_area [MAX_ORDER+1]
 
char g_memory [1<< MAX_ORDER]
 
page_t g_pages [(1<< MAX_ORDER)/PAGE_SIZE]
 

Macro Definition Documentation

#define ADDR_TO_PAGE (   addr)    ((unsigned long)((void *)addr - (void *)g_memory) / PAGE_SIZE)

Definition at line 32 of file buddy.c.

#define BUDDY_ADDR (   addr,
 
)
Value:
(void *)((((unsigned long)addr - (unsigned long)g_memory) ^ (1<<o)) \
+ (unsigned long)g_memory)
char g_memory[1<< MAX_ORDER]
Definition: buddy.c:63

Definition at line 35 of file buddy.c.

#define IFDEBUG (   x)

Definition at line 45 of file buddy.c.

#define MAX_ORDER   20

Definition at line 25 of file buddy.c.

#define MIN_ORDER   12

Definition at line 24 of file buddy.c.

#define PAGE_SIZE   (1<<MIN_ORDER)

Definition at line 27 of file buddy.c.

#define PAGE_TO_ADDR (   page_idx)    (void *)((page_idx*PAGE_SIZE) + g_memory)

Definition at line 29 of file buddy.c.

#define PDEBUG (   fmt,
  ... 
)

Definition at line 44 of file buddy.c.

#define USE_DEBUG   0

Buddy Allocator

For the list library usage, see http://www.mcs.anl.gov/~kazutomo/list/

Definition at line 10 of file buddy.c.

Function Documentation

void* buddy_alloc ( int  size)

Allocate a memory block.

On a memory request, the allocator returns the head of a free-list of the matching size (i.e., smallest block that satisfies the request). If the free-list of the matching block size is empty, then a larger block size will be selected. The selected (large) block is then splitted into two smaller blocks. Among the two blocks, left block will be used for allocation or be further splitted while the right block will be added to the appropriate free-list.

Parameters
sizesize in bytes
Returns
memory block address

Definition at line 110 of file buddy.c.

void buddy_dump ( )

Print the buddy system status—order oriented

print free pages in each order.

Definition at line 135 of file buddy.c.

void buddy_free ( void *  addr)

Free an allocated memory block.

Whenever a block is freed, the allocator checks its buddy. If the buddy is free as well, then the two buddies are combined to form a bigger block. This process continues until one of the buddies is not free.

Parameters
addrmemory block address to be freed

Definition at line 125 of file buddy.c.

void buddy_init ( )

Initialize the buddy system

Definition at line 79 of file buddy.c.

Variable Documentation

struct list_head free_area[MAX_ORDER+1]

Definition at line 60 of file buddy.c.

char g_memory[1<< MAX_ORDER]

Definition at line 63 of file buddy.c.

page_t g_pages[(1<< MAX_ORDER)/PAGE_SIZE]

Definition at line 66 of file buddy.c.