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