xref: /freebsd/cddl/contrib/opensolaris/lib/libdtrace/common/dt_buf.c (revision 6ff6d951ade3f3379932df7f878ef3ea272cfc59)
16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License, Version 1.0 only
66ff6d951SJohn Birrell  * (the "License").  You may not use this file except in compliance
76ff6d951SJohn Birrell  * with the License.
86ff6d951SJohn Birrell  *
96ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
106ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
116ff6d951SJohn Birrell  * See the License for the specific language governing permissions
126ff6d951SJohn Birrell  * and limitations under the License.
136ff6d951SJohn Birrell  *
146ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
156ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
166ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
176ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
186ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
196ff6d951SJohn Birrell  *
206ff6d951SJohn Birrell  * CDDL HEADER END
216ff6d951SJohn Birrell  */
226ff6d951SJohn Birrell /*
236ff6d951SJohn Birrell  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
246ff6d951SJohn Birrell  * Use is subject to license terms.
256ff6d951SJohn Birrell  */
266ff6d951SJohn Birrell 
276ff6d951SJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
286ff6d951SJohn Birrell 
296ff6d951SJohn Birrell /*
306ff6d951SJohn Birrell  * DTrace Memory Buffer Routines
316ff6d951SJohn Birrell  *
326ff6d951SJohn Birrell  * The routines in this file are used to create an automatically resizing
336ff6d951SJohn Birrell  * memory buffer that can be written to like a file.  Memory buffers are
346ff6d951SJohn Birrell  * used to construct DOF to ioctl() to dtrace(7D), and provide semantics that
356ff6d951SJohn Birrell  * simplify caller code.  Specifically, any allocation errors result in an
366ff6d951SJohn Birrell  * error code being set inside the buffer which is maintained persistently and
376ff6d951SJohn Birrell  * propagates to another buffer if the buffer in error is concatenated.  These
386ff6d951SJohn Birrell  * semantics permit callers to execute a large series of writes without needing
396ff6d951SJohn Birrell  * to check for errors and then perform a single check before using the buffer.
406ff6d951SJohn Birrell  */
416ff6d951SJohn Birrell 
426ff6d951SJohn Birrell #include <sys/sysmacros.h>
436ff6d951SJohn Birrell #include <strings.h>
446ff6d951SJohn Birrell 
456ff6d951SJohn Birrell #include <dt_impl.h>
466ff6d951SJohn Birrell #include <dt_buf.h>
476ff6d951SJohn Birrell 
486ff6d951SJohn Birrell void
dt_buf_create(dtrace_hdl_t * dtp,dt_buf_t * bp,const char * name,size_t len)496ff6d951SJohn Birrell dt_buf_create(dtrace_hdl_t *dtp, dt_buf_t *bp, const char *name, size_t len)
506ff6d951SJohn Birrell {
516ff6d951SJohn Birrell 	if (len == 0)
526ff6d951SJohn Birrell 		len = _dtrace_bufsize;
536ff6d951SJohn Birrell 
546ff6d951SJohn Birrell 	bp->dbu_buf = bp->dbu_ptr = dt_zalloc(dtp, len);
556ff6d951SJohn Birrell 	bp->dbu_len = len;
566ff6d951SJohn Birrell 
576ff6d951SJohn Birrell 	if (bp->dbu_buf == NULL)
586ff6d951SJohn Birrell 		bp->dbu_err = dtrace_errno(dtp);
596ff6d951SJohn Birrell 	else
606ff6d951SJohn Birrell 		bp->dbu_err = 0;
616ff6d951SJohn Birrell 
626ff6d951SJohn Birrell 	bp->dbu_resizes = 0;
636ff6d951SJohn Birrell 	bp->dbu_name = name;
646ff6d951SJohn Birrell }
656ff6d951SJohn Birrell 
666ff6d951SJohn Birrell void
dt_buf_destroy(dtrace_hdl_t * dtp,dt_buf_t * bp)676ff6d951SJohn Birrell dt_buf_destroy(dtrace_hdl_t *dtp, dt_buf_t *bp)
686ff6d951SJohn Birrell {
696ff6d951SJohn Birrell 	dt_dprintf("dt_buf_destroy(%s): size=%lu resizes=%u\n",
706ff6d951SJohn Birrell 	    bp->dbu_name, (ulong_t)bp->dbu_len, bp->dbu_resizes);
716ff6d951SJohn Birrell 
726ff6d951SJohn Birrell 	dt_free(dtp, bp->dbu_buf);
736ff6d951SJohn Birrell }
746ff6d951SJohn Birrell 
756ff6d951SJohn Birrell void
dt_buf_reset(dtrace_hdl_t * dtp,dt_buf_t * bp)766ff6d951SJohn Birrell dt_buf_reset(dtrace_hdl_t *dtp, dt_buf_t *bp)
776ff6d951SJohn Birrell {
786ff6d951SJohn Birrell 	if ((bp->dbu_ptr = bp->dbu_buf) != NULL)
796ff6d951SJohn Birrell 		bp->dbu_err = 0;
806ff6d951SJohn Birrell 	else
816ff6d951SJohn Birrell 		dt_buf_create(dtp, bp, bp->dbu_name, bp->dbu_len);
826ff6d951SJohn Birrell }
836ff6d951SJohn Birrell 
846ff6d951SJohn Birrell void
dt_buf_write(dtrace_hdl_t * dtp,dt_buf_t * bp,const void * buf,size_t len,size_t align)856ff6d951SJohn Birrell dt_buf_write(dtrace_hdl_t *dtp, dt_buf_t *bp,
866ff6d951SJohn Birrell     const void *buf, size_t len, size_t align)
876ff6d951SJohn Birrell {
886ff6d951SJohn Birrell 	size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf);
896ff6d951SJohn Birrell 	size_t adj = roundup(off, align) - off;
906ff6d951SJohn Birrell 
916ff6d951SJohn Birrell 	if (bp->dbu_err != 0) {
926ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, bp->dbu_err);
936ff6d951SJohn Birrell 		return; /* write silently fails */
946ff6d951SJohn Birrell 	}
956ff6d951SJohn Birrell 
966ff6d951SJohn Birrell 	if (bp->dbu_ptr + adj + len > bp->dbu_buf + bp->dbu_len) {
976ff6d951SJohn Birrell 		size_t new_len = bp->dbu_len * 2;
986ff6d951SJohn Birrell 		uchar_t *new_buf;
996ff6d951SJohn Birrell 		uint_t r = 1;
1006ff6d951SJohn Birrell 
1016ff6d951SJohn Birrell 		while (bp->dbu_ptr + adj + len > bp->dbu_buf + new_len) {
1026ff6d951SJohn Birrell 			new_len *= 2;
1036ff6d951SJohn Birrell 			r++;
1046ff6d951SJohn Birrell 		}
1056ff6d951SJohn Birrell 
1066ff6d951SJohn Birrell 		if ((new_buf = dt_zalloc(dtp, new_len)) == NULL) {
1076ff6d951SJohn Birrell 			bp->dbu_err = dtrace_errno(dtp);
1086ff6d951SJohn Birrell 			return;
1096ff6d951SJohn Birrell 		}
1106ff6d951SJohn Birrell 
1116ff6d951SJohn Birrell 		bcopy(bp->dbu_buf, new_buf, off);
1126ff6d951SJohn Birrell 		dt_free(dtp, bp->dbu_buf);
1136ff6d951SJohn Birrell 
1146ff6d951SJohn Birrell 		bp->dbu_buf = new_buf;
1156ff6d951SJohn Birrell 		bp->dbu_ptr = new_buf + off;
1166ff6d951SJohn Birrell 		bp->dbu_len = new_len;
1176ff6d951SJohn Birrell 		bp->dbu_resizes += r;
1186ff6d951SJohn Birrell 	}
1196ff6d951SJohn Birrell 
1206ff6d951SJohn Birrell 	bp->dbu_ptr += adj;
1216ff6d951SJohn Birrell 	bcopy(buf, bp->dbu_ptr, len);
1226ff6d951SJohn Birrell 	bp->dbu_ptr += len;
1236ff6d951SJohn Birrell }
1246ff6d951SJohn Birrell 
1256ff6d951SJohn Birrell void
dt_buf_concat(dtrace_hdl_t * dtp,dt_buf_t * dst,const dt_buf_t * src,size_t align)1266ff6d951SJohn Birrell dt_buf_concat(dtrace_hdl_t *dtp, dt_buf_t *dst,
1276ff6d951SJohn Birrell     const dt_buf_t *src, size_t align)
1286ff6d951SJohn Birrell {
1296ff6d951SJohn Birrell 	if (dst->dbu_err == 0 && src->dbu_err != 0) {
1306ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, src->dbu_err);
1316ff6d951SJohn Birrell 		dst->dbu_err = src->dbu_err;
1326ff6d951SJohn Birrell 	} else {
1336ff6d951SJohn Birrell 		dt_buf_write(dtp, dst, src->dbu_buf,
1346ff6d951SJohn Birrell 		    (size_t)(src->dbu_ptr - src->dbu_buf), align);
1356ff6d951SJohn Birrell 	}
1366ff6d951SJohn Birrell }
1376ff6d951SJohn Birrell 
1386ff6d951SJohn Birrell size_t
dt_buf_offset(const dt_buf_t * bp,size_t align)1396ff6d951SJohn Birrell dt_buf_offset(const dt_buf_t *bp, size_t align)
1406ff6d951SJohn Birrell {
1416ff6d951SJohn Birrell 	size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf);
1426ff6d951SJohn Birrell 	return (roundup(off, align));
1436ff6d951SJohn Birrell }
1446ff6d951SJohn Birrell 
1456ff6d951SJohn Birrell size_t
dt_buf_len(const dt_buf_t * bp)1466ff6d951SJohn Birrell dt_buf_len(const dt_buf_t *bp)
1476ff6d951SJohn Birrell {
1486ff6d951SJohn Birrell 	return (bp->dbu_ptr - bp->dbu_buf);
1496ff6d951SJohn Birrell }
1506ff6d951SJohn Birrell 
1516ff6d951SJohn Birrell int
dt_buf_error(const dt_buf_t * bp)1526ff6d951SJohn Birrell dt_buf_error(const dt_buf_t *bp)
1536ff6d951SJohn Birrell {
1546ff6d951SJohn Birrell 	return (bp->dbu_err);
1556ff6d951SJohn Birrell }
1566ff6d951SJohn Birrell 
1576ff6d951SJohn Birrell void *
dt_buf_ptr(const dt_buf_t * bp)1586ff6d951SJohn Birrell dt_buf_ptr(const dt_buf_t *bp)
1596ff6d951SJohn Birrell {
1606ff6d951SJohn Birrell 	return (bp->dbu_buf);
1616ff6d951SJohn Birrell }
1626ff6d951SJohn Birrell 
1636ff6d951SJohn Birrell void *
dt_buf_claim(dtrace_hdl_t * dtp,dt_buf_t * bp)1646ff6d951SJohn Birrell dt_buf_claim(dtrace_hdl_t *dtp, dt_buf_t *bp)
1656ff6d951SJohn Birrell {
1666ff6d951SJohn Birrell 	void *buf = bp->dbu_buf;
1676ff6d951SJohn Birrell 
1686ff6d951SJohn Birrell 	if (bp->dbu_err != 0) {
1696ff6d951SJohn Birrell 		dt_free(dtp, buf);
1706ff6d951SJohn Birrell 		buf = NULL;
1716ff6d951SJohn Birrell 	}
1726ff6d951SJohn Birrell 
1736ff6d951SJohn Birrell 	bp->dbu_buf = bp->dbu_ptr = NULL;
1746ff6d951SJohn Birrell 	bp->dbu_len = 0;
1756ff6d951SJohn Birrell 
1766ff6d951SJohn Birrell 	return (buf);
1776ff6d951SJohn Birrell }
178