Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | #include "AudioCompactor.h" |
michael@0 | 7 | #if defined(MOZ_MEMORY) |
michael@0 | 8 | # include "mozmemory.h" |
michael@0 | 9 | #endif |
michael@0 | 10 | |
michael@0 | 11 | namespace mozilla { |
michael@0 | 12 | |
michael@0 | 13 | static size_t |
michael@0 | 14 | MallocGoodSize(size_t aSize) |
michael@0 | 15 | { |
michael@0 | 16 | # if defined(MOZ_MEMORY) |
michael@0 | 17 | return malloc_good_size(aSize); |
michael@0 | 18 | # else |
michael@0 | 19 | return aSize; |
michael@0 | 20 | # endif |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | static size_t |
michael@0 | 24 | TooMuchSlop(size_t aSize, size_t aAllocSize, size_t aMaxSlop) |
michael@0 | 25 | { |
michael@0 | 26 | // If the allocated size is less then our target size, then we |
michael@0 | 27 | // are chunking. This means it will be completely filled with |
michael@0 | 28 | // zero slop. |
michael@0 | 29 | size_t slop = (aAllocSize > aSize) ? (aAllocSize - aSize) : 0; |
michael@0 | 30 | return slop > aMaxSlop; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | uint32_t |
michael@0 | 34 | AudioCompactor::GetChunkSamples(uint32_t aFrames, uint32_t aChannels, |
michael@0 | 35 | size_t aMaxSlop) |
michael@0 | 36 | { |
michael@0 | 37 | size_t size = AudioDataSize(aFrames, aChannels); |
michael@0 | 38 | size_t chunkSize = MallocGoodSize(size); |
michael@0 | 39 | |
michael@0 | 40 | // Reduce the chunk size until we meet our slop goal or the chunk |
michael@0 | 41 | // approaches an unreasonably small size. |
michael@0 | 42 | while (chunkSize > 64 && TooMuchSlop(size, chunkSize, aMaxSlop)) { |
michael@0 | 43 | chunkSize = MallocGoodSize(chunkSize / 2); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | // Calculate the number of samples based on expected malloc size |
michael@0 | 47 | // in order to allow as many frames as possible to be packed. |
michael@0 | 48 | return chunkSize / sizeof(AudioDataValue); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | uint32_t |
michael@0 | 52 | AudioCompactor::NativeCopy::operator()(AudioDataValue *aBuffer, |
michael@0 | 53 | uint32_t aSamples) |
michael@0 | 54 | { |
michael@0 | 55 | NS_ASSERTION(aBuffer, "cannot copy to null buffer pointer"); |
michael@0 | 56 | NS_ASSERTION(aSamples, "cannot copy zero values"); |
michael@0 | 57 | |
michael@0 | 58 | size_t bufferBytes = aSamples * sizeof(AudioDataValue); |
michael@0 | 59 | size_t maxBytes = std::min(bufferBytes, mSourceBytes - mNextByte); |
michael@0 | 60 | uint32_t frames = maxBytes / BytesPerFrame(mChannels); |
michael@0 | 61 | size_t bytes = frames * BytesPerFrame(mChannels); |
michael@0 | 62 | |
michael@0 | 63 | NS_ASSERTION((mNextByte + bytes) <= mSourceBytes, |
michael@0 | 64 | "tried to copy beyond source buffer"); |
michael@0 | 65 | NS_ASSERTION(bytes <= bufferBytes, "tried to copy beyond destination buffer"); |
michael@0 | 66 | |
michael@0 | 67 | memcpy(aBuffer, mSource + mNextByte, bytes); |
michael@0 | 68 | |
michael@0 | 69 | mNextByte += bytes; |
michael@0 | 70 | return frames; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | } // namespace mozilla |