File: addrmap.c

package info (click to toggle)
dwarfutils 20201201-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,868 kB
  • sloc: ansic: 104,667; sh: 5,947; cpp: 4,675; python: 878; makefile: 646; awk: 11
file content (131 lines) | stat: -rw-r--r-- 3,623 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
  Copyright 2010-2012 David Anderson. All rights reserved.
  Portions Copyright 2012 SN Systems Ltd. All rights reserved.

  This program is free software; you can redistribute it and/or
  modify it under the terms of version 2 of the GNU General
  Public License as published by the Free Software Foundation.

  This program is distributed in the hope that it would be
  useful, but WITHOUT ANY WARRANTY; without even the implied
  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.

  Further, this software is distributed without any warranty
  that it is free of the rightful claim of any third person
  regarding infringement or the like.  Any license provided
  herein, whether implied or otherwise, applies only to this
  software file.  Patent licenses, if any, provided herein
  do not apply to combinations of this program with other
  software, or any other product whatsoever.

  You should have received a copy of the GNU General Public
  License along with this program; if not, write the Free
  Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  Boston MA 02110-1301, USA.

*/

/*  If memory full  we do not exit, we just keep going as if
    all were well. */

#include "globals.h"
#include <stdio.h>
#ifdef HAVE_STDINT_H
#include <stdint.h> /* For uintptr_t */
#endif /* HAVE_STDINT_H */
#include "addrmap.h"
#include "dwarf_tsearch.h"


static struct Addr_Map_Entry *
addr_map_create_entry(Dwarf_Unsigned k,char *name)
{
    struct Addr_Map_Entry *mp =
        (struct Addr_Map_Entry *)malloc(sizeof(struct Addr_Map_Entry));
    if (!mp) {
        return 0;
    }
    mp->mp_key = k;
    if (name) {
        mp->mp_name = (char *)strdup(name);
    } else {
        mp->mp_name = 0;
    }
    return mp;
}
static void
addr_map_free_func(void *mx)
{
    struct Addr_Map_Entry *m = mx;
    if (!m) {
        return;
    }
    free(m->mp_name);
    m->mp_name = 0;
    free(m);
    return;
}

static int
addr_map_compare_func(const void *l, const void *r)
{
    const struct Addr_Map_Entry *ml = l;
    const struct Addr_Map_Entry *mr = r;
    if (ml->mp_key < mr->mp_key) {
        return -1;
    }
    if (ml->mp_key > mr->mp_key) {
        return 1;
    }
    return 0;
}
struct Addr_Map_Entry *
addr_map_insert( Dwarf_Unsigned addr,char *name,void **tree1)
{
    void *retval = 0;
    struct Addr_Map_Entry *re = 0;
    struct Addr_Map_Entry *e;
    e  = addr_map_create_entry(addr,name);
    /*  tsearch records e's contents unless e
        is already present . We must not free it till
        destroy time if it got added to tree1.  */
    retval = dwarf_tsearch(e,tree1, addr_map_compare_func);
    if (retval) {
        re = *(struct Addr_Map_Entry **)retval;
        if (re != e) {
            /* We returned an existing record, e not needed. */
            addr_map_free_func(e);
        } else {
            /* Record e got added to tree1, do not free record e. */
        }
    }
    return re;
}
struct Addr_Map_Entry *
addr_map_find(Dwarf_Unsigned addr,void **tree1)
{
    void *retval = 0;
    struct Addr_Map_Entry *re = 0;
    struct Addr_Map_Entry *e = 0;

    e = addr_map_create_entry(addr,NULL);
    retval = dwarf_tfind(e,tree1, addr_map_compare_func);
    if (retval) {
        re = *(struct Addr_Map_Entry **)retval;
    }
    /*  The one we created here must be deleted, it is dead.
        We look at the returned one instead. */
    addr_map_free_func(e);
    return re;
}


void
addr_map_destroy(void *map)
{
    /* tdestroy is not part of Posix, it is a GNU libc function. */
    if (map) {
        dwarf_tdestroy(map,addr_map_free_func);
    }
}