Buddy Allocator  0.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
buddy.c
Go to the documentation of this file.
1 
7 /**************************************************************************
8  * Conditional Compilation Options
9  **************************************************************************/
10 #define USE_DEBUG 0
11 
12 /**************************************************************************
13  * Included Files
14  **************************************************************************/
15 #include <stdio.h>
16 #include <stdlib.h>
17 
18 #include "buddy.h"
19 #include "list.h"
20 
21 /**************************************************************************
22  * Public Definitions
23  **************************************************************************/
24 #define MIN_ORDER 12
25 #define MAX_ORDER 20
26 
27 #define PAGE_SIZE (1<<MIN_ORDER)
28 /* page index to address */
29 #define PAGE_TO_ADDR(page_idx) (void *)((page_idx*PAGE_SIZE) + g_memory)
30 
31 /* address to page index */
32 #define ADDR_TO_PAGE(addr) ((unsigned long)((void *)addr - (void *)g_memory) / PAGE_SIZE)
33 
34 /* find buddy address */
35 #define BUDDY_ADDR(addr, o) (void *)((((unsigned long)addr - (unsigned long)g_memory) ^ (1<<o)) \
36  + (unsigned long)g_memory)
37 
38 #if USE_DEBUG == 1
39 # define PDEBUG(fmt, ...) \
40  fprintf(stderr, "%s(), %s:%d: " fmt, \
41  __func__, __FILE__, __LINE__, ##__VA_ARGS__)
42 # define IFDEBUG(x) x
43 #else
44 # define PDEBUG(fmt, ...)
45 # define IFDEBUG(x)
46 #endif
47 
48 /**************************************************************************
49  * Public Types
50  **************************************************************************/
51 typedef struct {
52  struct list_head list;
53  /* TODO: DECLARE NECESSARY MEMBER VARIABLES */
54 } page_t;
55 
56 /**************************************************************************
57  * Global Variables
58  **************************************************************************/
59 /* free lists*/
61 
62 /* memory area */
64 
65 /* page structures */
67 
68 /**************************************************************************
69  * Public Function Prototypes
70  **************************************************************************/
71 
72 /**************************************************************************
73  * Local Functions
74  **************************************************************************/
75 
79 void buddy_init()
80 {
81  int i;
82  int n_pages = (1<<MAX_ORDER) / PAGE_SIZE;
83  for (i = 0; i < n_pages; i++) {
84  /* TODO: INITIALIZE PAGE STRUCTURES */
85  }
86 
87  /* initialize freelist */
88  for (i = MIN_ORDER; i <= MAX_ORDER; i++) {
90  }
91 
92  /* add the entire memory as a freeblock */
93  list_add(&g_pages[0].list, &free_area[MAX_ORDER]);
94 }
95 
110 void *buddy_alloc(int size)
111 {
112  /* TODO: IMPLEMENT THIS FUNCTION */
113  return NULL;
114 }
115 
125 void buddy_free(void *addr)
126 {
127  /* TODO: IMPLEMENT THIS FUNCTION */
128 }
129 
136 {
137  int o;
138  for (o = MIN_ORDER; o <= MAX_ORDER; o++) {
139  struct list_head *pos;
140  int cnt = 0;
141  list_for_each(pos, &free_area[o]) {
142  cnt++;
143  }
144  printf("%d:%dK ", cnt, (1<<o)/1024);
145  }
146  printf("\n");
147 }
#define PAGE_SIZE
Definition: buddy.c:27
static void list_add(struct list_head *new, struct list_head *head)
Definition: list.h:58
#define list_for_each(pos, head)
Definition: list.h:198
char g_memory[1<< MAX_ORDER]
Definition: buddy.c:63
Definition: list.h:21
void buddy_dump()
Definition: buddy.c:135
#define MAX_ORDER
Definition: buddy.c:25
#define MIN_ORDER
Definition: buddy.c:24
page_t g_pages[(1<< MAX_ORDER)/PAGE_SIZE]
Definition: buddy.c:66
void * buddy_alloc(int size)
Definition: buddy.c:110
void buddy_free(void *addr)
Definition: buddy.c:125
Definition: buddy.c:51
#define INIT_LIST_HEAD(ptr)
Definition: list.h:30
void buddy_init()
Definition: buddy.c:79
struct list_head free_area[MAX_ORDER+1]
Definition: buddy.c:60