/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  |  | //  | 
4  |  | // Use of this source code is governed by a BSD-style  | 
5  |  | // license that can be found in the LICENSE file or at  | 
6  |  | // https://developers.google.com/open-source/licenses/bsd  | 
7  |  |  | 
8  |  | // This file provides alignment utilities for use in arenas.  | 
9  |  | //  | 
10  |  | // `ArenaAlign` contains a single `align` data member and provides  | 
11  |  | // the below functions which operate on the given alignment.  | 
12  |  | //  | 
13  |  | //   Ceil(size_t n)      - rounds `n` up to the nearest `align` boundary.  | 
14  |  | //   Floor(size_t n)     - rounds `n` down to the nearest `align` boundary.  | 
15  |  | //   Padded(size_t n)    - returns the unaligned size to align 'n' bytes. (1)  | 
16  |  |  | 
17  |  | //   Ceil(T* P)          - rounds `p` up to the nearest `align` boundary. (2)  | 
18  |  | //   IsAligned(size_t n) - returns true if `n` is aligned to `align`  | 
19  |  | //   IsAligned(T* p)     - returns true if `p` is aligned to `align`  | 
20  |  | //   CheckAligned(T* p)  - returns `p`. Checks alignment of `p` in debug.  | 
21  |  | //  | 
22  |  | // 1) `Padded(n)` returns the minimum size needed to align an object of size 'n'  | 
23  |  | //    into a memory area that is default aligned. For example, allocating 'n'  | 
24  |  | //    bytes aligned at 32 bytes requires a size of 'n + 32 - 8' to align at 32  | 
25  |  | //    bytes for any 8 byte boundary.  | 
26  |  | //  | 
27  |  | // 2) There is an optimized `CeilDefaultAligned(T*)` method which is equivalent  | 
28  |  | //    to `Ceil(ArenaAlignDefault::CheckAlign(p))` but more efficiently  | 
29  |  | //    implemented as a 'check only' for ArenaAlignDefault.  | 
30  |  | //  | 
31  |  | // These classes allow for generic arena logic using 'alignment policies'.  | 
32  |  | //  | 
33  |  | // For example:  | 
34  |  | //  | 
35  |  | //  template <Align>  | 
36  |  | //  void* NaiveAlloc(size_t n, Align align) { | 
37  |  | //    ABSL_ASSERT(align.IsAligned(n));  | 
38  |  | //    const size_t required = align.Padded(n);  | 
39  |  | //    if (required <= static_cast<size_t>(ptr_ - limit_)) { | 
40  |  | //      uint8_t* ptr = align.CeilDefaultAligned(ptr_);  | 
41  |  | //      ptr_ = ptr + n;  | 
42  |  | //      return ptr;  | 
43  |  | //    }  | 
44  |  | //    return nullptr;  | 
45  |  | //  }  | 
46  |  | //  | 
47  |  | //  void CallSites() { | 
48  |  | //    void *p1 = NaiveAlloc(n, ArenaAlignDefault());  | 
49  |  | //    void *p2 = NaiveAlloc(n, ArenaAlignAs(32));  | 
50  |  | //  }  | 
51  |  | //  | 
52  |  | #ifndef GOOGLE_PROTOBUF_ARENA_ALIGN_H__  | 
53  |  | #define GOOGLE_PROTOBUF_ARENA_ALIGN_H__  | 
54  |  |  | 
55  |  | #include <cstddef>  | 
56  |  | #include <cstdint>  | 
57  |  |  | 
58  |  | #include "absl/base/macros.h"  | 
59  |  | #include "absl/log/absl_check.h"  | 
60  |  | #include "absl/numeric/bits.h"  | 
61  |  |  | 
62  |  | // Must be included last.  | 
63  |  | #include "google/protobuf/port_def.inc"  | 
64  |  |  | 
65  |  | namespace google { | 
66  |  | namespace protobuf { | 
67  |  | namespace internal { | 
68  |  |  | 
69  |  | struct ArenaAlignDefault { | 
70  |  |   PROTOBUF_EXPORT static constexpr size_t align = 8;  // NOLINT  | 
71  |  |  | 
72  | 0  |   static constexpr bool IsAligned(size_t n) { return (n & (align - 1)) == 0U; } | 
73  |  |  | 
74  |  |   template <typename T>  | 
75  | 0  |   static inline PROTOBUF_ALWAYS_INLINE bool IsAligned(T* ptr) { | 
76  | 0  |     return (reinterpret_cast<uintptr_t>(ptr) & (align - 1)) == 0U;  | 
77  | 0  |   } Unexecuted instantiation: bool google::protobuf::internal::ArenaAlignDefault::IsAligned<void>(void*) Unexecuted instantiation: bool google::protobuf::internal::ArenaAlignDefault::IsAligned<char>(char*)  | 
78  |  |  | 
79  | 0  |   static inline PROTOBUF_ALWAYS_INLINE constexpr size_t Ceil(size_t n) { | 
80  | 0  |     return (n + align - 1) & ~(align - 1);  | 
81  | 0  |   }  | 
82  | 0  |   static inline PROTOBUF_ALWAYS_INLINE constexpr size_t Floor(size_t n) { | 
83  | 0  |     return (n & ~(align - 1));  | 
84  | 0  |   }  | 
85  |  |  | 
86  | 0  |   static inline PROTOBUF_ALWAYS_INLINE size_t Padded(size_t n) { | 
87  | 0  |     ABSL_ASSERT(IsAligned(n));  | 
88  | 0  |     return n;  | 
89  | 0  |   }  | 
90  |  |  | 
91  |  |   template <typename T>  | 
92  |  |   static inline PROTOBUF_ALWAYS_INLINE T* Ceil(T* ptr) { | 
93  |  |     uintptr_t intptr = reinterpret_cast<uintptr_t>(ptr);  | 
94  |  |     return reinterpret_cast<T*>((intptr + align - 1) & ~(align - 1));  | 
95  |  |   }  | 
96  |  |  | 
97  |  |   template <typename T>  | 
98  | 0  |   static inline PROTOBUF_ALWAYS_INLINE T* CeilDefaultAligned(T* ptr) { | 
99  | 0  |     ABSL_ASSERT(IsAligned(ptr));  | 
100  | 0  |     return ptr;  | 
101  | 0  |   }  | 
102  |  |  | 
103  |  |   // Address sanitizer enabled alignment check  | 
104  |  |   template <typename T>  | 
105  |  |   static inline PROTOBUF_ALWAYS_INLINE T* CheckAligned(T* ptr) { | 
106  |  |     ABSL_ASSERT(IsAligned(ptr));  | 
107  |  |     return ptr;  | 
108  |  |   }  | 
109  |  | };  | 
110  |  |  | 
111  |  | struct ArenaAlign { | 
112  | 0  |   static constexpr bool IsDefault() { return false; }; | 
113  |  |  | 
114  |  |   size_t align;  | 
115  |  |  | 
116  | 0  |   constexpr bool IsAligned(size_t n) const { return (n & (align - 1)) == 0U; } | 
117  |  |  | 
118  |  |   template <typename T>  | 
119  |  |   bool IsAligned(T* ptr) const { | 
120  |  |     return (reinterpret_cast<uintptr_t>(ptr) & (align - 1)) == 0U;  | 
121  |  |   }  | 
122  |  |  | 
123  | 0  |   constexpr size_t Ceil(size_t n) const { | 
124  | 0  |     return (n + align - 1) & ~(align - 1);  | 
125  | 0  |   }  | 
126  | 0  |   constexpr size_t Floor(size_t n) const { return (n & ~(align - 1)); } | 
127  |  |  | 
128  | 0  |   constexpr size_t Padded(size_t n) const { | 
129  |  |     // TODO: there are direct callers of AllocateAligned() that violate  | 
130  |  |     // `size` being a multiple of `align`: that should be an error / assert.  | 
131  |  |     //  ABSL_ASSERT(IsAligned(n));  | 
132  | 0  |     ABSL_ASSERT(ArenaAlignDefault::IsAligned(align));  | 
133  | 0  |     return n + align - ArenaAlignDefault::align;  | 
134  | 0  |   }  | 
135  |  |  | 
136  |  |   template <typename T>  | 
137  | 0  |   T* Ceil(T* ptr) const { | 
138  | 0  |     uintptr_t intptr = reinterpret_cast<uintptr_t>(ptr);  | 
139  | 0  |     return reinterpret_cast<T*>((intptr + align - 1) & ~(align - 1));  | 
140  | 0  |   } Unexecuted instantiation: char* google::protobuf::internal::ArenaAlign::Ceil<char>(char*) const Unexecuted instantiation: void* google::protobuf::internal::ArenaAlign::Ceil<void>(void*) const  | 
141  |  |  | 
142  |  |   template <typename T>  | 
143  | 0  |   T* CeilDefaultAligned(T* ptr) const { | 
144  | 0  |     ABSL_ASSERT(ArenaAlignDefault::IsAligned(ptr));  | 
145  | 0  |     return Ceil(ptr);  | 
146  | 0  |   } Unexecuted instantiation: void* google::protobuf::internal::ArenaAlign::CeilDefaultAligned<void>(void*) const Unexecuted instantiation: char* google::protobuf::internal::ArenaAlign::CeilDefaultAligned<char>(char*) const  | 
147  |  |  | 
148  |  |   // Address sanitizer enabled alignment check  | 
149  |  |   template <typename T>  | 
150  |  |   T* CheckAligned(T* ptr) const { | 
151  |  |     ABSL_ASSERT(IsAligned(ptr));  | 
152  |  |     return ptr;  | 
153  |  |   }  | 
154  |  | };  | 
155  |  |  | 
156  | 0  | inline ArenaAlign ArenaAlignAs(size_t align) { | 
157  |  |   // align must be a non zero power of 2 >= 8  | 
158  | 0  |   ABSL_DCHECK_NE(align, 0U);  | 
159  | 0  |   ABSL_DCHECK(absl::has_single_bit(align)) << "Invalid alignment " << align;  | 
160  | 0  |   return ArenaAlign{align}; | 
161  | 0  | }  | 
162  |  |  | 
163  |  | template <bool, size_t align>  | 
164  |  | struct AlignFactory { | 
165  |  |   static_assert(align > ArenaAlignDefault::align, "Not over-aligned");  | 
166  |  |   static_assert((align & (align - 1)) == 0U, "Not power of 2");  | 
167  |  |   static constexpr ArenaAlign Create() { return ArenaAlign{align}; } | 
168  |  | };  | 
169  |  |  | 
170  |  | template <size_t align>  | 
171  |  | struct AlignFactory<true, align> { | 
172  |  |   static_assert(align <= ArenaAlignDefault::align, "Over-aligned");  | 
173  |  |   static_assert((align & (align - 1)) == 0U, "Not power of 2");  | 
174  |  |   static constexpr ArenaAlignDefault Create() { return ArenaAlignDefault{}; } | 
175  |  | };  | 
176  |  |  | 
177  |  | // Returns an `ArenaAlignDefault` instance for `align` less than or equal to the  | 
178  |  | // default alignment, and `AlignAs(align)` for over-aligned values of `align`.  | 
179  |  | // The purpose is to take advantage of invoking functions accepting a template  | 
180  |  | // overloaded 'Align align` argument reducing the alignment operations on  | 
181  |  | // `ArenaAlignDefault` implementations to no-ops.  | 
182  |  | template <size_t align>  | 
183  |  | inline constexpr auto ArenaAlignAs() { | 
184  |  |   return AlignFactory<align <= ArenaAlignDefault::align, align>::Create();  | 
185  |  | }  | 
186  |  |  | 
187  |  | // Returns ArenaAlignAs<alignof(T)>  | 
188  |  | template <typename T>  | 
189  |  | inline constexpr auto ArenaAlignOf() { | 
190  |  |   return ArenaAlignAs<alignof(T)>();  | 
191  |  | }  | 
192  |  |  | 
193  |  | }  // namespace internal  | 
194  |  | }  // namespace protobuf  | 
195  |  | }  // namespace google  | 
196  |  |  | 
197  |  | #include "google/protobuf/port_undef.inc"  | 
198  |  |  | 
199  |  | #endif  // GOOGLE_PROTOBUF_ARENA_ALIGN_H__  |