NOOP, remove trailing tabs/whitespace.
[ardour.git] / libs / appleutility / CABufferList.cpp
1 /*      Copyright:      � Copyright 2005 Apple Computer, Inc. All rights reserved.
2
3         Disclaimer:     IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
4                         ("Apple") in consideration of your agreement to the following terms, and your
5                         use, installation, modification or redistribution of this Apple software
6                         constitutes acceptance of these terms.  If you do not agree with these terms,
7                         please do not use, install, modify or redistribute this Apple software.
8
9                         In consideration of your agreement to abide by the following terms, and subject
10                         to these terms, Apple grants you a personal, non-exclusive license, under Apple�s
11                         copyrights in this original Apple software (the "Apple Software"), to use,
12                         reproduce, modify and redistribute the Apple Software, with or without
13                         modifications, in source and/or binary forms; provided that if you redistribute
14                         the Apple Software in its entirety and without modifications, you must retain
15                         this notice and the following text and disclaimers in all such redistributions of
16                         the Apple Software.  Neither the name, trademarks, service marks or logos of
17                         Apple Computer, Inc. may be used to endorse or promote products derived from the
18                         Apple Software without specific prior written permission from Apple.  Except as
19                         expressly stated in this notice, no other rights or licenses, express or implied,
20                         are granted by Apple herein, including but not limited to any patent rights that
21                         may be infringed by your derivative works or by other works in which the Apple
22                         Software may be incorporated.
23
24                         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
25                         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
26                         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27                         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
28                         COMBINATION WITH YOUR PRODUCTS.
29
30                         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
31                         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32                         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33                         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
34                         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
35                         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
36                         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*=============================================================================
39         CABufferList.cpp
40
41 =============================================================================*/
42
43 #include "CABufferList.h"
44 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
45         #include <CoreServices/CoreServices.h>
46 #else
47         #include <Endian.h>
48 #endif
49
50 void            CABufferList::AllocateBuffers(UInt32 nBytes)
51 {
52         if (nBytes <= GetNumBytes()) return;
53
54         if (mNumberBuffers > 1)
55                 // align successive buffers for Altivec and to take alternating
56                 // cache line hits by spacing them by odd multiples of 16
57                 nBytes = (nBytes + (0x10 - (nBytes & 0xF))) | 0x10;
58         UInt32 memorySize = nBytes * mNumberBuffers;
59         Byte *newMemory = new Byte[memorySize], *p = newMemory;
60         memset(newMemory, 0, memorySize);       // get page faults now, not later
61
62         AudioBuffer *buf = mBuffers;
63         for (UInt32 i = mNumberBuffers; i--; ++buf) {
64                 if (buf->mData != NULL && buf->mDataByteSize > 0)
65                         // preserve existing buffer contents
66                         memcpy(p, buf->mData, buf->mDataByteSize);
67                 buf->mDataByteSize = nBytes;
68                 buf->mData = p;
69                 p += nBytes;
70         }
71         Byte *oldMemory = mBufferMemory;
72         mBufferMemory = newMemory;
73         delete[] oldMemory;
74 }
75
76 void            CABufferList::AllocateBuffersAndCopyFrom(UInt32 nBytes, CABufferList *inSrcList, CABufferList *inSetPtrList)
77 {
78         if (mNumberBuffers != inSrcList->mNumberBuffers) return;
79         if (mNumberBuffers != inSetPtrList->mNumberBuffers) return;
80         if (nBytes <= GetNumBytes()) {
81                 CopyAllFrom(inSrcList, inSetPtrList);
82                 return;
83         }
84         inSetPtrList->VerifyNotTrashingOwnedBuffer();
85         UInt32 fromByteSize = inSrcList->GetNumBytes();
86
87         if (mNumberBuffers > 1)
88                 // align successive buffers for Altivec and to take alternating
89                 // cache line hits by spacing them by odd multiples of 16
90                 nBytes = (nBytes + (0x10 - (nBytes & 0xF))) | 0x10;
91         UInt32 memorySize = nBytes * mNumberBuffers;
92         Byte *newMemory = new Byte[memorySize], *p = newMemory;
93         memset(newMemory, 0, memorySize);       // make buffer "hot"
94
95         AudioBuffer *buf = mBuffers;
96         AudioBuffer *ptrBuf = inSetPtrList->mBuffers;
97         AudioBuffer *srcBuf = inSrcList->mBuffers;
98         for (UInt32 i = mNumberBuffers; i--; ++buf, ++ptrBuf, ++srcBuf) {
99                 if (srcBuf->mData != NULL && srcBuf->mDataByteSize > 0)
100                         // preserve existing buffer contents
101                         memmove(p, srcBuf->mData, srcBuf->mDataByteSize);
102                 buf->mDataByteSize = nBytes;
103                 buf->mData = p;
104                 ptrBuf->mDataByteSize = srcBuf->mDataByteSize;
105                 ptrBuf->mData = p;
106                 p += nBytes;
107         }
108         Byte *oldMemory = mBufferMemory;
109         mBufferMemory = newMemory;
110         if (inSrcList != inSetPtrList)
111                 inSrcList->BytesConsumed(fromByteSize);
112         delete[] oldMemory;
113 }
114
115 void            CABufferList::DeallocateBuffers()
116 {
117         AudioBuffer *buf = mBuffers;
118         for (UInt32 i = mNumberBuffers; i--; ++buf) {
119                 buf->mData = NULL;
120                 buf->mDataByteSize = 0;
121         }
122         if (mBufferMemory != NULL) {
123                 delete[] mBufferMemory;
124                 mBufferMemory = NULL;
125         }
126
127 }
128
129 extern "C" void CAShowAudioBufferList(const AudioBufferList *abl, int framesToPrint, int wordSize)
130 {
131         printf("AudioBufferList @ %p:\n", abl);
132         const AudioBuffer *buf = abl->mBuffers;
133         for (UInt32 i = 0; i < abl->mNumberBuffers; ++i, ++buf) {
134                 printf("  [%2ld]: %2ldch, %5ld bytes @ %8p",
135                         i, buf->mNumberChannels, buf->mDataByteSize, buf->mData);
136                 if (framesToPrint) {
137                         printf(":");
138                         Byte *p = (Byte *)buf->mData;
139                         for (int j = framesToPrint * buf->mNumberChannels; --j >= 0; )
140                                 switch (wordSize) {
141                                 case 0:
142                                         printf(" %6.3f", *(Float32 *)p);
143                                         p += sizeof(Float32);
144                                         break;
145                                 case 1:
146                                 case -1:
147                                         printf(" %02X", *p);
148                                         p += 1;
149                                         break;
150                                 case 2:
151                                         printf(" %04X", EndianU16_BtoN(*(UInt16 *)p));
152                                         p += 2;
153                                         break;
154                                 case 3:
155                                         printf(" %06X", (p[0] << 16) | (p[1] << 8) | p[2]);
156                                         p += 3;
157                                         break;
158                                 case 4:
159                                         printf(" %08lX", EndianU32_BtoN(*(UInt32 *)p));
160                                         p += 4;
161                                         break;
162                                 case -2:
163                                         printf(" %04X", EndianU16_LtoN(*(UInt16 *)p));
164                                         p += 2;
165                                         break;
166                                 case -3:
167                                         printf(" %06X", (p[2] << 16) | (p[1] << 8) | p[0]);
168                                         p += 3;
169                                         break;
170                                 case -4:
171                                         printf(" %08lX", EndianU32_LtoN(*(UInt32 *)p));
172                                         p += 4;
173                                         break;
174                                 }
175                 }
176                 printf("\n");
177         }
178 }
179