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