xref: /illumos-gate/usr/src/lib/libdtrace/common/dt_buf.c (revision bbf215553c7233fbab8a0afdf1fac74c44781867)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * DTrace Memory Buffer Routines
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * The routines in this file are used to create an automatically resizing
317c478bd9Sstevel@tonic-gate  * memory buffer that can be written to like a file.  Memory buffers are
32*bbf21555SRichard Lowe  * used to construct DOF to ioctl() to dtrace(4D), and provide semantics that
337c478bd9Sstevel@tonic-gate  * simplify caller code.  Specifically, any allocation errors result in an
347c478bd9Sstevel@tonic-gate  * error code being set inside the buffer which is maintained persistently and
357c478bd9Sstevel@tonic-gate  * propagates to another buffer if the buffer in error is concatenated.  These
367c478bd9Sstevel@tonic-gate  * semantics permit callers to execute a large series of writes without needing
377c478bd9Sstevel@tonic-gate  * to check for errors and then perform a single check before using the buffer.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
417c478bd9Sstevel@tonic-gate #include <strings.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <dt_impl.h>
447c478bd9Sstevel@tonic-gate #include <dt_buf.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate void
dt_buf_create(dtrace_hdl_t * dtp,dt_buf_t * bp,const char * name,size_t len)477c478bd9Sstevel@tonic-gate dt_buf_create(dtrace_hdl_t *dtp, dt_buf_t *bp, const char *name, size_t len)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	if (len == 0)
507c478bd9Sstevel@tonic-gate 		len = _dtrace_bufsize;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	bp->dbu_buf = bp->dbu_ptr = dt_zalloc(dtp, len);
537c478bd9Sstevel@tonic-gate 	bp->dbu_len = len;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	if (bp->dbu_buf == NULL)
567c478bd9Sstevel@tonic-gate 		bp->dbu_err = dtrace_errno(dtp);
577c478bd9Sstevel@tonic-gate 	else
587c478bd9Sstevel@tonic-gate 		bp->dbu_err = 0;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	bp->dbu_resizes = 0;
617c478bd9Sstevel@tonic-gate 	bp->dbu_name = name;
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate void
dt_buf_destroy(dtrace_hdl_t * dtp,dt_buf_t * bp)657c478bd9Sstevel@tonic-gate dt_buf_destroy(dtrace_hdl_t *dtp, dt_buf_t *bp)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	dt_dprintf("dt_buf_destroy(%s): size=%lu resizes=%u\n",
687c478bd9Sstevel@tonic-gate 	    bp->dbu_name, (ulong_t)bp->dbu_len, bp->dbu_resizes);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	dt_free(dtp, bp->dbu_buf);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate void
dt_buf_reset(dtrace_hdl_t * dtp,dt_buf_t * bp)747c478bd9Sstevel@tonic-gate dt_buf_reset(dtrace_hdl_t *dtp, dt_buf_t *bp)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	if ((bp->dbu_ptr = bp->dbu_buf) != NULL)
777c478bd9Sstevel@tonic-gate 		bp->dbu_err = 0;
787c478bd9Sstevel@tonic-gate 	else
797c478bd9Sstevel@tonic-gate 		dt_buf_create(dtp, bp, bp->dbu_name, bp->dbu_len);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void
dt_buf_write(dtrace_hdl_t * dtp,dt_buf_t * bp,const void * buf,size_t len,size_t align)837c478bd9Sstevel@tonic-gate dt_buf_write(dtrace_hdl_t *dtp, dt_buf_t *bp,
847c478bd9Sstevel@tonic-gate     const void *buf, size_t len, size_t align)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf);
877c478bd9Sstevel@tonic-gate 	size_t adj = roundup(off, align) - off;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (bp->dbu_err != 0) {
907c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, bp->dbu_err);
917c478bd9Sstevel@tonic-gate 		return; /* write silently fails */
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (bp->dbu_ptr + adj + len > bp->dbu_buf + bp->dbu_len) {
957c478bd9Sstevel@tonic-gate 		size_t new_len = bp->dbu_len * 2;
967c478bd9Sstevel@tonic-gate 		uchar_t *new_buf;
977c478bd9Sstevel@tonic-gate 		uint_t r = 1;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 		while (bp->dbu_ptr + adj + len > bp->dbu_buf + new_len) {
1007c478bd9Sstevel@tonic-gate 			new_len *= 2;
1017c478bd9Sstevel@tonic-gate 			r++;
1027c478bd9Sstevel@tonic-gate 		}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		if ((new_buf = dt_zalloc(dtp, new_len)) == NULL) {
1057c478bd9Sstevel@tonic-gate 			bp->dbu_err = dtrace_errno(dtp);
1067c478bd9Sstevel@tonic-gate 			return;
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		bcopy(bp->dbu_buf, new_buf, off);
1107c478bd9Sstevel@tonic-gate 		dt_free(dtp, bp->dbu_buf);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		bp->dbu_buf = new_buf;
1137c478bd9Sstevel@tonic-gate 		bp->dbu_ptr = new_buf + off;
1147c478bd9Sstevel@tonic-gate 		bp->dbu_len = new_len;
1157c478bd9Sstevel@tonic-gate 		bp->dbu_resizes += r;
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	bp->dbu_ptr += adj;
1197c478bd9Sstevel@tonic-gate 	bcopy(buf, bp->dbu_ptr, len);
1207c478bd9Sstevel@tonic-gate 	bp->dbu_ptr += len;
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate void
dt_buf_concat(dtrace_hdl_t * dtp,dt_buf_t * dst,const dt_buf_t * src,size_t align)1247c478bd9Sstevel@tonic-gate dt_buf_concat(dtrace_hdl_t *dtp, dt_buf_t *dst,
1257c478bd9Sstevel@tonic-gate     const dt_buf_t *src, size_t align)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	if (dst->dbu_err == 0 && src->dbu_err != 0) {
1287c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, src->dbu_err);
1297c478bd9Sstevel@tonic-gate 		dst->dbu_err = src->dbu_err;
1307c478bd9Sstevel@tonic-gate 	} else {
1317c478bd9Sstevel@tonic-gate 		dt_buf_write(dtp, dst, src->dbu_buf,
1327c478bd9Sstevel@tonic-gate 		    (size_t)(src->dbu_ptr - src->dbu_buf), align);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate size_t
dt_buf_offset(const dt_buf_t * bp,size_t align)1377c478bd9Sstevel@tonic-gate dt_buf_offset(const dt_buf_t *bp, size_t align)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf);
1407c478bd9Sstevel@tonic-gate 	return (roundup(off, align));
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate size_t
dt_buf_len(const dt_buf_t * bp)1447c478bd9Sstevel@tonic-gate dt_buf_len(const dt_buf_t *bp)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	return (bp->dbu_ptr - bp->dbu_buf);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate int
dt_buf_error(const dt_buf_t * bp)1507c478bd9Sstevel@tonic-gate dt_buf_error(const dt_buf_t *bp)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	return (bp->dbu_err);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate void *
dt_buf_ptr(const dt_buf_t * bp)1567c478bd9Sstevel@tonic-gate dt_buf_ptr(const dt_buf_t *bp)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	return (bp->dbu_buf);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate void *
dt_buf_claim(dtrace_hdl_t * dtp,dt_buf_t * bp)1627c478bd9Sstevel@tonic-gate dt_buf_claim(dtrace_hdl_t *dtp, dt_buf_t *bp)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	void *buf = bp->dbu_buf;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (bp->dbu_err != 0) {
1677c478bd9Sstevel@tonic-gate 		dt_free(dtp, buf);
1687c478bd9Sstevel@tonic-gate 		buf = NULL;
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	bp->dbu_buf = bp->dbu_ptr = NULL;
1727c478bd9Sstevel@tonic-gate 	bp->dbu_len = 0;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	return (buf);
1757c478bd9Sstevel@tonic-gate }
176