| 1 |
/* |
| 2 |
Copyright (C) 2000,2004 Silicon Graphics, Inc. All Rights Reserved. |
| 3 |
Portions Copyright 2002-2010 Sun Microsystems, Inc. All rights reserved. |
| 4 |
Portions Copyright 2011-2019. David Anderson. All Rights Reserved. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it |
| 7 |
and/or modify it under the terms of version 2.1 of the |
| 8 |
GNU Lesser General Public License as published by the Free |
| 9 |
Software Foundation. |
| 10 |
|
| 11 |
This program is distributed in the hope that it would be |
| 12 |
useful, but WITHOUT ANY WARRANTY; without even the implied |
| 13 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
PURPOSE. |
| 15 |
|
| 16 |
Further, this software is distributed without any warranty |
| 17 |
that it is free of the rightful claim of any third person |
| 18 |
regarding infringement or the like. Any license provided |
| 19 |
herein, whether implied or otherwise, applies only to this |
| 20 |
software file. Patent licenses, if any, provided herein |
| 21 |
do not apply to combinations of this program with other |
| 22 |
software, or any other product whatsoever. |
| 23 |
|
| 24 |
You should have received a copy of the GNU Lesser General |
| 25 |
Public License along with this program; if not, write the |
| 26 |
Free Software Foundation, Inc., 51 Franklin Street - Fifth |
| 27 |
Floor, Boston MA 02110-1301, USA. |
| 28 |
|
| 29 |
*/ |
| 30 |
|
| 31 |
#include <config.h> |
| 32 |
|
| 33 |
#include <stdlib.h> /* free() malloc() */ |
| 34 |
#include <string.h> /* memset() */ |
| 35 |
|
| 36 |
#ifdef HAVE_STDINT_H |
| 37 |
#include <stdint.h> /* uintptr_t */ |
| 38 |
#endif |
| 39 |
|
| 40 |
#include "dwarf.h" |
| 41 |
#include "libdwarf.h" |
| 42 |
#include "dwarf_base_types.h" |
| 43 |
#include "libdwarfp.h" |
| 44 |
#include "dwarf_base_types.h" |
| 45 |
#include "dwarf_pro_incl.h" |
| 46 |
#include "dwarf_pro_opaque.h" |
| 47 |
#include "dwarf_pro_alloc.h" |
| 48 |
#include "dwarf_tsearch.h" |
| 49 |
|
| 50 |
/* When each block is allocated, there is a two-word structure |
| 51 |
allocated at the beginning so the block can go on a list. |
| 52 |
The address returned is the address *after* the two pointers |
| 53 |
at the start. But this allows us to be given a pointer to |
| 54 |
a generic block, and go backwards to find the list-node. Then |
| 55 |
we can remove this block from it's list without the need to search |
| 56 |
through a linked list in order to remove the node. It also allows |
| 57 |
us to 'delete' a memory block without needing the dbg structure. |
| 58 |
We still need the dbg structure on allocation so |
| 59 |
that we know which |
| 60 |
linked list to add the block to. |
| 61 |
|
| 62 |
Only the allocation of the dbg structure itself cannot use |
| 63 |
_dwarf_p_get_alloc. |
| 64 |
That structure should be set up by hand, and the two list |
| 65 |
pointers should be initialized to point at the node itself. |
| 66 |
That initializes |
| 67 |
the doubly linked list. */ |
| 68 |
|
| 69 |
#define LIST_TO_BLOCK(lst) \ |
| 70 |
((void*) (((char *)lst) + sizeof(memory_list_t))) |
| 71 |
#define BLOCK_TO_LIST(blk) \ |
| 72 |
((memory_list_t*) (((char*)blk) - sizeof(memory_list_t))) |
| 73 |
|
| 74 |
/* |
| 75 |
dbg should be NULL only when allocating dbg itself. In that |
| 76 |
case we initialize it to an empty circular doubly-linked list. |
| 77 |
*/ |
| 78 |
|
| 79 |
Dwarf_Ptr |
| 80 |
_dwarf_p_get_alloc(Dwarf_P_Debug dbg, Dwarf_Unsigned size) |
| 81 |
{ |
| 82 |
void *sp; |
| 83 |
memory_list_t *lp = NULL; |
| 84 |
memory_list_t *dbglp = NULL; |
| 85 |
memory_list_t *nextblock = NULL; |
| 86 |
|
| 87 |
/* Alloc control struct and data block together |
| 88 |
for performance reasons */ |
| 89 |
lp = (memory_list_t *) malloc(size + sizeof(memory_list_t)); |
| 90 |
if (lp == NULL) { |
| 91 |
/* should throw an error */ |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
/* point to 'size' bytes just beyond lp struct */ |
| 95 |
sp = LIST_TO_BLOCK(lp); |
| 96 |
memset(sp, 0, size); |
| 97 |
if (dbg == NULL) { |
| 98 |
lp->next = lp->prev = lp; |
| 99 |
} else { |
| 100 |
/* I always have to draw a picture to understand this part. */ |
| 101 |
|
| 102 |
dbglp = BLOCK_TO_LIST(dbg); |
| 103 |
nextblock = dbglp->next; |
| 104 |
|
| 105 |
/* Insert between dbglp and nextblock */ |
| 106 |
dbglp->next = lp; |
| 107 |
lp->prev = dbglp; |
| 108 |
lp->next = nextblock; |
| 109 |
nextblock->prev = lp; |
| 110 |
} |
| 111 |
return sp; |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
The dbg structure is not needed here anymore. |
| 116 |
*/ |
| 117 |
void |
| 118 |
_dwarf_p_dealloc(Dwarf_Small * ptr) /* ARGSUSED */ |
| 119 |
{ |
| 120 |
memory_list_t *lp; |
| 121 |
|
| 122 |
lp = BLOCK_TO_LIST(ptr); |
| 123 |
/* Remove from a doubly linked, circular list. |
| 124 |
Read carefully, use a white board if necessary. |
| 125 |
If this is an empty list, the following |
| 126 |
statements are no-ops, and |
| 127 |
will write to the same memory location they read from. |
| 128 |
This should only happen when we deallocate |
| 129 |
the dbg structure itself. |
| 130 |
*/ |
| 131 |
if (lp == lp->next) { |
| 132 |
/* The list has a single item, itself. */ |
| 133 |
lp->prev = 0; |
| 134 |
lp->next = 0; |
| 135 |
} else if (lp->next == lp->prev) { |
| 136 |
/* List had exactly two entries. Reduce it to one, |
| 137 |
cutting lp out. */ |
| 138 |
memory_list_t * remaining = lp->next; |
| 139 |
remaining->next = remaining; |
| 140 |
remaining->prev = remaining; |
| 141 |
} else { |
| 142 |
/* Multi=entry. Just cut lp out. */ |
| 143 |
lp->prev->next = lp->next; |
| 144 |
lp->next->prev = lp->prev; |
| 145 |
lp->prev = lp->next = 0; |
| 146 |
} |
| 147 |
free((void*)lp); |
| 148 |
} |
| 149 |
|
| 150 |
static void |
| 151 |
_dwarf_str_hashtab_freenode(void * nodep) |
| 152 |
{ |
| 153 |
free(nodep); |
| 154 |
} |
| 155 |
|
| 156 |
/* |
| 157 |
This routine deallocates all the nodes on the dbg list, |
| 158 |
and then deallocates the dbg structure itself. |
| 159 |
*/ |
| 160 |
|
| 161 |
void |
| 162 |
_dwarf_p_dealloc_all(Dwarf_P_Debug dbg) |
| 163 |
{ |
| 164 |
memory_list_t *dbglp; |
| 165 |
memory_list_t *base_dbglp; |
| 166 |
|
| 167 |
if (dbg == NULL) { |
| 168 |
/* should throw an error */ |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
base_dbglp = BLOCK_TO_LIST(dbg); |
| 173 |
dbglp = base_dbglp->next; |
| 174 |
|
| 175 |
while (dbglp != base_dbglp) { |
| 176 |
memory_list_t*next = dbglp->next; |
| 177 |
|
| 178 |
_dwarf_p_dealloc( LIST_TO_BLOCK(dbglp)); |
| 179 |
dbglp = next; |
| 180 |
} |
| 181 |
dwarf_tdestroy(dbg->de_debug_str_hashtab, |
| 182 |
_dwarf_str_hashtab_freenode); |
| 183 |
dwarf_tdestroy(dbg->de_debug_line_str_hashtab, |
| 184 |
_dwarf_str_hashtab_freenode); |
| 185 |
free((void *)base_dbglp); |
| 186 |
} |