From 7665dec3140b70079f29162c26a780310f78a4e0 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sun, 31 Mar 2024 18:54:56 +0200 Subject: [PATCH] Add from and to memory copy --- TP/TP1/rendu/config.h | 7 +++++++ TP/TP1/rendu/linked_list.h | 7 ++++++- TP/TP1/rendu/mmap.c | 31 ++++++++++++++-------------- TP/TP1/rendu/mmap.h | 7 ++++++- TP/TP1/rendu/test.c | 16 ++++++++++++--- TP/TP1/rendu/vmap.c | 42 ++++++++++++++++++++++++++++++++------ TP/TP1/rendu/vmap.h | 5 +++++ 7 files changed, 89 insertions(+), 26 deletions(-) create mode 100644 TP/TP1/rendu/config.h diff --git a/TP/TP1/rendu/config.h b/TP/TP1/rendu/config.h new file mode 100644 index 0000000..f3a822f --- /dev/null +++ b/TP/TP1/rendu/config.h @@ -0,0 +1,7 @@ +#ifndef DEF_CONFIG_H +#define DEF_CONFIG_H + +#define PAGE_SIZE 4096 +#define PAGE_COUNT 100 + +#endif \ No newline at end of file diff --git a/TP/TP1/rendu/linked_list.h b/TP/TP1/rendu/linked_list.h index cfeae99..e935a4d 100644 --- a/TP/TP1/rendu/linked_list.h +++ b/TP/TP1/rendu/linked_list.h @@ -1,3 +1,6 @@ +#ifndef DEF_LLIST_H +#define DEF_LLIST_H + #include typedef struct node { @@ -16,4 +19,6 @@ node* deleteFirst(linkedList *list); node* find(linkedList *list, int key); node* deleteElement(linkedList *list, int key); bool isEmpty(linkedList *list); -int length(linkedList *list); \ No newline at end of file +int length(linkedList *list); + +#endif \ No newline at end of file diff --git a/TP/TP1/rendu/mmap.c b/TP/TP1/rendu/mmap.c index babef1c..0f48f8a 100644 --- a/TP/TP1/rendu/mmap.c +++ b/TP/TP1/rendu/mmap.c @@ -5,31 +5,32 @@ #define CHECK_FERROR(f) { if (ferror(f)) {fprintf(stderr, "IO Error"); exit(1);} } +#include "config.h" #include "mmap.h" MMap* mmap_init(){ MMap *mmap = malloc(sizeof(MMap)); - mmap->size = 4096*100; + mmap->size = PAGE_SIZE*PAGE_COUNT; mmap->memory = malloc(mmap->size); mmap->count = 0; mmap->support = malloc(sizeof(PageInfo)); - mmap->support->data = malloc(4096); + mmap->support->data = malloc(PAGE_SIZE); mmap->free_list = createLinkedList(); - // insert 100 pages in the free list - for (int i=0; i < 100; i++) { + // insert PAGE_COUNT pages in the free list + for (int i=0; i < PAGE_COUNT; i++) { PageInfo *page = malloc(sizeof(PageInfo)); - page->data = mmap->memory + i*4096; + page->data = mmap->memory + i*PAGE_SIZE; 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++) { + for (int i=0; i < PAGE_COUNT; i++) { fwrite(&tmp, sizeof(int), 1, mmap->swap); } char tmp2 = '\0'; - for (int i=0; i < 100*4096; i++) { + for (int i=0; i < PAGE_COUNT*PAGE_SIZE; i++) { fwrite(&tmp2, 1, 1, mmap->swap); } return mmap; @@ -74,7 +75,7 @@ int move_to_swap(MMap *mmap, PageInfo *page) { fseek(mmap->swap, 0, SEEK_SET); CHECK_FERROR(mmap->swap); - for (int i=0; i < 100; i++) { + for (int i=0; i < PAGE_COUNT; i++) { fread(&key, sizeof(int), 1, mmap->swap); CHECK_FERROR(mmap->swap); @@ -85,10 +86,10 @@ int move_to_swap(MMap *mmap, PageInfo *page) { fwrite(&page->key, sizeof(int), 1, mmap->swap); CHECK_FERROR(mmap->swap); - fseek(mmap->swap, 100*sizeof(int) + i*4096, SEEK_SET); + fseek(mmap->swap, PAGE_COUNT*sizeof(int) + i*PAGE_SIZE, SEEK_SET); CHECK_FERROR(mmap->swap); - fwrite(page->data, 4096, 1, mmap->swap); + fwrite(page->data, PAGE_SIZE, 1, mmap->swap); CHECK_FERROR(mmap->swap); return 0; @@ -102,17 +103,17 @@ PageInfo* read_from_swap(MMap* mmap, PageInfo *page) { fseek(mmap->swap, 0, SEEK_SET); CHECK_FERROR(mmap->swap); - for (int i=0; i < 100; i++) { + for (int i=0; i < PAGE_COUNT; 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); + fseek(mmap->swap, PAGE_COUNT*sizeof(int) + i*PAGE_SIZE, SEEK_SET); CHECK_FERROR(mmap->swap); - fread(page->data, 4096, 1, mmap->swap); + fread(page->data, PAGE_SIZE, 1, mmap->swap); CHECK_FERROR(mmap->swap); fseek(mmap->swap, i*sizeof(int), SEEK_SET); @@ -139,7 +140,7 @@ PageInfo* page_lookup(MMap* mmap, int key) { PageInfo* swapped_page = deleteFirst(mmap->alloc_list)->data; move_to_swap(mmap, swapped_page); - memcpy(swapped_page->data, mmap->support->data, 4096); + memcpy(swapped_page->data, mmap->support->data, PAGE_SIZE); swapped_page->key = key; insertFirst(mmap->alloc_list, key, swapped_page); return page; @@ -157,7 +158,7 @@ void page_remove(MMap *mmap, int key) { } PageInfo *page_create(MMap *mmap, bool *moved_to_swap) { - if (mmap->count >= 200) + if (mmap->count >= 2*PAGE_COUNT) return NULL; PageInfo* page = page_alloc(mmap); diff --git a/TP/TP1/rendu/mmap.h b/TP/TP1/rendu/mmap.h index 1b3fc21..5a0d268 100644 --- a/TP/TP1/rendu/mmap.h +++ b/TP/TP1/rendu/mmap.h @@ -1,3 +1,6 @@ +#ifndef DEF_MMAP_H +#define DEF_MMAP_H + #include #include @@ -28,4 +31,6 @@ 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); \ No newline at end of file +PageInfo *page_create(MMap *mmap, bool *moved_to_swap); + +#endif \ No newline at end of file diff --git a/TP/TP1/rendu/test.c b/TP/TP1/rendu/test.c index 5855607..3da88cb 100644 --- a/TP/TP1/rendu/test.c +++ b/TP/TP1/rendu/test.c @@ -1,8 +1,18 @@ #include -#include "mmap.h" +#include "vmap.h" + +int main() { + // Add more tests here + VMap* vmap = vmap_init(); + + char* data1 = my_malloc(vmap, 30*sizeof(char)); + char* data2 = my_malloc(vmap, 30*sizeof(char)); + + my_copy(vmap, data1, data2, 30*sizeof(char)); + + my_free(vmap, data1); + my_free(vmap, data2); -int main(int argc, char **argv) { - // Some really cool tests! return 0; } diff --git a/TP/TP1/rendu/vmap.c b/TP/TP1/rendu/vmap.c index 70ad2e3..bc5874f 100644 --- a/TP/TP1/rendu/vmap.c +++ b/TP/TP1/rendu/vmap.c @@ -4,6 +4,7 @@ #include #include +#include "config.h" #include "vmap.h" VMap* vmap_init() { @@ -25,7 +26,7 @@ PageMapInfo* vmap_new_page(VMap* vmap) { page->nb_allocs = 0; page->swapped = moved; - vmap->index += 4096; + vmap->index += PAGE_SIZE; insertFirst(vmap->pages, page->start, page); @@ -33,7 +34,7 @@ PageMapInfo* vmap_new_page(VMap* vmap) { } int page_map_available_size(PageMapInfo* page) { - int avail = page->start + 4096 - page->cursor; + int avail = page->start + PAGE_SIZE - page->cursor; assert(avail >= 0); return avail; } @@ -49,7 +50,7 @@ PageMapInfo* vmap_get_page(VMap* vmap, void* ptr) { node* current = vmap->pages->head; while (current) { PageMapInfo* page = current->data; - if (page->start <= (int64_t)ptr && page->start+4096 > (int64_t)ptr) { + if (page->start <= (int64_t)ptr && page->start+PAGE_SIZE > (int64_t)ptr) { return page; } current = current->next; @@ -67,6 +68,11 @@ void vmap_unswap(VMap* vmap, PageMapInfo* page) { void* my_malloc(VMap* vmap, int size) { + if (size > PAGE_SIZE) { + fprintf(stderr, "my_malloc: Requesting too much space: %d > %d\n", size, PAGE_SIZE); + exit(1); + } + // Parcours de la liste pour trouver un candidat. Sinon, nouvelle page node* current = vmap->pages->head; while (current) { @@ -108,12 +114,12 @@ void my_copy(VMap* vmap, void* src, void* dst, int size) { PageMapInfo* page_map_src = vmap_get_page(vmap, src); PageMapInfo* page_map_dst = vmap_get_page(vmap, dst); - if (((int64_t)src % 4096) + size >= 4096) { + if (((int64_t)src % PAGE_SIZE) + size >= PAGE_SIZE) { fprintf(stderr, "my_copy: size not available in this src page\n"); exit(1); } - if (((int64_t)dst % 4096) + size >= 4096) { + if (((int64_t)dst % PAGE_SIZE) + size >= PAGE_SIZE) { fprintf(stderr, "my_copy: size not available in this dst page\n"); exit(1); } @@ -128,5 +134,29 @@ void my_copy(VMap* vmap, void* src, void* dst, int size) { PageInfo* page_src = page_lookup(vmap->mmap, page_map_src->key); PageInfo* page_dst = page_lookup(vmap->mmap, page_map_dst->key); - memcpy(page_dst->data, page_src->data, size); + memcpy(page_dst->data+((int64_t)src % PAGE_SIZE), page_src->data+((int64_t)dst % PAGE_SIZE), size); +} + +void vmap_copy_to_memory(struct VMap *vmap, void *src, void *dst, int size) { + PageMapInfo* page_map = vmap_get_page(vmap, src); + PageInfo* page = page_lookup(vmap->mmap, page_map->key); + + if (!page) { + fprintf(stderr, "vmap_copy_to_memory: page not found\n"); + exit(1); + } + + memcpy(page->data + ((int64_t)src % PAGE_SIZE), dst, size); +} + +void vmap_copy_from_memory(struct VMap *vmap, void *src, void *dst, int size) { + PageMapInfo* page_map = vmap_get_page(vmap, dst); + PageInfo* page = page_lookup(vmap->mmap, page_map->key); + + if (!page) { + fprintf(stderr, "vmap_copy_to_memory: page not found\n"); + exit(1); + } + + memcpy(src, page->data + ((int64_t)dst % PAGE_SIZE), size); } \ No newline at end of file diff --git a/TP/TP1/rendu/vmap.h b/TP/TP1/rendu/vmap.h index 30a29c0..f6c9128 100644 --- a/TP/TP1/rendu/vmap.h +++ b/TP/TP1/rendu/vmap.h @@ -17,11 +17,16 @@ typedef struct PageMapInfo { bool swapped; } PageMapInfo; +VMap* vmap_init(); + /** * Get page hosting a certain pointer */ PageMapInfo* vmap_get_page(VMap* vmap, void* ptr); +void vmap_copy_from_memory(struct VMap *vmap, void *src, void *dst, int size); +void vmap_copy_to_memory(struct VMap *vmap, void *src, void *dst, int size); + void* my_malloc(VMap* vmap, int size); void my_free(VMap* vmap, void* ptr); void my_copy(VMap* vmap, void* src, void* dst, int size);