Remove old, now unused code

CQTexperiment
Christopher Snowhill 2022-02-06 21:50:23 -08:00
parent 85c7073649
commit 7bf4c4445d
3 changed files with 0 additions and 391 deletions

View File

@ -28,8 +28,6 @@
17D21CC50B8BE4BA00D1EBDE /* OutputCoreAudio.h in Headers */ = {isa = PBXBuildFile; fileRef = 17D21C9C0B8BE4BA00D1EBDE /* OutputCoreAudio.h */; settings = {ATTRIBUTES = (Public, ); }; };
17D21CC60B8BE4BA00D1EBDE /* OutputCoreAudio.m in Sources */ = {isa = PBXBuildFile; fileRef = 17D21C9D0B8BE4BA00D1EBDE /* OutputCoreAudio.m */; };
17D21CC70B8BE4BA00D1EBDE /* Status.h in Headers */ = {isa = PBXBuildFile; fileRef = 17D21C9E0B8BE4BA00D1EBDE /* Status.h */; settings = {ATTRIBUTES = (Public, ); }; };
17D21CDF0B8BE5B400D1EBDE /* VirtualRingBuffer.h in Headers */ = {isa = PBXBuildFile; fileRef = 17D21CDA0B8BE5B400D1EBDE /* VirtualRingBuffer.h */; settings = {ATTRIBUTES = (Public, ); }; };
17D21CE00B8BE5B400D1EBDE /* VirtualRingBuffer.m in Sources */ = {isa = PBXBuildFile; fileRef = 17D21CDB0B8BE5B400D1EBDE /* VirtualRingBuffer.m */; };
17D21CF30B8BE5EF00D1EBDE /* Semaphore.h in Headers */ = {isa = PBXBuildFile; fileRef = 17D21CF10B8BE5EF00D1EBDE /* Semaphore.h */; settings = {ATTRIBUTES = (Public, ); }; };
17D21CF40B8BE5EF00D1EBDE /* Semaphore.m in Sources */ = {isa = PBXBuildFile; fileRef = 17D21CF20B8BE5EF00D1EBDE /* Semaphore.m */; };
17D21DAD0B8BE76800D1EBDE /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 17D21DA90B8BE76800D1EBDE /* AudioToolbox.framework */; };
@ -126,8 +124,6 @@
17D21C9C0B8BE4BA00D1EBDE /* OutputCoreAudio.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = OutputCoreAudio.h; sourceTree = "<group>"; };
17D21C9D0B8BE4BA00D1EBDE /* OutputCoreAudio.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = OutputCoreAudio.m; sourceTree = "<group>"; };
17D21C9E0B8BE4BA00D1EBDE /* Status.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = Status.h; sourceTree = "<group>"; };
17D21CDA0B8BE5B400D1EBDE /* VirtualRingBuffer.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = VirtualRingBuffer.h; sourceTree = "<group>"; };
17D21CDB0B8BE5B400D1EBDE /* VirtualRingBuffer.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = VirtualRingBuffer.m; sourceTree = "<group>"; };
17D21CF10B8BE5EF00D1EBDE /* Semaphore.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = Semaphore.h; sourceTree = "<group>"; };
17D21CF20B8BE5EF00D1EBDE /* Semaphore.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = Semaphore.m; sourceTree = "<group>"; };
17D21DA90B8BE76800D1EBDE /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = /System/Library/Frameworks/AudioToolbox.framework; sourceTree = "<absolute>"; };
@ -336,20 +332,10 @@
835C88AE279811A500E28EAE /* hdcd */,
835C88A22797D4D400E28EAE /* lvqcl */,
17D21DC40B8BE79700D1EBDE /* CoreAudioUtils */,
17D21CD90B8BE5B400D1EBDE /* VirtualRingBuffer */,
);
path = ThirdParty;
sourceTree = "<group>";
};
17D21CD90B8BE5B400D1EBDE /* VirtualRingBuffer */ = {
isa = PBXGroup;
children = (
17D21CDA0B8BE5B400D1EBDE /* VirtualRingBuffer.h */,
17D21CDB0B8BE5B400D1EBDE /* VirtualRingBuffer.m */,
);
path = VirtualRingBuffer;
sourceTree = "<group>";
};
17D21CDC0B8BE5B400D1EBDE /* Utils */ = {
isa = PBXGroup;
children = (
@ -454,7 +440,6 @@
17D21CC50B8BE4BA00D1EBDE /* OutputCoreAudio.h in Headers */,
834FD4F427AFA2150063BC83 /* Downmix.h in Headers */,
17D21CC70B8BE4BA00D1EBDE /* Status.h in Headers */,
17D21CDF0B8BE5B400D1EBDE /* VirtualRingBuffer.h in Headers */,
835C88AB2797D4D400E28EAE /* lpc.h in Headers */,
17D21CF30B8BE5EF00D1EBDE /* Semaphore.h in Headers */,
17D21DC70B8BE79700D1EBDE /* CoreAudioUtils.h in Headers */,
@ -562,7 +547,6 @@
17D21CAA0B8BE4BA00D1EBDE /* OutputNode.m in Sources */,
834FD4F527AFA2150063BC83 /* Downmix.m in Sources */,
17D21CC60B8BE4BA00D1EBDE /* OutputCoreAudio.m in Sources */,
17D21CE00B8BE5B400D1EBDE /* VirtualRingBuffer.m in Sources */,
835C88B2279811A500E28EAE /* hdcd_decode2.c in Sources */,
834FD4ED27AF91220063BC83 /* AudioChunk.m in Sources */,
17D21CF40B8BE5EF00D1EBDE /* Semaphore.m in Sources */,

View File

@ -1,91 +0,0 @@
//
// VirtualRingBuffer.h
// PlayBufferedSoundFile
//
/*
Copyright (c) 2002, Kurt Revis. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 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.
* 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.
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.
*/
//
// VirtualRingBuffer implements a classic ring buffer (or circular buffer), with a couple of twists.
//
// * It allows reads and writes to happen in different threads, with no explicit locking,
// so readers and writers will never block. This is useful if either thread uses the
// time-constraint scheduling policy, since it is bad for such threads to block for
// indefinite amounts of time.
//
// * It uses a virtual memory trick to allow the client to read or write using just one
// operation, even if the data involved wraps around the end of the buffer. We allocate
// our buffer normally, and then place a VM region immediately after it in the address
// space which maps back to the "real" buffer. So reads and writes into both sections
// are transparently translated into the same physical memory.
// This makes the API much simpler to use, and saves us from doing some math to
// calculate the wraparound points.
// The tradeoff is that we use twice as much address space for the buffer than we would
// otherwise. Address space is not typically constrained for most applications, though,
// so this isn't a big problem.
// The idea for this trick came from <http://phil.ipal.org/freeware/vrb/> (via sweetcode.org),
// although none of that code is used here. (We use the Mach VM API directly.)
//
// Threading note:
// It is expected that this object will be shared between exactly two threads; one will
// always read and the other will always write. In that situation, the implementation is
// thread-safe, and this object will never block or yield.
// It will also work in one thread, of course (although I don't know why you'd bother).
// However, if you have multiple reader or writer threads, all bets are off!
#import <Foundation/Foundation.h>
#include <stdatomic.h>
@interface VirtualRingBuffer : NSObject
{
void *buffer;
UInt32 bufferLength;
// buffer is the start of the ring buffer's address space.
// bufferEnd is the end of the "real" buffer (always buffer + bufferLength).
// Note that the "virtual" portion of the buffer extends from bufferEnd to bufferEnd+bufferLength.
atomic_int readPointer;
atomic_int writePointer;
atomic_int bufferFilled;
NSRecursiveLock *accessLock;
}
- (id)initWithLength:(UInt32)length;
// Note: The specified length will be rounded up to an integral number of VM pages.
// Empties the buffer. It is NOT safe to do this while anyone is reading from or writing to the buffer.
- (void)empty;
// Checks if the buffer is empty or not. This is safe in any thread.
- (BOOL)isEmpty;
// Return amount buffered
- (UInt32)bufferedLength;
// Read operations:
// The reading thread must call this method first.
- (UInt32)lengthAvailableToReadReturningPointer:(void **)returnedReadPointer;
// Iff a value > 0 is returned, the reading thread may go on to read that much data from the returned pointer.
// Afterwards, the reading thread must call didReadLength:.
- (void)didReadLength:(UInt32)length;
// Write operations:
// The writing thread must call this method first.
- (UInt32)lengthAvailableToWriteReturningPointer:(void **)returnedWritePointer;
// Iff a value > 0 is returned, the writing thread may then write that much data into the returned pointer.
// Afterwards, the writing thread must call didWriteLength:.
- (void)didWriteLength:(UInt32)length;
@end

View File

@ -1,284 +0,0 @@
//
// VirtualRingBuffer.m
// PlayBufferedSoundFile
//
/*
Copyright (c) 2002, Kurt Revis. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 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.
* 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.
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.
*/
#import "VirtualRingBuffer.h"
#include <mach/mach.h>
#include <mach/mach_error.h>
#import "Logging.h"
@implementation VirtualRingBuffer
static void *allocateVirtualBuffer(UInt32 bufferLength);
static void deallocateVirtualBuffer(void *buffer, UInt32 bufferLength);
- (id)initWithLength:(UInt32)length
{
if (![super init])
return nil;
// We need to allocate entire VM pages, so round the specified length up to the next page if necessary.
bufferLength = (UInt32) round_page(length);
buffer = allocateVirtualBuffer(bufferLength);
if (!buffer)
{
self = nil;
return nil;
}
atomic_init(&readPointer, 0);
atomic_init(&writePointer, 0);
atomic_init(&bufferFilled, 0);
accessLock = [[NSRecursiveLock alloc] init];
return self;
}
- (void)dealloc
{
if (buffer)
deallocateVirtualBuffer(buffer, bufferLength);
}
- (void)empty
{
// Assumption:
// No one is reading or writing from the buffer, in any thread, when this method is called.
[accessLock lock];
atomic_init(&readPointer, 0);
atomic_init(&writePointer, 0);
atomic_init(&bufferFilled, 0);
[accessLock unlock];
}
- (BOOL)isEmpty
{
return (atomic_load_explicit(&bufferFilled, memory_order_relaxed) == 0);
}
- (UInt32)bufferedLength
{
return atomic_load_explicit(&bufferFilled, memory_order_relaxed);
}
//
// Theory of operation:
//
// This class keeps a pointer to the next byte to be read (readPointer) and a pointer to the next byte to be written (writePointer).
// readPointer is only advanced in the reading thread (except for one case: when the buffer first has data written to it).
// writePointer is only advanced in the writing thread.
//
// Since loading and storing word length data is atomic, each pointer can safely be modified in one thread while the other thread
// uses it, IF each thread is careful to make a local copy of the "opposite" pointer when necessary.
//
//
// Read operations
//
- (UInt32)lengthAvailableToReadReturningPointer:(void **)returnedReadPointer
{
// Assumptions:
// returnedReadPointer != NULL
[accessLock lock];
UInt32 length;
// Read this pointer exactly once, so we're safe in case it is changed in another thread
int localReadPointer = atomic_load_explicit(&readPointer, memory_order_relaxed);
int localBufferFilled = atomic_load_explicit(&bufferFilled, memory_order_relaxed);
length = bufferLength - localReadPointer;
if (length > localBufferFilled)
length = localBufferFilled;
// Depending on out-of-order execution and memory storage, either one of these may be NULL when the buffer is empty. So we must check both.
*returnedReadPointer = buffer + localReadPointer;
if (!length)
[accessLock unlock];
return length;
}
- (void)didReadLength:(UInt32)length
{
// Assumptions:
// [self lengthAvailableToReadReturningPointer:] currently returns a value >= length
// length > 0
if (atomic_fetch_add(&readPointer, length) + length >= bufferLength)
atomic_fetch_sub(&readPointer, bufferLength);
atomic_fetch_sub(&bufferFilled, length);
[accessLock unlock];
}
//
// Write operations
//
- (UInt32)lengthAvailableToWriteReturningPointer:(void **)returnedWritePointer
{
// Assumptions:
// returnedWritePointer != NULL
[accessLock lock];
UInt32 length;
// Read this pointer exactly once, so we're safe in case it is changed in another thread
int localWritePointer = atomic_load_explicit(&writePointer, memory_order_relaxed);
int localBufferFilled = atomic_load_explicit(&bufferFilled, memory_order_relaxed);
length = bufferLength - localBufferFilled;
if (length > bufferLength - localWritePointer)
length = bufferLength - localWritePointer;
*returnedWritePointer = buffer + localWritePointer;
if (!length)
[accessLock unlock];
return length;
}
- (void)didWriteLength:(UInt32)length
{
// Assumptions:
// [self lengthAvailableToWriteReturningPointer:] currently returns a value >= length
// length > 0
if (atomic_fetch_add(&writePointer, length) + length >= bufferLength)
atomic_fetch_sub(&writePointer, bufferLength);
atomic_fetch_add(&bufferFilled, length);
[accessLock unlock];
}
@end
void *allocateVirtualBuffer(UInt32 bufferLength)
{
kern_return_t error;
vm_address_t originalAddress = (vm_address_t)NULL;
vm_address_t realAddress = (vm_address_t)NULL;
mach_port_t memoryEntry;
vm_size_t memoryEntryLength;
vm_address_t virtualAddress = (vm_address_t)NULL;
// We want to find where we can get 2 * bufferLength bytes of contiguous address space.
// So let's just allocate that space, remember its address, and deallocate it.
// (This doesn't actually have to touch all of that memory so it's not terribly expensive.)
error = vm_allocate(mach_task_self(), &originalAddress, 2 * bufferLength, TRUE);
if (error) {
#if DEBUG
mach_error("vm_allocate initial chunk", error);
#endif
return NULL;
}
error = vm_deallocate(mach_task_self(), originalAddress, 2 * bufferLength);
if (error) {
#if DEBUG
mach_error("vm_deallocate initial chunk", error);
#endif
return NULL;
}
// Then allocate a "real" block of memory at the same address, but with the normal bufferLength.
realAddress = originalAddress;
error = vm_allocate(mach_task_self(), &realAddress, bufferLength, FALSE);
if (error) {
#if DEBUG
mach_error("vm_allocate real chunk", error);
#endif
return NULL;
}
if (realAddress != originalAddress) {
DLog(@"allocateVirtualBuffer: vm_allocate 2nd time didn't return same address (%p vs %p)", (void *) originalAddress, (void *) realAddress);
goto errorReturn;
}
// Then make a memory entry for the area we just allocated.
memoryEntryLength = bufferLength;
error = mach_make_memory_entry(mach_task_self(), &memoryEntryLength, realAddress, VM_PROT_READ | VM_PROT_WRITE, &memoryEntry, (vm_address_t)NULL);
if (error) {
#if DEBUG
mach_error("mach_make_memory_entry", error);
#endif
goto errorReturn;
}
if (!memoryEntry) {
DLog(@"mach_make_memory_entry: returned memoryEntry of NULL");
goto errorReturn;
}
if (memoryEntryLength != bufferLength) {
DLog(@"mach_make_memory_entry: size changed (from %0x to %0lx)", bufferLength, memoryEntryLength);
goto errorReturn;
}
// And map the area immediately after the first block, with length bufferLength, to that memory entry.
virtualAddress = realAddress + bufferLength;
error = vm_map(mach_task_self(), &virtualAddress, bufferLength, 0, FALSE, memoryEntry, 0, FALSE, VM_PROT_READ | VM_PROT_WRITE, VM_PROT_READ | VM_PROT_WRITE, VM_INHERIT_DEFAULT);
if (error) {
#if DEBUG
mach_error("vm_map", error);
#endif
// TODO Retry from the beginning, instead of failing completely. There is a tiny (but > 0) probability that someone
// will allocate this space out from under us.
virtualAddress = (vm_address_t)NULL;
goto errorReturn;
}
if (virtualAddress != realAddress + bufferLength) {
DLog(@"vm_map: didn't return correct address (%p vs %p)", (void *) realAddress + bufferLength, (void *) virtualAddress);
goto errorReturn;
}
// Success!
return (void *)realAddress;
errorReturn:
if (realAddress)
vm_deallocate(mach_task_self(), realAddress, bufferLength);
if (virtualAddress)
vm_deallocate(mach_task_self(), virtualAddress, bufferLength);
return NULL;
}
void deallocateVirtualBuffer(void *buffer, UInt32 bufferLength)
{
kern_return_t error;
// We can conveniently deallocate both the vm_allocated memory and
// the vm_mapped region at the same time.
error = vm_deallocate(mach_task_self(), (vm_address_t)buffer, bufferLength * 2);
if (error) {
#if DEBUG
mach_error("vm_deallocate in dealloc", error);
#endif
}
}