Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
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 AlignedTArray_h__
8 : #define AlignedTArray_h__
9 :
10 : #include "mozilla/Alignment.h"
11 : #include "nsTArray.h"
12 :
13 : /**
14 : * E: element type, must be a POD type.
15 : * N: N bytes alignment for the first element, defaults to 32
16 : * S: S bytes of inline storage
17 : */
18 : template <typename E, int S, int N = 32>
19 0 : class AlignedAutoTArray : private AutoTArray<E, S + N>
20 : {
21 : static_assert((N & (N-1)) == 0, "N must be power of 2");
22 : typedef AutoTArray<E, S + N> base_type;
23 : public:
24 : typedef E elem_type;
25 : typedef typename base_type::size_type size_type;
26 : typedef typename base_type::index_type index_type;
27 :
28 0 : AlignedAutoTArray() {}
29 : explicit AlignedAutoTArray(size_type capacity) : base_type(capacity + sExtra) {}
30 0 : elem_type* Elements() { return getAligned(base_type::Elements()); }
31 : const elem_type* Elements() const { return getAligned(base_type::Elements()); }
32 0 : elem_type& operator[](index_type i) { return Elements()[i];}
33 : const elem_type& operator[](index_type i) const { return Elements()[i]; }
34 :
35 0 : void SetLength(size_type newLen)
36 : {
37 0 : base_type::SetLength(newLen + sExtra);
38 0 : }
39 :
40 : MOZ_MUST_USE
41 : bool SetLength(size_type newLen, const mozilla::fallible_t&)
42 : {
43 : return base_type::SetLength(newLen + sExtra, mozilla::fallible);
44 : }
45 :
46 : size_type Length() const {
47 : return base_type::Length() <= sExtra ? 0 : base_type::Length() - sExtra;
48 : }
49 :
50 : using base_type::ShallowSizeOfExcludingThis;
51 : using base_type::ShallowSizeOfIncludingThis;
52 :
53 : private:
54 : AlignedAutoTArray(const AlignedAutoTArray& other) = delete;
55 : void operator=(const AlignedAutoTArray& other) = delete;
56 :
57 : static const size_type sPadding = N <= MOZ_ALIGNOF(E) ? 0 : N - MOZ_ALIGNOF(E);
58 : static const size_type sExtra = (sPadding + sizeof(E) - 1) / sizeof(E);
59 :
60 : template <typename U>
61 0 : static U* getAligned(U* p)
62 : {
63 0 : return reinterpret_cast<U*>(((uintptr_t)p + N - 1) & ~(N-1));
64 : }
65 : };
66 :
67 : /**
68 : * E: element type, must be a POD type.
69 : * N: N bytes alignment for the first element, defaults to 32
70 : */
71 : template <typename E, int N = 32>
72 0 : class AlignedTArray : private nsTArray_Impl<E, nsTArrayInfallibleAllocator>
73 : {
74 : static_assert((N & (N-1)) == 0, "N must be power of 2");
75 : typedef nsTArray_Impl<E, nsTArrayInfallibleAllocator> base_type;
76 : public:
77 : typedef E elem_type;
78 : typedef typename base_type::size_type size_type;
79 : typedef typename base_type::index_type index_type;
80 :
81 0 : AlignedTArray() {}
82 0 : explicit AlignedTArray(size_type capacity) : base_type(capacity + sExtra) {}
83 0 : elem_type* Elements() { return getAligned(base_type::Elements()); }
84 0 : const elem_type* Elements() const { return getAligned(base_type::Elements()); }
85 0 : elem_type& operator[](index_type i) { return Elements()[i];}
86 0 : const elem_type& operator[](index_type i) const { return Elements()[i]; }
87 :
88 0 : void SetLength(size_type newLen)
89 : {
90 0 : base_type::SetLength(newLen + sExtra);
91 0 : }
92 :
93 : MOZ_MUST_USE
94 0 : bool SetLength(size_type newLen, const mozilla::fallible_t&)
95 : {
96 0 : return base_type::SetLength(newLen + sExtra, mozilla::fallible);
97 : }
98 :
99 0 : size_type Length() const {
100 0 : return base_type::Length() <= sExtra ? 0 : base_type::Length() - sExtra;
101 : }
102 :
103 : using base_type::ShallowSizeOfExcludingThis;
104 : using base_type::ShallowSizeOfIncludingThis;
105 :
106 : private:
107 : AlignedTArray(const AlignedTArray& other) = delete;
108 : void operator=(const AlignedTArray& other) = delete;
109 :
110 : static const size_type sPadding = N <= MOZ_ALIGNOF(E) ? 0 : N - MOZ_ALIGNOF(E);
111 : static const size_type sExtra = (sPadding + sizeof(E) - 1) / sizeof(E);
112 :
113 : template <typename U>
114 0 : static U* getAligned(U* p)
115 : {
116 0 : return reinterpret_cast<U*>(((uintptr_t)p + N - 1) & ~(N-1));
117 : }
118 : };
119 :
120 :
121 : #endif // AlignedTArray_h__
|