Debian Codegraph @

src/bin/dwarfdump/dd_addrmap.c

raw
1 /*
2 Copyright 2010-2012 David Anderson. All rights reserved.
3 Portions Copyright 2012 SN Systems Ltd. All rights reserved.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of version 2 of the GNU General
7 Public License as published by the Free Software Foundation.
8
9 This program is distributed in the hope that it would be
10 useful, but WITHOUT ANY WARRANTY; without even the implied
11 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE.
13
14 Further, this software is distributed without any warranty
15 that it is free of the rightful claim of any third person
16 regarding infringement or the like. Any license provided
17 herein, whether implied or otherwise, applies only to this
18 software file. Patent licenses, if any, provided herein
19 do not apply to combinations of this program with other
20 software, or any other product whatsoever.
21
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write the Free
24 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 Boston MA 02110-1301, USA.
26
27 */
28
29 /* If memory full we do not exit, we just keep going as if
30 all were well. */
31
32 #include <config.h>
33
34 #include <stddef.h> /* NULL */
35 #include <stdlib.h> /* free() malloc() */
36 #include <string.h> /* strdup() */
37
38 /* Windows specific header files */
39 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
40 #include "stdafx.h"
41 #endif /* HAVE_STDAFX_H */
42
43 #include "dwarf.h"
44 #include "libdwarf.h"
45 #include "dd_defined_types.h"
46 #include "dd_checkutil.h"
47 #include "dd_glflags.h"
48 #include "dd_globals.h"
49 #include "dd_addrmap.h"
50 #include "dd_tsearchbal.h"
51 #include "libdwarf_private.h" /* For malloc/calloc debug */
52
53 static struct Addr_Map_Entry *
54 addr_map_create_entry(Dwarf_Unsigned k,char *name)
55 {
56 struct Addr_Map_Entry *mp =
57 (struct Addr_Map_Entry *)malloc(
58 sizeof(struct Addr_Map_Entry));
59 if (!mp) {
60 return 0;
61 }
62 mp->mp_key = k;
63 if (name) {
64 /* Might be zero if malloc fails. Ok. */
65 mp->mp_name = (char *)strdup(name);
66 } else {
67 mp->mp_name = 0;
68 }
69 return mp;
70 }
71 static void
72 addr_map_free_func(void *mx)
73 {
74 struct Addr_Map_Entry *m = mx;
75 if (!m) {
76 return;
77 }
78 free(m->mp_name);
79 m->mp_name = 0;
80 free(m);
81 return;
82 }
83
84 static int
85 addr_map_compare_func(const void *l, const void *r)
86 {
87 const struct Addr_Map_Entry *ml = l;
88 const struct Addr_Map_Entry *mr = r;
89 if (ml->mp_key < mr->mp_key) {
90 return -1;
91 }
92 if (ml->mp_key > mr->mp_key) {
93 return 1;
94 }
95 return 0;
96 }
97 struct Addr_Map_Entry *
98 addr_map_insert( Dwarf_Unsigned addr,char *name,void **tree1)
99 {
100 void *retval = 0;
101 struct Addr_Map_Entry *re = 0;
102 struct Addr_Map_Entry *e;
103 e = addr_map_create_entry(addr,name);
104 /* tsearch records e's contents unless e
105 is already present . We must not free it till
106 destroy time if it got added to tree1. */
107 retval = dwarf_tsearch(e,tree1, addr_map_compare_func);
108 if (retval) {
109 re = *(struct Addr_Map_Entry **)retval;
110 if (re != e) {
111 /* We returned an existing record, e not needed. */
112 addr_map_free_func(e);
113 } else {
114 /* Record e got added to tree1, do not free record e. */
115 }
116 }
117 return re;
118 }
119 struct Addr_Map_Entry *
120 addr_map_find(Dwarf_Unsigned addr,void **tree1)
121 {
122 void *retval = 0;
123 struct Addr_Map_Entry *re = 0;
124 struct Addr_Map_Entry *e = 0;
125
126 e = addr_map_create_entry(addr,NULL);
127 retval = dwarf_tfind(e,tree1, addr_map_compare_func);
128 if (retval) {
129 re = *(struct Addr_Map_Entry **)retval;
130 }
131 /* The one we created here must be deleted, it is dead.
132 We look at the returned one instead. */
133 addr_map_free_func(e);
134 return re;
135 }
136
137 void
138 addr_map_destroy(void *map)
139 {
140 /* tdestroy is not part of Posix, it is a GNU libc function. */
141 if (map) {
142 dwarf_tdestroy(map,addr_map_free_func);
143 }
144 }