xref: /titanic_51/usr/src/lib/libdwarf/common/dwarf_alloc.h (revision 7fd791373689a6af05e27efec3b1ab556e02aa23)
1*7fd79137SRobert Mustacchi /*
2*7fd79137SRobert Mustacchi 
3*7fd79137SRobert Mustacchi   Copyright (C) 2000,2005 Silicon Graphics, Inc.  All Rights Reserved.
4*7fd79137SRobert Mustacchi   Portions Copyright (C) 2008-2010  David Anderson. All Rights Reserved.
5*7fd79137SRobert Mustacchi 
6*7fd79137SRobert Mustacchi   This program is free software; you can redistribute it and/or modify it
7*7fd79137SRobert Mustacchi   under the terms of version 2.1 of the GNU Lesser General Public License
8*7fd79137SRobert Mustacchi   as published by the Free Software Foundation.
9*7fd79137SRobert Mustacchi 
10*7fd79137SRobert Mustacchi   This program is distributed in the hope that it would be useful, but
11*7fd79137SRobert Mustacchi   WITHOUT ANY WARRANTY; without even the implied warranty of
12*7fd79137SRobert Mustacchi   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13*7fd79137SRobert Mustacchi 
14*7fd79137SRobert Mustacchi   Further, this software is distributed without any warranty that it is
15*7fd79137SRobert Mustacchi   free of the rightful claim of any third person regarding infringement
16*7fd79137SRobert Mustacchi   or the like.  Any license provided herein, whether implied or
17*7fd79137SRobert Mustacchi   otherwise, applies only to this software file.  Patent licenses, if
18*7fd79137SRobert Mustacchi   any, provided herein do not apply to combinations of this program with
19*7fd79137SRobert Mustacchi   other software, or any other product whatsoever.
20*7fd79137SRobert Mustacchi 
21*7fd79137SRobert Mustacchi   You should have received a copy of the GNU Lesser General Public
22*7fd79137SRobert Mustacchi   License along with this program; if not, write the Free Software
23*7fd79137SRobert Mustacchi   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
24*7fd79137SRobert Mustacchi   USA.
25*7fd79137SRobert Mustacchi 
26*7fd79137SRobert Mustacchi   Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
27*7fd79137SRobert Mustacchi   Mountain View, CA 94043, or:
28*7fd79137SRobert Mustacchi 
29*7fd79137SRobert Mustacchi   http://www.sgi.com
30*7fd79137SRobert Mustacchi 
31*7fd79137SRobert Mustacchi   For further information regarding this notice, see:
32*7fd79137SRobert Mustacchi 
33*7fd79137SRobert Mustacchi   http://oss.sgi.com/projects/GenInfo/NoticeExplan
34*7fd79137SRobert Mustacchi 
35*7fd79137SRobert Mustacchi */
36*7fd79137SRobert Mustacchi 
37*7fd79137SRobert Mustacchi /* #define DWARF_SIMPLE_MALLOC 1  */
38*7fd79137SRobert Mustacchi 
39*7fd79137SRobert Mustacchi Dwarf_Ptr _dwarf_get_alloc(Dwarf_Debug, Dwarf_Small, Dwarf_Unsigned);
40*7fd79137SRobert Mustacchi Dwarf_Debug _dwarf_get_debug(void);
41*7fd79137SRobert Mustacchi Dwarf_Debug _dwarf_setup_debug(Dwarf_Debug);
42*7fd79137SRobert Mustacchi int _dwarf_free_all_of_one_debug(Dwarf_Debug);
43*7fd79137SRobert Mustacchi 
44*7fd79137SRobert Mustacchi typedef struct Dwarf_Alloc_Area_s *Dwarf_Alloc_Area;
45*7fd79137SRobert Mustacchi typedef struct Dwarf_Free_List_s *Dwarf_Free_List;
46*7fd79137SRobert Mustacchi 
47*7fd79137SRobert Mustacchi /* ALLOC_AREA_INDEX_TABLE_MAX is the size of the
48*7fd79137SRobert Mustacchi    struct ial_s index_into_allocated array in dwarf_alloc.c
49*7fd79137SRobert Mustacchi */
50*7fd79137SRobert Mustacchi #define ALLOC_AREA_INDEX_TABLE_MAX 45
51*7fd79137SRobert Mustacchi /* ALLOC_AREA_REAL_TABLE_MAX is the size of the array needed
52*7fd79137SRobert Mustacchi    to hold pointers to dwarf alloc chunk areas.
53*7fd79137SRobert Mustacchi    It's smaller as some of the index_into_allocated
54*7fd79137SRobert Mustacchi    entries (they look like {0,1,1,0,0} )
55*7fd79137SRobert Mustacchi    are treated specially and don't use 'chunks'.
56*7fd79137SRobert Mustacchi */
57*7fd79137SRobert Mustacchi #define ALLOC_AREA_REAL_TABLE_MAX 32
58*7fd79137SRobert Mustacchi 
59*7fd79137SRobert Mustacchi /*
60*7fd79137SRobert Mustacchi     This struct is used to chain all the deallocated
61*7fd79137SRobert Mustacchi     structs on the free list of each chain.  The structs
62*7fd79137SRobert Mustacchi     are chained internally, by using the memory they
63*7fd79137SRobert Mustacchi     contain.
64*7fd79137SRobert Mustacchi */
65*7fd79137SRobert Mustacchi struct Dwarf_Free_List_s {
66*7fd79137SRobert Mustacchi     Dwarf_Free_List fl_next;
67*7fd79137SRobert Mustacchi };
68*7fd79137SRobert Mustacchi 
69*7fd79137SRobert Mustacchi 
70*7fd79137SRobert Mustacchi /*
71*7fd79137SRobert Mustacchi     This struct is used to manage all the chunks malloc'ed
72*7fd79137SRobert Mustacchi     for a particular alloc_type.  Many of the fields are
73*7fd79137SRobert Mustacchi     initialized by dwarf_init().
74*7fd79137SRobert Mustacchi */
75*7fd79137SRobert Mustacchi struct Dwarf_Alloc_Hdr_s {
76*7fd79137SRobert Mustacchi 
77*7fd79137SRobert Mustacchi     /* Count of actual number of structs user app holds pointers to
78*7fd79137SRobert Mustacchi        currently. */
79*7fd79137SRobert Mustacchi     Dwarf_Sword ah_struct_user_holds;
80*7fd79137SRobert Mustacchi 
81*7fd79137SRobert Mustacchi     /*
82*7fd79137SRobert Mustacchi        Size of each struct that will be allocated for this alloc_type.
83*7fd79137SRobert Mustacchi        Initialized by dwarf_init(). */
84*7fd79137SRobert Mustacchi     Dwarf_Half ah_bytes_one_struct;
85*7fd79137SRobert Mustacchi 
86*7fd79137SRobert Mustacchi     /*
87*7fd79137SRobert Mustacchi        Number of structs of this alloc_type that will be contained in
88*7fd79137SRobert Mustacchi        each chunk that is malloc'ed. Initialized by dwarf_init(). */
89*7fd79137SRobert Mustacchi     Dwarf_Word ah_structs_per_chunk;
90*7fd79137SRobert Mustacchi 
91*7fd79137SRobert Mustacchi     /*
92*7fd79137SRobert Mustacchi        Number of bytes malloc'ed per chunk which is basically
93*7fd79137SRobert Mustacchi        (ah_bytes_one_struct+_DWARF_RESERVE) * ah_alloc_num. */
94*7fd79137SRobert Mustacchi     Dwarf_Word ah_bytes_malloc_per_chunk;
95*7fd79137SRobert Mustacchi 
96*7fd79137SRobert Mustacchi     /* Count of chunks currently allocated for type. */
97*7fd79137SRobert Mustacchi     Dwarf_Sword ah_chunks_allocated;
98*7fd79137SRobert Mustacchi 
99*7fd79137SRobert Mustacchi     /*
100*7fd79137SRobert Mustacchi        Points to a chain of Dwarf_Alloc_Area_s structs that represent
101*7fd79137SRobert Mustacchi        all the chunks currently allocated for the alloc_type. */
102*7fd79137SRobert Mustacchi     Dwarf_Alloc_Area ah_alloc_area_head;
103*7fd79137SRobert Mustacchi 
104*7fd79137SRobert Mustacchi     /* Last Alloc Area that was allocated by malloc. The
105*7fd79137SRobert Mustacchi        free-space-search area looks here first and only if it is full
106*7fd79137SRobert Mustacchi        goes thru the list pointed to by ah_alloc_area_head. */
107*7fd79137SRobert Mustacchi     Dwarf_Alloc_Area ah_last_alloc_area;
108*7fd79137SRobert Mustacchi };
109*7fd79137SRobert Mustacchi 
110*7fd79137SRobert Mustacchi 
111*7fd79137SRobert Mustacchi /*
112*7fd79137SRobert Mustacchi     This struct is used to manage each chunk that is
113*7fd79137SRobert Mustacchi     malloc'ed for a particular alloc_type.  For each
114*7fd79137SRobert Mustacchi     allocation type, the allocation header points to
115*7fd79137SRobert Mustacchi     a list of all the chunks malloc'ed for that type.
116*7fd79137SRobert Mustacchi */
117*7fd79137SRobert Mustacchi struct Dwarf_Alloc_Area_s {
118*7fd79137SRobert Mustacchi 
119*7fd79137SRobert Mustacchi     /* Points to the free list of structs in the chunk. */
120*7fd79137SRobert Mustacchi     Dwarf_Ptr aa_free_list;
121*7fd79137SRobert Mustacchi 
122*7fd79137SRobert Mustacchi     /*
123*7fd79137SRobert Mustacchi        Count of the number of free structs in the chunk. This includes
124*7fd79137SRobert Mustacchi        both those on the free list, and in the blob. */
125*7fd79137SRobert Mustacchi     Dwarf_Sword aa_free_structs_in_chunk;
126*7fd79137SRobert Mustacchi 
127*7fd79137SRobert Mustacchi     /*
128*7fd79137SRobert Mustacchi        Points to the first byte of the blob from which struct will be
129*7fd79137SRobert Mustacchi        allocated.  A struct is put on the free_list only when it
130*7fd79137SRobert Mustacchi        dwarf_deallocated.  Initial allocations are from the blob. */
131*7fd79137SRobert Mustacchi     Dwarf_Small *aa_blob_start;
132*7fd79137SRobert Mustacchi 
133*7fd79137SRobert Mustacchi     /* Points just past the last byte of the blob. */
134*7fd79137SRobert Mustacchi     Dwarf_Small *aa_blob_end;
135*7fd79137SRobert Mustacchi 
136*7fd79137SRobert Mustacchi     /* Points to alloc_hdr this alloc_area is linked to: The owner, in
137*7fd79137SRobert Mustacchi        other words. */
138*7fd79137SRobert Mustacchi     Dwarf_Alloc_Hdr aa_alloc_hdr;
139*7fd79137SRobert Mustacchi 
140*7fd79137SRobert Mustacchi     /*
141*7fd79137SRobert Mustacchi        Used for chaining Dwarf_Alloc_Area_s atructs. Alloc areas are
142*7fd79137SRobert Mustacchi        doubly linked to enable deletion from the list in constant time. */
143*7fd79137SRobert Mustacchi     Dwarf_Alloc_Area aa_next;
144*7fd79137SRobert Mustacchi     Dwarf_Alloc_Area aa_prev;
145*7fd79137SRobert Mustacchi };
146*7fd79137SRobert Mustacchi 
147*7fd79137SRobert Mustacchi struct Dwarf_Error_s *_dwarf_special_no_dbg_error_malloc(void);
148*7fd79137SRobert Mustacchi 
149*7fd79137SRobert Mustacchi #ifdef DWARF_SIMPLE_MALLOC
150*7fd79137SRobert Mustacchi /*
151*7fd79137SRobert Mustacchi    DWARF_SIMPLE_MALLOC is for testing the hypothesis that the existing
152*7fd79137SRobert Mustacchi    complex malloc scheme in libdwarf is pointless complexity.
153*7fd79137SRobert Mustacchi 
154*7fd79137SRobert Mustacchi    DWARF_SIMPLE_MALLOC also makes it easy for a malloc-tracing
155*7fd79137SRobert Mustacchi    tool to verify libdwarf malloc has no botches (though of course
156*7fd79137SRobert Mustacchi    such does not test the complicated standard-libdwarf-alloc code).
157*7fd79137SRobert Mustacchi 
158*7fd79137SRobert Mustacchi */
159*7fd79137SRobert Mustacchi 
160*7fd79137SRobert Mustacchi struct simple_malloc_entry_s {
161*7fd79137SRobert Mustacchi     Dwarf_Small   *se_addr;
162*7fd79137SRobert Mustacchi     unsigned long  se_size;
163*7fd79137SRobert Mustacchi     short          se_type;
164*7fd79137SRobert Mustacchi };
165*7fd79137SRobert Mustacchi #define DSM_BLOCK_COUNT (1000)
166*7fd79137SRobert Mustacchi #define DSM_BLOCK_SIZE (sizeof(struct simple_malloc_entry_s)*DSM_BLOCK_COUNT)
167*7fd79137SRobert Mustacchi 
168*7fd79137SRobert Mustacchi /* we do this so dwarf_dealloc can really free everything */
169*7fd79137SRobert Mustacchi struct simple_malloc_record_s {
170*7fd79137SRobert Mustacchi 	struct simple_malloc_record_s *sr_next;
171*7fd79137SRobert Mustacchi 	int 			       sr_used;
172*7fd79137SRobert Mustacchi 	struct simple_malloc_entry_s   sr_entry[DSM_BLOCK_COUNT];
173*7fd79137SRobert Mustacchi };
174*7fd79137SRobert Mustacchi 
175*7fd79137SRobert Mustacchi 
176*7fd79137SRobert Mustacchi 
177*7fd79137SRobert Mustacchi #endif /* DWARF_SIMPLE_MALLOC */
178