#include #include #include #include #include #include "config.h" #include "vmap.h" VMap* vmap_init() { VMap* vmap = (VMap*)malloc(sizeof(VMap)); vmap->mmap = mmap_init(); vmap->pages = createLinkedList(); vmap->index = 0; return vmap; } PageMapInfo* vmap_new_page(VMap* vmap) { PageMapInfo* page = (PageMapInfo*)malloc(sizeof(PageMapInfo)); bool moved; page->key = page_create(vmap->mmap, &moved)->key; page->start = vmap->index; page->cursor = 0; page->nb_allocs = 0; page->swapped = moved; vmap->index += PAGE_SIZE; insertFirst(vmap->pages, page->start, page); return page; } int page_map_available_size(PageMapInfo* page) { int avail = page->start + PAGE_SIZE - page->cursor; assert(avail >= 0); return avail; } void vmap_free_page(VMap* vmap, PageMapInfo* page) { deleteElement(vmap->pages, page->key); page_remove(vmap->mmap, page->key); free(page); } 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+PAGE_SIZE > (int64_t)ptr) { return page; } current = current->next; } return NULL; } void vmap_unswap(VMap* vmap, PageMapInfo* page) { if (!page->swapped) return; read_from_swap(vmap->mmap, page_lookup(vmap->mmap, page->key)); page->swapped = false; } 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) { PageMapInfo* page = current->data; if (page_map_available_size(page) >= size) { void* ptr = (void*)((uint64_t)page->start+page->cursor); page->cursor += size; page->nb_allocs++; return ptr; } current = current->next; } PageMapInfo* page = vmap_new_page(vmap); void* ptr = (void*)((uint64_t)page->start+page->cursor); page->cursor += size; page->nb_allocs++; return ptr; } void my_free(VMap* vmap, void* ptr) { PageMapInfo* page = vmap_get_page(vmap, ptr); if (!page) { fprintf(stderr, "my_free: Double free or memory corrupted\n"); exit(1); } page->nb_allocs--; if (page->nb_allocs == 0) { vmap_free_page(vmap, page); } } 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 % PAGE_SIZE) + size >= PAGE_SIZE) { fprintf(stderr, "my_copy: size not available in this src page\n"); exit(1); } if (((int64_t)dst % PAGE_SIZE) + size >= PAGE_SIZE) { fprintf(stderr, "my_copy: size not available in this dst page\n"); exit(1); } if (page_map_src->swapped) { vmap_unswap(vmap, page_map_src); } if (page_map_dst->swapped) { vmap_unswap(vmap, page_map_dst); } 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+((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); }