Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/common/dt_buf.c
39562 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2005 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#pragma ident "%Z%%M% %I% %E% SMI"2728/*29* DTrace Memory Buffer Routines30*31* The routines in this file are used to create an automatically resizing32* memory buffer that can be written to like a file. Memory buffers are33* used to construct DOF to ioctl() to dtrace(7D), and provide semantics that34* simplify caller code. Specifically, any allocation errors result in an35* error code being set inside the buffer which is maintained persistently and36* propagates to another buffer if the buffer in error is concatenated. These37* semantics permit callers to execute a large series of writes without needing38* to check for errors and then perform a single check before using the buffer.39*/4041#include <sys/sysmacros.h>42#include <strings.h>4344#include <dt_impl.h>45#include <dt_buf.h>4647void48dt_buf_create(dtrace_hdl_t *dtp, dt_buf_t *bp, const char *name, size_t len)49{50if (len == 0)51len = _dtrace_bufsize;5253bp->dbu_buf = bp->dbu_ptr = dt_zalloc(dtp, len);54bp->dbu_len = len;5556if (bp->dbu_buf == NULL)57bp->dbu_err = dtrace_errno(dtp);58else59bp->dbu_err = 0;6061bp->dbu_resizes = 0;62bp->dbu_name = name;63}6465void66dt_buf_destroy(dtrace_hdl_t *dtp, dt_buf_t *bp)67{68dt_dprintf("dt_buf_destroy(%s): size=%lu resizes=%u\n",69bp->dbu_name, (ulong_t)bp->dbu_len, bp->dbu_resizes);7071dt_free(dtp, bp->dbu_buf);72}7374void75dt_buf_reset(dtrace_hdl_t *dtp, dt_buf_t *bp)76{77if ((bp->dbu_ptr = bp->dbu_buf) != NULL)78bp->dbu_err = 0;79else80dt_buf_create(dtp, bp, bp->dbu_name, bp->dbu_len);81}8283void84dt_buf_write(dtrace_hdl_t *dtp, dt_buf_t *bp,85const void *buf, size_t len, size_t align)86{87size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf);88size_t adj = roundup(off, align) - off;8990if (bp->dbu_err != 0) {91(void) dt_set_errno(dtp, bp->dbu_err);92return; /* write silently fails */93}9495if (bp->dbu_ptr + adj + len > bp->dbu_buf + bp->dbu_len) {96size_t new_len = bp->dbu_len * 2;97uchar_t *new_buf;98uint_t r = 1;99100while (bp->dbu_ptr + adj + len > bp->dbu_buf + new_len) {101new_len *= 2;102r++;103}104105if ((new_buf = dt_zalloc(dtp, new_len)) == NULL) {106bp->dbu_err = dtrace_errno(dtp);107return;108}109110bcopy(bp->dbu_buf, new_buf, off);111dt_free(dtp, bp->dbu_buf);112113bp->dbu_buf = new_buf;114bp->dbu_ptr = new_buf + off;115bp->dbu_len = new_len;116bp->dbu_resizes += r;117}118119bp->dbu_ptr += adj;120bcopy(buf, bp->dbu_ptr, len);121bp->dbu_ptr += len;122}123124void125dt_buf_concat(dtrace_hdl_t *dtp, dt_buf_t *dst,126const dt_buf_t *src, size_t align)127{128if (dst->dbu_err == 0 && src->dbu_err != 0) {129(void) dt_set_errno(dtp, src->dbu_err);130dst->dbu_err = src->dbu_err;131} else {132dt_buf_write(dtp, dst, src->dbu_buf,133(size_t)(src->dbu_ptr - src->dbu_buf), align);134}135}136137size_t138dt_buf_offset(const dt_buf_t *bp, size_t align)139{140size_t off = (size_t)(bp->dbu_ptr - bp->dbu_buf);141return (roundup(off, align));142}143144size_t145dt_buf_len(const dt_buf_t *bp)146{147return (bp->dbu_ptr - bp->dbu_buf);148}149150int151dt_buf_error(const dt_buf_t *bp)152{153return (bp->dbu_err);154}155156void *157dt_buf_ptr(const dt_buf_t *bp)158{159return (bp->dbu_buf);160}161162void *163dt_buf_claim(dtrace_hdl_t *dtp, dt_buf_t *bp)164{165void *buf = bp->dbu_buf;166167if (bp->dbu_err != 0) {168dt_free(dtp, buf);169buf = NULL;170}171172bp->dbu_buf = bp->dbu_ptr = NULL;173bp->dbu_len = 0;174175return (buf);176}177178179