]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/bufferfifo.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // bufferfifo - a Sink that queues data in a FIFO of buffers for retrieval
22 #include "bufferfifo.h"
23 #include <Security/memutils.h>
30 // On destruction, throw away all queued buffers (that haven't been picked up)
32 BufferFifo::~BufferFifo()
34 while (!mBuffers
.empty()) {
35 delete mBuffers
.front();
40 void BufferFifo::clearBuffer()
42 while (!mBuffers
.empty()) {
43 delete mBuffers
.front();
50 // This is the put function of a Sink. We store the data in at most two buffers:
51 // First we append to the last (partially filled) one; then we allocate a new one
52 // (if needed) to hold the rest.
54 void BufferFifo::consume(const void *data
, size_t size
)
58 // step 1: fill the rearmost (partially filled) buffer
59 if (size
> 0 && !mBuffers
.empty()) {
60 Buffer
*current
= mBuffers
.back();
61 size_t length
= current
->put(data
, size
);
62 data
= LowLevelMemoryUtilities::increment(data
, length
);
65 // step 2: if there's anything left, make a new buffer and fill it
66 if (size
> 0) { // not done
67 Buffer
*current
= new Buffer(max(bufferLength
, size
));
68 mBuffers
.push(current
);
69 assert(current
->available() >= size
);
70 current
->put(data
, size
);
76 // Pull the first (FI) buffer off the queue and deliver it.
77 // We retain no memory of it; it belongs to the caller now.
79 Buffer
*BufferFifo::pop()
81 assert(!mBuffers
.empty());
82 Buffer
*top
= mBuffers
.front();
88 } // end namespace Security