int main(void) { FILE *fp_mail; char *status = (char *)alloca(200); int b, d, m; b = BATTERY_INTERVAL; d = DATE_INTERVAL; m = MAIL_INTERVAL; struct timespec interval = { .tv_sec = UPDATE_INTERVAL, .tv_nsec = 0 }; int sockfd = socket(AF_INET, SOCK_DGRAM, 0); for ( ; ; nanosleep(&interval, NULL)) { if (++b > BATTERY_INTERVAL) { print_battery(); b = 0; } if (++d > DATE_INTERVAL) { print_time(); d = 0; } if (++m > MAIL_INTERVAL) { if (print_mail(fp_mail) == 0) { m = 0; } if (errno == EBADF) { fp_mail = mail_init(); } } if (sockfd != -1) { print_iw(sockfd); } strncpy(_root, print_free("/"), _FREE_LEN); strncpy(_home, print_free("/home"), _FREE_LEN); print_time(); print_cpu(); print_memory(); snprintf(status, 200, "%s %s | %s | %s | %s | %s | %s | %s" , _root, _home, _iw, _battery, _mail, _cpu , _mem, _date); printf("%s\n", status); }//break; } pclose(fp_mail); cleanup(sockfd); } double get_battery_stats(const char *path) { FILE *stats; double value = 0; if ((stats = fopen(path, "r"))== NULL) { return -1; } fscanf(stats, "%lf", &value); fclose(stats); return value; }
/** * check_count_free_list -Check for count of free blocks, iterating over blocks * and by going through next pointers */ static void check_count_free_list() { void *bp; unsigned int counti = 0; unsigned int countp = 0; /*Iterate over list*/ for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) { if(!GET_ALLOC(HDRP(bp))) { counti++; } } /* Moving free list by pointers*/ for (int i = 0; i < NO_OF_LISTS; i++) { for (bp = GET_SEGI(seg_list,i); (bp!=NULL) && (GET_SIZE(HDRP(bp)) > 0);bp = GET_NEXTP(bp)) { countp++; } } /*If count is not matching, print error, with debug Info*/ if(countp!=counti) { printf("ERROR: No. of free block mismatch\n"); dbg_printf("free\n"); print_free(); dbg_printf("all\n"); print_all(); } }
/* Interesting test case. Divides SIZE into 4 blocks and allocates smalloc each SIZE / 4 block of memory Tests - ability for smalloc to handle adding to an empty allocated_list - ability for smalloc to handle adding to an allocated_list with existing blocks - ability for smalloc to handle when smalloc is called for num_bytes > available size in freelist - ability for freelist to handle when freelist has no memory blocks available - ability for freelist to handle when the size in freelist is equal to the number of bytes called by smalloc (when the last memory block at ptrs[3] is called), and freelist should free this memory block from the list rather than break it into chunks */ int main(void) { mem_init(SIZE); char *ptrs[10]; int i; //Call smalloc 4 times ptrs[0] = smalloc(SIZE * 2); for(i = 0; i <4; i++) { int num_bytes = SIZE / 4; ptrs[i] = smalloc(num_bytes); write_to_mem(num_bytes, ptrs[i], i); } printf("List of allocated blocks:\n"); print_allocated(); printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); printf("freeing %p result = %d\n", ptrs[0], sfree(ptrs[0])); printf("freeing %p result = %d\n", ptrs[1], sfree(ptrs[1])); printf("freeing %p result = %d\n", ptrs[2], sfree(ptrs[2])); printf("freeing %p result = %d\n", ptrs[3], sfree(ptrs[3])); // sfree already called for address at ptrs[1]. Case should be handled printf("freeing %p result = %d\n", ptrs[1], sfree(ptrs[1])); printf("List of allocated blocks:\n"); print_allocated(); printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); mem_clean(); return 0; }
static void list_desc (ext2_filsys fs) { unsigned long i; blk64_t first_block, last_block; blk64_t super_blk, old_desc_blk, new_desc_blk; char *block_bitmap=NULL, *inode_bitmap=NULL; const char *units = _("blocks"); int inode_blocks_per_group, old_desc_blocks, reserved_gdt; int block_nbytes, inode_nbytes; int has_super; blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block); ext2_ino_t ino_itr = 1; errcode_t retval; if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, EXT4_FEATURE_RO_COMPAT_BIGALLOC)) units = _("clusters"); block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8; inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8; if (fs->block_map) block_bitmap = malloc(block_nbytes); if (fs->inode_map) inode_bitmap = malloc(inode_nbytes); inode_blocks_per_group = ((fs->super->s_inodes_per_group * EXT2_INODE_SIZE(fs->super)) + EXT2_BLOCK_SIZE(fs->super) - 1) / EXT2_BLOCK_SIZE(fs->super); reserved_gdt = fs->super->s_reserved_gdt_blocks; fputc('\n', stdout); first_block = fs->super->s_first_data_block; if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) old_desc_blocks = fs->super->s_first_meta_bg; else old_desc_blocks = fs->desc_blocks; for (i = 0; i < fs->group_desc_count; i++) { first_block = ext2fs_group_first_block2(fs, i); last_block = ext2fs_group_last_block2(fs, i); ext2fs_super_and_bgd_loc2(fs, i, &super_blk, &old_desc_blk, &new_desc_blk, 0); printf (_("Group %lu: (Blocks "), i); print_range(first_block, last_block); fputs(")", stdout); print_bg_opts(fs, i); if (ext2fs_has_group_desc_csum(fs)) { unsigned csum = ext2fs_bg_checksum(fs, i); unsigned exp_csum = ext2fs_group_desc_csum(fs, i); printf(_(" Checksum 0x%04x"), csum); if (csum != exp_csum) printf(_(" (EXPECTED 0x%04x)"), exp_csum); printf(_(", unused inodes %u\n"), ext2fs_bg_itable_unused(fs, i)); } has_super = ((i==0) || super_blk); if (has_super) { printf (_(" %s superblock at "), i == 0 ? _("Primary") : _("Backup")); print_number(super_blk); } if (old_desc_blk) { printf("%s", _(", Group descriptors at ")); print_range(old_desc_blk, old_desc_blk + old_desc_blocks - 1); if (reserved_gdt) { printf("%s", _("\n Reserved GDT blocks at ")); print_range(old_desc_blk + old_desc_blocks, old_desc_blk + old_desc_blocks + reserved_gdt - 1); } } else if (new_desc_blk) { fputc(has_super ? ',' : ' ', stdout); printf("%s", _(" Group descriptor at ")); print_number(new_desc_blk); has_super++; } if (has_super) fputc('\n', stdout); fputs(_(" Block bitmap at "), stdout); print_number(ext2fs_block_bitmap_loc(fs, i)); print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0, first_block, last_block); if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) printf(_(", csum 0x%08x"), ext2fs_block_bitmap_checksum(fs, i)); fputs(_(", Inode bitmap at "), stdout); print_number(ext2fs_inode_bitmap_loc(fs, i)); print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0, first_block, last_block); if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) printf(_(", csum 0x%08x"), ext2fs_inode_bitmap_checksum(fs, i)); fputs(_("\n Inode table at "), stdout); print_range(ext2fs_inode_table_loc(fs, i), ext2fs_inode_table_loc(fs, i) + inode_blocks_per_group - 1); print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1, first_block, last_block); printf (_("\n %u free %s, %u free inodes, " "%u directories%s"), ext2fs_bg_free_blocks_count(fs, i), units, ext2fs_bg_free_inodes_count(fs, i), ext2fs_bg_used_dirs_count(fs, i), ext2fs_bg_itable_unused(fs, i) ? "" : "\n"); if (ext2fs_bg_itable_unused(fs, i)) printf (_(", %u unused inodes\n"), ext2fs_bg_itable_unused(fs, i)); if (block_bitmap) { fputs(_(" Free blocks: "), stdout); retval = ext2fs_get_block_bitmap_range2(fs->block_map, blk_itr, block_nbytes << 3, block_bitmap); if (retval) com_err("list_desc", retval, "while reading block bitmap"); else print_free(i, block_bitmap, fs->super->s_clusters_per_group, fs->super->s_first_data_block, EXT2FS_CLUSTER_RATIO(fs)); fputc('\n', stdout); blk_itr += fs->super->s_clusters_per_group; } if (inode_bitmap) { fputs(_(" Free inodes: "), stdout); retval = ext2fs_get_inode_bitmap_range2(fs->inode_map, ino_itr, inode_nbytes << 3, inode_bitmap); if (retval) com_err("list_desc", retval, "while reading inode bitmap"); else print_free(i, inode_bitmap, fs->super->s_inodes_per_group, 1, 1); fputc('\n', stdout); ino_itr += fs->super->s_inodes_per_group; } } if (block_bitmap) free(block_bitmap); if (inode_bitmap) free(inode_bitmap); }
static void list_desc (ext2_filsys fs) { unsigned long i; blk_t first_block, last_block; blk_t super_blk, old_desc_blk, new_desc_blk; char *block_bitmap=NULL, *inode_bitmap=NULL; int inode_blocks_per_group, old_desc_blocks, reserved_gdt; int block_nbytes, inode_nbytes; int has_super; blk_t blk_itr = fs->super->s_first_data_block; ext2_ino_t ino_itr = 1; block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8; inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8; if (fs->block_map) block_bitmap = malloc(block_nbytes); if (fs->inode_map) inode_bitmap = malloc(inode_nbytes); inode_blocks_per_group = ((fs->super->s_inodes_per_group * EXT2_INODE_SIZE(fs->super)) + EXT2_BLOCK_SIZE(fs->super) - 1) / EXT2_BLOCK_SIZE(fs->super); reserved_gdt = fs->super->s_reserved_gdt_blocks; fputc('\n', stdout); first_block = fs->super->s_first_data_block; if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) old_desc_blocks = fs->super->s_first_meta_bg; else old_desc_blocks = fs->desc_blocks; for (i = 0; i < fs->group_desc_count; i++) { first_block = ext2fs_group_first_block(fs, i); last_block = ext2fs_group_last_block(fs, i); ext2fs_super_and_bgd_loc(fs, i, &super_blk, &old_desc_blk, &new_desc_blk, 0); printf (_("Group %lu: (Blocks "), i); print_range(first_block, last_block); fputs(")", stdout); print_bg_opts(fs, i); if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) printf(_(" Checksum 0x%04x, unused inodes %d\n"), fs->group_desc[i].bg_checksum, fs->group_desc[i].bg_itable_unused); has_super = ((i==0) || super_blk); if (has_super) { printf (_(" %s superblock at "), i == 0 ? _("Primary") : _("Backup")); print_number(super_blk); } if (old_desc_blk) { printf(_(", Group descriptors at ")); print_range(old_desc_blk, old_desc_blk + old_desc_blocks - 1); if (reserved_gdt) { printf(_("\n Reserved GDT blocks at ")); print_range(old_desc_blk + old_desc_blocks, old_desc_blk + old_desc_blocks + reserved_gdt - 1); } } else if (new_desc_blk) { fputc(has_super ? ',' : ' ', stdout); printf(_(" Group descriptor at ")); print_number(new_desc_blk); has_super++; } if (has_super) fputc('\n', stdout); fputs(_(" Block bitmap at "), stdout); print_number(fs->group_desc[i].bg_block_bitmap); print_bg_rel_offset(fs, fs->group_desc[i].bg_block_bitmap, 0, first_block, last_block); fputs(_(", Inode bitmap at "), stdout); print_number(fs->group_desc[i].bg_inode_bitmap); print_bg_rel_offset(fs, fs->group_desc[i].bg_inode_bitmap, 0, first_block, last_block); fputs(_("\n Inode table at "), stdout); print_range(fs->group_desc[i].bg_inode_table, fs->group_desc[i].bg_inode_table + inode_blocks_per_group - 1); print_bg_rel_offset(fs, fs->group_desc[i].bg_inode_table, 1, first_block, last_block); printf (_("\n %u free blocks, %u free inodes, " "%u directories%s"), fs->group_desc[i].bg_free_blocks_count, fs->group_desc[i].bg_free_inodes_count, fs->group_desc[i].bg_used_dirs_count, fs->group_desc[i].bg_itable_unused ? "" : "\n"); if (fs->group_desc[i].bg_itable_unused) printf (_(", %u unused inodes\n"), fs->group_desc[i].bg_itable_unused); if (block_bitmap) { fputs(_(" Free blocks: "), stdout); ext2fs_get_block_bitmap_range(fs->block_map, blk_itr, block_nbytes << 3, block_bitmap); print_free (i, block_bitmap, fs->super->s_blocks_per_group, fs->super->s_first_data_block); fputc('\n', stdout); blk_itr += fs->super->s_blocks_per_group; } if (inode_bitmap) { fputs(_(" Free inodes: "), stdout); ext2fs_get_inode_bitmap_range(fs->inode_map, ino_itr, inode_nbytes << 3, inode_bitmap); print_free (i, inode_bitmap, fs->super->s_inodes_per_group, 1); fputc('\n', stdout); ino_itr += fs->super->s_inodes_per_group; } } if (block_bitmap) free(block_bitmap); if (inode_bitmap) free(inode_bitmap); }
/* Simple test for malloc and free. We will want a proper test driver */ int main(int argc, char **argv) { int opt; int pr = 0; struct sigaction newact; /* fill in newact */ newact.sa_handler = handle_seg; newact.sa_flags = 0; sigaction(SIGSEGV, &newact, NULL); while ((opt = getopt(argc, argv, "p")) != -1) { switch (opt) { case 'p': pr = 1; break; default: /* '?' */ fprintf(stderr, "Usage: %s [-p ] tracefile\n", argv[0]); exit(EXIT_FAILURE); } } if(optind >= argc) { fprintf(stderr, "Usage: %s [-p ] tracefile\n", argv[0]); exit(EXIT_FAILURE); } char *tracefile = argv[optind]; mem_init(SIZE); char *ptrs[MAXOPS]; int i; /* Call my malloc 4 times */ //ptrs[i] = smalloc(num_bytes); //write_to_mem(num_bytes, ptrs[i], i); FILE *fp = fopen(tracefile, "r"); if(!fp) { fprintf(stderr, "Usage: driver [-p] <tracefile>\n"); exit(1); } load_trace(fp); fclose(fp); for(i = 0; i < num_ops; i++) { switch(trace[i].type) { case 'm': ptrs[trace[i].index] = smalloc(trace[i].size); if(pr) { printf("smalloc block %d addr %p size %d\n", trace[i].index, ptrs[trace[i].index], trace[i].size); } if(!ptrs[trace[i].index]) { fprintf(stderr, "Error: smalloc returned NULL index=%d, size=%d\n", trace[i].index, trace[i].size); } // check for valid pointer if((ptrs[trace[i].index] < (char *)mem) || (ptrs[trace[i].index] + trace[i].size) > ((char *)mem + SIZE)) { fprintf(stderr, "Error: malloc block %d addr %p size %d heap overflow\n", trace[i].index, ptrs[trace[i].index], trace[i].size); } else { write_to_mem(trace[i].size, ptrs[trace[i].index],trace[i].index); } break; case 'f': if(pr) { printf("sfree block %d addr %p\n", trace[i].index, ptrs[trace[i].index]); } if(sfree(ptrs[trace[i].index])) { fprintf(stderr, "Error: free block %d addr %p\n", trace[i].index, ptrs[trace[i].index]); } break; default : fprintf(stderr, "ERROR: bad type of instruction %c for block %d\n", trace[i].type, trace[i].index); } } if(pr) { printf("Total free mem: %d\n", total_space(freelist)); printf("Total allocated mem: %d\n", total_space(allocated_list)); printf("List of allocated blocks:\n"); print_allocated(); printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); } else { printf("Total free mem: %d\n", total_space(freelist)); printf("Total allocated mem: %d\n", total_space(allocated_list)); printf("Number of allocated blocks: %d\n", num_blocks(allocated_list)); } mem_clean(); return 0; }
/* Test for smalloc and sfree. * Test covers the following scenarios for memory allocation: * - allocation where required size of memory exactly fits free block * - allocation where the first available free block is larger than the required size * resulting in a split. * - multiple allocation that results in consumption of all available memory and * further allocation results in error. * - freeing blocks of memory that are not beside eachother. * - freeing blocks that have a shared edge which results in merge of multiple blocks. * - Reuse of freed merged blocks. * - Releasing all allocated memory. */ int main(void) { mem_init(SIZE); char *ptrs[12]; int i; /* Call smalloc 6 times. */ /* Reference in the comments to individual blocks starts at 1 */ int num_bytes = 10; for(i = 0; i < 6; i++) { ptrs[i] = smalloc(num_bytes); write_to_mem(num_bytes, ptrs[i], i); } /* Should diplay 6 blocks of reserved memory, 10 bytes each in allocated_list*/ printf("List of allocated blocks:\n"); print_allocated(); printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); /* Frees the 2nd and 3rd blocks allocated.*/ printf("freeing %p result = %d\n", ptrs[1], sfree(ptrs[1])); printf("freeing %p result = %d\n", ptrs[2], sfree(ptrs[2])); /* 2nd and 3rd blocks should be removed from allocated_list */ printf("List of allocated blocks:\n"); print_allocated(); /* freelist now should have ONE NEW free block: the result of freeing the 2nd and 3rd block and merging them to produce a free block of size '20 bytes' */ printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); /* Frees the 5th block */ printf("freeing %p result = %d\n", ptrs[4], sfree(ptrs[4])); printf("List of allocated blocks:\n"); print_allocated(); /* ONE new free block (5th - 10 bytes) located in sequence AFTER 2nd-3rd blocks (20 bytes), and BEFORE unused block left over (size = 80 - 60 = 20 bytes)*/ printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); /* Frees the 4th block */ printf("freeing %p result = %d\n", ptrs[3], sfree(ptrs[3])); printf("List of allocated blocks:\n"); print_allocated(); /* 4th block should merge with 2nd-3rd and 5th to produce ONE 40 byte block. */ printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); /* Allocate 7th block with size 35 bytes*/ ptrs[6] = smalloc(35); printf("allocating %p size = %d\n", ptrs[6], 35); write_to_mem(35, ptrs[6], 6); /* For 7th block, 40 byte block of memory freed previously should be reused. Since 40 available bytes > 35 required bytes, 40 block will be SPLIT. The result will be 35 byte block in allocated_list, and 5 byte remainder in freelist.*/ printf("List of allocated blocks:\n"); print_allocated(); printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); /* Allocate 8th block with size 20 bytes*/ ptrs[7] = smalloc(20); printf("allocating %p size = %d\n", ptrs[7], 20); write_to_mem(20, ptrs[7], 7); printf("List of allocated blocks:\n"); print_allocated(); /* freelist should contain only 5 byte block. */ printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); /* Allocate 9th block with size 5 bytes*/ ptrs[8] = smalloc(5); printf("allocating %p size = %d\n", ptrs[8], 5); write_to_mem(5, ptrs[8], 8); printf("List of allocated blocks:\n"); print_allocated(); /* freelist should be empty. */ printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); /* Memory now full*/ /* Allocate 10th block with size 30 bytes*/ ptrs[9] = smalloc(30); printf("allocating for ptrs[9] size = %d. Expected: NULL. Result: %p\n", 30, ptrs[9]); /* Allocate 11th block with size 1 byte*/ ptrs[10] = smalloc(1); printf("allocating for ptrs[10] size = %d. Expected: NULL. Result: %p\n", 1, ptrs[10]); /* Unchanged */ printf("List of allocated blocks:\n"); print_allocated(); printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); /* Free all. Expected: no crash */ for (i = 0; i < 11; i++){ printf("freeing ptrs[%d] %p, result = %d\n", i, ptrs[i], sfree(ptrs[i])); } /* Restart: 1 free block of 80 total SIZE bytes*/ printf("List of allocated blocks:\n"); print_allocated(); printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); /* Clean all */ printf("Cleaning memory\n"); mem_clean(); /* Each print-out should be blank.*/ printf("List of allocated blocks:\n"); print_allocated(); printf("List of free blocks:\n"); print_free(); printf("Contents of allocated memory:\n"); print_mem(); return 0; }