2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * DTrace Memory Buffer Routines
2N/A *
2N/A * The routines in this file are used to create an automatically resizing
2N/A * memory buffer that can be written to like a file. Memory buffers are
2N/A * used to construct DOF to ioctl() to dtrace(7D), and provide semantics that
2N/A * simplify caller code. Specifically, any allocation errors result in an
2N/A * error code being set inside the buffer which is maintained persistently and
2N/A * propagates to another buffer if the buffer in error is concatenated. These
2N/A * semantics permit callers to execute a large series of writes without needing
2N/A * to check for errors and then perform a single check before using the buffer.
2N/A */
2N/A
2N/A#include <sys/sysmacros.h>
2N/A#include <strings.h>
2N/A
2N/A#include <dt_impl.h>
2N/A#include <dt_buf.h>
2N/A
2N/Avoid
2N/Adt_buf_create(dtrace_hdl_t *dtp, dt_buf_t *bp, const char *name, size_t len)
2N/A{
2N/A if (len == 0)
2N/A len = _dtrace_bufsize;
2N/A
2N/A bp->dbu_buf = bp->dbu_ptr = dt_zalloc(dtp, len);
2N/A bp->dbu_len = len;
2N/A
2N/A if (bp->dbu_buf == NULL)
2N/A bp->dbu_err = dtrace_errno(dtp);
2N/A else
2N/A bp->dbu_err = 0;
2N/A
2N/A bp->dbu_resizes = 0;
2N/A bp->dbu_name = name;
2N/A}
2N/A
2N/Avoid
2N/Adt_buf_destroy(dtrace_hdl_t *dtp, dt_buf_t *bp)
2N/A{
2N/A dt_dprintf("dt_buf_destroy(%s): size=%lu resizes=%u\n",
2N/A bp->dbu_name, (ulong_t)bp->dbu_len, bp->dbu_resizes);
2N/A
2N/A dt_free(dtp, bp->dbu_buf);
2N/A}
2N/A
2N/Avoid
2N/Adt_buf_reset(dtrace_hdl_t *dtp, dt_buf_t *bp)
2N/A{
2N/A if ((bp->dbu_ptr = bp->dbu_buf) != NULL)
2N/A bp->dbu_err = 0;
2N/A else
2N/A dt_buf_create(dtp, bp, bp->dbu_name, bp->dbu_len);
2N/A}
2N/A
2N/Avoid
2N/Adt_buf_write(dtrace_hdl_t *dtp, dt_buf_t *bp,
2N/A const void *buf, size_t len, size_t align)
2N/A{
2N/A size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf);
2N/A size_t adj = roundup(off, align) - off;
2N/A
2N/A if (bp->dbu_err != 0) {
2N/A (void) dt_set_errno(dtp, bp->dbu_err);
2N/A return; /* write silently fails */
2N/A }
2N/A
2N/A if (bp->dbu_ptr + adj + len > bp->dbu_buf + bp->dbu_len) {
2N/A size_t new_len = bp->dbu_len * 2;
2N/A uchar_t *new_buf;
2N/A uint_t r = 1;
2N/A
2N/A while (bp->dbu_ptr + adj + len > bp->dbu_buf + new_len) {
2N/A new_len *= 2;
2N/A r++;
2N/A }
2N/A
2N/A if ((new_buf = dt_zalloc(dtp, new_len)) == NULL) {
2N/A bp->dbu_err = dtrace_errno(dtp);
2N/A return;
2N/A }
2N/A
2N/A bcopy(bp->dbu_buf, new_buf, off);
2N/A dt_free(dtp, bp->dbu_buf);
2N/A
2N/A bp->dbu_buf = new_buf;
2N/A bp->dbu_ptr = new_buf + off;
2N/A bp->dbu_len = new_len;
2N/A bp->dbu_resizes += r;
2N/A }
2N/A
2N/A bp->dbu_ptr += adj;
2N/A bcopy(buf, bp->dbu_ptr, len);
2N/A bp->dbu_ptr += len;
2N/A}
2N/A
2N/Avoid
2N/Adt_buf_concat(dtrace_hdl_t *dtp, dt_buf_t *dst,
2N/A const dt_buf_t *src, size_t align)
2N/A{
2N/A if (dst->dbu_err == 0 && src->dbu_err != 0) {
2N/A (void) dt_set_errno(dtp, src->dbu_err);
2N/A dst->dbu_err = src->dbu_err;
2N/A } else {
2N/A dt_buf_write(dtp, dst, src->dbu_buf,
2N/A (size_t)(src->dbu_ptr - src->dbu_buf), align);
2N/A }
2N/A}
2N/A
2N/Asize_t
2N/Adt_buf_offset(const dt_buf_t *bp, size_t align)
2N/A{
2N/A size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf);
2N/A return (roundup(off, align));
2N/A}
2N/A
2N/Asize_t
2N/Adt_buf_len(const dt_buf_t *bp)
2N/A{
2N/A return (bp->dbu_ptr - bp->dbu_buf);
2N/A}
2N/A
2N/Aint
2N/Adt_buf_error(const dt_buf_t *bp)
2N/A{
2N/A return (bp->dbu_err);
2N/A}
2N/A
2N/Avoid *
2N/Adt_buf_ptr(const dt_buf_t *bp)
2N/A{
2N/A return (bp->dbu_buf);
2N/A}
2N/A
2N/Avoid *
2N/Adt_buf_claim(dtrace_hdl_t *dtp, dt_buf_t *bp)
2N/A{
2N/A void *buf = bp->dbu_buf;
2N/A
2N/A if (bp->dbu_err != 0) {
2N/A dt_free(dtp, buf);
2N/A buf = NULL;
2N/A }
2N/A
2N/A bp->dbu_buf = bp->dbu_ptr = NULL;
2N/A bp->dbu_len = 0;
2N/A
2N/A return (buf);
2N/A}