Coverage Report

Created: 2023-06-07 07:09

/src/LPM/external.protobuf/include/google/protobuf/arena_align.h
Line
Count
Source (jump to first uncovered line)
1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc.  All rights reserved.
3
// https://developers.google.com/protocol-buffers/
4
//
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
7
// met:
8
//
9
//     * Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
11
//     * Redistributions in binary form must reproduce the above
12
// copyright notice, this list of conditions and the following disclaimer
13
// in the documentation and/or other materials provided with the
14
// distribution.
15
//     * Neither the name of Google Inc. nor the names of its
16
// contributors may be used to endorse or promote products derived from
17
// this software without specific prior written permission.
18
//
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31
// This file provides alignment utilities for use in arenas.
32
//
33
// `ArenaAlign` contains a single `align` data member and provides
34
// the below functions which operate on the given alignment.
35
//
36
//   Ceil(size_t n)      - rounds `n` up to the nearest `align` boundary.
37
//   Floor(size_t n)     - rounds `n` down to the nearest `align` boundary.
38
//   Padded(size_t n)    - returns the unaligned size to align 'n' bytes. (1)
39
40
//   Ceil(T* P)          - rounds `p` up to the nearest `align` boundary. (2)
41
//   IsAligned(size_t n) - returns true if `n` is aligned to `align`
42
//   IsAligned(T* p)     - returns true if `p` is aligned to `align`
43
//   CheckAligned(T* p)  - returns `p`. Checks alignment of `p` in debug.
44
//
45
// 1) `Padded(n)` returns the minimum size needed to align an object of size 'n'
46
//    into a memory area that is default aligned. For example, allocating 'n'
47
//    bytes aligned at 32 bytes requires a size of 'n + 32 - 8' to align at 32
48
//    bytes for any 8 byte boundary.
49
//
50
// 2) There is an optimized `CeilDefaultAligned(T*)` method which is equivalent
51
//    to `Ceil(ArenaAlignDefault::CheckAlign(p))` but more efficiently
52
//    implemented as a 'check only' for ArenaAlignDefault.
53
//
54
// These classes allow for generic arena logic using 'alignment policies'.
55
//
56
// For example:
57
//
58
//  template <Align>
59
//  void* NaiveAlloc(size_t n, Align align) {
60
//    ABSL_ASSERT(align.IsAligned(n));
61
//    const size_t required = align.Padded(n);
62
//    if (required <= static_cast<size_t>(ptr_ - limit_)) {
63
//      uint8_t* ptr = align.CeilDefaultAligned(ptr_);
64
//      ptr_ = ptr + n;
65
//      return ptr;
66
//    }
67
//    return nullptr;
68
//  }
69
//
70
//  void CallSites() {
71
//    void *p1 = NaiveAlloc(n, ArenaAlignDefault());
72
//    void *p2 = NaiveAlloc(n, ArenaAlignAs(32));
73
//  }
74
//
75
#ifndef GOOGLE_PROTOBUF_ARENA_ALIGN_H__
76
#define GOOGLE_PROTOBUF_ARENA_ALIGN_H__
77
78
#include <cstddef>
79
#include <cstdint>
80
81
#include "absl/log/absl_check.h"
82
#include "absl/numeric/bits.h"
83
84
// Must be included last.
85
#include "google/protobuf/port_def.inc"
86
87
namespace google {
88
namespace protobuf {
89
namespace internal {
90
91
struct ArenaAlignDefault {
92
  PROTOBUF_EXPORT static constexpr size_t align = 8;  // NOLINT
93
94
0
  static constexpr bool IsAligned(size_t n) { return (n & (align - 1)) == 0U; }
95
96
  template <typename T>
97
0
  static inline PROTOBUF_ALWAYS_INLINE bool IsAligned(T* ptr) {
98
0
    return (reinterpret_cast<uintptr_t>(ptr) & (align - 1)) == 0U;
99
0
  }
Unexecuted instantiation: bool google::protobuf::internal::ArenaAlignDefault::IsAligned<void>(void*)
Unexecuted instantiation: bool google::protobuf::internal::ArenaAlignDefault::IsAligned<char>(char*)
100
101
0
  static inline PROTOBUF_ALWAYS_INLINE constexpr size_t Ceil(size_t n) {
102
0
    return (n + align - 1) & -align;
103
0
  }
104
0
  static inline PROTOBUF_ALWAYS_INLINE constexpr size_t Floor(size_t n) {
105
0
    return (n & ~(align - 1));
106
0
  }
107
108
0
  static inline PROTOBUF_ALWAYS_INLINE size_t Padded(size_t n) {
109
0
    ABSL_ASSERT(IsAligned(n));
110
0
    return n;
111
0
  }
112
113
  template <typename T>
114
  static inline PROTOBUF_ALWAYS_INLINE T* Ceil(T* ptr) {
115
    uintptr_t intptr = reinterpret_cast<uintptr_t>(ptr);
116
    return reinterpret_cast<T*>((intptr + align - 1) & -align);
117
  }
118
119
  template <typename T>
120
0
  static inline PROTOBUF_ALWAYS_INLINE T* CeilDefaultAligned(T* ptr) {
121
0
    ABSL_ASSERT(IsAligned(ptr));
122
0
    return ptr;
123
0
  }
124
125
  // Address sanitizer enabled alignment check
126
  template <typename T>
127
  static inline PROTOBUF_ALWAYS_INLINE T* CheckAligned(T* ptr) {
128
    ABSL_ASSERT(IsAligned(ptr));
129
    return ptr;
130
  }
131
};
132
133
struct ArenaAlign {
134
0
  static constexpr bool IsDefault() { return false; };
135
136
  size_t align;
137
138
0
  constexpr bool IsAligned(size_t n) const { return (n & (align - 1)) == 0U; }
139
140
  template <typename T>
141
  bool IsAligned(T* ptr) const {
142
    return (reinterpret_cast<uintptr_t>(ptr) & (align - 1)) == 0U;
143
  }
144
145
0
  constexpr size_t Ceil(size_t n) const { return (n + align - 1) & -align; }
146
0
  constexpr size_t Floor(size_t n) const { return (n & ~(align - 1)); }
147
148
0
  constexpr size_t Padded(size_t n) const {
149
    // TODO(mvels): there are direct callers of AllocateAligned() that violate
150
    // `size` being a multiple of `align`: that should be an error / assert.
151
    //  ABSL_ASSERT(IsAligned(n));
152
0
    ABSL_ASSERT(ArenaAlignDefault::IsAligned(align));
153
0
    return n + align - ArenaAlignDefault::align;
154
0
  }
155
156
  template <typename T>
157
0
  T* Ceil(T* ptr) const {
158
0
    uintptr_t intptr = reinterpret_cast<uintptr_t>(ptr);
159
0
    return reinterpret_cast<T*>((intptr + align - 1) & -align);
160
0
  }
Unexecuted instantiation: char* google::protobuf::internal::ArenaAlign::Ceil<char>(char*) const
Unexecuted instantiation: void* google::protobuf::internal::ArenaAlign::Ceil<void>(void*) const
161
162
  template <typename T>
163
0
  T* CeilDefaultAligned(T* ptr) const {
164
0
    ABSL_ASSERT(ArenaAlignDefault::IsAligned(ptr));
165
0
    return Ceil(ptr);
166
0
  }
Unexecuted instantiation: void* google::protobuf::internal::ArenaAlign::CeilDefaultAligned<void>(void*) const
Unexecuted instantiation: char* google::protobuf::internal::ArenaAlign::CeilDefaultAligned<char>(char*) const
167
168
  // Address sanitizer enabled alignment check
169
  template <typename T>
170
  T* CheckAligned(T* ptr) const {
171
    ABSL_ASSERT(IsAligned(ptr));
172
    return ptr;
173
  }
174
};
175
176
0
inline ArenaAlign ArenaAlignAs(size_t align) {
177
  // align must be a non zero power of 2 >= 8
178
0
  ABSL_DCHECK_NE(align, 0U);
179
0
  ABSL_DCHECK(absl::has_single_bit(align)) << "Invalid alignment " << align;
180
0
  return ArenaAlign{align};
181
0
}
182
183
template <bool, size_t align>
184
struct AlignFactory {
185
  static_assert(align > ArenaAlignDefault::align, "Not over-aligned");
186
  static_assert((align & (align - 1)) == 0U, "Not power of 2");
187
  static constexpr ArenaAlign Create() { return ArenaAlign{align}; }
188
};
189
190
template <size_t align>
191
struct AlignFactory<true, align> {
192
  static_assert(align <= ArenaAlignDefault::align, "Over-aligned");
193
  static_assert((align & (align - 1)) == 0U, "Not power of 2");
194
  static constexpr ArenaAlignDefault Create() { return ArenaAlignDefault{}; }
195
};
196
197
// Returns an `ArenaAlignDefault` instance for `align` less than or equal to the
198
// default alignment, and `AlignAs(align)` for over-aligned values of `align`.
199
// The purpose is to take advantage of invoking functions accepting a template
200
// overloaded 'Align align` argument reducing the alignment operations on
201
// `ArenaAlignDefault` implementations to no-ops.
202
template <size_t align>
203
inline constexpr auto ArenaAlignAs() {
204
  return AlignFactory<align <= ArenaAlignDefault::align, align>::Create();
205
}
206
207
// Returns ArenaAlignAs<alignof(T)>
208
template <typename T>
209
inline constexpr auto ArenaAlignOf() {
210
  return ArenaAlignAs<alignof(T)>();
211
}
212
213
}  // namespace internal
214
}  // namespace protobuf
215
}  // namespace google
216
217
#include "google/protobuf/port_undef.inc"
218
219
#endif  // GOOGLE_PROTOBUF_ARENA_ALIGN_H__