void mem_init(char* mem, size_t taille) { mem_fit(&mem_fit_first); ptr_first = mem; ptr_last = mem + taille; ptr_init = (fb*) mem; ptr_init->size = taille; ptr_init->next = NULL; }
void mem_init(char* mem, size_t taille) { /* On initialise notre stratégie à mem_fit_first */ mem_fit(&mem_fit_first); /* On fait pointer notre début de zone mémoire sur le tableau envoyé */ mem_begin = mem; mem_size = taille; /* On initialise notre premier block de zone libre qui prend toute la zone */ struct fb *init = (struct fb*)mem_begin; init->size = taille; init->next = NULL; /* On fait pointer mem_ll sur cette zone */ mem_ll.next = (struct fb*)mem_begin; }
/* initialisation de la liste chainee * avec un seul bloc libre de taille passee en parametre */ void mem_init(size_t taille) { if(SemInit(&sem, 1) == -1) { PutString("[mem.c] erreur sem init\n"); Exit(0); } taille_memoire = taille; char* mem = (char*)Mmap(taille); if(mem == NULL) { PutString("[mem.c] erreur de recuperation de memoire\n"); Exit(1); } // stockage du pointeur du tableau memoire afin de placer la premiere structure head = (struct fb*) mem; // sauvegarde du debut du tableau et de la taille totale de la memoire dans des variables globales d_memoire = mem; head -> size = taille; head -> next = NULL; // initialisation de l'alignement alignement = sizeof(void*); mem_fit(mem_fit_first); }
int main() { my_mem_init(memoire, TAILLE_MEMOIRE); printf("Test de l'etat initial de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 1); printf("TEST INIT OK\n\n"); printf("\n--------------------------\n\n"); printf("Test de free :\n"); mem_fit(&mem_fit_best); my_mem_alloc(500); my_mem_alloc(400); my_mem_alloc(300); my_mem_alloc(200); //my_mem_free(0); my_mem_free(1); //my_mem_free(2); my_mem_free(3); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); printf("nb_free = %d ; nb_busy = %d\n", nb_free, nb_busy); assert(nb_free == 2 && nb_busy == 2); printf("TEST FREE OK\n\n"); my_mem_init(memoire, TAILLE_MEMOIRE); printf("\n--------------------------\n\n"); printf("Test de best fit :\n"); my_mem_alloc(500); my_mem_alloc(400); my_mem_alloc(300); my_mem_alloc(200); my_mem_alloc(100); my_mem_alloc(200); my_mem_alloc(300); my_mem_alloc(400); my_mem_alloc(500); printf("Etat de la memoire :\n"); my_mem_free(0); my_mem_free(2); my_mem_free(4); my_mem_free(7); my_mem_alloc(200); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); printf("nb_free = %d ; nb_busy = %d\n", nb_free, nb_busy); assert(nb_free == 5 && nb_busy == 6); printf("TEST BEST OK\n\n"); my_mem_init(memoire, TAILLE_MEMOIRE); printf("\n--------------------------\n\n"); printf("Test de worst fit :\n"); mem_fit(&mem_fit_worst); my_mem_alloc(500); my_mem_alloc(400); my_mem_alloc(300); my_mem_alloc(200); my_mem_alloc(100); my_mem_alloc(200); my_mem_alloc(300); my_mem_alloc(400); my_mem_alloc(500); my_mem_alloc(1096); my_mem_free(0);//Libere 500 my_mem_free(2);//Libere 300 my_mem_free(4);//Libere 100 my_mem_alloc(200); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); printf("nb_free = %d ; nb_busy = %d\n", nb_free, nb_busy); assert(nb_free == 3 && nb_busy == 8); printf("TEST WORST OK\n\n"); printf("\n--------------------------\n\n"); printf("1.Best Fit est meilleur que First Fit :\n"); printf(" 1.1 Test avec Best fit :\n"); my_mem_init(memoire, TAILLE_MEMOIRE); mem_fit(&mem_fit_best); my_mem_alloc(200); my_mem_alloc(300); my_mem_alloc(100); my_mem_alloc(3456); my_mem_free(0);//Libere 200 my_mem_free(2);//Libere 100 my_mem_alloc(90); my_mem_alloc(190); my_mem_show(afficher_zone); printf("\n 1.2 Test avec First fit :\n"); my_mem_init(memoire, TAILLE_MEMOIRE); my_mem_alloc(200); my_mem_alloc(300); my_mem_alloc(100); my_mem_alloc(3456); my_mem_free(0);//Libere 200 my_mem_free(2);//Libere 100 my_mem_alloc(90); my_mem_alloc(190);//Allocaionimpossible avec first fit my_mem_show(afficher_zone); printf("\n2.First Fit est meilleur que Best Fit :\n"); printf(" 2.1 Test avec First fit :\n"); my_mem_init(memoire, TAILLE_MEMOIRE); my_mem_alloc(200); my_mem_alloc(300); my_mem_alloc(100); my_mem_alloc(3456); my_mem_free(0);//Libere 200 my_mem_free(2);//Libere 100 my_mem_alloc(72); my_mem_alloc(112); my_mem_alloc(92); my_mem_show(afficher_zone); printf(" 2.2 Test avec Best fit :\n"); my_mem_init(memoire, TAILLE_MEMOIRE); mem_fit(&mem_fit_best); my_mem_alloc(200); my_mem_alloc(300); my_mem_alloc(100); my_mem_alloc(3456); my_mem_free(0);//Libere 200 my_mem_free(2);//Libere 100 my_mem_alloc(72); my_mem_alloc(112);//Allocation impossible avec best_fit my_mem_alloc(92); my_mem_show(afficher_zone); }