Compare commits

..

No commits in common. "130eaa54f0a3d2e4b90094014d61bafda19421d0" and "df2ed3acda93691d264b5450535a04a4e7ba04b3" have entirely different histories.

9 changed files with 83 additions and 326 deletions

View File

@ -1,20 +1,10 @@
SRC = $(wildcard *.c) SRC = $(wildcard *.c)
OBJ = $(filter-out build/main.o build/test.o, $(SRC:%.c=build/%.o)) OBJ = $(filter-out build/main.o build/test.o, $(SRC:%.c=build/%.o))
FLAGS = -Wall -Wextra -g -O3 FLAGS = -Wall -Wextra -g -O3
TEST_SRCDIR = tests
FAIL_TESTS_SRC += $(wildcard $(TEST_SRCDIR)/fail/*.c) all: build/test
SUCC_TESTS_SRC += $(wildcard $(TEST_SRCDIR)/success/*.c)
TESTS_OBJ = $(FAIL_TESTS_SRC:$(TEST_SRCDIR)/fail/%.c=build/test-fail-%) $(SUCC_TESTS_SRC:$(TEST_SRCDIR)/success/%.c=build/test-success-%)
all: build-tests build/test: test.c $(OBJ)
build-tests: $(TESTS_OBJ)
build/test-fail-%: tests/fail/%.c $(OBJ)
gcc $^ -o $@ $(FLAGS)
build/test-success-%: tests/success/%.c $(OBJ)
gcc $^ -o $@ $(FLAGS) gcc $^ -o $@ $(FLAGS)
build/%.o: %.c %.h build/%.o: %.c %.h

View File

@ -1,14 +0,0 @@
# TP2
## Tests
Exécuter tous les tests et s'arrêter quand l'un produit un résultat différent de celui attendu
```bash
./make.sh
```
## Implémentation
Pour les différents processus:
- une structure de `(VMap, pid) list` est utilisée pour gérer les VMaps reliées à chaque processus
- Dès qu'une VMap est récupérée, elle est verrouillée (Un mutex par VMap), ne fonctionne pas

View File

@ -1,25 +0,0 @@
#!/bin/bash
test_failed () {
exit 1
}
make build-tests
for file in build/test-fail-*; do
echo "===== $file =====";
$file &>/dev/null
if [ $? -eq 0 ]; then
echo "$file should have failed"
test_failed
fi
done
for file in build/test-success-*; do
echo "===== $file =====";
$file &>/dev/null
if [ $? -ne 0 ]; then
echo "$file failed"
test_failed
fi
done

View File

@ -1,11 +1,11 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "../../vmap.h" #include "vmap.h"
int main() { int main() {
Memory* mem = memory_init(-1); // Add more tests here
int pid = 12; VMap* vmap = vmap_init();
char* real1 = malloc(sizeof(char)*32); char* real1 = malloc(sizeof(char)*32);
real1[0] = 'h'; real1[0] = 'h';
@ -16,18 +16,18 @@ int main() {
real1[5] = '!'; real1[5] = '!';
real1[6] = '\0'; real1[6] = '\0';
char* data1 = my_malloc(mem, 32*sizeof(char), pid); char* data1 = my_malloc(vmap, 32*sizeof(char));
char* data2 = my_malloc(mem, 32*sizeof(char), pid); char* data2 = my_malloc(vmap, 32*sizeof(char));
vmap_copy_from_memory(mem, real1, data1, 8, pid); vmap_copy_from_memory(vmap, real1, data1, 8);
my_copy(mem, data1, data2, 32*sizeof(char), pid); my_copy(vmap, data1, data2, 32*sizeof(char));
char* real = malloc(sizeof(char)*32); char* real = malloc(sizeof(char)*32);
vmap_copy_to_memory(mem, data2, real, 7, pid); vmap_copy_to_memory(vmap, data2, real, 7);
if (strcmp(real, real1)) { if (strcmp(real1, real1)) {
printf("%d\n", strcmp(real, real1)); printf("%d\n", strcmp(real, real1));
fprintf(stderr, "copy test failed\n"); fprintf(stderr, "copy test failed\n");
return 1; return 1;
@ -36,8 +36,8 @@ int main() {
free(real); free(real);
free(real1); free(real1);
my_free(mem, data1, pid); my_free(vmap, data1);
my_free(mem, data2, pid); my_free(vmap, data2);
return 0; return 0;
} }

View File

@ -1,48 +0,0 @@
#include <string.h>
#include <stdlib.h>
#include "../../vmap.h"
int main() {
Memory* mem = memory_init(-1);
int pid1 = 12;
int pid2 = 24;
char* real1 = malloc(sizeof(char)*32);
real1[0] = 'h';
real1[1] = 'e';
real1[2] = 'l';
real1[3] = 'l';
real1[4] = 'o';
real1[5] = '!';
real1[6] = '\0';
char* data1 = my_malloc(mem, 32*sizeof(char), pid1);
char* data2 = my_malloc(mem, 32*sizeof(char), pid2);
vmap_copy_from_memory(mem, real1, data1, 8, pid1);
my_copy(mem, data1, data2, 32*sizeof(char), pid1); //! src and dst have different PIDs
char* real = malloc(sizeof(char)*32);
vmap_copy_to_memory(mem, data2, real, 7, pid2);
if (strcmp(real, real1)) {
printf("%d\n", strcmp(real, real1));
fprintf(stderr, "copy test failed\n");
return 1;
}
free(real);
free(real1);
my_free(mem, data1, pid1);
my_free(mem, data2, pid2);
return 0;
}

View File

@ -1,27 +0,0 @@
#include <string.h>
#include <stdlib.h>
#include "../../vmap.h"
int main() {
Memory* mem = memory_init(-1);
int pid1 = 12;
int pid2 = 24;
char* real1 = malloc(sizeof(char)*32);
real1[0] = 'h';
real1[1] = 'e';
real1[2] = 'l';
real1[3] = 'l';
real1[4] = 'o';
real1[5] = '!';
real1[6] = '\0';
char* data1 = my_malloc(mem, 32*sizeof(char), pid1);
char* data2 = my_malloc(mem, 32*sizeof(char), pid2);
my_free(mem, data1, pid1);
my_free(mem, data2, pid1); //! bad PID
return 0;
}

View File

@ -1,56 +0,0 @@
#include <string.h>
#include <stdlib.h>
#include "../../vmap.h"
#define N 6
struct args {
Memory* mem;
int count;
int size;
int pid;
};
void* thread(void* void_args) {
struct args* args = (struct args*)void_args;
Memory* mem = args->mem;
int count = args->count;
int size = args->size;
int pid = args->pid;
void** ptrs = malloc(sizeof(void*)*count);
for (int i=0; i < count; i++) {
ptrs[i] = my_malloc(mem, size, pid);
}
for (int i=0; i < count; i++) {
my_free(mem, ptrs[i], pid);
}
free(ptrs);
return NULL;
}
int main() {
struct args* args = malloc(sizeof(struct args));
int pid = 12;
Memory* mem = memory_init(pid);
args->mem = mem;
args->pid = pid;
args->count = 20;
args->size = 4;
pthread_t tid[N];
for (int i=0; i < N; i++) {
pthread_create(&(tid[i]), NULL, &thread, (void*)args);
}
for (int i=0; i < N; i++) {
pthread_join(tid[i], NULL);
}
return 0;
}

View File

@ -7,35 +7,6 @@
#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();
@ -96,117 +67,96 @@ void vmap_unswap(VMap* vmap, PageMapInfo* page) {
} }
void* my_malloc(Memory* mem, int size, int pid) { void* my_malloc(VMap* vmap, int size) {
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
pthread_mutex_lock(&vmap->lock); node* current = vmap->pages->head;
// Parcours de la liste pour trouver un candidat. Sinon, nouvelle page while (current) {
node* current = vmap->pages->head; PageMapInfo* page = current->data;
while (current) { if (page_map_available_size(page) >= size) {
PageMapInfo* page = current->data; void* ptr = (void*)((uint64_t)page->start+page->cursor);
if (page_map_available_size(page) >= size) { page->cursor += size;
void* ptr = (void*)((uint64_t)page->start+page->cursor); page->nb_allocs++;
page->cursor += size;
page->nb_allocs++;
pthread_mutex_unlock(&vmap->lock); return ptr;
return ptr;
}
current = current->next;
} }
current = current->next;
}
PageMapInfo* page = vmap_new_page(vmap); PageMapInfo* page = vmap_new_page(vmap);
void* ptr = (void*)((uint64_t)page->start+page->cursor); void* ptr = (void*)((uint64_t)page->start+page->cursor);
page->cursor += size; page->cursor += size;
page->nb_allocs++; page->nb_allocs++;
pthread_mutex_unlock(&vmap->lock);
return ptr; return ptr;
} }
void my_free(Memory* mem, void* ptr, int pid) { void my_free(VMap* vmap, void* ptr) {
VMap* vmap = memory_get(mem, pid); PageMapInfo* page = vmap_get_page(vmap, ptr);
pthread_mutex_lock(&vmap->lock);
PageMapInfo* page = vmap_get_page(vmap, ptr);
if (!page) { if (!page) {
fprintf(stderr, "my_free: double free or memory corrupted\n"); fprintf(stderr, "my_free: Double free or memory corrupted\n");
exit(1); exit(1);
} }
page->nb_allocs--; page->nb_allocs--;
if (page->nb_allocs == 0) { if (page->nb_allocs == 0) {
vmap_free_page(vmap, page); vmap_free_page(vmap, page);
} }
pthread_mutex_unlock(&vmap->lock);
} }
void my_copy(Memory* mem, void* src, void* dst, int size, int pid) { void my_copy(VMap* vmap, void* src, void* dst, int size) {
VMap* vmap = memory_get(mem, pid); PageMapInfo* page_map_src = vmap_get_page(vmap, src);
pthread_mutex_lock(&vmap->lock); 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) { if (((int64_t)src % PAGE_SIZE) + size >= PAGE_SIZE) {
fprintf(stderr, "my_copy: segmentation fault\n"); // Trying to access bad page fprintf(stderr, "my_copy: size not available in this src page\n");
exit(1); exit(1);
} }
if (((int64_t)src % PAGE_SIZE) + size >= PAGE_SIZE) { if (((int64_t)dst % 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 dst page\n");
exit(1); exit(1);
} }
if (((int64_t)dst % PAGE_SIZE) + size >= PAGE_SIZE) { if (page_map_src->swapped) {
fprintf(stderr, "my_copy: size not available in this dst page\n"); vmap_unswap(vmap, page_map_src);
exit(1); }
} if (page_map_dst->swapped) {
vmap_unswap(vmap, page_map_dst);
}
if (page_map_src->swapped) { PageInfo* page_src = page_lookup(vmap->mmap, page_map_src->key);
vmap_unswap(vmap, page_map_src); PageInfo* page_dst = page_lookup(vmap->mmap, page_map_dst->key);
}
if (page_map_dst->swapped) {
vmap_unswap(vmap, page_map_dst);
}
PageInfo* page_src = page_lookup(vmap->mmap, page_map_src->key); memcpy(page_dst->data+((int64_t)src % PAGE_SIZE), page_src->data+((int64_t)dst % PAGE_SIZE), size);
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) { void vmap_copy_to_memory(struct VMap *vmap, void *src, void *dst, int size) {
VMap* vmap = memory_get(mem, pid); PageMapInfo* page_map = vmap_get_page(vmap, src);
pthread_mutex_lock(&vmap->lock); PageInfo* page = page_lookup(vmap->mmap, page_map->key);
PageMapInfo* page_map = vmap_get_page(vmap, src);
PageInfo* page = page_lookup(vmap->mmap, page_map->key);
if (!page) { if (!page) {
fprintf(stderr, "vmap_copy_to_memory: page not found\n"); fprintf(stderr, "vmap_copy_to_memory: page not found\n");
exit(1); exit(1);
} }
memcpy(dst, page->data + ((int64_t)src % PAGE_SIZE), size); memcpy(page->data + ((int64_t)src % PAGE_SIZE), dst, size);
pthread_mutex_unlock(&vmap->lock);
} }
void vmap_copy_from_memory(Memory* mem, void *src, void *dst, int size, int pid) { void vmap_copy_from_memory(struct VMap *vmap, void *src, void *dst, int size) {
VMap* vmap = memory_get(mem, pid); PageMapInfo* page_map = vmap_get_page(vmap, dst);
pthread_mutex_lock(&vmap->lock); PageInfo* page = page_lookup(vmap->mmap, page_map->key);
PageMapInfo* page_map = vmap_get_page(vmap, dst);
PageInfo* page = page_lookup(vmap->mmap, page_map->key);
if (!page) { if (!page) {
fprintf(stderr, "vmap_copy_to_memory: page not found\n"); fprintf(stderr, "vmap_copy_to_memory: page not found\n");
exit(1); exit(1);
} }
memcpy(page->data + ((int64_t)dst % PAGE_SIZE), src, size); memcpy(src, page->data + ((int64_t)dst % PAGE_SIZE), size);
pthread_mutex_unlock(&vmap->lock);
} }

View File

@ -1,23 +1,14 @@
#ifndef DEF_VMAP_H #ifndef DEF_VMAP_H
#define DEF_VMAP_H #define DEF_VMAP_H
#include <pthread.h>
#include "mmap.h" #include "mmap.h"
typedef struct VMap { typedef struct VMap {
MMap* mmap; MMap* mmap;
linkedList* pages; linkedList* pages;
pthread_mutex_t lock; int index; // index of virt memory
int index; // size already alloc'ed
} 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
@ -26,10 +17,6 @@ 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();
/** /**
@ -40,15 +27,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(Memory* mem, void *src, void *dst, int size, int pid); void vmap_copy_to_memory(struct VMap *vmap, void *src, void *dst, int size);
/** /**
* From real memory to implemented memory * From real memory to implemented memory
*/ */
void vmap_copy_from_memory(Memory* mem, void *src, void *dst, int size, int pid); void vmap_copy_from_memory(struct VMap *vmap, void *src, void *dst, int size);
void* my_malloc(Memory* mem, int size, int pid); void* my_malloc(VMap* vmap, int size);
void my_free(Memory* mem, void* ptr, int pid); void my_free(VMap* vmap, void* ptr);
void my_copy(Memory* mem, void* src, void* dst, int size, int pid); void my_copy(VMap* vmap, void* src, void* dst, int size);
#endif #endif