void NESTED_FUNC_ATTR read_blocklist (grub_disk_addr_t sector, unsigned offset, unsigned length) { if (num_sectors > 0) { if (start_sector + num_sectors == sector && offset == 0 && length == GRUB_DISK_SECTOR_SIZE) { num_sectors++; return; } print_blocklist (start_sector, num_sectors, 0, 0); num_sectors = 0; } if (offset == 0 && length == GRUB_DISK_SECTOR_SIZE) { start_sector = sector; num_sectors++; } else print_blocklist (sector, 0, offset, length); }
/*** function run_heap *** Run the memory allocation as outlined in lab 3: https://eee.uci.edu/16s/36680/labs/lab3_malloc.pdf */ void heap_alloc() { char *heap = malloc (400); struct Command input; // Input read in from the command line. // Set all values in the heap to zero memset(heap, '\0', 400); *(header_t*)heap = 400; // Run the heap_alloc's loop. while(1) { // Read in input from the console and run the command. if(read_command(&input) > 0) { // Quit condition. if(strcmp(input.program, "quit") == 0) { // End Program free_command(&input); break; } else if (strcmp(input.program, "allocate") == 0 && input.len == 2){ //Allocate Heap Space allocate_block(heap, input.array); } else if (strcmp(input.program, "free") == 0 && input.len == 2){ //Free Heap Space free_block(heap, input.array); } else if (strcmp(input.program, "blocklist") == 0 && input.len == 1){ //Prints out Block Info print_blocklist(heap, input.array); } else if (strcmp(input.program, "writeheap") == 0 && input.len == 4){ //Writes X chars to heap block write_block(heap, input.array); } else if (strcmp(input.program, "printheap") == 0 && input.len == 3){ //Prints out heap w/out header print_heap(heap, input.array); } else{ fprintf(stderr, "Error: Invalid command or invalid arguments.\n"); } } else { fprintf(stderr, "Error: Unable to read in input.\n"); } free_command(&input); } // Deallocate the heap. free(heap); }
/* Helper for grub_cmd_blocklist. */ static void read_blocklist (grub_disk_addr_t sector, unsigned offset, unsigned length, void *data) { struct blocklist_ctx *ctx = data; if (ctx->num_sectors > 0) { if (ctx->start_sector + ctx->num_sectors == sector && offset == 0 && length >= GRUB_DISK_SECTOR_SIZE) { ctx->num_sectors += length >> GRUB_DISK_SECTOR_BITS; sector += length >> GRUB_DISK_SECTOR_BITS; length &= (GRUB_DISK_SECTOR_SIZE - 1); } if (!length) return; print_blocklist (ctx->start_sector, ctx->num_sectors, 0, 0, ctx); ctx->num_sectors = 0; }
static grub_err_t grub_cmd_blocklist (grub_command_t cmd __attribute__ ((unused)), int argc, char **args) { grub_file_t file; char buf[GRUB_DISK_SECTOR_SIZE]; unsigned long start_sector = 0; unsigned num_sectors = 0; int num_entries = 0; grub_disk_addr_t part_start = 0; auto void NESTED_FUNC_ATTR read_blocklist (grub_disk_addr_t sector, unsigned offset, unsigned length); auto void NESTED_FUNC_ATTR print_blocklist (grub_disk_addr_t sector, unsigned num, unsigned offset, unsigned length); void NESTED_FUNC_ATTR read_blocklist (grub_disk_addr_t sector, unsigned offset, unsigned length) { if (num_sectors > 0) { if (start_sector + num_sectors == sector && offset == 0 && length == GRUB_DISK_SECTOR_SIZE) { num_sectors++; return; } print_blocklist (start_sector, num_sectors, 0, 0); num_sectors = 0; } if (offset == 0 && length == GRUB_DISK_SECTOR_SIZE) { start_sector = sector; num_sectors++; } else print_blocklist (sector, 0, offset, length); } void NESTED_FUNC_ATTR print_blocklist (grub_disk_addr_t sector, unsigned num, unsigned offset, unsigned length) { if (num_entries++) grub_printf (","); grub_printf ("%llu", (unsigned long long) (sector - part_start)); if (num > 0) grub_printf ("+%u", num); if (offset != 0 || length != 0) grub_printf ("[%u-%u]", offset, offset + length); } if (argc < 1) return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified"); file = grub_file_open (args[0]); if (! file) return grub_errno; if (! file->device->disk) return grub_error (GRUB_ERR_BAD_DEVICE, "this command is available only for disk devices"); part_start = grub_partition_get_start (file->device->disk->partition); file->read_hook = read_blocklist; while (grub_file_read (file, buf, sizeof (buf)) > 0) ; if (num_sectors > 0) print_blocklist (start_sector, num_sectors, 0, 0); grub_file_close (file); return grub_errno; }