]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/bufferfifo.cpp
Security-54.1.3.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / bufferfifo.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // bufferfifo - a Sink that queues data in a FIFO of buffers for retrieval
21 //
22 #include "bufferfifo.h"
23 #include <Security/memutils.h>
24
25
26 namespace Security {
27
28
29 //
30 // On destruction, throw away all queued buffers (that haven't been picked up)
31 //
32 BufferFifo::~BufferFifo()
33 {
34 while (!mBuffers.empty()) {
35 delete mBuffers.front();
36 mBuffers.pop();
37 }
38 }
39
40 void BufferFifo::clearBuffer()
41 {
42 while (!mBuffers.empty()) {
43 delete mBuffers.front();
44 mBuffers.pop();
45 }
46 mSize = 0;
47 }
48
49 //
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.
53 //
54 void BufferFifo::consume(const void *data, size_t size)
55 {
56 mSize += size;
57
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);
63 size -= length;
64 }
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);
71 }
72 }
73
74
75 //
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.
78 //
79 Buffer *BufferFifo::pop()
80 {
81 assert(!mBuffers.empty());
82 Buffer *top = mBuffers.front();
83 mBuffers.pop();
84 return top;
85 }
86
87
88 } // end namespace Security