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