xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_buf.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * DTrace Memory Buffer Routines
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * The routines in this file are used to create an automatically resizing
33*0Sstevel@tonic-gate  * memory buffer that can be written to like a file.  Memory buffers are
34*0Sstevel@tonic-gate  * used to construct DOF to ioctl() to dtrace(7D), and provide semantics that
35*0Sstevel@tonic-gate  * simplify caller code.  Specifically, any allocation errors result in an
36*0Sstevel@tonic-gate  * error code being set inside the buffer which is maintained persistently and
37*0Sstevel@tonic-gate  * propagates to another buffer if the buffer in error is concatenated.  These
38*0Sstevel@tonic-gate  * semantics permit callers to execute a large series of writes without needing
39*0Sstevel@tonic-gate  * to check for errors and then perform a single check before using the buffer.
40*0Sstevel@tonic-gate  */
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include <sys/sysmacros.h>
43*0Sstevel@tonic-gate #include <strings.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include <dt_impl.h>
46*0Sstevel@tonic-gate #include <dt_buf.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate void
dt_buf_create(dtrace_hdl_t * dtp,dt_buf_t * bp,const char * name,size_t len)49*0Sstevel@tonic-gate dt_buf_create(dtrace_hdl_t *dtp, dt_buf_t *bp, const char *name, size_t len)
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	if (len == 0)
52*0Sstevel@tonic-gate 		len = _dtrace_bufsize;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	bp->dbu_buf = bp->dbu_ptr = dt_zalloc(dtp, len);
55*0Sstevel@tonic-gate 	bp->dbu_len = len;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	if (bp->dbu_buf == NULL)
58*0Sstevel@tonic-gate 		bp->dbu_err = dtrace_errno(dtp);
59*0Sstevel@tonic-gate 	else
60*0Sstevel@tonic-gate 		bp->dbu_err = 0;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	bp->dbu_resizes = 0;
63*0Sstevel@tonic-gate 	bp->dbu_name = name;
64*0Sstevel@tonic-gate }
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate void
dt_buf_destroy(dtrace_hdl_t * dtp,dt_buf_t * bp)67*0Sstevel@tonic-gate dt_buf_destroy(dtrace_hdl_t *dtp, dt_buf_t *bp)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate 	dt_dprintf("dt_buf_destroy(%s): size=%lu resizes=%u\n",
70*0Sstevel@tonic-gate 	    bp->dbu_name, (ulong_t)bp->dbu_len, bp->dbu_resizes);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	dt_free(dtp, bp->dbu_buf);
73*0Sstevel@tonic-gate }
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate void
dt_buf_reset(dtrace_hdl_t * dtp,dt_buf_t * bp)76*0Sstevel@tonic-gate dt_buf_reset(dtrace_hdl_t *dtp, dt_buf_t *bp)
77*0Sstevel@tonic-gate {
78*0Sstevel@tonic-gate 	if ((bp->dbu_ptr = bp->dbu_buf) != NULL)
79*0Sstevel@tonic-gate 		bp->dbu_err = 0;
80*0Sstevel@tonic-gate 	else
81*0Sstevel@tonic-gate 		dt_buf_create(dtp, bp, bp->dbu_name, bp->dbu_len);
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate void
dt_buf_write(dtrace_hdl_t * dtp,dt_buf_t * bp,const void * buf,size_t len,size_t align)85*0Sstevel@tonic-gate dt_buf_write(dtrace_hdl_t *dtp, dt_buf_t *bp,
86*0Sstevel@tonic-gate     const void *buf, size_t len, size_t align)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf);
89*0Sstevel@tonic-gate 	size_t adj = roundup(off, align) - off;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if (bp->dbu_err != 0) {
92*0Sstevel@tonic-gate 		(void) dt_set_errno(dtp, bp->dbu_err);
93*0Sstevel@tonic-gate 		return; /* write silently fails */
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	if (bp->dbu_ptr + adj + len > bp->dbu_buf + bp->dbu_len) {
97*0Sstevel@tonic-gate 		size_t new_len = bp->dbu_len * 2;
98*0Sstevel@tonic-gate 		uchar_t *new_buf;
99*0Sstevel@tonic-gate 		uint_t r = 1;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 		while (bp->dbu_ptr + adj + len > bp->dbu_buf + new_len) {
102*0Sstevel@tonic-gate 			new_len *= 2;
103*0Sstevel@tonic-gate 			r++;
104*0Sstevel@tonic-gate 		}
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 		if ((new_buf = dt_zalloc(dtp, new_len)) == NULL) {
107*0Sstevel@tonic-gate 			bp->dbu_err = dtrace_errno(dtp);
108*0Sstevel@tonic-gate 			return;
109*0Sstevel@tonic-gate 		}
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 		bcopy(bp->dbu_buf, new_buf, off);
112*0Sstevel@tonic-gate 		dt_free(dtp, bp->dbu_buf);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 		bp->dbu_buf = new_buf;
115*0Sstevel@tonic-gate 		bp->dbu_ptr = new_buf + off;
116*0Sstevel@tonic-gate 		bp->dbu_len = new_len;
117*0Sstevel@tonic-gate 		bp->dbu_resizes += r;
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	bp->dbu_ptr += adj;
121*0Sstevel@tonic-gate 	bcopy(buf, bp->dbu_ptr, len);
122*0Sstevel@tonic-gate 	bp->dbu_ptr += len;
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate void
dt_buf_concat(dtrace_hdl_t * dtp,dt_buf_t * dst,const dt_buf_t * src,size_t align)126*0Sstevel@tonic-gate dt_buf_concat(dtrace_hdl_t *dtp, dt_buf_t *dst,
127*0Sstevel@tonic-gate     const dt_buf_t *src, size_t align)
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate 	if (dst->dbu_err == 0 && src->dbu_err != 0) {
130*0Sstevel@tonic-gate 		(void) dt_set_errno(dtp, src->dbu_err);
131*0Sstevel@tonic-gate 		dst->dbu_err = src->dbu_err;
132*0Sstevel@tonic-gate 	} else {
133*0Sstevel@tonic-gate 		dt_buf_write(dtp, dst, src->dbu_buf,
134*0Sstevel@tonic-gate 		    (size_t)(src->dbu_ptr - src->dbu_buf), align);
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate size_t
dt_buf_offset(const dt_buf_t * bp,size_t align)139*0Sstevel@tonic-gate dt_buf_offset(const dt_buf_t *bp, size_t align)
140*0Sstevel@tonic-gate {
141*0Sstevel@tonic-gate 	size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf);
142*0Sstevel@tonic-gate 	return (roundup(off, align));
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate size_t
dt_buf_len(const dt_buf_t * bp)146*0Sstevel@tonic-gate dt_buf_len(const dt_buf_t *bp)
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate 	return (bp->dbu_ptr - bp->dbu_buf);
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate int
dt_buf_error(const dt_buf_t * bp)152*0Sstevel@tonic-gate dt_buf_error(const dt_buf_t *bp)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	return (bp->dbu_err);
155*0Sstevel@tonic-gate }
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate void *
dt_buf_ptr(const dt_buf_t * bp)158*0Sstevel@tonic-gate dt_buf_ptr(const dt_buf_t *bp)
159*0Sstevel@tonic-gate {
160*0Sstevel@tonic-gate 	return (bp->dbu_buf);
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate void *
dt_buf_claim(dtrace_hdl_t * dtp,dt_buf_t * bp)164*0Sstevel@tonic-gate dt_buf_claim(dtrace_hdl_t *dtp, dt_buf_t *bp)
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate 	void *buf = bp->dbu_buf;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	if (bp->dbu_err != 0) {
169*0Sstevel@tonic-gate 		dt_free(dtp, buf);
170*0Sstevel@tonic-gate 		buf = NULL;
171*0Sstevel@tonic-gate 	}
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	bp->dbu_buf = bp->dbu_ptr = NULL;
174*0Sstevel@tonic-gate 	bp->dbu_len = 0;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	return (buf);
177*0Sstevel@tonic-gate }
178