1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * DTrace Memory Buffer Routines 31*7c478bd9Sstevel@tonic-gate * 32*7c478bd9Sstevel@tonic-gate * The routines in this file are used to create an automatically resizing 33*7c478bd9Sstevel@tonic-gate * memory buffer that can be written to like a file. Memory buffers are 34*7c478bd9Sstevel@tonic-gate * used to construct DOF to ioctl() to dtrace(7D), and provide semantics that 35*7c478bd9Sstevel@tonic-gate * simplify caller code. Specifically, any allocation errors result in an 36*7c478bd9Sstevel@tonic-gate * error code being set inside the buffer which is maintained persistently and 37*7c478bd9Sstevel@tonic-gate * propagates to another buffer if the buffer in error is concatenated. These 38*7c478bd9Sstevel@tonic-gate * semantics permit callers to execute a large series of writes without needing 39*7c478bd9Sstevel@tonic-gate * to check for errors and then perform a single check before using the buffer. 40*7c478bd9Sstevel@tonic-gate */ 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 43*7c478bd9Sstevel@tonic-gate #include <strings.h> 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate #include <dt_impl.h> 46*7c478bd9Sstevel@tonic-gate #include <dt_buf.h> 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate void 49*7c478bd9Sstevel@tonic-gate dt_buf_create(dtrace_hdl_t *dtp, dt_buf_t *bp, const char *name, size_t len) 50*7c478bd9Sstevel@tonic-gate { 51*7c478bd9Sstevel@tonic-gate if (len == 0) 52*7c478bd9Sstevel@tonic-gate len = _dtrace_bufsize; 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate bp->dbu_buf = bp->dbu_ptr = dt_zalloc(dtp, len); 55*7c478bd9Sstevel@tonic-gate bp->dbu_len = len; 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate if (bp->dbu_buf == NULL) 58*7c478bd9Sstevel@tonic-gate bp->dbu_err = dtrace_errno(dtp); 59*7c478bd9Sstevel@tonic-gate else 60*7c478bd9Sstevel@tonic-gate bp->dbu_err = 0; 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate bp->dbu_resizes = 0; 63*7c478bd9Sstevel@tonic-gate bp->dbu_name = name; 64*7c478bd9Sstevel@tonic-gate } 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate void 67*7c478bd9Sstevel@tonic-gate dt_buf_destroy(dtrace_hdl_t *dtp, dt_buf_t *bp) 68*7c478bd9Sstevel@tonic-gate { 69*7c478bd9Sstevel@tonic-gate dt_dprintf("dt_buf_destroy(%s): size=%lu resizes=%u\n", 70*7c478bd9Sstevel@tonic-gate bp->dbu_name, (ulong_t)bp->dbu_len, bp->dbu_resizes); 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate dt_free(dtp, bp->dbu_buf); 73*7c478bd9Sstevel@tonic-gate } 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate void 76*7c478bd9Sstevel@tonic-gate dt_buf_reset(dtrace_hdl_t *dtp, dt_buf_t *bp) 77*7c478bd9Sstevel@tonic-gate { 78*7c478bd9Sstevel@tonic-gate if ((bp->dbu_ptr = bp->dbu_buf) != NULL) 79*7c478bd9Sstevel@tonic-gate bp->dbu_err = 0; 80*7c478bd9Sstevel@tonic-gate else 81*7c478bd9Sstevel@tonic-gate dt_buf_create(dtp, bp, bp->dbu_name, bp->dbu_len); 82*7c478bd9Sstevel@tonic-gate } 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate void 85*7c478bd9Sstevel@tonic-gate dt_buf_write(dtrace_hdl_t *dtp, dt_buf_t *bp, 86*7c478bd9Sstevel@tonic-gate const void *buf, size_t len, size_t align) 87*7c478bd9Sstevel@tonic-gate { 88*7c478bd9Sstevel@tonic-gate size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf); 89*7c478bd9Sstevel@tonic-gate size_t adj = roundup(off, align) - off; 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate if (bp->dbu_err != 0) { 92*7c478bd9Sstevel@tonic-gate (void) dt_set_errno(dtp, bp->dbu_err); 93*7c478bd9Sstevel@tonic-gate return; /* write silently fails */ 94*7c478bd9Sstevel@tonic-gate } 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gate if (bp->dbu_ptr + adj + len > bp->dbu_buf + bp->dbu_len) { 97*7c478bd9Sstevel@tonic-gate size_t new_len = bp->dbu_len * 2; 98*7c478bd9Sstevel@tonic-gate uchar_t *new_buf; 99*7c478bd9Sstevel@tonic-gate uint_t r = 1; 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate while (bp->dbu_ptr + adj + len > bp->dbu_buf + new_len) { 102*7c478bd9Sstevel@tonic-gate new_len *= 2; 103*7c478bd9Sstevel@tonic-gate r++; 104*7c478bd9Sstevel@tonic-gate } 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate if ((new_buf = dt_zalloc(dtp, new_len)) == NULL) { 107*7c478bd9Sstevel@tonic-gate bp->dbu_err = dtrace_errno(dtp); 108*7c478bd9Sstevel@tonic-gate return; 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate bcopy(bp->dbu_buf, new_buf, off); 112*7c478bd9Sstevel@tonic-gate dt_free(dtp, bp->dbu_buf); 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate bp->dbu_buf = new_buf; 115*7c478bd9Sstevel@tonic-gate bp->dbu_ptr = new_buf + off; 116*7c478bd9Sstevel@tonic-gate bp->dbu_len = new_len; 117*7c478bd9Sstevel@tonic-gate bp->dbu_resizes += r; 118*7c478bd9Sstevel@tonic-gate } 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate bp->dbu_ptr += adj; 121*7c478bd9Sstevel@tonic-gate bcopy(buf, bp->dbu_ptr, len); 122*7c478bd9Sstevel@tonic-gate bp->dbu_ptr += len; 123*7c478bd9Sstevel@tonic-gate } 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate void 126*7c478bd9Sstevel@tonic-gate dt_buf_concat(dtrace_hdl_t *dtp, dt_buf_t *dst, 127*7c478bd9Sstevel@tonic-gate const dt_buf_t *src, size_t align) 128*7c478bd9Sstevel@tonic-gate { 129*7c478bd9Sstevel@tonic-gate if (dst->dbu_err == 0 && src->dbu_err != 0) { 130*7c478bd9Sstevel@tonic-gate (void) dt_set_errno(dtp, src->dbu_err); 131*7c478bd9Sstevel@tonic-gate dst->dbu_err = src->dbu_err; 132*7c478bd9Sstevel@tonic-gate } else { 133*7c478bd9Sstevel@tonic-gate dt_buf_write(dtp, dst, src->dbu_buf, 134*7c478bd9Sstevel@tonic-gate (size_t)(src->dbu_ptr - src->dbu_buf), align); 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate size_t 139*7c478bd9Sstevel@tonic-gate dt_buf_offset(const dt_buf_t *bp, size_t align) 140*7c478bd9Sstevel@tonic-gate { 141*7c478bd9Sstevel@tonic-gate size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf); 142*7c478bd9Sstevel@tonic-gate return (roundup(off, align)); 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate size_t 146*7c478bd9Sstevel@tonic-gate dt_buf_len(const dt_buf_t *bp) 147*7c478bd9Sstevel@tonic-gate { 148*7c478bd9Sstevel@tonic-gate return (bp->dbu_ptr - bp->dbu_buf); 149*7c478bd9Sstevel@tonic-gate } 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate int 152*7c478bd9Sstevel@tonic-gate dt_buf_error(const dt_buf_t *bp) 153*7c478bd9Sstevel@tonic-gate { 154*7c478bd9Sstevel@tonic-gate return (bp->dbu_err); 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate void * 158*7c478bd9Sstevel@tonic-gate dt_buf_ptr(const dt_buf_t *bp) 159*7c478bd9Sstevel@tonic-gate { 160*7c478bd9Sstevel@tonic-gate return (bp->dbu_buf); 161*7c478bd9Sstevel@tonic-gate } 162*7c478bd9Sstevel@tonic-gate 163*7c478bd9Sstevel@tonic-gate void * 164*7c478bd9Sstevel@tonic-gate dt_buf_claim(dtrace_hdl_t *dtp, dt_buf_t *bp) 165*7c478bd9Sstevel@tonic-gate { 166*7c478bd9Sstevel@tonic-gate void *buf = bp->dbu_buf; 167*7c478bd9Sstevel@tonic-gate 168*7c478bd9Sstevel@tonic-gate if (bp->dbu_err != 0) { 169*7c478bd9Sstevel@tonic-gate dt_free(dtp, buf); 170*7c478bd9Sstevel@tonic-gate buf = NULL; 171*7c478bd9Sstevel@tonic-gate } 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate bp->dbu_buf = bp->dbu_ptr = NULL; 174*7c478bd9Sstevel@tonic-gate bp->dbu_len = 0; 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate return (buf); 177*7c478bd9Sstevel@tonic-gate } 178