Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=4 et sw=4 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 0 : class HeapCopyOfStackArray
23 : {
24 : public:
25 : template<size_t N>
26 0 : MOZ_IMPLICIT HeapCopyOfStackArray(ElemType (&array)[N])
27 : : mArrayLength(N)
28 0 : , mArrayData(MakeUnique<ElemType[]>(N))
29 : {
30 0 : memcpy(mArrayData.get(), &array[0], N * sizeof(ElemType));
31 0 : }
32 :
33 0 : ElemType* Data() const { return mArrayData.get(); }
34 : size_t ArrayLength() const { return mArrayLength; }
35 0 : size_t ByteLength() const { return mArrayLength * sizeof(ElemType); }
36 :
37 : private:
38 : HeapCopyOfStackArray() = delete;
39 : HeapCopyOfStackArray(const HeapCopyOfStackArray&) = delete;
40 :
41 : const size_t mArrayLength;
42 : UniquePtr<ElemType[]> const mArrayData;
43 : };
44 :
45 : } // namespace mozilla
46 :
47 : #endif // HEAPCOPYOFSTACKARRAY_H_
|