typedef
This commit is contained in:
parent
678b8c2808
commit
1edff7956e
@ -6,16 +6,16 @@
|
||||
#include "linked_list.h"
|
||||
|
||||
|
||||
struct linkedList* createLinkedList() {
|
||||
struct linkedList *list = (struct linkedList*)malloc(sizeof(struct linkedList));
|
||||
linkedList* createLinkedList() {
|
||||
linkedList *list = (linkedList*)malloc(sizeof(linkedList));
|
||||
list->head = NULL;
|
||||
return list;
|
||||
}
|
||||
|
||||
//insert link at the first location
|
||||
void insertFirst(struct linkedList *list, int key, void *data) {
|
||||
void insertFirst(linkedList *list, int key, void *data) {
|
||||
//create a link
|
||||
struct node *link = (struct node*) malloc(sizeof(struct node));
|
||||
node *link = (node*) malloc(sizeof(node));
|
||||
|
||||
link->key = key;
|
||||
link->data = data;
|
||||
@ -28,13 +28,13 @@ void insertFirst(struct linkedList *list, int key, void *data) {
|
||||
}
|
||||
|
||||
//delete first item
|
||||
struct node* deleteFirst(struct linkedList *list) {
|
||||
node* deleteFirst(linkedList *list) {
|
||||
if (list->head == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//save reference to first link
|
||||
struct node *tempLink = list->head;
|
||||
node *tempLink = list->head;
|
||||
|
||||
//mark next to first link as first
|
||||
list->head = list->head->next;
|
||||
@ -44,10 +44,10 @@ struct node* deleteFirst(struct linkedList *list) {
|
||||
}
|
||||
|
||||
//find a link with given key
|
||||
struct node* find(struct linkedList *list, int key) {
|
||||
node* find(linkedList *list, int key) {
|
||||
|
||||
//start from the first link
|
||||
struct node* current = list->head;
|
||||
node* current = list->head;
|
||||
|
||||
//if list is empty
|
||||
if(list->head == NULL) {
|
||||
@ -71,11 +71,11 @@ struct node* find(struct linkedList *list, int key) {
|
||||
}
|
||||
|
||||
//delete a link with given key
|
||||
struct node* deleteElement(struct linkedList *list, int key) {
|
||||
node* deleteElement(linkedList *list, int key) {
|
||||
|
||||
//start from the first link
|
||||
struct node* current = list->head;
|
||||
struct node* previous = NULL;
|
||||
node* current = list->head;
|
||||
node* previous = NULL;
|
||||
|
||||
//if list is empty
|
||||
if(list->head == NULL) {
|
||||
@ -109,13 +109,13 @@ struct node* deleteElement(struct linkedList *list, int key) {
|
||||
}
|
||||
|
||||
//is list empty
|
||||
bool isEmpty(struct linkedList *list) {
|
||||
bool isEmpty(linkedList *list) {
|
||||
return list->head == NULL;
|
||||
}
|
||||
|
||||
int length(struct linkedList *list) {
|
||||
int length(linkedList *list) {
|
||||
int length = 0;
|
||||
struct node *current;
|
||||
node *current;
|
||||
|
||||
for(current = list->head; current != NULL; current = current->next) {
|
||||
length++;
|
||||
|
@ -1,19 +1,19 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
struct node {
|
||||
typedef struct node {
|
||||
void *data;
|
||||
int key;
|
||||
struct node *next;
|
||||
};
|
||||
node *next;
|
||||
} node;
|
||||
|
||||
struct linkedList {
|
||||
struct node *head;
|
||||
};
|
||||
typedef struct linkedList {
|
||||
node *head;
|
||||
} linkedList;
|
||||
|
||||
struct linkedList* createLinkedList();
|
||||
void insertFirst(struct linkedList *list, int key, void *data);
|
||||
struct node* deleteFirst(struct linkedList *list);
|
||||
struct node* find(struct linkedList *list, int key);
|
||||
struct node* deleteElement(struct linkedList *list, int key);
|
||||
bool isEmpty(struct linkedList *list);
|
||||
int length(struct linkedList *list);
|
||||
linkedList* createLinkedList();
|
||||
void insertFirst(linkedList *list, int key, void *data);
|
||||
node* deleteFirst(linkedList *list);
|
||||
node* find(linkedList *list, int key);
|
||||
node* deleteElement(linkedList *list, int key);
|
||||
bool isEmpty(linkedList *list);
|
||||
int length(linkedList *list);
|
@ -7,17 +7,17 @@
|
||||
|
||||
#include "mmap.h"
|
||||
|
||||
struct MMap* mmap_init(){
|
||||
struct MMap *mmap = malloc(sizeof(struct MMap));
|
||||
MMap* mmap_init(){
|
||||
MMap *mmap = malloc(sizeof(MMap));
|
||||
mmap->size = 4096*100;
|
||||
mmap->memory = malloc(mmap->size);
|
||||
mmap->count = 0;
|
||||
mmap->support = malloc(sizeof(struct PageInfo));
|
||||
mmap->support = malloc(sizeof(PageInfo));
|
||||
mmap->support->data = malloc(4096);
|
||||
mmap->free_list = createLinkedList();
|
||||
// insert 100 pages in the free list
|
||||
for (int i=0; i < 100; i++) {
|
||||
struct PageInfo *page = malloc(sizeof(struct PageInfo));
|
||||
PageInfo *page = malloc(sizeof(PageInfo));
|
||||
page->data = mmap->memory + i*4096;
|
||||
page->key = i;
|
||||
insertFirst(mmap->free_list, i, page);
|
||||
@ -35,15 +35,15 @@ struct MMap* mmap_init(){
|
||||
return mmap;
|
||||
}
|
||||
|
||||
void mmap_clean(struct MMap *mmap) {
|
||||
void mmap_clean(MMap *mmap) {
|
||||
free(mmap->memory);
|
||||
fclose(mmap->swap);
|
||||
free(mmap);
|
||||
remove("/tmp/myswap");
|
||||
}
|
||||
|
||||
struct PageInfo* page_alloc(struct MMap *mmap) {
|
||||
struct PageInfo *page = NULL;
|
||||
PageInfo* page_alloc(MMap *mmap) {
|
||||
PageInfo *page = NULL;
|
||||
page = page_free(mmap);
|
||||
if (page) {
|
||||
insertFirst(mmap->alloc_list, page->key, page);
|
||||
@ -52,24 +52,24 @@ struct PageInfo* page_alloc(struct MMap *mmap) {
|
||||
return page;
|
||||
}
|
||||
|
||||
struct PageInfo* page_free(struct MMap *mmap) {
|
||||
struct PageInfo *page = NULL;
|
||||
struct node *node = deleteFirst(mmap->free_list);
|
||||
PageInfo* page_free(MMap *mmap) {
|
||||
PageInfo *page = NULL;
|
||||
node *node = deleteFirst(mmap->free_list);
|
||||
if (node) {
|
||||
page = node->data;
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
int check_page_free_list(struct MMap *mmap) {
|
||||
int check_page_free_list(MMap *mmap) {
|
||||
return length(mmap->free_list);
|
||||
}
|
||||
|
||||
int check_page_alloc(struct MMap *mmap) {
|
||||
int check_page_alloc(MMap *mmap) {
|
||||
return length(mmap->alloc_list);
|
||||
}
|
||||
|
||||
int move_to_swap(struct MMap *mmap, struct PageInfo *page) {
|
||||
int move_to_swap(MMap *mmap, PageInfo *page) {
|
||||
int key = -1;
|
||||
fseek(mmap->swap, 0, SEEK_SET);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
@ -97,7 +97,7 @@ int move_to_swap(struct MMap *mmap, struct PageInfo *page) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) {
|
||||
PageInfo* read_from_swap(MMap* mmap, PageInfo *page) {
|
||||
int key = -1;
|
||||
fseek(mmap->swap, 0, SEEK_SET);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
@ -127,16 +127,16 @@ struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct PageInfo* page_lookup(struct MMap* mmap, int key) {
|
||||
struct node* data = find(mmap->alloc_list, key);
|
||||
PageInfo* page_lookup(MMap* mmap, int key) {
|
||||
node* data = find(mmap->alloc_list, key);
|
||||
if (data) {
|
||||
return data->data;
|
||||
}
|
||||
|
||||
mmap->support->key = key;
|
||||
struct PageInfo* page = read_from_swap(mmap, mmap->support);
|
||||
PageInfo* page = read_from_swap(mmap, mmap->support);
|
||||
if (page) { // Move one elem from mem to swap
|
||||
struct PageInfo* swapped_page = deleteFirst(mmap->alloc_list)->data;
|
||||
PageInfo* swapped_page = deleteFirst(mmap->alloc_list)->data;
|
||||
move_to_swap(mmap, swapped_page);
|
||||
|
||||
memcpy(swapped_page->data, mmap->support->data, 4096);
|
||||
@ -147,8 +147,8 @@ struct PageInfo* page_lookup(struct MMap* mmap, int key) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void page_remove(struct MMap *mmap, int key) {
|
||||
struct PageInfo* page = page_lookup(mmap, key);
|
||||
void page_remove(MMap *mmap, int key) {
|
||||
PageInfo* page = page_lookup(mmap, key);
|
||||
if (!page)
|
||||
return;
|
||||
|
||||
@ -156,11 +156,11 @@ void page_remove(struct MMap *mmap, int key) {
|
||||
mmap->count--;
|
||||
}
|
||||
|
||||
struct PageInfo *page_create(struct MMap *mmap, bool *moved_to_swap) {
|
||||
PageInfo *page_create(MMap *mmap, bool *moved_to_swap) {
|
||||
if (mmap->count >= 200)
|
||||
return NULL;
|
||||
|
||||
struct PageInfo* page = page_alloc(mmap);
|
||||
PageInfo* page = page_alloc(mmap);
|
||||
*moved_to_swap = false;
|
||||
|
||||
if (page)
|
||||
|
@ -3,29 +3,29 @@
|
||||
|
||||
#include "linked_list.h"
|
||||
|
||||
struct MMap {
|
||||
typedef struct MMap {
|
||||
void *memory;
|
||||
int size;
|
||||
int count;
|
||||
struct PageInfo *support;
|
||||
struct linkedList *free_list;
|
||||
struct linkedList *alloc_list;
|
||||
PageInfo *support;
|
||||
linkedList *free_list;
|
||||
linkedList *alloc_list;
|
||||
FILE *swap;
|
||||
};
|
||||
} MMap;
|
||||
|
||||
struct PageInfo {
|
||||
typedef struct PageInfo {
|
||||
int key;
|
||||
void *data;
|
||||
};
|
||||
} PageInfo;
|
||||
|
||||
struct MMap* mmap_init();
|
||||
void mmap_clean(struct MMap *mmap);
|
||||
struct PageInfo* page_alloc(struct MMap *mmap);
|
||||
struct PageInfo* page_free(struct MMap *mmap);
|
||||
int check_page_free_list(struct MMap *mmap);
|
||||
int check_page_alloc(struct MMap *mmap);
|
||||
int move_to_swap(struct MMap *mmap, struct PageInfo *page);
|
||||
struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page);
|
||||
struct PageInfo *page_lookup(struct MMap *mmap, int key);
|
||||
void page_remove(struct MMap *mmap, int key);
|
||||
struct PageInfo *page_create(struct MMap *mmap, bool *moved_to_swap);
|
||||
MMap* mmap_init();
|
||||
void mmap_clean(MMap *mmap);
|
||||
PageInfo* page_alloc(MMap *mmap);
|
||||
PageInfo* page_free(MMap *mmap);
|
||||
int check_page_free_list(MMap *mmap);
|
||||
int check_page_alloc(MMap *mmap);
|
||||
int move_to_swap(MMap *mmap, PageInfo *page);
|
||||
PageInfo* read_from_swap(MMap* mmap, PageInfo *page);
|
||||
PageInfo *page_lookup(MMap *mmap, int key);
|
||||
void page_remove(MMap *mmap, int key);
|
||||
PageInfo *page_create(MMap *mmap, bool *moved_to_swap);
|
Loading…
Reference in New Issue
Block a user