Debian Codegraph @

src/bin/dwarfdump/dd_helpertree.c

raw
1 /*
2 Copyright 2015-2016 David Anderson. All rights reserved.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of version 2 of the GNU General
6 Public License as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it would be
9 useful, but WITHOUT ANY WARRANTY; without even the implied
10 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE.
12
13 Further, this software is distributed without any warranty
14 that it is free of the rightful claim of any third person
15 regarding infringement or the like. Any license provided
16 herein, whether implied or otherwise, applies only to this
17 software file. Patent licenses, if any, provided herein
18 do not apply to combinations of this program with other
19 software, or any other product whatsoever.
20
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write the Free
23 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 Boston MA 02110-1301, USA.
25
26 */
27
28 #include <config.h>
29
30 #include <stddef.h> /* NULL */
31 #include <stdlib.h> /* calloc() free() */
32
33 #include "dwarf.h"
34 #include "libdwarf.h"
35 #include "dd_defined_types.h"
36 #include "dd_checkutil.h"
37 #include "dd_glflags.h"
38 #include "dd_globals.h"
39 #include "dd_tsearchbal.h"
40 #include "dd_helpertree.h"
41 #include "libdwarf_private.h" /* For malloc/calloc debug */
42
43 #define TRUE 1
44 #define FALSE 0
45 /* WARNING: the tree walk functions will, if presented **tree
46 when *tree is wanted, simply find nothing. No error,
47 just bad results. So when a walk produces nothing
48 suspect a code mistake here.
49 The basic problem is void* is a terrible way to
50 pass in a pointer. But it's how tsearch was defined
51 long ago.
52 */
53 /* For .debug_info (not for tied file) */
54 struct Helpertree_Base_s helpertree_offsets_base_info;
55 /* For .debug_types (not for tied file) */
56 struct Helpertree_Base_s helpertree_offsets_base_types;
57
58 static struct Helpertree_Map_Entry_s *
59 helpertree_map_create_entry(Dwarf_Unsigned offset,
60 int val)
61 {
62 struct Helpertree_Map_Entry_s *mp =
63 (struct Helpertree_Map_Entry_s *)
64 calloc(1,sizeof(struct Helpertree_Map_Entry_s));
65 if (!mp) {
66 return 0;
67 }
68 mp->hm_key = offset;
69 mp->hm_val = val;
70 return mp;
71 }
72 static void
73 helpertree_map_free_func(void *mx)
74 {
75 struct Helpertree_Map_Entry_s *m = mx;
76 free(m);
77 }
78
79 static int
80 helpertree_map_compare_func(const void *l, const void *r)
81 {
82 const struct Helpertree_Map_Entry_s *ml = l;
83 const struct Helpertree_Map_Entry_s *mr = r;
84 if (ml->hm_key < mr->hm_key) {
85 return -1;
86 }
87 if (ml->hm_key > mr->hm_key) {
88 return 1;
89 }
90 return 0;
91 }
92 static void
93 helpertree_map_destroy(void *map)
94 {
95 /* tdestroy is not part of Posix. */
96 dwarf_tdestroy(map,helpertree_map_free_func);
97 }
98
99 /* Globally-visible functions follow this line. */
100
101 struct Helpertree_Map_Entry_s *
102 helpertree_add_entry(Dwarf_Unsigned offset,
103 int val,struct Helpertree_Base_s *base)
104 {
105 void *retval = 0;
106 struct Helpertree_Map_Entry_s *re = 0;
107 struct Helpertree_Map_Entry_s *e;
108 void **tree1 = 0;
109
110 tree1 = &base->hb_base;
111 e = helpertree_map_create_entry(offset,val);
112 /* tsearch records e's contents unless e
113 is already present . We must not free it till
114 destroy time if it got added to tree1. */
115 retval = dwarf_tsearch(e,tree1, helpertree_map_compare_func);
116 if (retval) {
117 re = *(struct Helpertree_Map_Entry_s **)retval;
118 if (re != e) {
119 /* We returned an existing record, e not needed.
120 Set val. */
121 re->hm_val = val;
122 helpertree_map_free_func(e);
123 } else {
124 /* Record e got added to tree1, do not free record e. */
125 }
126 return retval;
127 }
128 return NULL;
129 }
130
131 struct Helpertree_Map_Entry_s *
132 helpertree_find(Dwarf_Unsigned offset,struct Helpertree_Base_s *base)
133 {
134 void *retval = 0;
135 struct Helpertree_Map_Entry_s *re = 0;
136 struct Helpertree_Map_Entry_s *e = 0;
137
138 e = helpertree_map_create_entry(offset,0);
139 retval = dwarf_tfind(e,&base->hb_base,
140 helpertree_map_compare_func);
141 if (retval) {
142 re = *(struct Helpertree_Map_Entry_s **)retval;
143 }
144 /* The one we created here must be deleted, it is dead.
145 We look at the returned one instead. */
146 helpertree_map_free_func(e);
147 return re;
148 }
149
150 void
151 helpertree_clear_statistics(struct Helpertree_Base_s *base)
152 {
153 if (!base) {
154 return;
155 }
156 if (!base->hb_base) {
157 return;
158 }
159 helpertree_map_destroy(base->hb_base);
160 base->hb_base = 0;
161 }