/* DWARF_SIMPLE_MALLOC is for testing the hypothesis that the existing complex malloc scheme in libdwarf is pointless complexity. DWARF_SIMPLE_MALLOC also makes it easy for a malloc-tracing tool to verify libdwarf malloc has no botches (though of course such does not test the complicated standard-libdwarf-alloc code). To properly answer the question, the simple-malloc allocate and delete should be something other than a simple list. Perhaps a heap, or perhaps a red-black tree. */ static void _dwarf_simple_malloc_delete_from_list(Dwarf_Debug dbg, Dwarf_Ptr space, short alloc_type) { if(space == 0) { _dwarf_simple_malloc_botch(6); } if(dbg->de_simple_malloc_base) { struct simple_malloc_record_s *smp = dbg->de_simple_malloc_base; while( smp) { int i; for(i = 0; i < smp->sr_used; ++i) { struct simple_malloc_entry_s *cur; cur = &smp->sr_entry[i]; if(cur->se_addr == space) { if(cur->se_type != alloc_type ) { _dwarf_simple_malloc_botch(0); } cur->se_addr = 0; return; } } smp = smp->sr_next; } } /* Never found the space */ _dwarf_simple_malloc_botch(1); return; }
static void _dwarf_simple_malloc_add_to_list(Dwarf_Debug dbg, Dwarf_Ptr addr, unsigned long size, short alloc_type) { struct simple_malloc_record_s *cur; struct simple_malloc_entry_s *newentry; if (!dbg->de_simple_malloc_base) { /* First entry to this routine. */ dbg->de_simple_malloc_base = malloc(sizeof(struct simple_malloc_record_s)); if (!dbg->de_simple_malloc_base) { _dwarf_simple_malloc_botch(7); return; /* no memory, give up */ } memset(dbg->de_simple_malloc_base, 0, sizeof(struct simple_malloc_record_s)); } cur = dbg->de_simple_malloc_base; if (cur->sr_used >= DSM_BLOCK_COUNT) { /* Better not be > than as that means chaos */ /* Create a new block to link at the head. */ struct simple_malloc_record_s *newblock = malloc(sizeof(struct simple_malloc_record_s)); if (!newblock) { _dwarf_simple_malloc_botch(8); return; /* Can do nothing, out of memory */ } memset(newblock, 0, sizeof(struct simple_malloc_record_s)); /* Link the new block at the head of the chain, and make it 'current' */ dbg->de_simple_malloc_base = newblock; newblock->sr_next = cur; cur = newblock; } newentry = &cur->sr_entry[cur->sr_used]; newentry->se_addr = addr; newentry->se_size = size; newentry->se_type = alloc_type; ++cur->sr_used; }
/* This function is used to deallocate a region of memory that was obtained by a call to _dwarf_get_alloc. Note that though dwarf_dealloc() is a public function, _dwarf_get_alloc() isn't. For lists, typically arrays of pointers, it is assumed that the space was allocated by a direct call to malloc, and so a straight free() is done. This is also the case for variable length blocks such as DW_DLA_FRAME_BLOCK and DW_DLA_LOC_BLOCK. For strings, the pointer might point to a string in .debug_info or .debug_string. After this is checked, and if found not to be the case, a free() is done, again on the assumption that a malloc was used to obtain the space. For other types of structs, a pointer to the chunk that the struct was allocated out of, is present in the bytes preceding the pointer passed in. For this chunk it is checked whether all the structs in that chunk are now free. If so, the entire chunk is free_ed. Otherwise, the space is added to the free list for that chunk, and the free count incremented. This function does not return anything. */ void dwarf_dealloc(Dwarf_Debug dbg, Dwarf_Ptr space, Dwarf_Unsigned alloc_type) { Dwarf_Alloc_Hdr alloc_hdr; Dwarf_Alloc_Area alloc_area; unsigned int type = alloc_type; unsigned int index; if (space == NULL) { return; } if (alloc_type == DW_DLA_ERROR) { /* Get pointer to Dwarf_Alloc_Area this struct came from. See dwarf_alloc.h ROUND_SIZE_WITH_POINTER stuff */ alloc_area = *(Dwarf_Alloc_Area *) ((char *) space - _DW_RESERVE); if (alloc_area == 0) { /* This is the special case of a failed dwarf_init(). Also (and more signficantly) there are a variety of other situations where libdwarf does not *know* what dbg is involved (because of a libdwarf-caller-error) so libdwarf uses NULL as the dbg. Those too wind up here. */ _dwarf_free_special_error(space); return; } } if (dbg == NULL) { /* App error, or an app that failed to succeed in a dwarf_init() call. */ return; } if (type >= ALLOC_AREA_INDEX_TABLE_MAX) { /* internal or user app error */ return; } index = index_into_allocated[alloc_type].ia_al_num; /* A string pointer may point into .debug_info or .debug_string. Otherwise, they are directly malloc'ed. */ if (index == 0) { if (alloc_type == DW_DLA_STRING) { if ((Dwarf_Small *) space >= dbg->de_debug_info && (Dwarf_Small *) space < dbg->de_debug_info + dbg->de_debug_info_size) return; if (dbg->de_debug_line != NULL && (Dwarf_Small *) space >= dbg->de_debug_line && (Dwarf_Small *) space < dbg->de_debug_line + dbg->de_debug_line_size) return; if (dbg->de_debug_pubnames != NULL && (Dwarf_Small *) space >= dbg->de_debug_pubnames && (Dwarf_Small *) space < dbg->de_debug_pubnames + dbg->de_debug_pubnames_size) return; if (dbg->de_debug_frame != NULL && (Dwarf_Small *) space >= dbg->de_debug_frame && (Dwarf_Small *) space < dbg->de_debug_frame + dbg->de_debug_frame_size) return; if (dbg->de_debug_str != NULL && (Dwarf_Small *) space >= dbg->de_debug_str && (Dwarf_Small *) space < dbg->de_debug_str + dbg->de_debug_str_size) return; if (dbg->de_debug_funcnames != NULL && (Dwarf_Small *) space >= dbg->de_debug_funcnames && (Dwarf_Small *) space < dbg->de_debug_funcnames + dbg->de_debug_funcnames_size) return; if (dbg->de_debug_typenames != NULL && (Dwarf_Small *) space >= dbg->de_debug_typenames && (Dwarf_Small *) space < dbg->de_debug_typenames + dbg->de_debug_typenames_size) return; if (dbg->de_debug_varnames != NULL && (Dwarf_Small *) space >= dbg->de_debug_varnames && (Dwarf_Small *) space < dbg->de_debug_varnames + dbg->de_debug_varnames_size) return; if (dbg->de_debug_weaknames != NULL && (Dwarf_Small *) space >= dbg->de_debug_weaknames && (Dwarf_Small *) space < dbg->de_debug_weaknames + dbg->de_debug_weaknames_size) return; free(space); return; } if (alloc_type == DW_DLA_LIST || alloc_type == DW_DLA_FRAME_BLOCK || alloc_type == DW_DLA_LOC_BLOCK || alloc_type == DW_DLA_ADDR) { free(space); return; } /* else is an alloc type that is not used */ /* app or internal error */ #ifdef DWARF_SIMPLE_MALLOC _dwarf_simple_malloc_botch(4); #endif return; } #ifdef DWARF_SIMPLE_MALLOC _dwarf_simple_malloc_delete_from_list(dbg, space, alloc_type); free(space); #else /* !DWARF_SIMPLE_MALLOC */ alloc_hdr = &dbg->de_alloc_hdr[index]; /* Get pointer to Dwarf_Alloc_Area this struct came from. See dwarf_alloc.h ROUND_SIZE_WITH_POINTER stuff */ alloc_area = *(Dwarf_Alloc_Area *) ((char *) space - _DW_RESERVE); /* ASSERT: alloc_area != NULL If NULL we could abort, let it coredump below, or return, pretending all is well. We go on, letting program crash. Is caller error. */ /* Check that the alloc_hdr field of the alloc_area we have is pointing to the right alloc_hdr. This is used to catch use of incorrect deallocation code by the user. */ if (alloc_area->aa_alloc_hdr != alloc_hdr) { /* If we get here, the user has called dwarf_dealloc wrongly or there is some other disastrous error. By leaking mem here we try to be safe... */ #ifdef DEBUG fprintf(stderr, "libdwarf Internal error: type %d hdr mismatch %x %x area ptr %x\n", (int) alloc_type, (int) alloc_area->aa_alloc_hdr, (int) alloc_hdr, (int) alloc_area); #endif return; } alloc_hdr->ah_struct_user_holds--; alloc_area->aa_free_structs_in_chunk++; /* Give chunk back to malloc only when every struct is freed */ if (alloc_area->aa_free_structs_in_chunk == alloc_hdr->ah_structs_per_chunk) { if (alloc_area->aa_prev != NULL) { alloc_area->aa_prev->aa_next = alloc_area->aa_next; } else { alloc_hdr->ah_alloc_area_head = alloc_area->aa_next; } if (alloc_area->aa_next != NULL) { alloc_area->aa_next->aa_prev = alloc_area->aa_prev; } alloc_hdr->ah_chunks_allocated--; if (alloc_area == alloc_hdr->ah_last_alloc_area) { alloc_hdr->ah_last_alloc_area = NULL; } memset(alloc_area,0, sizeof(*alloc_area)); free(alloc_area); } else { ((Dwarf_Free_List) space)->fl_next = alloc_area->aa_free_list; alloc_area->aa_free_list = space; } #endif /* !DWARF_SIMPLE_MALLOC */ }
/* This function returns a pointer to a region of memory. For alloc_types that are not strings or lists of pointers, only 1 struct can be requested at a time. This is indicated by an input count of 1. For strings, count equals the length of the string it will contain, i.e it the length of the string plus 1 for the terminating null. For lists of pointers, count is equal to the number of pointers. For DW_DLA_FRAME_BLOCK, and DW_DLA_LOC_BLOCK allocation types also, count is the count of the number of structs needed. This function cannot be used to allocate a Dwarf_Debug_s struct. */ Dwarf_Ptr _dwarf_get_alloc(Dwarf_Debug dbg, Dwarf_Small alloc_type, Dwarf_Unsigned count) { Dwarf_Alloc_Hdr alloc_hdr; Dwarf_Ptr ret_mem; Dwarf_Signed size = 0; unsigned int index; unsigned int type = alloc_type; if (dbg == NULL) { return (NULL); } if (type >= ALLOC_AREA_INDEX_TABLE_MAX) { /* internal error */ return NULL; } index = index_into_allocated[type].ia_al_num; /* zero also illegal but not tested for */ /* If the Dwarf_Debug is not fully set up, we will get index 0 for any type and must do something. 'Not fully set up' can only happen for DW_DLA_ERROR, I (davea) believe, and for that we call special code here.. */ if (index == 0) { if (alloc_type == DW_DLA_STRING) { size = count; } else if (alloc_type == DW_DLA_LIST) { size = count * sizeof(Dwarf_Ptr); } else if (alloc_type == DW_DLA_FRAME_BLOCK) { size = count * sizeof(Dwarf_Frame_Op); } else if (alloc_type == DW_DLA_LOC_BLOCK) { size = count * sizeof(Dwarf_Loc); } else if (alloc_type == DW_DLA_ADDR) { size = count * (sizeof(Dwarf_Addr) > sizeof(Dwarf_Off) ? sizeof(Dwarf_Addr) : sizeof(Dwarf_Off)); } else if (alloc_type == DW_DLA_ERROR) { return _dwarf_special_no_dbg_error_malloc(); } else { /* If we get here, there is a disastrous programming error somewhere. */ #ifdef DEBUG fprintf(stderr, "libdwarf Internal error: type %d unexpected\n", (int) type); #endif } } else { alloc_hdr = &dbg->de_alloc_hdr[index]; if (alloc_hdr->ah_bytes_one_struct > 0) { #ifdef DWARF_SIMPLE_MALLOC size = alloc_hdr->ah_bytes_one_struct; #else return (_dwarf_find_memory(alloc_hdr)); #endif } else { /* Special case: should not really happen at all. */ if (type == DW_DLA_ERROR) { /* dwarf_init failure. Because dbg is incomplete we won't use it to record the malloc. */ return _dwarf_special_no_dbg_error_malloc(); } else { /* If we get here, there is a disastrous programming error somewhere. */ #ifdef DWARF_SIMPLE_MALLOC _dwarf_simple_malloc_botch(3); #endif #ifdef DEBUG fprintf(stderr, "libdwarf Internal error: Type %d unexpected\n", (int) type); #endif } } } ret_mem = malloc(size); #ifdef DWARF_SIMPLE_MALLOC _dwarf_simple_malloc_add_to_list(dbg,ret_mem,(unsigned long)size, alloc_type); #endif if (ret_mem != NULL) memset(ret_mem,0, size); return (ret_mem); }