#include <config.h>
#include <stddef.h> /* NULL */
#include <stdlib.h> /* free() malloc() */
#include <string.h> /* strdup() */
#if defined(_WIN32) && defined(HAVE_STDAFX_H)
#include "stdafx.h"
#endif /* HAVE_STDAFX_H */
#include "dwarf.h"
#include "libdwarf.h"
#include "dd_defined_types.h"
#include "dd_checkutil.h"
#include "dd_glflags.h"
#include "dd_globals.h"
#include "dd_addrmap.h"
#include "dd_tsearchbal.h"
#include "libdwarf_private.h" /* For malloc/calloc debug */
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);
retval = dwarf_tsearch(e,tree1, addr_map_compare_func);
if
(retval) {
re = *(
struct
Addr_Map_Entry **)retval;
if
(re != e) {
addr_map_free_func(e);
}
else
{
}
}
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;
}
addr_map_free_func(e);
return
re;
}
void
addr_map_destroy(
void
*map)
{
if
(map) {
dwarf_tdestroy(map,addr_map_free_func);
}
}