michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "PeriodicWave.h" michael@0: #include "AudioContext.h" michael@0: #include "mozilla/dom/PeriodicWaveBinding.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(PeriodicWave, mContext) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(PeriodicWave, AddRef) michael@0: NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(PeriodicWave, Release) michael@0: michael@0: PeriodicWave::PeriodicWave(AudioContext* aContext, michael@0: const float* aRealData, michael@0: const float* aImagData, michael@0: const uint32_t aLength, michael@0: ErrorResult& aRv) michael@0: : mContext(aContext) michael@0: { michael@0: MOZ_ASSERT(aContext); michael@0: SetIsDOMBinding(); michael@0: michael@0: // Caller should have checked this and thrown. michael@0: MOZ_ASSERT(aLength > 0); michael@0: MOZ_ASSERT(aLength <= 4096); michael@0: mLength = aLength; michael@0: michael@0: // Copy coefficient data. The two arrays share an allocation. michael@0: mCoefficients = new ThreadSharedFloatArrayBufferList(2); michael@0: float* buffer = static_cast(malloc(aLength*sizeof(float)*2)); michael@0: if (buffer == nullptr) { michael@0: aRv.Throw(NS_ERROR_OUT_OF_MEMORY); michael@0: return; michael@0: } michael@0: PodCopy(buffer, aRealData, aLength); michael@0: mCoefficients->SetData(0, buffer, buffer); michael@0: PodCopy(buffer+aLength, aImagData, aLength); michael@0: mCoefficients->SetData(1, nullptr, buffer+aLength); michael@0: } michael@0: michael@0: size_t michael@0: PeriodicWave::SizeOfExcludingThisIfNotShared(MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: // Not owned: michael@0: // - mContext michael@0: size_t amount = 0; michael@0: if (!mCoefficients->IsShared()) { michael@0: amount += mCoefficients->SizeOfIncludingThis(aMallocSizeOf); michael@0: } michael@0: michael@0: return amount; michael@0: } michael@0: michael@0: JSObject* michael@0: PeriodicWave::WrapObject(JSContext* aCx) michael@0: { michael@0: return PeriodicWaveBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla michael@0: