dwarf_alloc.c 27.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
/*
	dwarf_alloc.c
	$Revision: 1.1.1.1 $   $Date: 2002/05/02 03:29:20 $
*/

#include <sys/types.h>
#include <malloc.h>

#include <stdlib.h>
#include <stdio.h>
#include "dwarf_incl.h"
#include "bstring.h"

/*
    These files are included to get the sizes
    of structs to set the ah_alloc_size field
    of the Dwarf_Alloc_Hdr_s structs for each
    allocation type.
*/
#include "dwarf_line.h"
#include "dwarf_global.h"
#include "dwarf_arange.h"
#include "dwarf_abbrev.h"
#include "dwarf_die_deliv.h"
#include "dwarf_frame.h"
#include "dwarf_loc.h"
#include "dwarf_funcs.h"
#include "dwarf_types.h"
#include "dwarf_vars.h"
#include "dwarf_weaks.h"


/*
    This function is given a pointer to the header
    structure that is used to allocate 1 struct of
    the type given by alloc_type.  It first checks
    if a struct is available in its free list.  If
    not, it checks if 1 is available in its blob, 
    which is a chunk of memory that is reserved for
    its use.  If not, it malloc's a chunk.  The
    initial part of it is used to store the end
    address of the chuck, and also to keep track 
    of the number of free structs in that chunk.
    This information is used for freeing the chunk
    when all the structs in it are free.

    Assume all input arguments have been validated.

    This function can be used only to allocate 1
    struct of the given type.

    It returns a pointer to the struct that the
    user can use.  It returns NULL only when it
    is out of free structs, and cannot malloc 
    any more.  The struct returned is zero-ed.

    A pointer to the chunk that the struct belongs
    to is stored in the bytes preceding the
    returned address.  Since this pointer it
    never overwritten, when a struct is allocated
    from the free_list this pointer does not
    have to be written.  In the 2 other cases,
    where the struct is allocated from a new
    chunk, or the blob, a pointer to the chunk
    is written.
*/
Dwarf_Ptr 
_dwarf_find_memory (
    Dwarf_Alloc_Hdr	alloc_hdr,
    Dwarf_Small		alloc_type
)
{
	/* Pointer to the struct allocated. */
    Dwarf_Small		*ret_mem;

	/* Pointer to info about chunks allocated. */
    Dwarf_Alloc_Area	alloc_area;

	/* Size of chunk malloc'ed when no free structs left. */
    Dwarf_Signed	mem_block_size;

	/* Pointer to block malloc'ed. */
    Dwarf_Small		*mem_block;

    /* ***** BEGIN CODE ***** */

	/* 
	    If every struct in the chunks malloc'ed is busy, go
	    directly to mallocing a new chunk.  Otherwise, check
	    the alloc_area from which the last allocation was
	    made.  If that is not succesful, then search the list
	    of alloc_area's from alloc_header.
	*/
    if (alloc_hdr->ah_struct_alloc_count > 0 &&
	alloc_hdr->ah_struct_alloc_count % alloc_hdr->ah_alloc_num == 0) 
	alloc_area = NULL;
    else {
        alloc_area = alloc_hdr->ah_last_alloc_area;
        if (alloc_area == NULL || alloc_area->aa_free_count == 0)
            for (alloc_area = alloc_hdr->ah_alloc_area_head;
	        alloc_area != NULL && alloc_area->aa_free_count == 0;
	        alloc_area = alloc_area->aa_next);
    }

    if (alloc_area != NULL) {
        alloc_area->aa_free_count--;

        if (alloc_area->aa_free_list != NULL) {
	    ret_mem = alloc_area->aa_free_list;

	        /* 
		    Update the free list.  The initial part of the struct
	            is used to hold a pointer to the next struct on the free
	            list.  In this way, the free list chain is maintained at
	            0 memory cost.
	        */
	    alloc_area->aa_free_list = 
	        ((Dwarf_Free_List)alloc_area->aa_free_list)->fl_next;
        }

        else if (alloc_area->aa_blob_start < alloc_area->aa_blob_end) {
	    ret_mem = alloc_area->aa_blob_start;

		/* 
		    Store pointer to chunk this struct belongs to in the 
		    first few bytes.  Return pointer to bytes after this
		    pointer storage.
		*/
	    *(Dwarf_Alloc_Area *)ret_mem = alloc_area;
	    ret_mem += _DW_RESERVE;

	    alloc_area->aa_blob_start += alloc_hdr->ah_alloc_size;
        }
    }

	/* New memory has to malloc'ed since there are no free structs. */
    else {

	    /* 
		Increment count of chunks currently malloc'ed
	        for this allocation type, and update the maximum
	        count if is smaller than the current count.
	    */
	alloc_hdr->ah_curr_alloc++;
	if (alloc_hdr->ah_curr_alloc > alloc_hdr->ah_max_alloc)
	    alloc_hdr->ah_max_alloc = alloc_hdr->ah_curr_alloc;

	    /* 
		Allocate memory to contain the required number 
		of structs and the Dwarf_Alloc_Area_s to control
		it.
	    */
	mem_block_size = alloc_hdr->ah_malloc_length + 
	    ROUND_SIZE(sizeof(struct Dwarf_Alloc_Area_s));

	mem_block = malloc(mem_block_size);
	if (mem_block == NULL) {
	    return(NULL);
	}

	    /* 
		Zero out the Dwarf_Alloc_Area_s part of the
	        chunk.  The rest is zero-ed out as each struct
	        is returned to the user.
	    */
        bzero(mem_block, ROUND_SIZE(sizeof(struct Dwarf_Alloc_Area_s)));

	    /*
		Attach the Dwarf_Alloc_Area_s struct to
		the list of chunks malloc'ed for this
		struct type.
	    */
	alloc_area = (Dwarf_Alloc_Area)mem_block;
	if (alloc_hdr->ah_alloc_area_head != NULL)
	    alloc_hdr->ah_alloc_area_head->aa_prev = alloc_area;
	alloc_area->aa_next = alloc_hdr->ah_alloc_area_head;
	alloc_hdr->ah_alloc_area_head = alloc_area;

	alloc_area->aa_alloc_hdr = alloc_hdr;
	alloc_area->aa_free_count = alloc_hdr->ah_alloc_num - 1;

	    /* 
		The struct returned begins immediately after
	        the Dwarf_Alloc_Area_s struct.
	    */
	ret_mem = mem_block + 
		ROUND_SIZE(sizeof(struct Dwarf_Alloc_Area_s));
	alloc_area->aa_blob_start = ret_mem + alloc_hdr->ah_alloc_size;
	alloc_area->aa_blob_end = mem_block + mem_block_size;

	    /* 
	        Store pointer to chunk this struct belongs to in the 
	        first few bytes.  Return pointer to bytes after this
	        pointer storage.
	    */
	*(Dwarf_Alloc_Area *)ret_mem = alloc_area;
    	ret_mem += _DW_RESERVE;
    }

    alloc_hdr->ah_last_alloc_area = alloc_area;
    alloc_hdr->ah_struct_alloc_count++;
    bzero(ret_mem, alloc_hdr->ah_alloc_size - _DW_RESERVE);
    return(ret_mem);
}


/*
    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 is 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;

    if (dbg == NULL || dbg->de_alloc_hdr == NULL) 
	return(NULL);

    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_STRING && alloc_type <= MAX_DW_DLA) {
	alloc_hdr = dbg->de_alloc_hdr[alloc_type];
	if (alloc_hdr != NULL)
	    return(_dwarf_find_memory(alloc_hdr, alloc_type));
	
	/*  
	    This is for situations where there is a Dwarf Error
	    even before _dwarf_setup_debug() has executed, namely 
	    in _dwarf_setup().  The reason _dwarf_setup_debug() 
	    has to execute after _dwarf_setup() is to take advantage 
	    of the size information that _dwarf_setup() can provide.  
	    In this case, a Dwarf_Error_s struct is simply malloc'ed.  
	    This memory will most probably leak.
	*/
	if (alloc_type == DW_DLA_ERROR)
	    size = sizeof(struct Dwarf_Error_s);
	else
	    return(NULL);
    }

    ret_mem = malloc(size);
    if (ret_mem != NULL) 
	bzero(ret_mem, size);

    return(ret_mem);
}


/*
    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;

    if (dbg == NULL || dbg->de_alloc_hdr == NULL || space == NULL) {
	return;
    }

	/* 
	    A string pointer may point into .debug_info or 
	    .debug_string.  Otherwise, they are directly malloc'ed.
	*/
    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 ((Dwarf_Small *)space >= dbg->de_debug_line &&
	    (Dwarf_Small *)space < dbg->de_debug_line + dbg->de_debug_line_size)
	    return;

	if ((Dwarf_Small *)space >= dbg->de_debug_pubnames &&
	    (Dwarf_Small *)space < 
	    dbg->de_debug_pubnames + dbg->de_debug_pubnames_size)
	    return;

	if ((Dwarf_Small *)space >= dbg->de_debug_frame &&
	    (Dwarf_Small *)space < 
	    dbg->de_debug_frame + dbg->de_debug_frame_size)
	    return;

	if ((Dwarf_Small *)space >= dbg->de_debug_str &&
	    (Dwarf_Small *)space < dbg->de_debug_str + dbg->de_debug_str_size)
	    return;

	if ((Dwarf_Small *)space >= dbg->de_debug_funcnames &&
	    (Dwarf_Small *)space < 
	    dbg->de_debug_funcnames + dbg->de_debug_funcnames_size)
	    return;

	if ((Dwarf_Small *)space >= dbg->de_debug_typenames &&
	    (Dwarf_Small *)space < 
	    dbg->de_debug_typenames + dbg->de_debug_typenames_size)
	    return;

	if ((Dwarf_Small *)space >= dbg->de_debug_varnames &&
	    (Dwarf_Small *)space < 
	    dbg->de_debug_varnames + dbg->de_debug_varnames_size)
	    return;

	if ((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;
    }


    if (alloc_type < DW_DLA_STRING || alloc_type > MAX_DW_DLA) {
	return;
    }

	/* Ah_alloc_num equal 1 indicates an unused alloc type. */
    alloc_hdr = dbg->de_alloc_hdr[alloc_type];
    if (alloc_hdr->ah_alloc_num == 1) {
	return;
    }
	/* 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 0
    if (alloc_area == 0) {
	/* ERROR!  This should not be null!
	   We can either abort, or let it coredump below.
	   Or return, pretending all is well.
	   We go on, letting program crash. Is caller error.
	*/
    }
#endif

	/* 
	    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)
	return;

    alloc_hdr->ah_struct_alloc_count--;
    alloc_area->aa_free_count++;

	/*
	    Give chunk back to malloc only when every struct is
	    free'd, and there is more than one chunk malloc'ed
	    for the type.
	*/
    if (alloc_area->aa_free_count == alloc_hdr->ah_alloc_num &&
	alloc_hdr->ah_struct_alloc_count > 0) {

	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_curr_alloc--;

	if (alloc_area == alloc_hdr->ah_last_alloc_area)
	    alloc_hdr->ah_last_alloc_area = NULL;
	free(alloc_area); 
    }

    else {
	((Dwarf_Free_List)space)->fl_next = alloc_area->aa_free_list;
	alloc_area->aa_free_list = space;
    }
}


/*
    Allocates space for a Dwarf_Debug_s struct,
    since one does not exist.
*/
Dwarf_Debug
_dwarf_get_debug (
    void
)
{
    Dwarf_Debug		dbg;

    dbg = (Dwarf_Debug)malloc(sizeof(struct Dwarf_Debug_s));
    if (dbg == NULL) 
	return(NULL);
    else 
	bzero(dbg, sizeof(struct Dwarf_Debug_s));

    return(dbg);
}


/*
    Sets up the Dwarf_Debug_s struct for all the
    allocation types currently defined.  
    Allocation types DW_DLA_STRING, DW_DLA_LIST,
    DW_DLA_FRAME_BLOCK, DW_DLA_LOC_BLOCK are 
    malloc'ed directly.

    This routine should be called after _dwarf_setup(),
    so that information about the sizes of the Dwarf
    sections can be used to decide the number of
    structs of each type malloc'ed.

    Also DW_DLA_ELLIST, DW_DLA_BOUNDS, DW_DLA_TYPE,
    DW_DLA_SUBSCR, DW_DLA_LINEBUF allocation types
    are currently not used.  Also, DW_DLA_DEBUG
    structs cannot be deallocated using this routine.
    The ah_alloc_size and ah_alloc_num fields for
    these types have been set to 1 for efficiency
    in dwarf_get_alloc().
 
    Ah_alloc_num should be greater than 1 for all
    types that are currently being used.

    Therefore, for these allocation types the
    ah_alloc_size, and ah_alloc_num fields do not
    need to be initialized.

    Being an internal routine, assume proper dbg.
*/
Dwarf_Debug
_dwarf_setup_debug (
    Dwarf_Debug		dbg
)
{
    Dwarf_Alloc_Hdr	alloc_hdr;

    Dwarf_Shalf		i;


    for (i = 1; i <= MAX_DW_DLA; i++) {

	alloc_hdr = (Dwarf_Alloc_Hdr)malloc(sizeof(struct Dwarf_Alloc_Hdr_s));
	if (alloc_hdr == NULL) 
	    return(NULL);
	else 
	    bzero(alloc_hdr, sizeof(struct Dwarf_Alloc_Hdr_s));

	dbg->de_alloc_hdr[i] = alloc_hdr;

	switch (i) {
	    case DW_DLA_STRING :
		/* not actually used */
		alloc_hdr->ah_alloc_size = 1;
		alloc_hdr->ah_alloc_num = 1;
		alloc_hdr->ah_malloc_length = 1;
		break;

	    case DW_DLA_LOC :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(Dwarf_Loc));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 
		    64 * ROUND_SIZE_WITH_POINTER(sizeof(Dwarf_Loc));
		break;

	    case DW_DLA_LOCDESC :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(Dwarf_Locdesc));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 
		    64 * ROUND_SIZE_WITH_POINTER(sizeof(Dwarf_Locdesc));
		break;

	    case DW_DLA_ELLIST :
		alloc_hdr->ah_alloc_size = 1;
		alloc_hdr->ah_alloc_num = 1;
		alloc_hdr->ah_malloc_length = 1;
		break;

	    case DW_DLA_BOUNDS :
		alloc_hdr->ah_alloc_size = 1;
		alloc_hdr->ah_alloc_num = 1;
		alloc_hdr->ah_malloc_length = 1;
		break;

	    case DW_DLA_BLOCK :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(Dwarf_Block));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 
		    64 * ROUND_SIZE_WITH_POINTER(sizeof(Dwarf_Block));
		break;
	    
	    case DW_DLA_DEBUG :
		alloc_hdr->ah_alloc_size = 1;
		alloc_hdr->ah_alloc_num = 1;
		alloc_hdr->ah_malloc_length = 1;
		break;

	    case DW_DLA_DIE :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Die_s));
		alloc_hdr->ah_alloc_num = 128;
		alloc_hdr->ah_malloc_length = 
		    128 * ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Die_s));
		break;

	    case DW_DLA_LINE :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Line_s));
		alloc_hdr->ah_alloc_num = 128;
		alloc_hdr->ah_malloc_length = 
		    128 * ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Line_s));
		break;

	    case DW_DLA_ATTR :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Attribute_s));
		alloc_hdr->ah_alloc_num = 256;
		alloc_hdr->ah_malloc_length = 256 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Attribute_s));
		break;

	    case DW_DLA_TYPE :
		alloc_hdr->ah_alloc_size = 1;
		alloc_hdr->ah_alloc_num = 1;
		alloc_hdr->ah_malloc_length = 1;
		break;

	    case DW_DLA_SUBSCR :
		alloc_hdr->ah_alloc_size = 1;
		alloc_hdr->ah_alloc_num = 1;
		alloc_hdr->ah_malloc_length = 1;
		break;

	    case DW_DLA_GLOBAL :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Global_s));
		alloc_hdr->ah_alloc_num = 128;
		alloc_hdr->ah_malloc_length = 128 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Global_s));
		break;

	    case DW_DLA_ERROR :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Error_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 64 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Error_s));
		break;

	    case DW_DLA_LIST :
		/* not actually used */
		alloc_hdr->ah_alloc_size = 1;
		alloc_hdr->ah_alloc_num = 1;
		alloc_hdr->ah_malloc_length = 1;
		break;

	    case DW_DLA_LINEBUF :
		alloc_hdr->ah_alloc_size = 1;
		alloc_hdr->ah_alloc_num = 1;
		alloc_hdr->ah_malloc_length = 1;
		break;

	    case DW_DLA_ARANGE :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Arange_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 64 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Arange_s));
		break;

	    case DW_DLA_ABBREV :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Abbrev_s));
		alloc_hdr->ah_alloc_num = 128;
		alloc_hdr->ah_malloc_length = 128 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Abbrev_s));
		break;

	    case DW_DLA_ABBREV_LIST :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Abbrev_List_s));
		alloc_hdr->ah_alloc_num = 128;
		alloc_hdr->ah_malloc_length = 128 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Abbrev_List_s));
		break;

	    case DW_DLA_CHAIN :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Chain_s));
		alloc_hdr->ah_alloc_num = 128;
		alloc_hdr->ah_malloc_length = 128 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Chain_s));
		break;
	    
	    case DW_DLA_CU_CONTEXT :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_CU_Context_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 64 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_CU_Context_s));
		break;

	    case DW_DLA_FRAME :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Frame_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 64 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Frame_s));
		break;

	    case DW_DLA_FRAME_OP :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(Dwarf_Frame_Op));
		alloc_hdr->ah_alloc_num = 128;
		alloc_hdr->ah_malloc_length = 
		    128 * ROUND_SIZE_WITH_POINTER(sizeof(Dwarf_Frame_Op));
		break;

	    case DW_DLA_CIE :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Cie_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 
		    64 * ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Cie_s));
		break;
	
	    case DW_DLA_FDE :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Fde_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 
		    64 * ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Fde_s));
		break;
	
	    case DW_DLA_GLOBAL_CONTEXT :
		alloc_hdr->ah_alloc_size = 
	        ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Global_Context_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 64 * 
	        ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Global_Context_s));
		break;

	    case DW_DLA_FILE_ENTRY :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_File_Entry_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 64 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_File_Entry_s));
		break;

	    case DW_DLA_LINE_CONTEXT :
		alloc_hdr->ah_alloc_size = 
	        ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Line_Context_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 64 * 
		ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Line_Context_s));
		break;

	    case DW_DLA_LOC_CHAIN :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Loc_Chain_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 64 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Loc_Chain_s));
		break;

	    case DW_DLA_LOC_BLOCK :
		/* not actually used */
		alloc_hdr->ah_alloc_size = 1;
		alloc_hdr->ah_alloc_num = 1;
		alloc_hdr->ah_malloc_length = 1;
		break;

	    case DW_DLA_FRAME_BLOCK :
		/* not actually used */
		alloc_hdr->ah_alloc_size = 1;
		alloc_hdr->ah_alloc_num = 1;
		alloc_hdr->ah_malloc_length = 1;
		break;

	    case DW_DLA_HASH_TABLE :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(ABBREV_HASH_TABLE_SIZE * 2 * sizeof(Dwarf_Abbrev_List));
		alloc_hdr->ah_alloc_num = 32;
		alloc_hdr->ah_malloc_length = 32 * 
		    ROUND_SIZE_WITH_POINTER(ABBREV_HASH_TABLE_SIZE * 2 * sizeof(Dwarf_Abbrev_List));
		break;

	    case DW_DLA_FUNC :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Func_s));
		alloc_hdr->ah_alloc_num = 128;
		alloc_hdr->ah_malloc_length = 128 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Func_s));
		break;

	    case DW_DLA_FUNC_CONTEXT :
		alloc_hdr->ah_alloc_size = 
	        ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Func_Context_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 64 * 
		ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Func_Context_s));
		break;

	    case DW_DLA_TYPENAME :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Type_s));
		alloc_hdr->ah_alloc_num = 128;
		alloc_hdr->ah_malloc_length = 128 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Type_s));
		break;

	    case DW_DLA_TYPENAME_CONTEXT :
		alloc_hdr->ah_alloc_size = 
	        ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Type_Context_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 64 * 
		ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Type_Context_s));
		break;

	    case DW_DLA_VAR :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Var_s));
		alloc_hdr->ah_alloc_num = 128;
		alloc_hdr->ah_malloc_length = 128 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Var_s));
		break;

	    case DW_DLA_VAR_CONTEXT :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Var_Context_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 64 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Var_Context_s));
		break;

	    case DW_DLA_WEAK :
		alloc_hdr->ah_alloc_size = 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Weak_s));
		alloc_hdr->ah_alloc_num = 128;
		alloc_hdr->ah_malloc_length = 128 * 
		    ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Weak_s));
		break;

	    case DW_DLA_ADDR :
		/* not actually used */
		alloc_hdr->ah_alloc_size = 1; 
		alloc_hdr->ah_alloc_num = 1;
		alloc_hdr->ah_malloc_length =  1;
		break;

	    case DW_DLA_WEAK_CONTEXT :
		alloc_hdr->ah_alloc_size = 
	        ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Weak_Context_s));
		alloc_hdr->ah_alloc_num = 64;
		alloc_hdr->ah_malloc_length = 64 * 
		ROUND_SIZE_WITH_POINTER(sizeof(struct Dwarf_Weak_Context_s));
		break;
	}

    }
    return(dbg);
}


/*
    This function prints out the statistics
    collected on allocation of memory chunks.
*/
void 
dwarf_print_memory_stats (
    Dwarf_Debug 	dbg
)
{
    Dwarf_Alloc_Hdr	alloc_hdr;
    Dwarf_Shalf		i;

        /* 
	    Alloc types start at 1, not 0.   
	    Hence, the first NULL string, and 
	    also a size of MAX_DW_DLA + 1.
	*/
    char		*alloc_type_name [MAX_DW_DLA + 1] = {
			    "",	
			    "DW_DLA_STRING",
			    "DW_DLA_LOC",
			    "DW_DLA_LOCDESC",
			    "DW_DLA_ELLIST",
			    "DW_DLA_BOUNDS",
			    "DW_DLA_BLOCK",
			    "DW_DLA_DEBUG",
			    "DW_DLA_DIE",
			    "DW_DLA_LINE",
			    "DW_DLA_ATTR",
			    "DW_DLA_TYPE",
			    "DW_DLA_SUBSCR",
			    "DW_DLA_GLOBAL",
			    "DW_DLA_ERROR",
			    "DW_DLA_LIST",
			    "DW_DLA_LINEBUF",
			    "DW_DLA_ARANGE",
			    "DW_DLA_ABBREV",
			    "DW_DLA_FRAME_OP",
			    "DW_DLA_CIE",
			    "DW_DLA_FDE",
			    "DW_DLA_LOC_BLOCK",
			    "DW_DLA_FRAME_BLOCK",
			    "DW_DLA_FUNC",
			    "DW_DLA_TYPENAME",
			    "DW_DLA_VAR",
			    "DW_DLA_WEAK",
			    "DW_DLA_ADDR",
			    "DW_DLA_ABBREV_LIST",
			    "DW_DLA_CHAIN",
			    "DW_DLA_CU_CONTEXT",
			    "DW_DLA_FRAME",
			    "DW_DLA_GLOBAL_CONTEXT",
			    "DW_DLA_FILE_ENTRY",
			    "DW_DLA_LINE_CONTEXT",
			    "DW_DLA_LOC_CHAIN",
			    "DW_DLA_HASH_TABLE",
			    "DW_DLA_FUNC_CONTEXT",
			    "DW_DLA_TYPENAME_CONTEXT",
			    "DW_DLA_VAR_CONTEXT"
			    "DW_DLA_WEAK_CONTEXT"
			};

    if (dbg == NULL) 
	return;

    if (dbg->de_alloc_hdr == NULL) 
	return;

    printf("Alloc Type                   Max    Curr  Structs\n");
    printf("----------                   ---    ----  -------\n");
    for (i = 1; i <= MAX_DW_DLA; i++) {
	alloc_hdr = dbg->de_alloc_hdr[i];

	if (alloc_hdr != NULL) {
	    printf("%-25s %6d  %6d %8d\n", alloc_type_name[i], 
	        alloc_hdr->ah_max_alloc, alloc_hdr->ah_curr_alloc,
		alloc_hdr->ah_struct_alloc_count);
	}
    }
}


/*
    This function is used to recursively
    free the chunks still allocated, and
    forward chained through the aa_next
    pointer.
*/
static void
_dwarf_recursive_free (
    Dwarf_Alloc_Area	alloc_area
)
{
    if (alloc_area->aa_next != NULL)
	_dwarf_recursive_free(alloc_area->aa_next);

    free(alloc_area);
}


/*
    Used to free all space allocated for this Dwarf_Debug.
    The caller should assume that the Dwarf_Debug pointer 
    itself is no longer valid upon return from this function.

    In case of difficulty, this function simply returns quietly.
*/
int
_dwarf_free_all_of_one_debug (
    Dwarf_Debug 	dbg
)
{
    Dwarf_Alloc_Hdr	alloc_hdr;
    Dwarf_Shalf		i;

    if (dbg == NULL) 
	return(DW_DLV_ERROR);

    if (dbg->de_alloc_hdr == NULL) 
	return(DW_DLV_ERROR);

    for (i = 1; i <= MAX_DW_DLA; i++) {
	alloc_hdr = dbg->de_alloc_hdr[i];

	if (alloc_hdr != NULL) {

	    if (alloc_hdr->ah_alloc_area_head != NULL)
		_dwarf_recursive_free(alloc_hdr->ah_alloc_area_head);

	    free(alloc_hdr);
	}
    }

    free(dbg);
    return(DW_DLV_OK);
}


/*
    The application calls dwarf_transform_to_disk_form()
    and that function, when it has done its work, calls
    this function to free all it can.

    Leaving byte streams and other minimal support structure
    in memory.

*/
void 
_dwarf_free_after_transform_to_disk (
    Dwarf_Debug 	dbg
)
{
    return;
}