]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/bufferfifo.cpp
2 * Copyright (c) 2000-2004,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // bufferfifo - a Sink that queues data in a FIFO of buffers for retrieval
28 #include "bufferfifo.h"
29 #include <security_utilities/memutils.h>
36 // On destruction, throw away all queued buffers (that haven't been picked up)
38 BufferFifo::~BufferFifo()
40 while (!mBuffers
.empty()) {
41 delete mBuffers
.front();
46 void BufferFifo::clearBuffer()
48 while (!mBuffers
.empty()) {
49 delete mBuffers
.front();
56 // This is the put function of a Sink. We store the data in at most two buffers:
57 // First we append to the last (partially filled) one; then we allocate a new one
58 // (if needed) to hold the rest.
60 void BufferFifo::consume(const void *data
, size_t size
)
64 // step 1: fill the rearmost (partially filled) buffer
65 if (size
> 0 && !mBuffers
.empty()) {
66 Buffer
*current
= mBuffers
.back();
67 size_t length
= current
->put(data
, size
);
68 data
= LowLevelMemoryUtilities::increment(data
, length
);
71 // step 2: if there's anything left, make a new buffer and fill it
72 if (size
> 0) { // not done
73 Buffer
*current
= new Buffer(max(bufferLength
, size
));
74 mBuffers
.push(current
);
75 assert(current
->available() >= size
);
76 current
->put(data
, size
);
82 // Pull the first (FI) buffer off the queue and deliver it.
83 // We retain no memory of it; it belongs to the caller now.
85 Buffer
*BufferFifo::pop()
87 assert(!mBuffers
.empty());
88 Buffer
*top
= mBuffers
.front();
94 } // end namespace Security