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