xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_addrvec.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 #include <mdb/mdb_addrvec.h>
28 #include <mdb/mdb_debug.h>
29 #include <mdb/mdb_modapi.h>
30 
31 #include <strings.h>
32 
33 #define	AD_INIT	16	/* initial size of addrvec array */
34 #define	AD_GROW	2	/* array growth multiplier */
35 
36 void
mdb_addrvec_create(mdb_addrvec_t * adp)37 mdb_addrvec_create(mdb_addrvec_t *adp)
38 {
39 	bzero(adp, sizeof (mdb_addrvec_t));
40 }
41 
42 void
mdb_addrvec_destroy(mdb_addrvec_t * adp)43 mdb_addrvec_destroy(mdb_addrvec_t *adp)
44 {
45 	mdb_free(adp->ad_data, sizeof (uintptr_t) * adp->ad_size);
46 	bzero(adp, sizeof (mdb_addrvec_t));
47 }
48 
49 void
mdb_addrvec_unshift(mdb_addrvec_t * adp,uintptr_t value)50 mdb_addrvec_unshift(mdb_addrvec_t *adp, uintptr_t value)
51 {
52 	if (adp->ad_nelems >= adp->ad_size) {
53 		size_t size = adp->ad_size ? adp->ad_size * AD_GROW : AD_INIT;
54 		void *data = mdb_alloc(sizeof (uintptr_t) * size, UM_SLEEP);
55 
56 		bcopy(adp->ad_data, data, sizeof (uintptr_t) * adp->ad_size);
57 		mdb_free(adp->ad_data, sizeof (uintptr_t) * adp->ad_size);
58 
59 		adp->ad_data = data;
60 		adp->ad_size = size;
61 	}
62 
63 	adp->ad_data[adp->ad_nelems++] = value;
64 }
65 
66 uintptr_t
mdb_addrvec_shift(mdb_addrvec_t * adp)67 mdb_addrvec_shift(mdb_addrvec_t *adp)
68 {
69 	if (adp->ad_ndx < adp->ad_nelems)
70 		return (adp->ad_data[adp->ad_ndx++]);
71 
72 	return ((uintptr_t)-1L);
73 }
74 
75 size_t
mdb_addrvec_length(mdb_addrvec_t * adp)76 mdb_addrvec_length(mdb_addrvec_t *adp)
77 {
78 	if (adp != NULL) {
79 		ASSERT(adp->ad_nelems >= adp->ad_ndx);
80 		return (adp->ad_nelems - adp->ad_ndx);
81 	}
82 
83 	return (0); /* convenience for callers */
84 }
85