1178479Sjb/* 2178479Sjb * CDDL HEADER START 3178479Sjb * 4178479Sjb * The contents of this file are subject to the terms of the 5178479Sjb * Common Development and Distribution License, Version 1.0 only 6178479Sjb * (the "License"). You may not use this file except in compliance 7178479Sjb * with the License. 8178479Sjb * 9178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10178479Sjb * or http://www.opensolaris.org/os/licensing. 11178479Sjb * See the License for the specific language governing permissions 12178479Sjb * and limitations under the License. 13178479Sjb * 14178479Sjb * When distributing Covered Code, include this CDDL HEADER in each 15178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16178479Sjb * If applicable, add the following below this CDDL HEADER, with the 17178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying 18178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner] 19178479Sjb * 20178479Sjb * CDDL HEADER END 21178479Sjb */ 22178479Sjb/* 23178479Sjb * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24178479Sjb * Use is subject to license terms. 25178479Sjb */ 26178479Sjb 27178479Sjb#pragma ident "%Z%%M% %I% %E% SMI" 28178479Sjb 29178479Sjb/* 30178479Sjb * DTrace Memory Buffer Routines 31178479Sjb * 32178479Sjb * The routines in this file are used to create an automatically resizing 33178479Sjb * memory buffer that can be written to like a file. Memory buffers are 34178479Sjb * used to construct DOF to ioctl() to dtrace(7D), and provide semantics that 35178479Sjb * simplify caller code. Specifically, any allocation errors result in an 36178479Sjb * error code being set inside the buffer which is maintained persistently and 37178479Sjb * propagates to another buffer if the buffer in error is concatenated. These 38178479Sjb * semantics permit callers to execute a large series of writes without needing 39178479Sjb * to check for errors and then perform a single check before using the buffer. 40178479Sjb */ 41178479Sjb 42178479Sjb#include <sys/sysmacros.h> 43178479Sjb#include <strings.h> 44178479Sjb 45178479Sjb#include <dt_impl.h> 46178479Sjb#include <dt_buf.h> 47178479Sjb 48178479Sjbvoid 49178479Sjbdt_buf_create(dtrace_hdl_t *dtp, dt_buf_t *bp, const char *name, size_t len) 50178479Sjb{ 51178479Sjb if (len == 0) 52178479Sjb len = _dtrace_bufsize; 53178479Sjb 54178479Sjb bp->dbu_buf = bp->dbu_ptr = dt_zalloc(dtp, len); 55178479Sjb bp->dbu_len = len; 56178479Sjb 57178479Sjb if (bp->dbu_buf == NULL) 58178479Sjb bp->dbu_err = dtrace_errno(dtp); 59178479Sjb else 60178479Sjb bp->dbu_err = 0; 61178479Sjb 62178479Sjb bp->dbu_resizes = 0; 63178479Sjb bp->dbu_name = name; 64178479Sjb} 65178479Sjb 66178479Sjbvoid 67178479Sjbdt_buf_destroy(dtrace_hdl_t *dtp, dt_buf_t *bp) 68178479Sjb{ 69178479Sjb dt_dprintf("dt_buf_destroy(%s): size=%lu resizes=%u\n", 70178479Sjb bp->dbu_name, (ulong_t)bp->dbu_len, bp->dbu_resizes); 71178479Sjb 72178479Sjb dt_free(dtp, bp->dbu_buf); 73178479Sjb} 74178479Sjb 75178479Sjbvoid 76178479Sjbdt_buf_reset(dtrace_hdl_t *dtp, dt_buf_t *bp) 77178479Sjb{ 78178479Sjb if ((bp->dbu_ptr = bp->dbu_buf) != NULL) 79178479Sjb bp->dbu_err = 0; 80178479Sjb else 81178479Sjb dt_buf_create(dtp, bp, bp->dbu_name, bp->dbu_len); 82178479Sjb} 83178479Sjb 84178479Sjbvoid 85178479Sjbdt_buf_write(dtrace_hdl_t *dtp, dt_buf_t *bp, 86178479Sjb const void *buf, size_t len, size_t align) 87178479Sjb{ 88178479Sjb size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf); 89178479Sjb size_t adj = roundup(off, align) - off; 90178479Sjb 91178479Sjb if (bp->dbu_err != 0) { 92178479Sjb (void) dt_set_errno(dtp, bp->dbu_err); 93178479Sjb return; /* write silently fails */ 94178479Sjb } 95178479Sjb 96178479Sjb if (bp->dbu_ptr + adj + len > bp->dbu_buf + bp->dbu_len) { 97178479Sjb size_t new_len = bp->dbu_len * 2; 98178479Sjb uchar_t *new_buf; 99178479Sjb uint_t r = 1; 100178479Sjb 101178479Sjb while (bp->dbu_ptr + adj + len > bp->dbu_buf + new_len) { 102178479Sjb new_len *= 2; 103178479Sjb r++; 104178479Sjb } 105178479Sjb 106178479Sjb if ((new_buf = dt_zalloc(dtp, new_len)) == NULL) { 107178479Sjb bp->dbu_err = dtrace_errno(dtp); 108178479Sjb return; 109178479Sjb } 110178479Sjb 111178479Sjb bcopy(bp->dbu_buf, new_buf, off); 112178479Sjb dt_free(dtp, bp->dbu_buf); 113178479Sjb 114178479Sjb bp->dbu_buf = new_buf; 115178479Sjb bp->dbu_ptr = new_buf + off; 116178479Sjb bp->dbu_len = new_len; 117178479Sjb bp->dbu_resizes += r; 118178479Sjb } 119178479Sjb 120178479Sjb bp->dbu_ptr += adj; 121178479Sjb bcopy(buf, bp->dbu_ptr, len); 122178479Sjb bp->dbu_ptr += len; 123178479Sjb} 124178479Sjb 125178479Sjbvoid 126178479Sjbdt_buf_concat(dtrace_hdl_t *dtp, dt_buf_t *dst, 127178479Sjb const dt_buf_t *src, size_t align) 128178479Sjb{ 129178479Sjb if (dst->dbu_err == 0 && src->dbu_err != 0) { 130178479Sjb (void) dt_set_errno(dtp, src->dbu_err); 131178479Sjb dst->dbu_err = src->dbu_err; 132178479Sjb } else { 133178479Sjb dt_buf_write(dtp, dst, src->dbu_buf, 134178479Sjb (size_t)(src->dbu_ptr - src->dbu_buf), align); 135178479Sjb } 136178479Sjb} 137178479Sjb 138178479Sjbsize_t 139178479Sjbdt_buf_offset(const dt_buf_t *bp, size_t align) 140178479Sjb{ 141178479Sjb size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf); 142178479Sjb return (roundup(off, align)); 143178479Sjb} 144178479Sjb 145178479Sjbsize_t 146178479Sjbdt_buf_len(const dt_buf_t *bp) 147178479Sjb{ 148178479Sjb return (bp->dbu_ptr - bp->dbu_buf); 149178479Sjb} 150178479Sjb 151178479Sjbint 152178479Sjbdt_buf_error(const dt_buf_t *bp) 153178479Sjb{ 154178479Sjb return (bp->dbu_err); 155178479Sjb} 156178479Sjb 157178479Sjbvoid * 158178479Sjbdt_buf_ptr(const dt_buf_t *bp) 159178479Sjb{ 160178479Sjb return (bp->dbu_buf); 161178479Sjb} 162178479Sjb 163178479Sjbvoid * 164178479Sjbdt_buf_claim(dtrace_hdl_t *dtp, dt_buf_t *bp) 165178479Sjb{ 166178479Sjb void *buf = bp->dbu_buf; 167178479Sjb 168178479Sjb if (bp->dbu_err != 0) { 169178479Sjb dt_free(dtp, buf); 170178479Sjb buf = NULL; 171178479Sjb } 172178479Sjb 173178479Sjb bp->dbu_buf = bp->dbu_ptr = NULL; 174178479Sjb bp->dbu_len = 0; 175178479Sjb 176178479Sjb return (buf); 177178479Sjb} 178