#include #include #include #include #include #include #include "bloc.h" #include "freeblock.h" uint32_t new_block(unsigned int volume) { struct dskinfo_t info = dskinfo(); struct superblock_desc_t super; struct freeblock_t fblock; load_super(volume, &super); /* Plus d'espace disponible */ if ( super.first_free_block == 0 ) return 0; uintptr_t *data = malloc(info.taille_sec); read_bloc(volume, super.first_free_block, data); memcpy(&fblock, data, sizeof(struct freeblock_t)); uint32_t chosen_block=0; /* Si on a plus d'un bloc disponible, on donne le dernier */ if ( fblock.block_count > 1 ) { /* modification de l'indicateur de blocs libres */ fblock.block_count--; memcpy(data, &fblock, sizeof(struct freeblock_t)); write_bloc(volume, super.first_free_block, data); chosen_block = super.first_free_block + fblock.block_count; } /* Sinon, on donne le bloc courant et on lie le suivant */ else { chosen_block = super.first_free_block; super.first_free_block = fblock.next_block; } free(data); /* Ecriture du super block */ super.free_space--; save_super(volume, &super); return chosen_block; } void free_block(unsigned int volume, uint32_t block) { struct dskinfo_t info = dskinfo(); struct superblock_desc_t super; struct freeblock_t newfreeblock; load_super(volume, &super); uintptr_t *data = malloc(info.taille_sec); /* Le nouveau bloc libre */ newfreeblock.block_count = 1; newfreeblock.next_block = super.first_free_block; memcpy(data, &newfreeblock, sizeof(struct freeblock_t)); write_bloc(volume, block, data); /* Incrémenter le compteur d'espace du volume */ super.free_space++; /* Relier le nouvel élément */ super.first_free_block = block; free(data); /* Sauvegarde du superbloc */ save_super(volume, &super); }