• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 //                           MOTIVATION AND TUTORIAL
16 //
17 // If you want to put in a single heap allocation N doubles followed by M ints,
18 // it's easy if N and M are known at compile time.
19 //
20 //   struct S {
21 //     double a[N];
22 //     int b[M];
23 //   };
24 //
25 //   S* p = new S;
26 //
27 // But what if N and M are known only in run time? Class template Layout to the
28 // rescue! It's a portable generalization of the technique known as struct hack.
29 //
30 //   // This object will tell us everything we need to know about the memory
31 //   // layout of double[N] followed by int[M]. It's structurally identical to
32 //   // size_t[2] that stores N and M. It's very cheap to create.
33 //   const Layout<double, int> layout(N, M);
34 //
35 //   // Allocate enough memory for both arrays. `AllocSize()` tells us how much
36 //   // memory is needed. We are free to use any allocation function we want as
37 //   // long as it returns aligned memory.
38 //   std::unique_ptr<unsigned char[]> p(new unsigned char[layout.AllocSize()]);
39 //
40 //   // Obtain the pointer to the array of doubles.
41 //   // Equivalent to `reinterpret_cast<double*>(p.get())`.
42 //   //
43 //   // We could have written layout.Pointer<0>(p) instead. If all the types are
44 //   // unique you can use either form, but if some types are repeated you must
45 //   // use the index form.
46 //   double* a = layout.Pointer<double>(p.get());
47 //
48 //   // Obtain the pointer to the array of ints.
49 //   // Equivalent to `reinterpret_cast<int*>(p.get() + N * 8)`.
50 //   int* b = layout.Pointer<int>(p);
51 //
52 // If we are unable to specify sizes of all fields, we can pass as many sizes as
53 // we can to `Partial()`. In return, it'll allow us to access the fields whose
54 // locations and sizes can be computed from the provided information.
55 // `Partial()` comes in handy when the array sizes are embedded into the
56 // allocation.
57 //
58 //   // size_t[1] containing N, size_t[1] containing M, double[N], int[M].
59 //   using L = Layout<size_t, size_t, double, int>;
60 //
61 //   unsigned char* Allocate(size_t n, size_t m) {
62 //     const L layout(1, 1, n, m);
63 //     unsigned char* p = new unsigned char[layout.AllocSize()];
64 //     *layout.Pointer<0>(p) = n;
65 //     *layout.Pointer<1>(p) = m;
66 //     return p;
67 //   }
68 //
69 //   void Use(unsigned char* p) {
70 //     // First, extract N and M.
71 //     // Specify that the first array has only one element. Using `prefix` we
72 //     // can access the first two arrays but not more.
73 //     constexpr auto prefix = L::Partial(1);
74 //     size_t n = *prefix.Pointer<0>(p);
75 //     size_t m = *prefix.Pointer<1>(p);
76 //
77 //     // Now we can get pointers to the payload.
78 //     const L layout(1, 1, n, m);
79 //     double* a = layout.Pointer<double>(p);
80 //     int* b = layout.Pointer<int>(p);
81 //   }
82 //
83 // The layout we used above combines fixed-size with dynamically-sized fields.
84 // This is quite common. Layout is optimized for this use case and generates
85 // optimal code. All computations that can be performed at compile time are
86 // indeed performed at compile time.
87 //
88 // Efficiency tip: The order of fields matters. In `Layout<T1, ..., TN>` try to
89 // ensure that `alignof(T1) >= ... >= alignof(TN)`. This way you'll have no
90 // padding in between arrays.
91 //
92 // You can manually override the alignment of an array by wrapping the type in
93 // `Aligned<T, N>`. `Layout<..., Aligned<T, N>, ...>` has exactly the same API
94 // and behavior as `Layout<..., T, ...>` except that the first element of the
95 // array of `T` is aligned to `N` (the rest of the elements follow without
96 // padding). `N` cannot be less than `alignof(T)`.
97 //
98 // `AllocSize()` and `Pointer()` are the most basic methods for dealing with
99 // memory layouts. Check out the reference or code below to discover more.
100 //
101 //                            EXAMPLE
102 //
103 //   // Immutable move-only string with sizeof equal to sizeof(void*). The
104 //   // string size and the characters are kept in the same heap allocation.
105 //   class CompactString {
106 //    public:
107 //     CompactString(const char* s = "") {
108 //       const size_t size = strlen(s);
109 //       // size_t[1] followed by char[size + 1].
110 //       const L layout(1, size + 1);
111 //       p_.reset(new unsigned char[layout.AllocSize()]);
112 //       // If running under ASAN, mark the padding bytes, if any, to catch
113 //       // memory errors.
114 //       layout.PoisonPadding(p_.get());
115 //       // Store the size in the allocation.
116 //       *layout.Pointer<size_t>(p_.get()) = size;
117 //       // Store the characters in the allocation.
118 //       memcpy(layout.Pointer<char>(p_.get()), s, size + 1);
119 //     }
120 //
121 //     size_t size() const {
122 //       // Equivalent to reinterpret_cast<size_t&>(*p).
123 //       return *L::Partial().Pointer<size_t>(p_.get());
124 //     }
125 //
126 //     const char* c_str() const {
127 //       // Equivalent to reinterpret_cast<char*>(p.get() + sizeof(size_t)).
128 //       // The argument in Partial(1) specifies that we have size_t[1] in front
129 //       // of the characters.
130 //       return L::Partial(1).Pointer<char>(p_.get());
131 //     }
132 //
133 //    private:
134 //     // Our heap allocation contains a size_t followed by an array of chars.
135 //     using L = Layout<size_t, char>;
136 //     std::unique_ptr<unsigned char[]> p_;
137 //   };
138 //
139 //   int main() {
140 //     CompactString s = "hello";
141 //     assert(s.size() == 5);
142 //     assert(strcmp(s.c_str(), "hello") == 0);
143 //   }
144 //
145 //                               DOCUMENTATION
146 //
147 // The interface exported by this file consists of:
148 // - class `Layout<>` and its public members.
149 // - The public members of class `internal_layout::LayoutImpl<>`. That class
150 //   isn't intended to be used directly, and its name and template parameter
151 //   list are internal implementation details, but the class itself provides
152 //   most of the functionality in this file. See comments on its members for
153 //   detailed documentation.
154 //
155 // `Layout<T1,... Tn>::Partial(count1,..., countm)` (where `m` <= `n`) returns a
156 // `LayoutImpl<>` object. `Layout<T1,..., Tn> layout(count1,..., countn)`
157 // creates a `Layout` object, which exposes the same functionality by inheriting
158 // from `LayoutImpl<>`.
159 
160 #ifndef ABSL_CONTAINER_INTERNAL_LAYOUT_H_
161 #define ABSL_CONTAINER_INTERNAL_LAYOUT_H_
162 
163 #include <assert.h>
164 #include <stddef.h>
165 #include <stdint.h>
166 #include <ostream>
167 #include <string>
168 #include <tuple>
169 #include <type_traits>
170 #include <typeinfo>
171 #include <utility>
172 
173 #ifdef ADDRESS_SANITIZER
174 #include <sanitizer/asan_interface.h>
175 #endif
176 
177 #include "absl/meta/type_traits.h"
178 #include "absl/strings/str_cat.h"
179 #include "absl/types/span.h"
180 #include "absl/utility/utility.h"
181 
182 #if defined(__GXX_RTTI)
183 #define ABSL_INTERNAL_HAS_CXA_DEMANGLE
184 #endif
185 
186 #ifdef ABSL_INTERNAL_HAS_CXA_DEMANGLE
187 #include <cxxabi.h>
188 #endif
189 
190 namespace absl {
191 ABSL_NAMESPACE_BEGIN
192 namespace container_internal {
193 
194 // A type wrapper that instructs `Layout` to use the specific alignment for the
195 // array. `Layout<..., Aligned<T, N>, ...>` has exactly the same API
196 // and behavior as `Layout<..., T, ...>` except that the first element of the
197 // array of `T` is aligned to `N` (the rest of the elements follow without
198 // padding).
199 //
200 // Requires: `N >= alignof(T)` and `N` is a power of 2.
201 template <class T, size_t N>
202 struct Aligned;
203 
204 namespace internal_layout {
205 
206 template <class T>
207 struct NotAligned {};
208 
209 template <class T, size_t N>
210 struct NotAligned<const Aligned<T, N>> {
211   static_assert(sizeof(T) == 0, "Aligned<T, N> cannot be const-qualified");
212 };
213 
214 template <size_t>
215 using IntToSize = size_t;
216 
217 template <class>
218 using TypeToSize = size_t;
219 
220 template <class T>
221 struct Type : NotAligned<T> {
222   using type = T;
223 };
224 
225 template <class T, size_t N>
226 struct Type<Aligned<T, N>> {
227   using type = T;
228 };
229 
230 template <class T>
231 struct SizeOf : NotAligned<T>, std::integral_constant<size_t, sizeof(T)> {};
232 
233 template <class T, size_t N>
234 struct SizeOf<Aligned<T, N>> : std::integral_constant<size_t, sizeof(T)> {};
235 
236 // Note: workaround for https://gcc.gnu.org/PR88115
237 template <class T>
238 struct AlignOf : NotAligned<T> {
239   static constexpr size_t value = alignof(T);
240 };
241 
242 template <class T, size_t N>
243 struct AlignOf<Aligned<T, N>> {
244   static_assert(N % alignof(T) == 0,
245                 "Custom alignment can't be lower than the type's alignment");
246   static constexpr size_t value = N;
247 };
248 
249 // Does `Ts...` contain `T`?
250 template <class T, class... Ts>
251 using Contains = absl::disjunction<std::is_same<T, Ts>...>;
252 
253 template <class From, class To>
254 using CopyConst =
255     typename std::conditional<std::is_const<From>::value, const To, To>::type;
256 
257 // Note: We're not qualifying this with absl:: because it doesn't compile under
258 // MSVC.
259 template <class T>
260 using SliceType = Span<T>;
261 
262 // This namespace contains no types. It prevents functions defined in it from
263 // being found by ADL.
264 namespace adl_barrier {
265 
266 template <class Needle, class... Ts>
267 constexpr size_t Find(Needle, Needle, Ts...) {
268   static_assert(!Contains<Needle, Ts...>(), "Duplicate element type");
269   return 0;
270 }
271 
272 template <class Needle, class T, class... Ts>
273 constexpr size_t Find(Needle, T, Ts...) {
274   return adl_barrier::Find(Needle(), Ts()...) + 1;
275 }
276 
277 constexpr bool IsPow2(size_t n) { return !(n & (n - 1)); }
278 
279 // Returns `q * m` for the smallest `q` such that `q * m >= n`.
280 // Requires: `m` is a power of two. It's enforced by IsLegalElementType below.
281 constexpr size_t Align(size_t n, size_t m) { return (n + m - 1) & ~(m - 1); }
282 
283 constexpr size_t Min(size_t a, size_t b) { return b < a ? b : a; }
284 
285 constexpr size_t Max(size_t a) { return a; }
286 
287 template <class... Ts>
288 constexpr size_t Max(size_t a, size_t b, Ts... rest) {
289   return adl_barrier::Max(b < a ? a : b, rest...);
290 }
291 
292 template <class T>
293 std::string TypeName() {
294   std::string out;
295   int status = 0;
296   char* demangled = nullptr;
297 #ifdef ABSL_INTERNAL_HAS_CXA_DEMANGLE
298   demangled = abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status);
299 #endif
300   if (status == 0 && demangled != nullptr) {  // Demangling succeeded.
301     absl::StrAppend(&out, "<", demangled, ">");
302     free(demangled);
303   } else {
304 #if defined(__GXX_RTTI) || defined(_CPPRTTI)
305     absl::StrAppend(&out, "<", typeid(T).name(), ">");
306 #endif
307   }
308   return out;
309 }
310 
311 }  // namespace adl_barrier
312 
313 template <bool C>
314 using EnableIf = typename std::enable_if<C, int>::type;
315 
316 // Can `T` be a template argument of `Layout`?
317 template <class T>
318 using IsLegalElementType = std::integral_constant<
319     bool, !std::is_reference<T>::value && !std::is_volatile<T>::value &&
320               !std::is_reference<typename Type<T>::type>::value &&
321               !std::is_volatile<typename Type<T>::type>::value &&
322               adl_barrier::IsPow2(AlignOf<T>::value)>;
323 
324 template <class Elements, class SizeSeq, class OffsetSeq>
325 class LayoutImpl;
326 
327 // Public base class of `Layout` and the result type of `Layout::Partial()`.
328 //
329 // `Elements...` contains all template arguments of `Layout` that created this
330 // instance.
331 //
332 // `SizeSeq...` is `[0, NumSizes)` where `NumSizes` is the number of arguments
333 // passed to `Layout::Partial()` or `Layout::Layout()`.
334 //
335 // `OffsetSeq...` is `[0, NumOffsets)` where `NumOffsets` is
336 // `Min(sizeof...(Elements), NumSizes + 1)` (the number of arrays for which we
337 // can compute offsets).
338 template <class... Elements, size_t... SizeSeq, size_t... OffsetSeq>
339 class LayoutImpl<std::tuple<Elements...>, absl::index_sequence<SizeSeq...>,
340                  absl::index_sequence<OffsetSeq...>> {
341  private:
342   static_assert(sizeof...(Elements) > 0, "At least one field is required");
343   static_assert(absl::conjunction<IsLegalElementType<Elements>...>::value,
344                 "Invalid element type (see IsLegalElementType)");
345 
346   enum {
347     NumTypes = sizeof...(Elements),
348     NumSizes = sizeof...(SizeSeq),
349     NumOffsets = sizeof...(OffsetSeq),
350   };
351 
352   // These are guaranteed by `Layout`.
353   static_assert(NumOffsets == adl_barrier::Min(NumTypes, NumSizes + 1),
354                 "Internal error");
355   static_assert(NumTypes > 0, "Internal error");
356 
357   // Returns the index of `T` in `Elements...`. Results in a compilation error
358   // if `Elements...` doesn't contain exactly one instance of `T`.
359   template <class T>
360   static constexpr size_t ElementIndex() {
361     static_assert(Contains<Type<T>, Type<typename Type<Elements>::type>...>(),
362                   "Type not found");
363     return adl_barrier::Find(Type<T>(),
364                              Type<typename Type<Elements>::type>()...);
365   }
366 
367   template <size_t N>
368   using ElementAlignment =
369       AlignOf<typename std::tuple_element<N, std::tuple<Elements...>>::type>;
370 
371  public:
372   // Element types of all arrays packed in a tuple.
373   using ElementTypes = std::tuple<typename Type<Elements>::type...>;
374 
375   // Element type of the Nth array.
376   template <size_t N>
377   using ElementType = typename std::tuple_element<N, ElementTypes>::type;
378 
379   constexpr explicit LayoutImpl(IntToSize<SizeSeq>... sizes)
380       : size_{sizes...} {}
381 
382   // Alignment of the layout, equal to the strictest alignment of all elements.
383   // All pointers passed to the methods of layout must be aligned to this value.
384   static constexpr size_t Alignment() {
385     return adl_barrier::Max(AlignOf<Elements>::value...);
386   }
387 
388   // Offset in bytes of the Nth array.
389   //
390   //   // int[3], 4 bytes of padding, double[4].
391   //   Layout<int, double> x(3, 4);
392   //   assert(x.Offset<0>() == 0);   // The ints starts from 0.
393   //   assert(x.Offset<1>() == 16);  // The doubles starts from 16.
394   //
395   // Requires: `N <= NumSizes && N < sizeof...(Ts)`.
396   template <size_t N, EnableIf<N == 0> = 0>
397   constexpr size_t Offset() const {
398     return 0;
399   }
400 
401   template <size_t N, EnableIf<N != 0> = 0>
402   constexpr size_t Offset() const {
403     static_assert(N < NumOffsets, "Index out of bounds");
404     return adl_barrier::Align(
405         Offset<N - 1>() + SizeOf<ElementType<N - 1>>() * size_[N - 1],
406         ElementAlignment<N>::value);
407   }
408 
409   // Offset in bytes of the array with the specified element type. There must
410   // be exactly one such array and its zero-based index must be at most
411   // `NumSizes`.
412   //
413   //   // int[3], 4 bytes of padding, double[4].
414   //   Layout<int, double> x(3, 4);
415   //   assert(x.Offset<int>() == 0);      // The ints starts from 0.
416   //   assert(x.Offset<double>() == 16);  // The doubles starts from 16.
417   template <class T>
418   constexpr size_t Offset() const {
419     return Offset<ElementIndex<T>()>();
420   }
421 
422   // Offsets in bytes of all arrays for which the offsets are known.
423   constexpr std::array<size_t, NumOffsets> Offsets() const {
424     return {{Offset<OffsetSeq>()...}};
425   }
426 
427   // The number of elements in the Nth array. This is the Nth argument of
428   // `Layout::Partial()` or `Layout::Layout()` (zero-based).
429   //
430   //   // int[3], 4 bytes of padding, double[4].
431   //   Layout<int, double> x(3, 4);
432   //   assert(x.Size<0>() == 3);
433   //   assert(x.Size<1>() == 4);
434   //
435   // Requires: `N < NumSizes`.
436   template <size_t N>
437   constexpr size_t Size() const {
438     static_assert(N < NumSizes, "Index out of bounds");
439     return size_[N];
440   }
441 
442   // The number of elements in the array with the specified element type.
443   // There must be exactly one such array and its zero-based index must be
444   // at most `NumSizes`.
445   //
446   //   // int[3], 4 bytes of padding, double[4].
447   //   Layout<int, double> x(3, 4);
448   //   assert(x.Size<int>() == 3);
449   //   assert(x.Size<double>() == 4);
450   template <class T>
451   constexpr size_t Size() const {
452     return Size<ElementIndex<T>()>();
453   }
454 
455   // The number of elements of all arrays for which they are known.
456   constexpr std::array<size_t, NumSizes> Sizes() const {
457     return {{Size<SizeSeq>()...}};
458   }
459 
460   // Pointer to the beginning of the Nth array.
461   //
462   // `Char` must be `[const] [signed|unsigned] char`.
463   //
464   //   // int[3], 4 bytes of padding, double[4].
465   //   Layout<int, double> x(3, 4);
466   //   unsigned char* p = new unsigned char[x.AllocSize()];
467   //   int* ints = x.Pointer<0>(p);
468   //   double* doubles = x.Pointer<1>(p);
469   //
470   // Requires: `N <= NumSizes && N < sizeof...(Ts)`.
471   // Requires: `p` is aligned to `Alignment()`.
472   template <size_t N, class Char>
473   CopyConst<Char, ElementType<N>>* Pointer(Char* p) const {
474     using C = typename std::remove_const<Char>::type;
475     static_assert(
476         std::is_same<C, char>() || std::is_same<C, unsigned char>() ||
477             std::is_same<C, signed char>(),
478         "The argument must be a pointer to [const] [signed|unsigned] char");
479     constexpr size_t alignment = Alignment();
480     (void)alignment;
481     assert(reinterpret_cast<uintptr_t>(p) % alignment == 0);
482     return reinterpret_cast<CopyConst<Char, ElementType<N>>*>(p + Offset<N>());
483   }
484 
485   // Pointer to the beginning of the array with the specified element type.
486   // There must be exactly one such array and its zero-based index must be at
487   // most `NumSizes`.
488   //
489   // `Char` must be `[const] [signed|unsigned] char`.
490   //
491   //   // int[3], 4 bytes of padding, double[4].
492   //   Layout<int, double> x(3, 4);
493   //   unsigned char* p = new unsigned char[x.AllocSize()];
494   //   int* ints = x.Pointer<int>(p);
495   //   double* doubles = x.Pointer<double>(p);
496   //
497   // Requires: `p` is aligned to `Alignment()`.
498   template <class T, class Char>
499   CopyConst<Char, T>* Pointer(Char* p) const {
500     return Pointer<ElementIndex<T>()>(p);
501   }
502 
503   // Pointers to all arrays for which pointers are known.
504   //
505   // `Char` must be `[const] [signed|unsigned] char`.
506   //
507   //   // int[3], 4 bytes of padding, double[4].
508   //   Layout<int, double> x(3, 4);
509   //   unsigned char* p = new unsigned char[x.AllocSize()];
510   //
511   //   int* ints;
512   //   double* doubles;
513   //   std::tie(ints, doubles) = x.Pointers(p);
514   //
515   // Requires: `p` is aligned to `Alignment()`.
516   //
517   // Note: We're not using ElementType alias here because it does not compile
518   // under MSVC.
519   template <class Char>
520   std::tuple<CopyConst<
521       Char, typename std::tuple_element<OffsetSeq, ElementTypes>::type>*...>
522   Pointers(Char* p) const {
523     return std::tuple<CopyConst<Char, ElementType<OffsetSeq>>*...>(
524         Pointer<OffsetSeq>(p)...);
525   }
526 
527   // The Nth array.
528   //
529   // `Char` must be `[const] [signed|unsigned] char`.
530   //
531   //   // int[3], 4 bytes of padding, double[4].
532   //   Layout<int, double> x(3, 4);
533   //   unsigned char* p = new unsigned char[x.AllocSize()];
534   //   Span<int> ints = x.Slice<0>(p);
535   //   Span<double> doubles = x.Slice<1>(p);
536   //
537   // Requires: `N < NumSizes`.
538   // Requires: `p` is aligned to `Alignment()`.
539   template <size_t N, class Char>
540   SliceType<CopyConst<Char, ElementType<N>>> Slice(Char* p) const {
541     return SliceType<CopyConst<Char, ElementType<N>>>(Pointer<N>(p), Size<N>());
542   }
543 
544   // The array with the specified element type. There must be exactly one
545   // such array and its zero-based index must be less than `NumSizes`.
546   //
547   // `Char` must be `[const] [signed|unsigned] char`.
548   //
549   //   // int[3], 4 bytes of padding, double[4].
550   //   Layout<int, double> x(3, 4);
551   //   unsigned char* p = new unsigned char[x.AllocSize()];
552   //   Span<int> ints = x.Slice<int>(p);
553   //   Span<double> doubles = x.Slice<double>(p);
554   //
555   // Requires: `p` is aligned to `Alignment()`.
556   template <class T, class Char>
557   SliceType<CopyConst<Char, T>> Slice(Char* p) const {
558     return Slice<ElementIndex<T>()>(p);
559   }
560 
561   // All arrays with known sizes.
562   //
563   // `Char` must be `[const] [signed|unsigned] char`.
564   //
565   //   // int[3], 4 bytes of padding, double[4].
566   //   Layout<int, double> x(3, 4);
567   //   unsigned char* p = new unsigned char[x.AllocSize()];
568   //
569   //   Span<int> ints;
570   //   Span<double> doubles;
571   //   std::tie(ints, doubles) = x.Slices(p);
572   //
573   // Requires: `p` is aligned to `Alignment()`.
574   //
575   // Note: We're not using ElementType alias here because it does not compile
576   // under MSVC.
577   template <class Char>
578   std::tuple<SliceType<CopyConst<
579       Char, typename std::tuple_element<SizeSeq, ElementTypes>::type>>...>
580   Slices(Char* p) const {
581     // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63875 (fixed
582     // in 6.1).
583     (void)p;
584     return std::tuple<SliceType<CopyConst<Char, ElementType<SizeSeq>>>...>(
585         Slice<SizeSeq>(p)...);
586   }
587 
588   // The size of the allocation that fits all arrays.
589   //
590   //   // int[3], 4 bytes of padding, double[4].
591   //   Layout<int, double> x(3, 4);
592   //   unsigned char* p = new unsigned char[x.AllocSize()];  // 48 bytes
593   //
594   // Requires: `NumSizes == sizeof...(Ts)`.
595   constexpr size_t AllocSize() const {
596     static_assert(NumTypes == NumSizes, "You must specify sizes of all fields");
597     return Offset<NumTypes - 1>() +
598            SizeOf<ElementType<NumTypes - 1>>() * size_[NumTypes - 1];
599   }
600 
601   // If built with --config=asan, poisons padding bytes (if any) in the
602   // allocation. The pointer must point to a memory block at least
603   // `AllocSize()` bytes in length.
604   //
605   // `Char` must be `[const] [signed|unsigned] char`.
606   //
607   // Requires: `p` is aligned to `Alignment()`.
608   template <class Char, size_t N = NumOffsets - 1, EnableIf<N == 0> = 0>
609   void PoisonPadding(const Char* p) const {
610     Pointer<0>(p);  // verify the requirements on `Char` and `p`
611   }
612 
613   template <class Char, size_t N = NumOffsets - 1, EnableIf<N != 0> = 0>
614   void PoisonPadding(const Char* p) const {
615     static_assert(N < NumOffsets, "Index out of bounds");
616     (void)p;
617 #ifdef ADDRESS_SANITIZER
618     PoisonPadding<Char, N - 1>(p);
619     // The `if` is an optimization. It doesn't affect the observable behaviour.
620     if (ElementAlignment<N - 1>::value % ElementAlignment<N>::value) {
621       size_t start =
622           Offset<N - 1>() + SizeOf<ElementType<N - 1>>() * size_[N - 1];
623       ASAN_POISON_MEMORY_REGION(p + start, Offset<N>() - start);
624     }
625 #endif
626   }
627 
628   // Human-readable description of the memory layout. Useful for debugging.
629   // Slow.
630   //
631   //   // char[5], 3 bytes of padding, int[3], 4 bytes of padding, followed
632   //   // by an unknown number of doubles.
633   //   auto x = Layout<char, int, double>::Partial(5, 3);
634   //   assert(x.DebugString() ==
635   //          "@0<char>(1)[5]; @8<int>(4)[3]; @24<double>(8)");
636   //
637   // Each field is in the following format: @offset<type>(sizeof)[size] (<type>
638   // may be missing depending on the target platform). For example,
639   // @8<int>(4)[3] means that at offset 8 we have an array of ints, where each
640   // int is 4 bytes, and we have 3 of those ints. The size of the last field may
641   // be missing (as in the example above). Only fields with known offsets are
642   // described. Type names may differ across platforms: one compiler might
643   // produce "unsigned*" where another produces "unsigned int *".
644   std::string DebugString() const {
645     const auto offsets = Offsets();
646     const size_t sizes[] = {SizeOf<ElementType<OffsetSeq>>()...};
647     const std::string types[] = {
648         adl_barrier::TypeName<ElementType<OffsetSeq>>()...};
649     std::string res = absl::StrCat("@0", types[0], "(", sizes[0], ")");
650     for (size_t i = 0; i != NumOffsets - 1; ++i) {
651       absl::StrAppend(&res, "[", size_[i], "]; @", offsets[i + 1], types[i + 1],
652                       "(", sizes[i + 1], ")");
653     }
654     // NumSizes is a constant that may be zero. Some compilers cannot see that
655     // inside the if statement "size_[NumSizes - 1]" must be valid.
656     int last = static_cast<int>(NumSizes) - 1;
657     if (NumTypes == NumSizes && last >= 0) {
658       absl::StrAppend(&res, "[", size_[last], "]");
659     }
660     return res;
661   }
662 
663  private:
664   // Arguments of `Layout::Partial()` or `Layout::Layout()`.
665   size_t size_[NumSizes > 0 ? NumSizes : 1];
666 };
667 
668 template <size_t NumSizes, class... Ts>
669 using LayoutType = LayoutImpl<
670     std::tuple<Ts...>, absl::make_index_sequence<NumSizes>,
671     absl::make_index_sequence<adl_barrier::Min(sizeof...(Ts), NumSizes + 1)>>;
672 
673 }  // namespace internal_layout
674 
675 // Descriptor of arrays of various types and sizes laid out in memory one after
676 // another. See the top of the file for documentation.
677 //
678 // Check out the public API of internal_layout::LayoutImpl above. The type is
679 // internal to the library but its methods are public, and they are inherited
680 // by `Layout`.
681 template <class... Ts>
682 class Layout : public internal_layout::LayoutType<sizeof...(Ts), Ts...> {
683  public:
684   static_assert(sizeof...(Ts) > 0, "At least one field is required");
685   static_assert(
686       absl::conjunction<internal_layout::IsLegalElementType<Ts>...>::value,
687       "Invalid element type (see IsLegalElementType)");
688 
689   // The result type of `Partial()` with `NumSizes` arguments.
690   template <size_t NumSizes>
691   using PartialType = internal_layout::LayoutType<NumSizes, Ts...>;
692 
693   // `Layout` knows the element types of the arrays we want to lay out in
694   // memory but not the number of elements in each array.
695   // `Partial(size1, ..., sizeN)` allows us to specify the latter. The
696   // resulting immutable object can be used to obtain pointers to the
697   // individual arrays.
698   //
699   // It's allowed to pass fewer array sizes than the number of arrays. E.g.,
700   // if all you need is to the offset of the second array, you only need to
701   // pass one argument -- the number of elements in the first array.
702   //
703   //   // int[3] followed by 4 bytes of padding and an unknown number of
704   //   // doubles.
705   //   auto x = Layout<int, double>::Partial(3);
706   //   // doubles start at byte 16.
707   //   assert(x.Offset<1>() == 16);
708   //
709   // If you know the number of elements in all arrays, you can still call
710   // `Partial()` but it's more convenient to use the constructor of `Layout`.
711   //
712   //   Layout<int, double> x(3, 5);
713   //
714   // Note: The sizes of the arrays must be specified in number of elements,
715   // not in bytes.
716   //
717   // Requires: `sizeof...(Sizes) <= sizeof...(Ts)`.
718   // Requires: all arguments are convertible to `size_t`.
719   template <class... Sizes>
720   static constexpr PartialType<sizeof...(Sizes)> Partial(Sizes&&... sizes) {
721     static_assert(sizeof...(Sizes) <= sizeof...(Ts), "");
722     return PartialType<sizeof...(Sizes)>(absl::forward<Sizes>(sizes)...);
723   }
724 
725   // Creates a layout with the sizes of all arrays specified. If you know
726   // only the sizes of the first N arrays (where N can be zero), you can use
727   // `Partial()` defined above. The constructor is essentially equivalent to
728   // calling `Partial()` and passing in all array sizes; the constructor is
729   // provided as a convenient abbreviation.
730   //
731   // Note: The sizes of the arrays must be specified in number of elements,
732   // not in bytes.
733   constexpr explicit Layout(internal_layout::TypeToSize<Ts>... sizes)
734       : internal_layout::LayoutType<sizeof...(Ts), Ts...>(sizes...) {}
735 };
736 
737 }  // namespace container_internal
738 ABSL_NAMESPACE_END
739 }  // namespace absl
740 
741 #endif  // ABSL_CONTAINER_INTERNAL_LAYOUT_H_
742