8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libdtrace / common / dt_buf.c
blob324e778213ca7ce720e32a9b7196916a3688b7f0
1 /*
2 * CDDL HEADER START
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.
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]
20 * CDDL HEADER END
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>
43 #include <strings.h>
45 #include <dt_impl.h>
46 #include <dt_buf.h>
48 void
49 dt_buf_create(dtrace_hdl_t *dtp, dt_buf_t *bp, const char *name, size_t len)
51 if (len == 0)
52 len = _dtrace_bufsize;
54 bp->dbu_buf = bp->dbu_ptr = dt_zalloc(dtp, len);
55 bp->dbu_len = len;
57 if (bp->dbu_buf == NULL)
58 bp->dbu_err = dtrace_errno(dtp);
59 else
60 bp->dbu_err = 0;
62 bp->dbu_resizes = 0;
63 bp->dbu_name = name;
66 void
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);
75 void
76 dt_buf_reset(dtrace_hdl_t *dtp, dt_buf_t *bp)
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);
84 void
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;
98 uchar_t *new_buf;
99 uint_t r = 1;
101 while (bp->dbu_ptr + adj + len > bp->dbu_buf + new_len) {
102 new_len *= 2;
103 r++;
106 if ((new_buf = dt_zalloc(dtp, new_len)) == NULL) {
107 bp->dbu_err = dtrace_errno(dtp);
108 return;
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;
120 bp->dbu_ptr += adj;
121 bcopy(buf, bp->dbu_ptr, len);
122 bp->dbu_ptr += len;
125 void
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;
132 } else {
133 dt_buf_write(dtp, dst, src->dbu_buf,
134 (size_t)(src->dbu_ptr - src->dbu_buf), align);
138 size_t
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));
145 size_t
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);
157 void *
158 dt_buf_ptr(const dt_buf_t *bp)
160 return (bp->dbu_buf);
163 void *
164 dt_buf_claim(dtrace_hdl_t *dtp, dt_buf_t *bp)
166 void *buf = bp->dbu_buf;
168 if (bp->dbu_err != 0) {
169 dt_free(dtp, buf);
170 buf = NULL;
173 bp->dbu_buf = bp->dbu_ptr = NULL;
174 bp->dbu_len = 0;
176 return (buf);