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
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.
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]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * DTrace Memory Buffer Routines
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.
42 #include <sys/sysmacros.h>
49 dt_buf_create(dtrace_hdl_t
*dtp
, dt_buf_t
*bp
, const char *name
, size_t len
)
52 len
= _dtrace_bufsize
;
54 bp
->dbu_buf
= bp
->dbu_ptr
= dt_zalloc(dtp
, len
);
57 if (bp
->dbu_buf
== NULL
)
58 bp
->dbu_err
= dtrace_errno(dtp
);
67 dt_buf_destroy(dtrace_hdl_t
*dtp
, dt_buf_t
*bp
)
69 dt_dprintf("dt_buf_destroy(%s): size=%lu resizes=%u\n",
70 bp
->dbu_name
, (ulong_t
)bp
->dbu_len
, bp
->dbu_resizes
);
72 dt_free(dtp
, bp
->dbu_buf
);
76 dt_buf_reset(dtrace_hdl_t
*dtp
, dt_buf_t
*bp
)
78 if ((bp
->dbu_ptr
= bp
->dbu_buf
) != NULL
)
81 dt_buf_create(dtp
, bp
, bp
->dbu_name
, bp
->dbu_len
);
85 dt_buf_write(dtrace_hdl_t
*dtp
, dt_buf_t
*bp
,
86 const void *buf
, size_t len
, size_t align
)
88 size_t off
= (size_t)(bp
->dbu_ptr
- bp
->dbu_buf
);
89 size_t adj
= roundup(off
, align
) - off
;
91 if (bp
->dbu_err
!= 0) {
92 (void) dt_set_errno(dtp
, bp
->dbu_err
);
93 return; /* write silently fails */
96 if (bp
->dbu_ptr
+ adj
+ len
> bp
->dbu_buf
+ bp
->dbu_len
) {
97 size_t new_len
= bp
->dbu_len
* 2;
101 while (bp
->dbu_ptr
+ adj
+ len
> bp
->dbu_buf
+ new_len
) {
106 if ((new_buf
= dt_zalloc(dtp
, new_len
)) == NULL
) {
107 bp
->dbu_err
= dtrace_errno(dtp
);
111 bcopy(bp
->dbu_buf
, new_buf
, off
);
112 dt_free(dtp
, bp
->dbu_buf
);
114 bp
->dbu_buf
= new_buf
;
115 bp
->dbu_ptr
= new_buf
+ off
;
116 bp
->dbu_len
= new_len
;
117 bp
->dbu_resizes
+= r
;
121 bcopy(buf
, bp
->dbu_ptr
, len
);
126 dt_buf_concat(dtrace_hdl_t
*dtp
, dt_buf_t
*dst
,
127 const dt_buf_t
*src
, size_t align
)
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
;
133 dt_buf_write(dtp
, dst
, src
->dbu_buf
,
134 (size_t)(src
->dbu_ptr
- src
->dbu_buf
), align
);
139 dt_buf_offset(const dt_buf_t
*bp
, size_t align
)
141 size_t off
= (size_t)(bp
->dbu_ptr
- bp
->dbu_buf
);
142 return (roundup(off
, align
));
146 dt_buf_len(const dt_buf_t
*bp
)
148 return (bp
->dbu_ptr
- bp
->dbu_buf
);
152 dt_buf_error(const dt_buf_t
*bp
)
154 return (bp
->dbu_err
);
158 dt_buf_ptr(const dt_buf_t
*bp
)
160 return (bp
->dbu_buf
);
164 dt_buf_claim(dtrace_hdl_t
*dtp
, dt_buf_t
*bp
)
166 void *buf
= bp
->dbu_buf
;
168 if (bp
->dbu_err
!= 0) {
173 bp
->dbu_buf
= bp
->dbu_ptr
= NULL
;