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