Backed out changeset 1ecf6afb4dc7 for causing failures.
[gecko.git] / gfx / gl / HeapCopyOfStackArray.h
blobffcadde46186317065678df0e6920c389caf15d3
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/. */
7 #ifndef HEAPCOPYOFSTACKARRAY_H_
8 #define HEAPCOPYOFSTACKARRAY_H_
10 #include "mozilla/Attributes.h"
11 #include "mozilla/UniquePtr.h"
13 #include <string.h>
15 namespace mozilla {
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).
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));
30 ElemType* Data() const { return mArrayData.get(); }
31 size_t ArrayLength() const { return mArrayLength; }
32 size_t ByteLength() const { return mArrayLength * sizeof(ElemType); }
34 private:
35 HeapCopyOfStackArray() = delete;
36 HeapCopyOfStackArray(const HeapCopyOfStackArray&) = delete;
38 const size_t mArrayLength;
39 UniquePtr<ElemType[]> const mArrayData;
42 } // namespace mozilla
44 #endif // HEAPCOPYOFSTACKARRAY_H_