packet.c revision 187938
1187938Semax/* $NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $ */ 2187938Semax 3187938Semax/*- 4187938Semax * Copyright (c) 2008 Iain Hibbert 5187938Semax * All rights reserved. 6187938Semax * 7187938Semax * Redistribution and use in source and binary forms, with or without 8187938Semax * modification, are permitted provided that the following conditions 9187938Semax * are met: 10187938Semax * 1. Redistributions of source code must retain the above copyright 11187938Semax * notice, this list of conditions and the following disclaimer. 12187938Semax * 2. Redistributions in binary form must reproduce the above copyright 13187938Semax * notice, this list of conditions and the following disclaimer in the 14187938Semax * documentation and/or other materials provided with the distribution. 15187938Semax * 16187938Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17187938Semax * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18187938Semax * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19187938Semax * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20187938Semax * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21187938Semax * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22187938Semax * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23187938Semax * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24187938Semax * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25187938Semax * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26187938Semax */ 27187938Semax 28187938Semax/* $FreeBSD: head/usr.sbin/bluetooth/btpand/packet.c 187938 2009-01-30 22:23:21Z emax $ */ 29187938Semax 30187938Semax#include <sys/cdefs.h> 31187938Semax__RCSID("$NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $"); 32187938Semax 33187938Semax#include "btpand.h" 34187938Semax 35187938Semaxpacket_t * 36187938Semaxpacket_alloc(channel_t *chan) 37187938Semax{ 38187938Semax packet_t *pkt; 39187938Semax 40187938Semax pkt = malloc(sizeof(packet_t) + chan->mru); 41187938Semax if (pkt == NULL) { 42187938Semax log_err("%s() failed: %m", __func__); 43187938Semax return NULL; 44187938Semax } 45187938Semax 46187938Semax memset(pkt, 0, sizeof(packet_t)); 47187938Semax STAILQ_INIT(&pkt->extlist); 48187938Semax pkt->ptr = pkt->buf; 49187938Semax 50187938Semax pkt->chan = chan; 51187938Semax chan->refcnt++; 52187938Semax 53187938Semax return pkt; 54187938Semax} 55187938Semax 56187938Semaxvoid 57187938Semaxpacket_free(packet_t *pkt) 58187938Semax{ 59187938Semax exthdr_t *eh; 60187938Semax 61187938Semax if (pkt->refcnt-- > 0) 62187938Semax return; 63187938Semax 64187938Semax while ((eh = STAILQ_FIRST(&pkt->extlist)) != NULL) { 65187938Semax STAILQ_REMOVE_HEAD(&pkt->extlist, next); 66187938Semax free(eh); 67187938Semax } 68187938Semax 69187938Semax pkt->chan->refcnt--; 70187938Semax if (pkt->chan->refcnt == 0) 71187938Semax channel_free(pkt->chan); 72187938Semax 73187938Semax free(pkt); 74187938Semax} 75187938Semax 76187938Semaxvoid 77187938Semaxpacket_adj(packet_t *pkt, size_t size) 78187938Semax{ 79187938Semax 80187938Semax assert(pkt->refcnt == 0); 81187938Semax assert(pkt->len >= size); 82187938Semax 83187938Semax pkt->ptr += size; 84187938Semax pkt->len -= size; 85187938Semax} 86187938Semax 87187938Semaxpkthdr_t * 88187938Semaxpkthdr_alloc(packet_t *pkt) 89187938Semax{ 90187938Semax pkthdr_t *ph; 91187938Semax 92187938Semax ph = malloc(sizeof(pkthdr_t)); 93187938Semax if (ph == NULL) { 94187938Semax log_err("%s() failed: %m", __func__); 95187938Semax return NULL; 96187938Semax } 97187938Semax 98187938Semax ph->data = pkt; 99187938Semax pkt->refcnt++; 100187938Semax 101187938Semax return ph; 102187938Semax} 103187938Semax 104187938Semaxvoid 105187938Semaxpkthdr_free(pkthdr_t *ph) 106187938Semax{ 107187938Semax 108187938Semax packet_free(ph->data); 109187938Semax free(ph); 110187938Semax} 111