pstore2
section.hpp
1 //===- include/pstore/mcrepo/section.hpp ------------------*- mode: C++ -*-===//
2 //* _ _ *
3 //* ___ ___ ___| |_(_) ___ _ __ *
4 //* / __|/ _ \/ __| __| |/ _ \| '_ \ *
5 //* \__ \ __/ (__| |_| | (_) | | | | *
6 //* |___/\___|\___|\__|_|\___/|_| |_| *
7 //* *
8 //===----------------------------------------------------------------------===//
9 //
10 // Part of the pstore project, under the Apache License v2.0 with LLVM Exceptions.
11 // See https://github.com/SNSystems/pstore/blob/master/LICENSE.txt for license
12 // information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //===----------------------------------------------------------------------===//
16 #ifndef PSTORE_MCREPO_SECTION_HPP
17 #define PSTORE_MCREPO_SECTION_HPP
18 
19 #include <cassert>
20 #include <cstdint>
21 #include <cstdlib>
22 #include <iosfwd>
23 #include <type_traits>
24 
26 
27 namespace pstore {
28  namespace repo {
29 
30 #define PSTORE_MCREPO_SECTION_KINDS \
31  X (text) \
32  X (data) \
33  X (bss) \
34  X (rel_ro) \
35  X (mergeable_1_byte_c_string) \
36  X (mergeable_2_byte_c_string) \
37  X (mergeable_4_byte_c_string) \
38  X (mergeable_const_4) \
39  X (mergeable_const_8) \
40  X (mergeable_const_16) \
41  X (mergeable_const_32) \
42  X (read_only) \
43  X (thread_data) \
44  X (thread_bss) \
45  X (debug_line) \
46  X (debug_string) \
47  X (debug_ranges) \
48  X (linked_definitions)
49 
50 #define X(a) a,
51  enum class section_kind : std::uint8_t {
52  PSTORE_MCREPO_SECTION_KINDS last // always last, never used.
53  };
54 #undef X
55  constexpr auto num_section_kinds =
56  static_cast<std::underlying_type<section_kind>::type> (section_kind::last);
57 
58  std::ostream & operator<< (std::ostream & os, section_kind kind);
59 
60  constexpr auto first_repo_metadata_section = section_kind::linked_definitions;
61 
62  constexpr bool is_target_section (section_kind const t) noexcept {
63  using utype = std::underlying_type<section_kind>::type;
64  return static_cast<utype> (t) < static_cast<utype> (first_repo_metadata_section);
65  }
66 
67 
69  struct section_base {};
70 
71 
72  template <typename T>
73  unsigned section_alignment (T const & section) noexcept;
74 
75  template <typename T>
76  std::uint64_t section_size (T const & section) noexcept;
77 
78  //* _ _ _ _ _ _ *
79  //* __ _ _ ___ __ _| |_(_)___ _ _ __| (_)____ __ __ _| |_ __| |_ ___ _ _ *
80  //* / _| '_/ -_) _` | _| / _ \ ' \ / _` | (_-< '_ \/ _` | _/ _| ' \/ -_) '_| *
81  //* \__|_| \___\__,_|\__|_\___/_||_| \__,_|_/__/ .__/\__,_|\__\__|_||_\___|_| *
82  //* |_| *
92  public:
93  explicit section_creation_dispatcher (section_kind const kind) noexcept
94  : kind_{kind} {}
97  virtual ~section_creation_dispatcher () noexcept;
98 
99  section_creation_dispatcher & operator= (section_creation_dispatcher const &) = delete;
101 
102  section_kind const & kind () const noexcept { return kind_; }
103 
107  template <typename IntType, typename = std::enable_if<std::is_unsigned<IntType>::value>>
108  std::size_t aligned (IntType a) const {
109  static_assert (sizeof (std::uintptr_t) >= sizeof (IntType),
110  "sizeof uintptr_t must be at least sizeof IntType");
111  return static_cast<std::size_t> (
112  this->aligned_impl (static_cast<std::uintptr_t> (a)));
113  }
117  std::uint8_t * aligned (std::uint8_t * const a) const {
118  return reinterpret_cast<std::uint8_t *> (
119  this->aligned_impl (reinterpret_cast<std::uintptr_t> (a)));
120  }
121 
124  virtual std::size_t size_bytes () const = 0;
125 
131  virtual std::uint8_t * write (std::uint8_t * out) const = 0;
132 
133  private:
137  virtual std::uintptr_t aligned_impl (std::uintptr_t v) const = 0;
138 
139  section_kind const kind_;
140  };
141 
145  template <typename T>
147 
151  template <typename ValueType>
152  class container {
153  public:
154  using value_type = ValueType const;
155  using size_type = std::size_t;
156  using difference_type = std::ptrdiff_t;
157  using reference = ValueType const &;
158  using const_reference = reference;
159  using pointer = ValueType const *;
160  using const_pointer = pointer;
161  using iterator = const_pointer;
162  using const_iterator = iterator;
163 
164  container () = default;
165  container (const_pointer begin, const_pointer end)
166  : begin_{begin}
167  , end_{end} {
168  PSTORE_ASSERT (end >= begin);
169  }
170  iterator begin () const { return begin_; }
171  iterator end () const { return end_; }
172  const_iterator cbegin () const { return begin (); }
173  const_iterator cend () const { return end (); }
174 
175  const_pointer data () const { return begin_; }
176 
177  size_type size () const noexcept {
178  PSTORE_ASSERT (end_ >= begin_);
179  return static_cast<size_type> (end_ - begin_);
180  }
181  bool empty () const noexcept { return size () == 0U; }
182 
183  private:
184  const_pointer begin_;
185  const_pointer end_;
186  };
187 
188  struct internal_fixup;
189  struct external_fixup;
190 
195  class dispatcher {
196  public:
197  virtual ~dispatcher () noexcept;
198 
199  virtual std::size_t size_bytes () const = 0;
200  virtual unsigned align () const = 0;
201  virtual std::size_t size () const = 0;
202  virtual container<internal_fixup> ifixups () const = 0;
203  virtual container<external_fixup> xfixups () const = 0;
206  virtual container<std::uint8_t> payload () const = 0;
207  };
208 
209 
213  template <typename T>
215 
216  } // end namespace repo
217 } // end namespace pstore
218 
219 #endif // PSTORE_MCREPO_SECTION_HPP
Definition: generic_section.hpp:112
A simple wrapper around the elements of one of the three arrays that make up a section.
Definition: section.hpp:152
Maps from the type of data that is associated with a fragment&#39;s section to a "dispatcher" subclass wh...
Definition: section.hpp:146
transaction< transaction_lock > begin(database &db, transaction_lock &lock)
Creates a new transaction. Every operation performed on a transaction instance can be potentially und...
Definition: transaction.hpp:311
This class is used to add virtual methods to a fragment&#39;s section.
Definition: section.hpp:195
An empty class used as the base type for all sections.
Definition: section.hpp:69
Maps from the type of data that is associated with a fragment&#39;s section to a "dispatcher" subclass wh...
Definition: section.hpp:214
Definition: nonpod2.cpp:40
std::uint8_t * aligned(std::uint8_t *const a) const
Definition: section.hpp:117
An implementation of the standard assert() macro with the exception that it will, on failure...
A section creation dispatcher is used to instantiate and construct each of a fragment&#39;s sections in p...
Definition: section.hpp:91
std::size_t aligned(IntType a) const
Definition: section.hpp:108
Definition: generic_section.hpp:46