xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_addrvec.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <mdb/mdb_addrvec.h>
30 #include <mdb/mdb_debug.h>
31 #include <mdb/mdb_modapi.h>
32 
33 #include <strings.h>
34 
35 #define	AD_INIT	16	/* initial size of addrvec array */
36 #define	AD_GROW	2	/* array growth multiplier */
37 
38 void
39 mdb_addrvec_create(mdb_addrvec_t *adp)
40 {
41 	bzero(adp, sizeof (mdb_addrvec_t));
42 }
43 
44 void
45 mdb_addrvec_destroy(mdb_addrvec_t *adp)
46 {
47 	mdb_free(adp->ad_data, sizeof (uintptr_t) * adp->ad_size);
48 	bzero(adp, sizeof (mdb_addrvec_t));
49 }
50 
51 void
52 mdb_addrvec_unshift(mdb_addrvec_t *adp, uintptr_t value)
53 {
54 	if (adp->ad_nelems >= adp->ad_size) {
55 		size_t size = adp->ad_size ? adp->ad_size * AD_GROW : AD_INIT;
56 		void *data = mdb_alloc(sizeof (uintptr_t) * size, UM_SLEEP);
57 
58 		bcopy(adp->ad_data, data, sizeof (uintptr_t) * adp->ad_size);
59 		mdb_free(adp->ad_data, sizeof (uintptr_t) * adp->ad_size);
60 
61 		adp->ad_data = data;
62 		adp->ad_size = size;
63 	}
64 
65 	adp->ad_data[adp->ad_nelems++] = value;
66 }
67 
68 uintptr_t
69 mdb_addrvec_shift(mdb_addrvec_t *adp)
70 {
71 	if (adp->ad_ndx < adp->ad_nelems)
72 		return (adp->ad_data[adp->ad_ndx++]);
73 
74 	return ((uintptr_t)-1L);
75 }
76 
77 size_t
78 mdb_addrvec_length(mdb_addrvec_t *adp)
79 {
80 	if (adp != NULL) {
81 		ASSERT(adp->ad_nelems >= adp->ad_ndx);
82 		return (adp->ad_nelems - adp->ad_ndx);
83 	}
84 
85 	return (0); /* convenience for callers */
86 }
87