HeapCopyOfStackArray.h (1400B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef HEAPCOPYOFSTACKARRAY_H_ 8 #define HEAPCOPYOFSTACKARRAY_H_ 9 10 #include "mozilla/Attributes.h" 11 #include "mozilla/UniquePtr.h" 12 13 #include <string.h> 14 15 namespace mozilla { 16 17 // Takes a stack array and copies it into a heap buffer. 18 // Useful to retain the convenience of declaring static arrays, while 19 // avoiding passing stack pointers to the GL (see bug 1005658). 20 21 template <typename ElemType> 22 class HeapCopyOfStackArray { 23 public: 24 template <size_t N> 25 MOZ_IMPLICIT HeapCopyOfStackArray(const ElemType (&array)[N]) 26 : mArrayLength(N), mArrayData(MakeUnique<ElemType[]>(N)) { 27 memcpy(mArrayData.get(), &array[0], N * sizeof(ElemType)); 28 } 29 30 ElemType* Data() const { return mArrayData.get(); } 31 size_t ArrayLength() const { return mArrayLength; } 32 size_t ByteLength() const { return mArrayLength * sizeof(ElemType); } 33 34 private: 35 HeapCopyOfStackArray() = delete; 36 HeapCopyOfStackArray(const HeapCopyOfStackArray&) = delete; 37 38 const size_t mArrayLength; 39 UniquePtr<ElemType[]> const mArrayData; 40 }; 41 42 } // namespace mozilla 43 44 #endif // HEAPCOPYOFSTACKARRAY_H_