|
| 1 | +//===--- HeaderFooterLayout.h -----------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef SWIFT_BASIC_HEADER_FOOTER_LAYOUT_H |
| 14 | +#define SWIFT_BASIC_HEADER_FOOTER_LAYOUT_H |
| 15 | + |
| 16 | +#include <cstddef> |
| 17 | + |
| 18 | +namespace swift { |
| 19 | + |
| 20 | +template <class T> |
| 21 | +class size_without_trailing_padding { |
| 22 | + struct ExtraByte { char _size_without_trailing_padding_probe; }; |
| 23 | + struct Probe: T, ExtraByte {}; |
| 24 | +public: |
| 25 | + enum { value = offsetof(Probe, _size_without_trailing_padding_probe) }; |
| 26 | +}; |
| 27 | + |
| 28 | +namespace detail { |
| 29 | + |
| 30 | +template <ptrdiff_t size> |
| 31 | +struct LayoutPadding { |
| 32 | + char padding[size]; |
| 33 | +}; |
| 34 | +template <> |
| 35 | +struct LayoutPadding<0> {}; |
| 36 | + |
| 37 | +template <class Header, class Footer, size_t TotalSize> |
| 38 | +struct HeaderFooterLayoutPaddingSize { |
| 39 | + enum : ptrdiff_t { |
| 40 | + maxFooterOffset = TotalSize - (ptrdiff_t)size_without_trailing_padding<Footer>::value, |
| 41 | + footerAlignment = (ptrdiff_t)alignof(Footer), |
| 42 | + footerOffset = maxFooterOffset - (maxFooterOffset % footerAlignment), |
| 43 | + value = footerOffset - (ptrdiff_t)size_without_trailing_padding<Header>::value |
| 44 | + }; |
| 45 | +}; |
| 46 | + |
| 47 | +} // namespace detail |
| 48 | + |
| 49 | +template <class Header, class Footer, size_t TotalSize> |
| 50 | +struct HeaderFooterLayout |
| 51 | + : Header, |
| 52 | + detail::LayoutPadding<detail::HeaderFooterLayoutPaddingSize< |
| 53 | + Header, Footer, TotalSize>::value>, |
| 54 | + Footer {}; |
| 55 | + |
| 56 | +} // namespace swift |
| 57 | + |
| 58 | +#endif // SWIFT_BASIC_HEADER_FOOTER_LAYOUT_H |
| 59 | + |
0 commit comments