1
0

Add simple page allocator and doubly-linked list implementation.

This commit is contained in:
2012-09-17 23:27:11 -04:00
parent b531496f8e
commit 0b3865d16a
5 changed files with 223 additions and 0 deletions

40
include/list.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef LIST_H
#define LIST_H
/*
* Assuming ptr is a pointer to a member variable of type parent_type, and ptr
* is named member in parent_type, find the address of the struct which owns
* ptr.
*/
#define container(ptr, parent_type, member) \
((parent_type *) ((char *)ptr - (char *)&((parent_type *)0)->member))
/*
* Given a pointer to a struct (head) of type dlist_node, which points to a
* list of type parent_type, and a pointer of that type (it), this macro will
* execute the contained instructions once for each list element, with 'it' set
* to each element in the list, in turn.
*/
#define for_each_list(it, head, parent_type, member) \
for (it = container((head)->next, parent_type, member); it->member.next != (head); it = container(it->member.next, parent_type, member))
#define for_each_list_noinit(it, head, parent_type, member) \
for (; it->member.next != (head); it = container(it->member.next, parent_type, member))
struct dlist_node {
struct dlist_node *next, *prev;
};
void init_list(struct dlist_node *n);
int list_empty(struct dlist_node *n);
//functions for dealing with single list elements
void insert_after(struct dlist_node *n, struct dlist_node *to_add);
void insert_before(struct dlist_node *n, struct dlist_node *to_add);
void remove(struct dlist_node *to_remove);
//functions for dealing with one or more elements
void remove_splice(struct dlist_node *from, struct dlist_node *to);
void insert_splice_before(struct dlist_node *n, struct dlist_node *first, struct dlist_node *last);
#endif /* LIST_H */

20
include/mm.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef MM_H
#define MM_H
#include <list.h>
#define MM_PAGE_SIZE 4096
struct page {
void *address;
struct dlist_node list;
char free;
};
void mm_init();
void mm_add_free_region(void *start, void *end);
struct page* mm_get_free_pages(unsigned int power);
int mm_put_free_pages(struct page *p);
#endif /* MM_H */