static void update_column_widths(int * widths, struct tfile * file) { char tmp[256]; int n; /* Links */ TRACE("links"); n = sprintf(tmp, "%d", file->statbuf.st_nlink); if (n > widths[0]) widths[0] = n; /* User */ TRACE("user"); n = print_username(tmp, file->statbuf.st_uid); if (n > widths[1]) widths[1] = n; /* Group */ TRACE("group"); n = print_username(tmp, file->statbuf.st_gid); if (n > widths[2]) widths[2] = n; /* File size */ TRACE("file size"); if (human_readable) { n = print_human_readable_size(tmp, file->statbuf.st_size); } else { n = sprintf(tmp, "%d", (int)file->statbuf.st_size); } if (n > widths[3]) widths[3] = n; }
/** * Initialize the physical page allocator. * * This takes RAM information from the multiboot header * and then sets up some data structures to manage this RAM. */ void init_page_allocator() { /* set all bits to busy .. we will clear bits which actually point to usable RAM */ for (unsigned x = 0; x < UINT_CNT; x++) free_bm[x] = (unsigned)~0; unsigned node_cnt; const ram_map_t *s = get_rammap_ptr(&node_cnt); printk("RAM address map received from multiboot:\n"); for (unsigned i = 0; i < node_cnt; i++) { printk("%d: Start: 0x%8x End:0x%8x :: Size = ", i, s[i].saddr, s[i].saddr + s[i].len - 1); print_human_readable_size(s[i].len); } printk("Area occupied by Ganoid:\n"); printk("Start: 0x%8x\n", GANOID_AREA_START); printk("End : 0x%8x\n", GANOID_AREA_END); /* set the bits which are already occupied. */ for (unsigned i = 0; i < node_cnt; i++) { unsigned next_block_addr = s[i].saddr + s[i].len; if (s[i].saddr <= GANOID_AREA_START && GANOID_AREA_END < next_block_addr) { /* kernel is inside current RAM area */ MDEBUG("This area has ganoid\n"); /* free pages upto kernel start */ if (s[i].saddr != GANOID_AREA_START) fp_clearbit_range(GET_PAGENO(s[i].saddr), GET_PAGENO(GANOID_AREA_START - 1)); if (next_block_addr != GANOID_AREA_END) { MDEBUG("SEQ 0: %x %x\n", GANOID_AREA_END + PAGE_SIZE, next_block_addr - 1); fp_clearbit_range(GET_PAGENO(GANOID_AREA_END + PAGE_SIZE), GET_PAGENO(next_block_addr - 1)); } } else { MDEBUG("\nFull free range: %x to %x\n", s[i].saddr, next_block_addr - 1); fp_clearbit_range(GET_PAGENO(s[i].saddr), GET_PAGENO(next_block_addr - 1)); } } #ifdef CONFIG_PAGE_ALLOC_DEBUG dump_free_info(); #endif /* CONFIG_PAGE_ALLOC_DEBUG */ }
void dump_free_info(void) { unsigned bit; MDEBUG("Free/Alloc map\n\n"); unsigned start = 0; bool init_value = fp_getbit(start); for (bit = 1; bit <= LAST_BIT_NO; bit++) { if ((fp_getbit(bit) != init_value) || (bit == LAST_BIT_NO)) { MDEBUG("0x%8x to 0x%8x :: ", PAGENO_TO_ADDR(start), PAGENO_TO_ADDR(bit) - 1); if (init_value) { MDEBUG("Busy - Size: "); } else { MDEBUG("Free - Size: "); } print_human_readable_size(PAGENO_TO_ADDR(bit) - PAGENO_TO_ADDR(start)); start = bit; init_value = fp_getbit(start); } } MDEBUG("Done\n"); }
/** * A function which changes the size of the hash table * and rehashes all the values currently present in the old hash table * to the new hash table using the updated hash functions. Among others, * this function also updates the value of the stree->tedge_size. * * @param * new_size The desired new size of the hash table. * When this function successfully finishes, it will be set * to the actual new size of the hash table. * @param * text the actual underlying text of the suffix tree * @param * stree the actual suffix tree * * @return On successful reallocation, this function returns 0. * If an error occurs, a positive error number is returned. */ int stree_shti_ht_rehash (size_t *new_size, const character_type *text, suffix_tree_shti *stree) { static const size_t max_rehash_attempts = 1024; /* the first part of the current hash key */ signed_integral_type source_node = 0; /* the second part of the current hash key */ character_type letter = 0; /* the current value in the hash table */ signed_integral_type target_node = 0; /* the original size of the hash table */ size_t original_tedge_size = stree->tedge_size; /* the original hash table data */ edge_record *original_tedge = stree->tedge; size_t original_new_size = (*new_size); size_t i = 0; size_t attempt_number = 0; int rehash_failed = 0; /* * The memory pointed to by stree->tedge is not lost * by the following call(s) to free and calloc, * because it has already been stored as the original_tedge. * * We set it to NULL at first in order to be able to easily allocate * the new memory without worrying about unintentionally freeing * the memory for the current hash table. In fact, we need * what's currently stored in it to create the new hash table * with the same content. */ stree->tedge = NULL; fprintf(stderr, "The rehashing of the hash table will now start.\n"); /* we will be trying to rehash the hash table until we succeed */ do { if (attempt_number == max_rehash_attempts) { fprintf(stderr, "Error: The maximum number of " "attempts to rehash\nthe hash " "table (%zu) has been reached!\n" "The rehash operation has failed " "permanently!\n", max_rehash_attempts); return (1); } ++attempt_number; fprintf(stderr, "Attempt number %zu:\n", attempt_number); rehash_failed = 0; (*new_size) = original_new_size; /* * We update the current hash settings with respect to * the current hash table size. */ if (hs_update(0, new_size, stree->hs) != 0) { fprintf(stderr, "Error: Can not correctly update " "the hash table settings.\n"); return (2); } /* * We deallocate the memory used by the previous attemt * to rehash the hash table. * * it is always safe to delete the NULL pointer, * so we need not to check for it */ free(stree->tedge); stree->tedge = NULL; /* * And now we allocate the new, cleared memory * for the hash table itself. * Note that due to the random access to the hash table, * it is essential that the memory is cleared * before begin used. That's why we use calloc * instead of realloc with stree->tedge set to NULL. * The call to realloc with the first argument * set to NULL is equivalent to malloc, * which does not clear the memory. */ stree->tedge = calloc((*new_size), sizeof (edge_record)); if (stree->tedge == NULL) { perror("calloc(stree->tedge)"); /* resetting the errno */ errno = 0; return (3); } else { /* * Despite that the call to the calloc seems * to have been successful, we reset the errno, * because at least on Mac OS X * it might have changed. */ errno = 0; } stree->tedge_size = (*new_size); /* * we reset the number of edges to zero, * as every insertion increases their number */ stree->edges = 0; for (i = 0; i < original_tedge_size; ++i) { /* if the original hash table record is not empty */ if (er_empty(original_tedge[i]) == 0) { source_node = original_tedge[i].source_node; target_node = original_tedge[i].target_node; if (stree_shti_edge_letter(source_node, &letter, target_node, text, stree) > 0) { fprintf(stderr, "Error: Could not get " "the first letter\n" "of an edge P(%d)--" "\"?\"-->C(%d). " "Exiting!\n", source_node, target_node); return (4); } /* * we insert the same hash table record * to the new hash table at a new position */ if (stree_shti_ht_insert(source_node, letter, target_node, 0, text, stree) != 0) { fprintf(stderr, "Error: Insertion " "of the edge " #ifdef SUFFIX_TREE_TEXT_WIDE_CHAR "P(%d)--\"%lc...\"-->C(%d)" #else "P(%d)--\"%c...\"-->C(%d)" #endif " failed permanently!\n" "This is very unfortunate, " "as we can not continue\n" "to rehash the hash table. " "We will start over again!\n", source_node, letter, target_node); rehash_failed = 1; break; } } } } while (rehash_failed == 1); /* * it is always safe to delete the NULL pointer, * so we need not to check for it */ free(original_tedge); original_tedge = NULL; fprintf(stderr, "Current hash table size:\n%zu cells of %zu " "bytes (totalling %zu bytes, ", stree->tedge_size, stree->er_size, stree->tedge_size * stree->er_size); print_human_readable_size(stderr, stree->tedge_size * stree->er_size); fprintf(stderr, ").\nThe rehashing of the hash table is complete.\n"); return (0); }
static void print_entry_long(int * widths, struct tfile * file) { const char * ansi_color_str = color_str(&file->statbuf); /* file permissions */ if (S_ISLNK(file->statbuf.st_mode)) { printf("l"); } else if (S_ISCHR(file->statbuf.st_mode)) { printf("c"); } else if (S_ISBLK(file->statbuf.st_mode)) { printf("b"); } else if (S_ISDIR(file->statbuf.st_mode)) { printf("d"); } else { printf("-"); } printf( (file->statbuf.st_mode & S_IRUSR) ? "r" : "-"); printf( (file->statbuf.st_mode & S_IWUSR) ? "w" : "-"); if (file->statbuf.st_mode & S_ISUID) { printf("s"); } else { printf( (file->statbuf.st_mode & S_IXUSR) ? "x" : "-"); } printf( (file->statbuf.st_mode & S_IRGRP) ? "r" : "-"); printf( (file->statbuf.st_mode & S_IWGRP) ? "w" : "-"); printf( (file->statbuf.st_mode & S_IXGRP) ? "x" : "-"); printf( (file->statbuf.st_mode & S_IROTH) ? "r" : "-"); printf( (file->statbuf.st_mode & S_IWOTH) ? "w" : "-"); printf( (file->statbuf.st_mode & S_IXOTH) ? "x" : "-"); printf( " %*d ", widths[0], file->statbuf.st_nlink); /* number of links, not supported */ char tmp[100]; print_username(tmp, file->statbuf.st_uid); printf("%-*s ", widths[1], tmp); print_username(tmp, file->statbuf.st_gid); printf("%-*s ", widths[2], tmp); if (human_readable) { print_human_readable_size(tmp, file->statbuf.st_size); printf("%*s ", widths[3], tmp); } else { printf("%*d ", widths[3], (int)file->statbuf.st_size); } char time_buf[80]; struct tm * timeinfo = localtime((time_t*)&file->statbuf.st_mtime); if (timeinfo->tm_year == this_year) { strftime(time_buf, 80, "%b %d %H:%M", timeinfo); } else { strftime(time_buf, 80, "%b %d %Y", timeinfo); } printf("%s ", time_buf); /* Print the file name */ if (stdout_is_tty) { printf("\033[%sm%s\033[0m", ansi_color_str, file->name); if (S_ISLNK(file->statbuf.st_mode)) { const char * s = color_str(&file->statbufl); printf(" -> \033[%sm%s\033[0m", s, file->link); } } else { printf("%s", file->name); if (S_ISLNK(file->statbuf.st_mode)) { printf(" -> %s", file->link); } } printf("\n"); }
static void print_entry_long(int * widths, struct tfile * file) { const char * ansi_color_str; if (S_ISDIR(file->statbuf.st_mode)) { // Directory ansi_color_str = DIR_COLOR; } else if (file->statbuf.st_mode & S_ISUID) { ansi_color_str = SETUID_COLOR; } else if (file->statbuf.st_mode & 0111) { // Executable ansi_color_str = EXE_COLOR; } else if (S_ISBLK(file->statbuf.st_mode) || S_ISCHR(file->statbuf.st_mode)) { /* Device file */ ansi_color_str = DEVICE_COLOR; } else { // Something else ansi_color_str = REG_COLOR; } /* file permissions */ if (S_ISLNK(file->statbuf.st_mode)) { printf("l"); } else if (S_ISCHR(file->statbuf.st_mode)) { printf("c"); } else if (S_ISBLK(file->statbuf.st_mode)) { printf("b"); } else if (S_ISDIR(file->statbuf.st_mode)) { printf("d"); } else { printf("-"); } printf( (file->statbuf.st_mode & S_IRUSR) ? "r" : "-"); printf( (file->statbuf.st_mode & S_IWUSR) ? "w" : "-"); if (file->statbuf.st_mode & S_ISUID) { printf("s"); } else { printf( (file->statbuf.st_mode & S_IXUSR) ? "x" : "-"); } printf( (file->statbuf.st_mode & S_IRGRP) ? "r" : "-"); printf( (file->statbuf.st_mode & S_IWGRP) ? "w" : "-"); printf( (file->statbuf.st_mode & S_IXGRP) ? "x" : "-"); printf( (file->statbuf.st_mode & S_IROTH) ? "r" : "-"); printf( (file->statbuf.st_mode & S_IWOTH) ? "w" : "-"); printf( (file->statbuf.st_mode & S_IXOTH) ? "x" : "-"); printf( " %*d ", widths[0], file->statbuf.st_nlink); /* number of links, not supported */ char tmp[100]; print_username(tmp, file->statbuf.st_uid); printf("%-*s ", widths[1], tmp); print_username(tmp, file->statbuf.st_gid); printf("%-*s ", widths[2], tmp); if (human_readable) { print_human_readable_size(tmp, file->statbuf.st_size); printf("%*s ", widths[3], tmp); } else { printf("%*d ", widths[3], file->statbuf.st_size); } char time_buf[80]; struct tm * timeinfo = localtime(&file->statbuf.st_mtime); if (timeinfo->tm_year == this_year) { strftime(time_buf, 80, "%b %d %H:%M", timeinfo); } else { strftime(time_buf, 80, "%b %d %Y", timeinfo); } printf("%s ", time_buf); /* Print the file name */ if (stdout_is_tty) { printf("\033[%sm%s\033[0m", ansi_color_str, file->name); } else { printf("%s", file->name); } printf("\n"); }