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