212 lines
5.8 KiB
C
212 lines
5.8 KiB
C
#include <inttypes.h>
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#include <string.h>
|
|
|
|
#include "config.h"
|
|
#include "vmap.h"
|
|
|
|
Memory* memory_init(int pid) {
|
|
Memory* mem = (Memory*)malloc(sizeof(Memory));
|
|
mem->next = NULL;
|
|
mem->pid = pid;
|
|
mem->vmap = vmap_init();
|
|
|
|
return mem;
|
|
}
|
|
|
|
VMap* memory_get(Memory* mem, int pid) {
|
|
if (!mem)
|
|
return NULL;
|
|
|
|
if (mem->pid == pid)
|
|
return mem->vmap;
|
|
|
|
if (mem->pid == -1) {
|
|
mem->pid = pid;
|
|
return mem->vmap;
|
|
}
|
|
|
|
if (!(mem->next)) {
|
|
mem->next = memory_init(pid);
|
|
return mem->next->vmap;
|
|
}
|
|
|
|
return memory_get(mem->next, pid);
|
|
}
|
|
|
|
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(Memory* mem, int size, int pid) {
|
|
if (size > PAGE_SIZE) {
|
|
fprintf(stderr, "my_malloc: Requesting too much space: %d > %d\n", size, PAGE_SIZE);
|
|
exit(1);
|
|
}
|
|
|
|
VMap* vmap = memory_get(mem, pid);
|
|
pthread_mutex_lock(&vmap->lock);
|
|
// 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++;
|
|
|
|
pthread_mutex_unlock(&vmap->lock);
|
|
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++;
|
|
|
|
pthread_mutex_unlock(&vmap->lock);
|
|
return ptr;
|
|
}
|
|
|
|
void my_free(Memory* mem, void* ptr, int pid) {
|
|
VMap* vmap = memory_get(mem, pid);
|
|
pthread_mutex_lock(&vmap->lock);
|
|
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);
|
|
}
|
|
pthread_mutex_unlock(&vmap->lock);
|
|
}
|
|
|
|
void my_copy(Memory* mem, void* src, void* dst, int size, int pid) {
|
|
VMap* vmap = memory_get(mem, pid);
|
|
pthread_mutex_lock(&vmap->lock);
|
|
PageMapInfo* page_map_src = vmap_get_page(vmap, src);
|
|
PageMapInfo* page_map_dst = vmap_get_page(vmap, dst);
|
|
|
|
if (!page_map_src || !page_map_dst) {
|
|
fprintf(stderr, "my_copy: segmentation fault\n"); // Trying to access bad page
|
|
exit(1);
|
|
}
|
|
|
|
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_src->data+((int64_t)dst % PAGE_SIZE), page_dst->data+((int64_t)src % PAGE_SIZE), size);
|
|
pthread_mutex_unlock(&vmap->lock);
|
|
}
|
|
|
|
void vmap_copy_to_memory(Memory* mem, void *src, void *dst, int size, int pid) {
|
|
VMap* vmap = memory_get(mem, pid);
|
|
pthread_mutex_lock(&vmap->lock);
|
|
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(dst, page->data + ((int64_t)src % PAGE_SIZE), size);
|
|
pthread_mutex_unlock(&vmap->lock);
|
|
}
|
|
|
|
void vmap_copy_from_memory(Memory* mem, void *src, void *dst, int size, int pid) {
|
|
VMap* vmap = memory_get(mem, pid);
|
|
pthread_mutex_lock(&vmap->lock);
|
|
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(page->data + ((int64_t)dst % PAGE_SIZE), src, size);
|
|
pthread_mutex_unlock(&vmap->lock);
|
|
} |