#include #include #include #include #define CHECK_FERROR(f) { if (ferror(f)) {fprintf(stderr, "IO Error"); exit(1);} } #include "mmap.h" MMap* mmap_init(){ MMap *mmap = malloc(sizeof(MMap)); mmap->size = 4096*100; mmap->memory = malloc(mmap->size); mmap->count = 0; 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++) { PageInfo *page = malloc(sizeof(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(MMap *mmap) { free(mmap->memory); fclose(mmap->swap); free(mmap); remove("/tmp/myswap"); } PageInfo* page_alloc(MMap *mmap) { PageInfo *page = NULL; page = page_free(mmap); if (page) { insertFirst(mmap->alloc_list, page->key, page); mmap->count++; } return page; } 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(MMap *mmap) { return length(mmap->free_list); } int check_page_alloc(MMap *mmap) { return length(mmap->alloc_list); } int move_to_swap(MMap *mmap, 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; } PageInfo* read_from_swap(MMap* mmap, PageInfo *page) { int key = -1; fseek(mmap->swap, 0, SEEK_SET); CHECK_FERROR(mmap->swap); for (int i=0; i < 100; i++) { (void)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; } PageInfo* page_lookup(MMap* mmap, int key) { node* data = find(mmap->alloc_list, key); if (data) { return data->data; } mmap->support->key = key; PageInfo* page = read_from_swap(mmap, mmap->support); if (page) { // Move one elem from mem to swap 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(MMap *mmap, int key) { PageInfo* page = page_lookup(mmap, key); if (!page) return; deleteElement(mmap->alloc_list, key); mmap->count--; } PageInfo *page_create(MMap *mmap, bool *moved_to_swap) { if (mmap->count >= 200) return NULL; 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; }