1 | /* |
2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "HeapKind.h" |
29 | #include <array> |
30 | |
31 | namespace bmalloc { |
32 | |
33 | template<typename T> |
34 | class PerHeapKindBase { |
35 | public: |
36 | PerHeapKindBase(const PerHeapKindBase&) = delete; |
37 | PerHeapKindBase& operator=(const PerHeapKindBase&) = delete; |
38 | |
39 | template<typename... Arguments> |
40 | PerHeapKindBase(Arguments&&... arguments) |
41 | { |
42 | for (unsigned i = numHeaps; i--;) |
43 | new (&at(i)) T(static_cast<HeapKind>(i), std::forward<Arguments>(arguments)...); |
44 | } |
45 | |
46 | static size_t size() { return numHeaps; } |
47 | |
48 | T& at(size_t i) |
49 | { |
50 | return *reinterpret_cast<T*>(&m_memory[i]); |
51 | } |
52 | |
53 | const T& at(size_t i) const |
54 | { |
55 | return *reinterpret_cast<T*>(&m_memory[i]); |
56 | } |
57 | |
58 | T& at(HeapKind heapKind) |
59 | { |
60 | return at(static_cast<size_t>(heapKind)); |
61 | } |
62 | |
63 | const T& at(HeapKind heapKind) const |
64 | { |
65 | return at(static_cast<size_t>(heapKind)); |
66 | } |
67 | |
68 | T& operator[](size_t i) { return at(i); } |
69 | const T& operator[](size_t i) const { return at(i); } |
70 | T& operator[](HeapKind heapKind) { return at(heapKind); } |
71 | const T& operator[](HeapKind heapKind) const { return at(heapKind); } |
72 | |
73 | private: |
74 | typedef typename std::array<typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type, numHeaps> Memory; |
75 | Memory m_memory; |
76 | }; |
77 | |
78 | template<typename T> |
79 | class StaticPerHeapKind : public PerHeapKindBase<T> { |
80 | public: |
81 | template<typename... Arguments> |
82 | StaticPerHeapKind(Arguments&&... arguments) |
83 | : PerHeapKindBase<T>(std::forward<Arguments>(arguments)...) |
84 | { |
85 | } |
86 | |
87 | ~StaticPerHeapKind() = delete; |
88 | }; |
89 | |
90 | template<typename T> |
91 | class PerHeapKind : public PerHeapKindBase<T> { |
92 | public: |
93 | template<typename... Arguments> |
94 | PerHeapKind(Arguments&&... arguments) |
95 | : PerHeapKindBase<T>(std::forward<Arguments>(arguments)...) |
96 | { |
97 | } |
98 | |
99 | ~PerHeapKind() |
100 | { |
101 | for (unsigned i = numHeaps; i--;) |
102 | this->at(i).~T(); |
103 | } |
104 | }; |
105 | |
106 | } // namespace bmalloc |
107 | |
108 | |