Compare commits
No commits in common. "678b8c28088dd84b7fe234d3d01f439cd7a4a153" and "26d2405496900aa6d222ac6b0faafefe6590486e" have entirely different histories.
678b8c2808
...
26d2405496
@ -1,16 +0,0 @@
|
|||||||
all: build/test
|
|
||||||
|
|
||||||
build/test: test.c build/vmap.o build/mmap.o build/linked_list.o
|
|
||||||
gcc -o test test.c vmap.o mmap.o linked_list.o -w
|
|
||||||
|
|
||||||
build/mmap.o: mmap.c mmap.h linked_list.c linked_list.h
|
|
||||||
gcc -c mmap.c
|
|
||||||
|
|
||||||
build/vmap.o: vmap.c vmap.h
|
|
||||||
gcc -c vmap.c
|
|
||||||
|
|
||||||
build/linked_list.o: linked_list.c linked_list.h
|
|
||||||
gcc -c linked_list.c
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -f build/*
|
|
@ -1,125 +0,0 @@
|
|||||||
// linked list
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
#include "linked_list.h"
|
|
||||||
|
|
||||||
|
|
||||||
struct linkedList* createLinkedList() {
|
|
||||||
struct linkedList *list = (struct linkedList*)malloc(sizeof(struct linkedList));
|
|
||||||
list->head = NULL;
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
//insert link at the first location
|
|
||||||
void insertFirst(struct linkedList *list, int key, void *data) {
|
|
||||||
//create a link
|
|
||||||
struct node *link = (struct node*) malloc(sizeof(struct node));
|
|
||||||
|
|
||||||
link->key = key;
|
|
||||||
link->data = data;
|
|
||||||
|
|
||||||
//point it to old first node
|
|
||||||
link->next = list->head;
|
|
||||||
|
|
||||||
//point first to new first node
|
|
||||||
list->head = link;
|
|
||||||
}
|
|
||||||
|
|
||||||
//delete first item
|
|
||||||
struct node* deleteFirst(struct linkedList *list) {
|
|
||||||
if (list->head == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
//save reference to first link
|
|
||||||
struct node *tempLink = list->head;
|
|
||||||
|
|
||||||
//mark next to first link as first
|
|
||||||
list->head = list->head->next;
|
|
||||||
|
|
||||||
//return the deleted link
|
|
||||||
return tempLink;
|
|
||||||
}
|
|
||||||
|
|
||||||
//find a link with given key
|
|
||||||
struct node* find(struct linkedList *list, int key) {
|
|
||||||
|
|
||||||
//start from the first link
|
|
||||||
struct node* current = list->head;
|
|
||||||
|
|
||||||
//if list is empty
|
|
||||||
if(list->head == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
//navigate through list
|
|
||||||
while(current->key != key) {
|
|
||||||
|
|
||||||
//if it is last node
|
|
||||||
if(current->next == NULL) {
|
|
||||||
return NULL;
|
|
||||||
} else {
|
|
||||||
//go to next link
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//if data found, return the current Link
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
|
|
||||||
//delete a link with given key
|
|
||||||
struct node* deleteElement(struct linkedList *list, int key) {
|
|
||||||
|
|
||||||
//start from the first link
|
|
||||||
struct node* current = list->head;
|
|
||||||
struct node* previous = NULL;
|
|
||||||
|
|
||||||
//if list is empty
|
|
||||||
if(list->head == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
//navigate through list
|
|
||||||
while(current->key != key) {
|
|
||||||
|
|
||||||
//if it is last node
|
|
||||||
if(current->next == NULL) {
|
|
||||||
return NULL;
|
|
||||||
} else {
|
|
||||||
//store reference to current link
|
|
||||||
previous = current;
|
|
||||||
//move to next link
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//found a match, update the link
|
|
||||||
if(current == list->head) {
|
|
||||||
//change first to point to next link
|
|
||||||
list->head = list->head->next;
|
|
||||||
} else {
|
|
||||||
//bypass the current link
|
|
||||||
previous->next = current->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
|
|
||||||
//is list empty
|
|
||||||
bool isEmpty(struct linkedList *list) {
|
|
||||||
return list->head == NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int length(struct linkedList *list) {
|
|
||||||
int length = 0;
|
|
||||||
struct node *current;
|
|
||||||
|
|
||||||
for(current = list->head; current != NULL; current = current->next) {
|
|
||||||
length++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return length;
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
struct node {
|
|
||||||
void *data;
|
|
||||||
int key;
|
|
||||||
struct node *next;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct linkedList {
|
|
||||||
struct node *head;
|
|
||||||
};
|
|
||||||
|
|
||||||
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);
|
|
@ -1,180 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#define CHECK_FERROR(f) { if (ferror(f)) {fprintf(stderr, "IO Error"); exit(1);} }
|
|
||||||
|
|
||||||
#include "mmap.h"
|
|
||||||
|
|
||||||
struct MMap* mmap_init(){
|
|
||||||
struct MMap *mmap = malloc(sizeof(struct MMap));
|
|
||||||
mmap->size = 4096*100;
|
|
||||||
mmap->memory = malloc(mmap->size);
|
|
||||||
mmap->count = 0;
|
|
||||||
mmap->support = malloc(sizeof(struct 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));
|
|
||||||
page->data = mmap->memory + i*4096;
|
|
||||||
page->key = i;
|
|
||||||
insertFirst(mmap->free_list, i, page);
|
|
||||||
}
|
|
||||||
mmap->alloc_list = createLinkedList();
|
|
||||||
mmap->swap = fopen("/tmp/myswap", "wb+");
|
|
||||||
int tmp = -1;
|
|
||||||
for (int i=0; i < 100; i++) {
|
|
||||||
fwrite(&tmp, sizeof(int), 1, mmap->swap);
|
|
||||||
}
|
|
||||||
char tmp2 = '\0';
|
|
||||||
for (int i=0; i < 100*4096; i++) {
|
|
||||||
fwrite(&tmp2, 1, 1, mmap->swap);
|
|
||||||
}
|
|
||||||
return mmap;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mmap_clean(struct MMap *mmap) {
|
|
||||||
free(mmap->memory);
|
|
||||||
fclose(mmap->swap);
|
|
||||||
free(mmap);
|
|
||||||
remove("/tmp/myswap");
|
|
||||||
}
|
|
||||||
|
|
||||||
struct PageInfo* page_alloc(struct MMap *mmap) {
|
|
||||||
struct PageInfo *page = NULL;
|
|
||||||
page = page_free(mmap);
|
|
||||||
if (page) {
|
|
||||||
insertFirst(mmap->alloc_list, page->key, page);
|
|
||||||
mmap->count++;
|
|
||||||
}
|
|
||||||
return page;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct PageInfo* page_free(struct MMap *mmap) {
|
|
||||||
struct PageInfo *page = NULL;
|
|
||||||
struct node *node = deleteFirst(mmap->free_list);
|
|
||||||
if (node) {
|
|
||||||
page = node->data;
|
|
||||||
}
|
|
||||||
return page;
|
|
||||||
}
|
|
||||||
|
|
||||||
int check_page_free_list(struct MMap *mmap) {
|
|
||||||
return length(mmap->free_list);
|
|
||||||
}
|
|
||||||
|
|
||||||
int check_page_alloc(struct MMap *mmap) {
|
|
||||||
return length(mmap->alloc_list);
|
|
||||||
}
|
|
||||||
|
|
||||||
int move_to_swap(struct MMap *mmap, struct PageInfo *page) {
|
|
||||||
int key = -1;
|
|
||||||
fseek(mmap->swap, 0, SEEK_SET);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
for (int i=0; i < 100; i++) {
|
|
||||||
fread(&key, sizeof(int), 1, mmap->swap);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
if (key == -1) {
|
|
||||||
fseek(mmap->swap, i*sizeof(int), SEEK_SET);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
fwrite(&page->key, sizeof(int), 1, mmap->swap);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
fseek(mmap->swap, 100*sizeof(int) + i*4096, SEEK_SET);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
fwrite(page->data, 4096, 1, mmap->swap);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) {
|
|
||||||
int key = -1;
|
|
||||||
fseek(mmap->swap, 0, SEEK_SET);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
for (int i=0; i < 100; i++) {
|
|
||||||
int ret = fread(&key, sizeof(int), 1, mmap->swap);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
if (key != page->key)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
fseek(mmap->swap, 100*sizeof(int) + i*4096, SEEK_SET);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
fread(page->data, 4096, 1, mmap->swap);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
fseek(mmap->swap, i*sizeof(int), SEEK_SET);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
int tmp = -1;
|
|
||||||
fwrite(&tmp, sizeof(int), 1, mmap->swap);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
return page;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct PageInfo* page_lookup(struct MMap* mmap, int key) {
|
|
||||||
struct 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);
|
|
||||||
if (page) { // Move one elem from mem to swap
|
|
||||||
struct PageInfo* swapped_page = deleteFirst(mmap->alloc_list)->data;
|
|
||||||
move_to_swap(mmap, swapped_page);
|
|
||||||
|
|
||||||
memcpy(swapped_page->data, mmap->support->data, 4096);
|
|
||||||
swapped_page->key = key;
|
|
||||||
insertFirst(mmap->alloc_list, key, swapped_page);
|
|
||||||
return page;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void page_remove(struct MMap *mmap, int key) {
|
|
||||||
struct PageInfo* page = page_lookup(mmap, key);
|
|
||||||
if (!page)
|
|
||||||
return;
|
|
||||||
|
|
||||||
deleteElement(mmap->alloc_list, key);
|
|
||||||
mmap->count--;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct PageInfo *page_create(struct MMap *mmap, bool *moved_to_swap) {
|
|
||||||
if (mmap->count >= 200)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
struct PageInfo* page = page_alloc(mmap);
|
|
||||||
*moved_to_swap = false;
|
|
||||||
|
|
||||||
if (page)
|
|
||||||
return page;
|
|
||||||
|
|
||||||
// Another page has been moved to swap
|
|
||||||
*moved_to_swap = true;
|
|
||||||
|
|
||||||
page = deleteFirst(mmap->alloc_list)->data;
|
|
||||||
move_to_swap(mmap, page);
|
|
||||||
// Reuse swapped page for the new one
|
|
||||||
page->key = mmap->count;
|
|
||||||
mmap->count++;
|
|
||||||
insertFirst(mmap->alloc_list, page->key, page);
|
|
||||||
|
|
||||||
return page;
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
#include "linked_list.h"
|
|
||||||
|
|
||||||
struct MMap {
|
|
||||||
void *memory;
|
|
||||||
int size;
|
|
||||||
int count;
|
|
||||||
struct PageInfo *support;
|
|
||||||
struct linkedList *free_list;
|
|
||||||
struct linkedList *alloc_list;
|
|
||||||
FILE *swap;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct PageInfo {
|
|
||||||
int key;
|
|
||||||
void *data;
|
|
||||||
};
|
|
||||||
|
|
||||||
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);
|
|
@ -1,8 +0,0 @@
|
|||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "mmap.h"
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
|
||||||
// Some really cool tests!
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
void *my_malloc(int size);
|
|
||||||
void my_free(void *data);
|
|
||||||
void my_copy(void *mem, void *data, int size);
|
|
BIN
TP/TP1/sujet.pdf
BIN
TP/TP1/sujet.pdf
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user