xref: /titanic_52/usr/src/lib/libdtrace/common/dt_buf.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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * DTrace Memory Buffer Routines
31  *
32  * The routines in this file are used to create an automatically resizing
33  * memory buffer that can be written to like a file.  Memory buffers are
34  * used to construct DOF to ioctl() to dtrace(7D), and provide semantics that
35  * simplify caller code.  Specifically, any allocation errors result in an
36  * error code being set inside the buffer which is maintained persistently and
37  * propagates to another buffer if the buffer in error is concatenated.  These
38  * semantics permit callers to execute a large series of writes without needing
39  * to check for errors and then perform a single check before using the buffer.
40  */
41 
42 #include <sys/sysmacros.h>
43 #include <strings.h>
44 
45 #include <dt_impl.h>
46 #include <dt_buf.h>
47 
48 void
49 dt_buf_create(dtrace_hdl_t *dtp, dt_buf_t *bp, const char *name, size_t len)
50 {
51 	if (len == 0)
52 		len = _dtrace_bufsize;
53 
54 	bp->dbu_buf = bp->dbu_ptr = dt_zalloc(dtp, len);
55 	bp->dbu_len = len;
56 
57 	if (bp->dbu_buf == NULL)
58 		bp->dbu_err = dtrace_errno(dtp);
59 	else
60 		bp->dbu_err = 0;
61 
62 	bp->dbu_resizes = 0;
63 	bp->dbu_name = name;
64 }
65 
66 void
67 dt_buf_destroy(dtrace_hdl_t *dtp, dt_buf_t *bp)
68 {
69 	dt_dprintf("dt_buf_destroy(%s): size=%lu resizes=%u\n",
70 	    bp->dbu_name, (ulong_t)bp->dbu_len, bp->dbu_resizes);
71 
72 	dt_free(dtp, bp->dbu_buf);
73 }
74 
75 void
76 dt_buf_reset(dtrace_hdl_t *dtp, dt_buf_t *bp)
77 {
78 	if ((bp->dbu_ptr = bp->dbu_buf) != NULL)
79 		bp->dbu_err = 0;
80 	else
81 		dt_buf_create(dtp, bp, bp->dbu_name, bp->dbu_len);
82 }
83 
84 void
85 dt_buf_write(dtrace_hdl_t *dtp, dt_buf_t *bp,
86     const void *buf, size_t len, size_t align)
87 {
88 	size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf);
89 	size_t adj = roundup(off, align) - off;
90 
91 	if (bp->dbu_err != 0) {
92 		(void) dt_set_errno(dtp, bp->dbu_err);
93 		return; /* write silently fails */
94 	}
95 
96 	if (bp->dbu_ptr + adj + len > bp->dbu_buf + bp->dbu_len) {
97 		size_t new_len = bp->dbu_len * 2;
98 		uchar_t *new_buf;
99 		uint_t r = 1;
100 
101 		while (bp->dbu_ptr + adj + len > bp->dbu_buf + new_len) {
102 			new_len *= 2;
103 			r++;
104 		}
105 
106 		if ((new_buf = dt_zalloc(dtp, new_len)) == NULL) {
107 			bp->dbu_err = dtrace_errno(dtp);
108 			return;
109 		}
110 
111 		bcopy(bp->dbu_buf, new_buf, off);
112 		dt_free(dtp, bp->dbu_buf);
113 
114 		bp->dbu_buf = new_buf;
115 		bp->dbu_ptr = new_buf + off;
116 		bp->dbu_len = new_len;
117 		bp->dbu_resizes += r;
118 	}
119 
120 	bp->dbu_ptr += adj;
121 	bcopy(buf, bp->dbu_ptr, len);
122 	bp->dbu_ptr += len;
123 }
124 
125 void
126 dt_buf_concat(dtrace_hdl_t *dtp, dt_buf_t *dst,
127     const dt_buf_t *src, size_t align)
128 {
129 	if (dst->dbu_err == 0 && src->dbu_err != 0) {
130 		(void) dt_set_errno(dtp, src->dbu_err);
131 		dst->dbu_err = src->dbu_err;
132 	} else {
133 		dt_buf_write(dtp, dst, src->dbu_buf,
134 		    (size_t)(src->dbu_ptr - src->dbu_buf), align);
135 	}
136 }
137 
138 size_t
139 dt_buf_offset(const dt_buf_t *bp, size_t align)
140 {
141 	size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf);
142 	return (roundup(off, align));
143 }
144 
145 size_t
146 dt_buf_len(const dt_buf_t *bp)
147 {
148 	return (bp->dbu_ptr - bp->dbu_buf);
149 }
150 
151 int
152 dt_buf_error(const dt_buf_t *bp)
153 {
154 	return (bp->dbu_err);
155 }
156 
157 void *
158 dt_buf_ptr(const dt_buf_t *bp)
159 {
160 	return (bp->dbu_buf);
161 }
162 
163 void *
164 dt_buf_claim(dtrace_hdl_t *dtp, dt_buf_t *bp)
165 {
166 	void *buf = bp->dbu_buf;
167 
168 	if (bp->dbu_err != 0) {
169 		dt_free(dtp, buf);
170 		buf = NULL;
171 	}
172 
173 	bp->dbu_buf = bp->dbu_ptr = NULL;
174 	bp->dbu_len = 0;
175 
176 	return (buf);
177 }
178