Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 87 lines (70 sloc) 4.817 kB
dc5be1c Initial check-in.
uli authored
1 //
2 // UKVirtualRingBuffer.h
3 // PlayBufferedSoundFile
4 //
5 /*
6 Copyright (c) 2002, Kurt Revis. All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
12 * Neither the name of Snoize nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 */
16
17 //
18 // UKVirtualRingBuffer implements a classic ring buffer (or circular buffer), with a couple of twists.
19 //
20 // * It allows reads and writes to happen in different threads, with no explicit locking,
21 // so readers and writers will never block. This is useful if either thread uses the
22 // time-constraint scheduling policy, since it is bad for such threads to block for
23 // indefinite amounts of time.
24 //
25 // * It uses a virtual memory trick to allow the client to read or write using just one
26 // operation, even if the data involved wraps around the end of the buffer. We allocate
27 // our buffer normally, and then place a VM region immediately after it in the address
28 // space which maps back to the "real" buffer. So reads and writes into both sections
29 // are transparently translated into the same physical memory.
30 // This makes the API much simpler to use, and saves us from doing some math to
31 // calculate the wraparound points.
32 // The tradeoff is that we use twice as much address space for the buffer than we would
33 // otherwise. Address space is not typically constrained for most applications, though,
34 // so this isn't a big problem.
35 // The idea for this trick came from <http://phil.ipal.org/freeware/vrb/> (via sweetcode.org),
36 // although none of that code is used here. (We use the Mach VM API directly.)
37 //
38
39 // Threading note:
40 // It is expected that this object will be shared between exactly two threads; one will
41 // always read and the other will always write. In that situation, the implementation is
42 // thread-safe, and this object will never block or yield.
43 // It will also work in one thread, of course (although I don't know why you'd bother).
44 // However, if you have multiple reader or writer threads, all bets are off!
45
46 #import <Foundation/Foundation.h>
47
48
49 @interface UKVirtualRingBuffer : NSObject
50 {
51 void *buffer;
52 void *bufferEnd;
53 UInt32 bufferLength;
54 // buffer is the start of the ring buffer's address space.
55 // bufferEnd is the end of the "real" buffer (always buffer + bufferLength).
56 // Note that the "virtual" portion of the buffer extends from bufferEnd to bufferEnd+bufferLength.
57
58 void *readPointer;
59 void *writePointer;
60 }
61
62 - (id)initWithLength:(UInt32)length;
63 // Note: The specified length will be rounded up to an integral number of VM pages.
64
65 // Empties the buffer. It is NOT safe to do this while anyone is reading from or writing to the buffer.
66 - (void)empty;
67 // Checks if the buffer is empty or not. This is safe in any thread.
68 - (BOOL)isEmpty;
69
70 // Read operations:
71
72 // The reading thread must call this method first.
73 - (UInt32)lengthAvailableToReadReturningPointer:(void **)returnedReadPointer;
74 // Iff a value > 0 is returned, the reading thread may go on to read that much data from the returned pointer.
75 // Afterwards, the reading thread must call didReadLength:.
76 - (void)didReadLength:(UInt32)length;
77
78 // Write operations:
79
80 // The writing thread must call this method first.
81 - (UInt32)lengthAvailableToWriteReturningPointer:(void **)returnedWritePointer;
82 // Iff a value > 0 is returned, the writing thread may then write that much data into the returned pointer.
83 // Afterwards, the writing thread must call didWriteLength:.
84 - (void)didWriteLength:(UInt32)length;
85
86 @end
Something went wrong with that request. Please try again.