Add Memory*
This commit is contained in:
parent
dd8ecf50db
commit
c4c6011427
@ -7,6 +7,35 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "vmap.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_init() {
|
||||||
VMap* vmap = (VMap*)malloc(sizeof(VMap));
|
VMap* vmap = (VMap*)malloc(sizeof(VMap));
|
||||||
vmap->mmap = mmap_init();
|
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) {
|
if (size > PAGE_SIZE) {
|
||||||
fprintf(stderr, "my_malloc: Requesting too much space: %d > %d\n", size, PAGE_SIZE);
|
fprintf(stderr, "my_malloc: Requesting too much space: %d > %d\n", size, PAGE_SIZE);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VMap* vmap = memory_get(mem, pid);
|
||||||
// Parcours de la liste pour trouver un candidat. Sinon, nouvelle page
|
// Parcours de la liste pour trouver un candidat. Sinon, nouvelle page
|
||||||
node* current = vmap->pages->head;
|
node* current = vmap->pages->head;
|
||||||
while (current) {
|
while (current) {
|
||||||
@ -96,7 +126,8 @@ void* my_malloc(VMap* vmap, int size) {
|
|||||||
return ptr;
|
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);
|
PageMapInfo* page = vmap_get_page(vmap, ptr);
|
||||||
|
|
||||||
if (!page) {
|
if (!page) {
|
||||||
@ -110,9 +141,15 @@ 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) {
|
||||||
PageMapInfo* page_map_src = vmap_get_page(vmap, src);
|
VMap* vmap = memory_get(mem, pid);
|
||||||
PageMapInfo* page_map_dst = vmap_get_page(vmap, dst);
|
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) {
|
if (((int64_t)src % PAGE_SIZE) + size >= PAGE_SIZE) {
|
||||||
fprintf(stderr, "my_copy: size not available in this src page\n");
|
fprintf(stderr, "my_copy: size not available in this src page\n");
|
||||||
@ -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);
|
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);
|
PageMapInfo* page_map = vmap_get_page(vmap, src);
|
||||||
PageInfo* page = page_lookup(vmap->mmap, page_map->key);
|
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);
|
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);
|
PageMapInfo* page_map = vmap_get_page(vmap, dst);
|
||||||
PageInfo* page = page_lookup(vmap->mmap, page_map->key);
|
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);
|
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
|
int index; // index of virt memory
|
||||||
} VMap;
|
} VMap;
|
||||||
|
|
||||||
|
typedef struct Memory { // List of VMaps
|
||||||
|
struct Memory* next;
|
||||||
|
VMap* vmap;
|
||||||
|
int pid;
|
||||||
|
} Memory;
|
||||||
|
|
||||||
typedef struct PageMapInfo {
|
typedef struct PageMapInfo {
|
||||||
int start;
|
int start;
|
||||||
int key; // key for mmap
|
int key; // key for mmap
|
||||||
@ -17,6 +23,10 @@ typedef struct PageMapInfo {
|
|||||||
bool swapped;
|
bool swapped;
|
||||||
} PageMapInfo;
|
} 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();
|
VMap* vmap_init();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,15 +37,15 @@ PageMapInfo* vmap_get_page(VMap* vmap, void* ptr);
|
|||||||
/**
|
/**
|
||||||
* From implemented memory to real memory
|
* 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
|
* 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_malloc(Memory* mem, int size, int pid);
|
||||||
void my_free(VMap* vmap, void* ptr);
|
void my_free(Memory* mem, void* ptr, int pid);
|
||||||
void my_copy(VMap* vmap, void* src, void* dst, int size);
|
void my_copy(Memory* mem, void* src, void* dst, int size, int pid);
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user