void *malloc(size_t size) { void *ptr; if (start == NULL) { start = sbrk(0); end = start; } #ifdef DEBUG printf("start = %p, end = %p\n", start, end); #endif ptr = findMemory(start, end, size); #ifdef DEBUG printf("\nafter findMemory ptr = %p\n", ptr); #endif if (addMemory(start, &end, &ptr, size) == 0) return (0); #ifdef DEBUG printf("\nafter addMemory ptr = %p\n", ptr); printf("after addMemory start = %p, end = %p\n", start, end); #endif useMemory(ptr, end, size); ptr += sizeof(t_metadata); #ifdef DEBUG printf("\n\n"); #endif return(ptr); }
/** * Inserting files into the disk * @param const char* File Name * @param int Size of file to be inserted * @return FileSysErrors Errors for the function that have occurred */ FileSysErrors createFile(const char* filename, int size) { FileSysErrors errors; /* Error Checking Needed: * • Returns DIRECTORY_FULL if maxfiles used * • Returns DUPLICATE_NAME if there is already a file with the name * • Returns ZERO_SIZE if size <= 0 * • Returns CREATE_FAIL if unable to find space for the file */ // 1. Check for simple errors before inserting errors = createFileErrorChecking(filename, size); if (errors != NO_ERR) { // Exit out return errors; } else { // 1. Check for full directory //if (DirectoryEntries > MAXFILES) { // return DIRECTORY_FULL; //} // 2. Check for space and position to insert into HDD int position = findMemory(size); if (position < 0) { errors = CREATE_FAIL; return errors; } else { // 3. Insert into directory int count = 0; for (count = 0; count < MAXFILES; count++) { if (Directory[count].Size == 0) { // Copy in data strcpy(Directory[count].FileName, filename); Directory[count].Start = position; Directory[count].Size = size; break; } } // 4. Insert into Disk for (count = 0; count < size; count++) { // Mark positions as read Disk[position] = 1; // Increment the position position++; } // Update stats FilesCreated++; // All is good, return no error errors = NO_ERR; return errors; } } }