Source to iokit/Families/IONetworking/IOPacketQueue.cpp
/*
* Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
*
* IOPacketQueue.cpp - Implements a FIFO mbuf packet queue.
*
* HISTORY
* 9-Dec-1998 Joe Liu (jliu) created.
*
*/
#include <IOKit/assert.h>
#include <IOKit/IOLib.h> // IOLog
#include <IOKit/network/IOPacketQueue.h>
// Get the mbuf definitions from BSD headers.
//
extern "C" {
#include <sys/param.h>
#include <sys/mbuf.h>
}
#define super OSObject
OSDefineMetaClassAndStructors( IOPacketQueue, OSObject )
//---------------------------------------------------------------------------
// Lock macros.
#define LOCK IOSimpleLockLock(_lock)
#define UNLOCK IOSimpleLockUnlock(_lock)
//---------------------------------------------------------------------------
// Factory method that will construct and initialize an IOPacketQueue instance.
//
// capacity: The initial capacity of the queue. Can be changed
// later through setCapacity().
//
// Returns an IOPacketQueue instance upon success, or 0 otherwise.
bool IOPacketQueue::initWithCapacity(UInt32 capacity)
{
_head = _tail = (struct mbuf *) 0;
_size = _peakSize = 0;
_capacity = capacity;
if (!super::init()) {
IOLog("IOPacketQueue: super::init() failed\n");
return false;
}
if ((_lock = IOSimpleLockAlloc()) == 0)
return false;
IOSimpleLockInit(_lock);
return true;
}
//---------------------------------------------------------------------------
// Factory method that will construct and initialize an IOPacketQueue instance.
//
// capacity: The initial capacity of the queue. Can be changed later
// through setCapacity().
//
// Returns an IOPacketQueue instance on success, or 0 otherwise.
IOPacketQueue * IOPacketQueue::withCapacity(UInt32 capacity)
{
IOPacketQueue * queue = new IOPacketQueue;
if (queue && !queue->initWithCapacity(capacity)) {
queue->release();
return 0;
}
return queue;
}
//---------------------------------------------------------------------------
// Frees the IOPacketQueue instance. Release all packets in the queue to the
// free mbuf pool, then call super::free().
void IOPacketQueue::free()
{
if (_lock) {
setCapacity(0);
lockFlush();
IOSimpleLockFree(_lock);
}
super::free();
}
//---------------------------------------------------------------------------
// Get the size of the queue.
// Returns the number of packets currently held in the queue.
UInt32 IOPacketQueue::getSize() const
{
return (_size);
}
//---------------------------------------------------------------------------
// The queue contains a counter that records the peak size of the queue.