📄 dmalloc_tab.c
字号:
size2 = last_p - right_first_p; /* is the 1st half small enough to just insert-sort? */ if (size1 < min_qsort_size) { /* use the pivot as our temporary space */ insert_sort(first_p, left_last_p, (unsigned char *)&pivot, ele_size); /* is the 2nd part small as well? */ if (size2 < min_qsort_size) { /* use the pivot as our temporary space */ insert_sort(right_first_p, last_p, (unsigned char *)&pivot, ele_size); /* pop a partition off our stack */ if (split_c == 0) { /* we are done */ return; } split_c--; first_p = firsts[split_c]; last_p = lasts[split_c]; } else { /* we can just handle the right side immediately */ first_p = right_first_p; /* last_p = last_p */ } } else if (size2 < min_qsort_size) { /* use the pivot as our temporary space */ insert_sort(right_first_p, last_p, (unsigned char *)&pivot, ele_size); /* we can just handle the left side immediately */ /* first_p = first_p */ last_p = left_last_p; } else { /* * neither partition is small, we'll have to push the larger one * of them on the stack */ if (split_c >= MAX_QSORT_SPLITS) { /* sanity check here -- we should never get here */ abort(); } if (size1 > size2) { /* push the left partition on the stack */ firsts[split_c] = first_p; lasts[split_c] = left_last_p; split_c++; /* continue handling the right side */ first_p = right_first_p; /* last_p = last_p */ } else { /* push the right partition on the stack */ firsts[split_c] = right_first_p; lasts[split_c] = last_p; split_c++; /* continue handling the left side */ /* first_p = first_p */ last_p = left_last_p; } } }}/* * static void log_slot * * DESCRIPTION: * * Log the information from the memory slot to the logfile. * * RETURNS: * * None. * * ARGUMENTS: * * tab_p -> Pointer to the memory table entry we are dumping. * * in_use_column_b -> Display the in-use numbers in a column. * * source -> Source description string. */static void log_entry(const mem_table_t *tab_p, const int in_use_column_b, const char *source){ if (in_use_column_b) { dmalloc_message("%11lu %6lu %11lu %6lu %s\n", tab_p->mt_total_size, tab_p->mt_total_c, tab_p->mt_in_use_size, tab_p->mt_in_use_c, source); } else { dmalloc_message("%11lu %6lu %s\n", tab_p->mt_total_size, tab_p->mt_total_c, source); }}/* * static void add_entry * * DESCRIPTION: * * Add a memory entry into our memory info total. * * RETURNS: * * None. * * ARGUMENTS: * * total_p <-> Pointer to the memory table entry we are using to total * our used size. * tab_p -> Pointer to the memory table entry we are adding into our total. */static void add_entry(mem_table_t *total_p, const mem_table_t *tab_p){ total_p->mt_total_size += tab_p->mt_total_size; total_p->mt_total_c += tab_p->mt_total_c; total_p->mt_in_use_size += tab_p->mt_in_use_size; total_p->mt_in_use_c += tab_p->mt_in_use_c;}/* * void _dmalloc_table_clear * * DESCRIPTION: * * Clear out the allocation information in our table. We are going to * be loading it with other info. * * RETURNS: * * None. * * ARGUMENTS: * * mem_table -> Memory table we are working on. * * entry_n -> Number of entries in the memory table. * * entry_cp <-> Pointer to an integer which records how many entries * are in the table. */void _dmalloc_table_clear(mem_table_t *mem_table, const int entry_n, int *entry_cp){ /* clear out our memory table */ memset(mem_table, 0, sizeof(mem_table[0]) * entry_n); SET_POINTER(entry_cp, 0);}/* * void _dmalloc_table_insert * * DESCRIPTION: * * Insert a pointer to the table. * * RETURNS: * * None. * * ARGUMENTS: * * mem_table -> Memory table we are working on. * * entry_n -> Number of entries in the memory table. * * file -> File name or return address of the allocation. * * line -> Line number of the allocation. * * size -> Size in bytes of the allocation. * * entry_cp <-> Pointer to an integer which records how many entries * are in the table. */void _dmalloc_table_insert(mem_table_t *mem_table, const int entry_n, const char *file, const unsigned int line, const unsigned long size, int *entry_cp){ unsigned int bucket; mem_table_t *tab_p, *tab_end_p, *tab_bounds_p, *tab_other_p; bucket = which_bucket(entry_n, file, line); tab_p = mem_table + bucket; /* the end is if we come around to the start again */ tab_end_p = tab_p; tab_other_p = mem_table + entry_n - 1; /* our boundary for our hash table is the other slot */ tab_bounds_p = tab_other_p; while (1) { if (tab_p->mt_file == file && tab_p->mt_line == line) { /* we found it */ break; } else if (tab_p->mt_file == NULL) { /* we didn't find it */ break; } tab_p++; if (tab_p == tab_bounds_p) { tab_p = mem_table; } /* we shouldn't have gone through the entire table */ if (tab_p == tab_end_p) { } } /* did we not find the pointer? */ if (tab_p->mt_file == NULL) { if (*entry_cp >= MEMORY_TABLE_SIZE) { /* if the table is too full then add info into other_pointers bucket */ tab_p = tab_other_p; } else { /* we found an open slot */ tab_p->mt_file = file; tab_p->mt_line = line; /* remember its position in the table so we can unsort it later */ tab_p->mt_entry_pos_p = tab_p; (*entry_cp)++; } } /* update the info for the entry */ tab_p->mt_total_size += size; tab_p->mt_total_c++; tab_p->mt_in_use_size += size; tab_p->mt_in_use_c++;}/* * void _dmalloc_table_delete * * DESCRIPTION: * * Remove a pointer from the table. * * RETURNS: * * None. * * ARGUMENTS: * * mem_table -> Memory table we are working on. * * entry_n -> Number of entries in the memory table. * * old_file -> File name or return address of the allocation to * delete. * * old_line -> Line number of the allocation to delete. * * size -> Size in bytes of the allocation. */void _dmalloc_table_delete(mem_table_t *mem_table, const int entry_n, const char *old_file, const unsigned int old_line, const DMALLOC_SIZE size){ unsigned int bucket; int found_b = 0; mem_table_t *tab_p, *tab_end_p, *tab_other_p, *tab_bounds_p; bucket = which_bucket(entry_n, old_file, old_line); tab_p = mem_table + bucket; /* the end is if we come around to the start again */ tab_end_p = tab_p; tab_other_p = mem_table + entry_n - 1; /* our boundary for our hash table is the other slot */ tab_bounds_p = tab_other_p; do { if (tab_p->mt_file == old_file && tab_p->mt_line == old_line) { /* we found it */ found_b = 1; break; } else if (tab_p->mt_file == NULL) { /* we didn't find it */ break; } tab_p++; if (tab_p == tab_bounds_p) { tab_p = mem_table; } } while (tab_p != tab_end_p); /* did we find the pointer? */ if (! found_b) { /* assume that we should take it out of the other_pointers bucket */ tab_p = tab_other_p; } /* update our pointer info if we can */ if (tab_p->mt_in_use_size >= size && tab_p->mt_in_use_c > 0) { tab_p->mt_in_use_size -= size; tab_p->mt_in_use_c--; }}/* * void _dmalloc_table_log_info * * DESCRIPTION: * * Log information from the memory table to the log file. * * RETURNS: * * None. * * ARGUMENTS: * * mem_table -> Memory table we are working on. * * current_n -> Number of current entries in the memory table. * * entry_n -> Number of total possible entries in the memory table. * * log_n -> Number of entries to log to the file. Set to 0 to * display all entries in the table. * * in_use_column_b -> Display the in-use numbers in a column. */void _dmalloc_table_log_info(mem_table_t *mem_table, const int current_n, const int entry_n, const int log_n, const int in_use_column_b){ mem_table_t *tab_p, *tab_other_p, *tab_bounds_p, total; int entry_c; char source[64]; /* is the table empty */ if (current_n == 0) { dmalloc_message(" memory table is empty"); return; } /* sort the entries by their total-size */ split((unsigned char *)mem_table, (unsigned char *)(mem_table + entry_n - 2), sizeof(mem_table[0])); /* display the column headers */ if (in_use_column_b) { dmalloc_message(" total-size count in-use-size count source"); } else { dmalloc_message(" total-size count source"); } memset(&total, 0, sizeof(total)); tab_other_p = mem_table + entry_n - 1; /* our boundary for our hash table is the other slot */ tab_bounds_p = tab_other_p; entry_c = 0; for (tab_p = mem_table; tab_p < tab_bounds_p; tab_p++) { if (tab_p->mt_file != NULL) { entry_c++; /* can we still print the pointer information? */ if (log_n == 0 || entry_c < log_n) { (void)_dmalloc_chunk_desc_pnt(source, sizeof(source), tab_p->mt_file, tab_p->mt_line); log_entry(tab_p, in_use_column_b, source); } add_entry(&total, tab_p); } } if (current_n >= MEMORY_TABLE_SIZE) { strncpy(source, "Other pointers", sizeof(source)); source[sizeof(source) - 1] = '\0'; log_entry(tab_other_p, in_use_column_b, source); add_entry(&total, tab_other_p); } /* dump our total */ (void)loc_snprintf(source, sizeof(source), "Total of %d", entry_c); log_entry(&total, in_use_column_b, source); /* * If we sorted the array, we have to put it back the way it was if * we want to continue and handle memory transactions. We should be * able to do this in O(N). */ tab_bounds_p = mem_table + entry_n - 1; for (tab_p = mem_table; tab_p < tab_bounds_p;) { mem_table_t swap_entry; /* * If we have a blank slot or if the entry is in the right * position then we can move on to the next one */ if (tab_p->mt_file == NULL || tab_p->mt_entry_pos_p == tab_p) { tab_p++; continue; } /* swap this entry for where it should go and do _not_ increment tab_p */ swap_entry = *tab_p->mt_entry_pos_p; *tab_p->mt_entry_pos_p = *tab_p; *tab_p = swap_entry; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -