//! Création du cache. struct Cache *Cache_Create(const char *fic, unsigned nblocks, unsigned nrecords, size_t recordsz, unsigned nderef) { struct Cache *cache = malloc(sizeof(struct Cache)); cache->file = (char*)fic; if(!(cache->fp = fopen(fic, "w+"))) { perror("fopen"); exit(-1); } rewind(cache->fp); cache->nblocks = nblocks; cache->nrecords = nrecords; cache->recordsz = recordsz; cache->blocksz = recordsz*nrecords; cache->nderef = nderef; cache->pstrategy = Strategy_Create(cache); struct Cache_Instrument instrument = {0, 0, 0, 0, 0}; cache->instrument = instrument; Create_Blocks(cache); return cache; }
/*! * \ingroup cache_interface * * Cette fonction crée et initialise le cache : * - Allocation du cache lui-même * - Ouverture du fichier * - Initialisation des champs du cache. * - Allocation des entêtes de blocs et des blocs eux-memes * * \param file nom du fichier * \param nblocks nombre de blocs dans le cache * \param nrecords nombre d'enregistrements par bloc * \param recordsz taille d'un enregistrement * \param nderef période de déréférençage (pour NUR) * \return un pointeur sur le nouveau cache ou le pointeur nul en cas d'erreur */ struct Cache *Cache_Create(const char *file, unsigned nblocks, unsigned nrecords, size_t recordsz, unsigned nderef) { int ib; /* Allocation de la structure du cache */ struct Cache *pcache = (struct Cache *)malloc(sizeof(struct Cache)); /* Sauvegarde du nom de fichier */ pcache->file = (char *)malloc(strlen(file) + 1); strcpy(pcache->file, file); /* Ouverture du fichier en mode "update" : * On verifie d'abord que le fichier existe ("r+"). * Sinon on le cree ("w+"). */ if ((pcache->fp = fopen(file, "r+")) == NULL) if ((pcache->fp = fopen(file, "w+")) == NULL) return CACHE_KO; /* Initialisation des valeurs de dimensionnement */ if (nblocks > 0) pcache->nblocks = nblocks; else return 0; if (nrecords > 0) pcache->nrecords = nrecords; else return 0; if (recordsz > 0) pcache->recordsz = recordsz; else return 0; if (recordsz > 0) pcache->nderef = nderef; else return 0; pcache->blocksz = nrecords*recordsz; /* Allocation des entetes de bloc, et des blocs eux-memes */ pcache->headers = malloc(nblocks*sizeof(struct Cache_Block_Header)); for (ib = 0; ib < nblocks; ib++) { pcache->headers[ib].data = (char *)malloc(pcache->blocksz); pcache->headers[ib].ibcache = ib; pcache->headers[ib].flags = 0; } /* Initialisation du pointeur sur le premier bloc libre, cad ici le premier * bloc */ pcache->pfree = pcache->headers; /* Mise à 0 des données d'instrumentation */ (void)Cache_Get_Instrument(pcache); /* Initialisation de la stratégie */ pcache->pstrategy = Strategy_Create(pcache); /* On rend ce que l'on vient de créer et d'initialiser */ return pcache; }
/** *crée une nouvelle cache **/ struct Cache *Cache_Create(const char *fic, unsigned nblocks, unsigned nrecords,size_t recordsz, unsigned nderef){ struct Cache *cache =malloc(sizeof(struct Cache)); //Ouvre le fichier FIC - et le crée si ça n'existe pas encore. if( (file = fopen(fic, "r+b")) == NULL) file = fopen(fic, "w+b"); FILE *file; cache ->fp=file; cache->file= fic; cache->nblocks= nblocks; cache->nrecords=nrecords; cache->blocksz=n*recordsz; cache->ndref= nderef; cache->instrument.n_reads = 0; cache->instrument.n_writes = 0; cache->instrument.n_hits = 0; cache->pstrategy = Strategy_Create(cache); cache->pfree = Get_Free_Block(cache); //1er bloc? cache->headers=create_block(cache); return cache; }