]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Families/IONetworking/IOPacketQueue.cpp
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
25 * IOPacketQueue.cpp - Implements a FIFO mbuf packet queue.
28 * 9-Dec-1998 Joe Liu (jliu) created.
32 #include <IOKit/assert.h>
33 #include <IOKit/IOLib.h> // IOLog
34 #include <IOKit/network/IOPacketQueue.h>
35 #include "IOMbufQueue.h"
37 #define super OSObject
38 OSDefineMetaClassAndStructors( IOPacketQueue
, OSObject
)
39 OSMetaClassDefineReservedUnused( IOPacketQueue
, 0);
40 OSMetaClassDefineReservedUnused( IOPacketQueue
, 1);
41 OSMetaClassDefineReservedUnused( IOPacketQueue
, 2);
42 OSMetaClassDefineReservedUnused( IOPacketQueue
, 3);
43 OSMetaClassDefineReservedUnused( IOPacketQueue
, 4);
44 OSMetaClassDefineReservedUnused( IOPacketQueue
, 5);
45 OSMetaClassDefineReservedUnused( IOPacketQueue
, 6);
46 OSMetaClassDefineReservedUnused( IOPacketQueue
, 7);
47 OSMetaClassDefineReservedUnused( IOPacketQueue
, 8);
48 OSMetaClassDefineReservedUnused( IOPacketQueue
, 9);
49 OSMetaClassDefineReservedUnused( IOPacketQueue
, 10);
50 OSMetaClassDefineReservedUnused( IOPacketQueue
, 11);
51 OSMetaClassDefineReservedUnused( IOPacketQueue
, 12);
52 OSMetaClassDefineReservedUnused( IOPacketQueue
, 13);
53 OSMetaClassDefineReservedUnused( IOPacketQueue
, 14);
54 OSMetaClassDefineReservedUnused( IOPacketQueue
, 15);
57 //---------------------------------------------------------------------------
60 #define LOCK IOSimpleLockLock(_lock)
61 #define UNLOCK IOSimpleLockUnlock(_lock)
63 //---------------------------------------------------------------------------
64 // Initialize an IOPacketQueue object.
66 bool IOPacketQueue::initWithCapacity(UInt32 capacity
)
68 if (super::init() == false)
70 IOLog("IOPacketQueue: super::init() failed\n");
74 _queue
= IONew(IOMbufQueue
, 1);
78 IOMbufQueueInit(_queue
, capacity
);
80 if ((_lock
= IOSimpleLockAlloc()) == 0)
83 IOSimpleLockInit(_lock
);
88 //---------------------------------------------------------------------------
89 // Factory method that will construct and initialize an IOPacketQueue object.
91 IOPacketQueue
* IOPacketQueue::withCapacity(UInt32 capacity
)
93 IOPacketQueue
* queue
= new IOPacketQueue
;
95 if (queue
&& !queue
->initWithCapacity(capacity
))
103 //---------------------------------------------------------------------------
104 // Frees the IOPacketQueue instance.
106 void IOPacketQueue::free()
110 IOSimpleLockFree(_lock
);
117 IODelete(_queue
, IOMbufQueue
, 1);
124 //---------------------------------------------------------------------------
125 // Get the current size of the queue.
127 UInt32
IOPacketQueue::getSize() const
129 return IOMbufQueueGetSize(_queue
);
132 //---------------------------------------------------------------------------
133 // Change the capacity of the queue.
135 bool IOPacketQueue::setCapacity(UInt32 capacity
)
137 IOMbufQueueSetCapacity(_queue
, capacity
);
141 //---------------------------------------------------------------------------
142 // Get the capacity of the queue.
144 UInt32
IOPacketQueue::getCapacity() const
146 return IOMbufQueueGetCapacity(_queue
);
149 //---------------------------------------------------------------------------
150 // Peek at the head of the queue without dequeueing the packet.
152 const struct mbuf
* IOPacketQueue::peek() const
154 return IOMbufQueuePeek(_queue
);
157 //---------------------------------------------------------------------------
158 // Add a packet chain to the head of the queue.
160 void IOPacketQueue::prepend(struct mbuf
* m
)
162 IOMbufQueuePrepend(_queue
, m
);
165 void IOPacketQueue::prepend(IOPacketQueue
* queue
)
167 IOMbufQueuePrepend(_queue
, queue
->_queue
);
170 //---------------------------------------------------------------------------
171 // Add a packet chain to the tail of the queue.
173 bool IOPacketQueue::enqueue(struct mbuf
* m
)
175 return IOMbufQueueEnqueue(_queue
, m
);
178 bool IOPacketQueue::enqueue(IOPacketQueue
* queue
)
180 return IOMbufQueueEnqueue(_queue
, queue
->_queue
);
183 UInt32
IOPacketQueue::enqueueWithDrop(struct mbuf
* m
)
185 return IOMbufQueueEnqueue(_queue
, m
) ? 0 : IOMbufFree(m
);
188 //---------------------------------------------------------------------------
189 // Remove a single packet from the head of the queue.
191 struct mbuf
* IOPacketQueue::dequeue()
193 return IOMbufQueueDequeue(_queue
);
196 //---------------------------------------------------------------------------
197 // Remove all packets from the queue and return the chain of packet(s).
199 struct mbuf
* IOPacketQueue::dequeueAll()
201 return IOMbufQueueDequeueAll(_queue
);
204 //---------------------------------------------------------------------------
205 // Remove all packets from the queue and put them back to the free mbuf
206 // pool. The size of the queue will be cleared to zero.
208 UInt32
IOPacketQueue::flush()
210 return IOMbufFree(IOMbufQueueDequeueAll(_queue
));
213 //---------------------------------------------------------------------------
214 // Locked forms of prepend/enqueue/dequeue/dequeueAll methods.
215 // A spinlock will enforce mutually exclusive queue access.
217 void IOPacketQueue::lockPrepend(struct mbuf
* m
)
220 IOMbufQueuePrepend(_queue
, m
);
224 bool IOPacketQueue::lockEnqueue(struct mbuf
* m
)
228 ok
= IOMbufQueueEnqueue(_queue
, m
);
233 UInt32
IOPacketQueue::lockEnqueueWithDrop(struct mbuf
* m
)
237 ok
= IOMbufQueueEnqueue(_queue
, m
);
239 return ok
? 0 : IOMbufFree(m
);
242 struct mbuf
* IOPacketQueue::lockDequeue()
246 m
= IOMbufQueueDequeue(_queue
);
251 struct mbuf
* IOPacketQueue::lockDequeueAll()
255 m
= IOMbufQueueDequeueAll(_queue
);
260 UInt32
IOPacketQueue::lockFlush()
264 m
= IOMbufQueueDequeueAll(_queue
);
266 return IOMbufFree(m
);