/* * Initialize: return -1 on error, 0 on success. */ int mm_init(void) { if ((heap_list_head = mem_sbrk(2 * HEADER_SIZE + 20*DSIZE)) == NULL ) return -1; PUT(heap_list_head, 0); //Alignment padding /*initialize dummy block header*/ PUT(heap_list_head + WSIZE, PACK(HEADER_SIZE, 1)); //WSIZE = padding PUT(heap_list_head + DSIZE, 0); //pointer to next free block PUT(heap_list_head + DSIZE+WSIZE, 0); //pointer to the previous free block free_list_head = heap_list_head + 2*DSIZE + WSIZE; /*initialize dummy block footer*/ PUT(heap_list_head + HEADER_SIZE, PACK(HEADER_SIZE, 1)); /*initialize epilogue*/ PUT(heap_list_head+WSIZE + HEADER_SIZE, PACK(0, 1)); /*initialize the free list pointer to the tail block*/ heap_list_head += DSIZE; heap_header = heap_list_head; init_free_list(heap_list_head); /*return -1 if unable to get heap space*/ if ((heap_list_head = extend_heap(CHUNKSIZE / WSIZE)) == NULL ) return -1; return 0; }
/* * Initialize: return -1 on error, 0 on success. */ int mm_init(void) { /* Create the initial empty heap */ if ((heap_listp = mem_sbrk(WSIZE + SEG_LEVLL * DSIZE)) == (void *)-1) return -1; flist_tbl = heap_listp; init_free_list(); int table_off = 2 * SEG_LEVLL * WSIZE; PUT(heap_listp + table_off, PACK(4, 1)); /* Prologue header */ PUT(heap_listp + table_off + 4, PACK(0, 1)); /* Epilogue header */ FLAG(heap_listp + table_off); FLAG(heap_listp + table_off + 4); /* set the alloc FLAG in next block */ heap_listp += table_off + 4; /* Extend the empty heap with a free block of CHUNKSIZE bytes */ char *freeb = extend_heap(CHUNKSIZE/WSIZE); if (freeb == NULL) { return -1; } return 0; }
void mksfs(int fresh){ //Implement mksfs here if (fresh == 1){ //deletes fs if it already exists if(access(DISK_FILE, F_OK) != -1){ unlink(DISK_FILE); } //fs creation printf("Initalizing sfs\n"); init_fresh_disk(DISK_FILE, BLOCK_SIZE, MAX_BLOCKS); //zero_everything(); // writes superblock to the first block printf("Writing superblocks\n"); init_superblock(); write_blocks(0, 1, &sb); // write the inode table to the 2nd block printf("Writing inode table\n"); init_inode_table(); //add_root_dir_inode(); //add_dummy_file_inode(); //write_blocks(1, 1, &inode_table); // creates free list init_free_list(); // write root directory data to the 3rd block printf("Writing root dir\n"); init_root_dir(); //allocate memory for directory inode inode_t *inode = malloc(ADJUST((MAX_FILES+1)*sizeof(inode_t))); read_blocks(INODE_TABLE, INODE_TABLE_SIZE, inode); if(inode == NULL){ return; } //set first inode to point to directory inode[0].size = DIRECTORY_SIZE*BLOCK_SIZE; inode[0].link_cnt = DIRECTORY_SIZE; inode[0].exists = 1; //check to see if we need to use ref_ptr //if(DIRECTORY_SIZE > 12){ //inode[0].ref_ptr = search(); //setAlloc(inode[0].ref_ptr); //unsigned int *buffer = malloc(BLOCK_SIZE); //write_blocks(19 + inode[0].ref_ptr, 1, buffer); //free(buffer); //assign the pointers the location of directory files int i; for(i = 0; i < DIRECTORY_SIZE; i++){ if(i > 11){ unsigned int *buffer = malloc(BLOCK_SIZE); read_blocks(19+ inode[0].ref_ptr, 1, buffer); buffer[i - 12] = 2 + i; write_blocks(19+ inode[0].ref_ptr, 1, buffer); free(buffer); } else{ inode[0].data_ptrs[i] = 2 + i; } } //update inode and free memory write_blocks(INODE_TABLE, INODE_TABLE_SIZE, inode); free(inode); } else if (fresh == 0){ if(init_disk(DISK_FILE, BLOCK_SIZE, MAX_BLOCKS) != 0){ printf("Error initializing disk\n"); return; } } //allocate main memory for filesystem data structures int *superblock = malloc(BLOCK_SIZE*SUPERBLOCK_SIZE); if(superblock == NULL){ printf("Error allocating memory for superblock\n"); return; } read_blocks(0, SUPERBLOCK_SIZE, superblock); //allocate main memory for directory root_dir = malloc(ADJUST(sizeof(dir_entry_t)*MAX_FILES)); if(root_dir == NULL){ printf("Error allocating memory for root directory\n"); return; } read_blocks(2, DIRECTORY_SIZE, root_dir); //allocate memory for inode table inode_table = malloc(ADJUST(sizeof(inode_t)*(MAX_FILES+1))); if(inode_table == NULL){ printf("Error allocating memory for inode table"); return; } read_blocks(INODE_TABLE, INODE_TABLE_SIZE, inode_table); fileCount = 0; fd_table = NULL; return; }