rendered paste body#include "mem.h"typedef struct fb{ size_t taille; struct fb *next;}fb_t;typedef struct ab{ size_t taille;}ab_t;static mem_fit_function_t *fonctionRecherche;static fb_t *tete;static char *memory;static size_t tailleMemoire;void mem_init(char *mem, size_t taille){ fb_t *nouveauBloc; memory = mem; tailleMemoire = taille; nouveauBloc = (fb_t*) memory; nouveauBloc->taille = tailleMemoire; nouveauBloc->next = NULL; tete = nouveauBloc; fonctionRecherche = mem_fit_first;}/*Post-Cond :prevRet pointe sur un pointeur le bloc prcdant celui renvoy. * Si le bloc renvoy est le premier, prevRet pointe sur un pointeur qui vaut nul */fb_t *mem_fit_first(fb_t *fb, size_t taille, fb_t** prevRet){ fb_t *courant=fb, *prev=NULL; while(courant) { if(courant->taille >= taille) { *prevRet = prev; return courant; } prev = courant; courant = courant->next; } return NULL;}fb_t *mem_fit_best(fb_t *fb, size_t taille, fb_t** prev){ return NULL;}fb_t *mem_fit_worst(fb_t *fb, size_t taille, fb_t** prev){ return NULL;}void *mem_alloc(size_t tailleAlloc){ fb_t *blocPrev, *blocSuiv; fb_t *blocUtilise;//descripteur du bloc libre avant allocation ab_t *blocAlloue;//descripteur du bloc allouer size_t tailleAlignee; size_t tailleLibre; //recherche d'un bloc blocUtilise = fonctionRecherche(tete, tailleAlloc, &blocPrev); //aucun bloc disponible if(!blocUtilise) { return NULL; } blocSuiv = blocUtilise->next; tailleLibre = blocUtilise->taille; //alloc blocAlloue = (ab_t*) blocUtilise; tailleAlignee = tailleAlloc + (sizeof(size_t) - 1 - (tailleAlloc-1) % sizeof(tailleAlloc)); blocAlloue->taille = sizeof(ab_t) + tailleAlignee; //on positionne le nouveau fb if(tailleLibre > blocAlloue->taille) { int diff = tailleLibre - blocAlloue->taille; //Si on a pas assez de place aprs allocation pour un descripteur de bloc libre, //on augmente le bloc allou pour qu'il fasse la taille du bloc libre if( diff >= sizeof(fb_t)) { fb_t *nouveau; nouveau = (fb_t *)(((char*) blocUtilise) + blocAlloue->taille); nouveau->taille = diff; nouveau->next = blocSuiv; if(blocPrev == NULL) tete = nouveau; else blocPrev->next = nouveau; } else { blocAlloue->taille = tailleLibre; } } //les blocs libre et align sont de la mme taille, //on supprime le bloc libre if(tailleLibre == blocAlloue->taille) { if(blocPrev == NULL) tete = blocSuiv; else blocPrev->next = blocSuiv; } return (void *) ( (char*)blocAlloue + sizeof(ab_t));}