Add Memory*
This commit is contained in:
parent
dd8ecf50db
commit
c4c6011427
@ -7,6 +7,35 @@
|
||||
#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();
|
||||
@ -67,12 +96,13 @@ void vmap_unswap(VMap* vmap, PageMapInfo* page) {
|
||||
}
|
||||
|
||||
|
||||
void* my_malloc(VMap* vmap, int size) {
|
||||
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);
|
||||
// Parcours de la liste pour trouver un candidat. Sinon, nouvelle page
|
||||
node* current = vmap->pages->head;
|
||||
while (current) {
|
||||
@ -96,7 +126,8 @@ void* my_malloc(VMap* vmap, int size) {
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void my_free(VMap* vmap, void* ptr) {
|
||||
void my_free(Memory* mem, void* ptr, int pid) {
|
||||
VMap* vmap = memory_get(mem, pid);
|
||||
PageMapInfo* page = vmap_get_page(vmap, ptr);
|
||||
|
||||
if (!page) {
|
||||
@ -110,10 +141,16 @@ void my_free(VMap* vmap, void* ptr) {
|
||||
}
|
||||
}
|
||||
|
||||
void my_copy(VMap* vmap, void* src, void* dst, int size) {
|
||||
void my_copy(Memory* mem, void* src, void* dst, int size, int pid) {
|
||||
VMap* vmap = memory_get(mem, pid);
|
||||
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);
|
||||
@ -137,7 +174,8 @@ void my_copy(VMap* vmap, void* src, void* dst, int 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) {
|
||||
void vmap_copy_to_memory(Memory* mem, void *src, void *dst, int size, int pid) {
|
||||
VMap* vmap = memory_get(mem, pid);
|
||||
PageMapInfo* page_map = vmap_get_page(vmap, src);
|
||||
PageInfo* page = page_lookup(vmap->mmap, page_map->key);
|
||||
|
||||
@ -146,10 +184,11 @@ void vmap_copy_to_memory(struct VMap *vmap, void *src, void *dst, int size) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memcpy(page->data + ((int64_t)src % PAGE_SIZE), dst, size);
|
||||
memcpy(dst, page->data + ((int64_t)src % PAGE_SIZE), size);
|
||||
}
|
||||
|
||||
void vmap_copy_from_memory(struct VMap *vmap, void *src, void *dst, int size) {
|
||||
void vmap_copy_from_memory(Memory* mem, void *src, void *dst, int size, int pid) {
|
||||
VMap* vmap = memory_get(mem, pid);
|
||||
PageMapInfo* page_map = vmap_get_page(vmap, dst);
|
||||
PageInfo* page = page_lookup(vmap->mmap, page_map->key);
|
||||
|
||||
@ -158,5 +197,5 @@ void vmap_copy_from_memory(struct VMap *vmap, void *src, void *dst, int size) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memcpy(src, page->data + ((int64_t)dst % PAGE_SIZE), size);
|
||||
memcpy(page->data + ((int64_t)dst % PAGE_SIZE), src, size);
|
||||
}
|
@ -9,6 +9,12 @@ typedef struct VMap {
|
||||
int index; // index of virt memory
|
||||
} VMap;
|
||||
|
||||
typedef struct Memory { // List of VMaps
|
||||
struct Memory* next;
|
||||
VMap* vmap;
|
||||
int pid;
|
||||
} Memory;
|
||||
|
||||
typedef struct PageMapInfo {
|
||||
int start;
|
||||
int key; // key for mmap
|
||||
@ -17,6 +23,10 @@ typedef struct PageMapInfo {
|
||||
bool swapped;
|
||||
} PageMapInfo;
|
||||
|
||||
/**
|
||||
* If PID is (-1), a blank VMap is created and will be assigned to the 1st pid requesting it
|
||||
*/
|
||||
Memory* memory_init(int pid);
|
||||
VMap* vmap_init();
|
||||
|
||||
/**
|
||||
@ -27,15 +37,15 @@ PageMapInfo* vmap_get_page(VMap* vmap, void* ptr);
|
||||
/**
|
||||
* From implemented memory to real memory
|
||||
*/
|
||||
void vmap_copy_to_memory(struct VMap *vmap, void *src, void *dst, int size);
|
||||
void vmap_copy_to_memory(Memory* mem, void *src, void *dst, int size, int pid);
|
||||
|
||||
/**
|
||||
* From real memory to implemented memory
|
||||
*/
|
||||
void vmap_copy_from_memory(struct VMap *vmap, void *src, void *dst, int size);
|
||||
void vmap_copy_from_memory(Memory* mem, void *src, void *dst, int size, int pid);
|
||||
|
||||
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);
|
||||
void* my_malloc(Memory* mem, int size, int pid);
|
||||
void my_free(Memory* mem, void* ptr, int pid);
|
||||
void my_copy(Memory* mem, void* src, void* dst, int size, int pid);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user