Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:06

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // This file provides alignment utilities for use in arenas.
0009 //
0010 // `ArenaAlign` contains a single `align` data member and provides
0011 // the below functions which operate on the given alignment.
0012 //
0013 //   Ceil(size_t n)      - rounds `n` up to the nearest `align` boundary.
0014 //   Floor(size_t n)     - rounds `n` down to the nearest `align` boundary.
0015 //   Padded(size_t n)    - returns the unaligned size to align 'n' bytes. (1)
0016 
0017 //   Ceil(T* P)          - rounds `p` up to the nearest `align` boundary. (2)
0018 //   IsAligned(size_t n) - returns true if `n` is aligned to `align`
0019 //   IsAligned(T* p)     - returns true if `p` is aligned to `align`
0020 //   CheckAligned(T* p)  - returns `p`. Checks alignment of `p` in debug.
0021 //
0022 // 1) `Padded(n)` returns the minimum size needed to align an object of size 'n'
0023 //    into a memory area that is default aligned. For example, allocating 'n'
0024 //    bytes aligned at 32 bytes requires a size of 'n + 32 - 8' to align at 32
0025 //    bytes for any 8 byte boundary.
0026 //
0027 // 2) There is an optimized `CeilDefaultAligned(T*)` method which is equivalent
0028 //    to `Ceil(ArenaAlignDefault::CheckAlign(p))` but more efficiently
0029 //    implemented as a 'check only' for ArenaAlignDefault.
0030 //
0031 // These classes allow for generic arena logic using 'alignment policies'.
0032 //
0033 // For example:
0034 //
0035 //  template <Align>
0036 //  void* NaiveAlloc(size_t n, Align align) {
0037 //    ABSL_ASSERT(align.IsAligned(n));
0038 //    const size_t required = align.Padded(n);
0039 //    if (required <= static_cast<size_t>(ptr_ - limit_)) {
0040 //      uint8_t* ptr = align.CeilDefaultAligned(ptr_);
0041 //      ptr_ = ptr + n;
0042 //      return ptr;
0043 //    }
0044 //    return nullptr;
0045 //  }
0046 //
0047 //  void CallSites() {
0048 //    void *p1 = NaiveAlloc(n, ArenaAlignDefault());
0049 //    void *p2 = NaiveAlloc(n, ArenaAlignAs(32));
0050 //  }
0051 //
0052 #ifndef GOOGLE_PROTOBUF_ARENA_ALIGN_H__
0053 #define GOOGLE_PROTOBUF_ARENA_ALIGN_H__
0054 
0055 #include <cstddef>
0056 #include <cstdint>
0057 
0058 #include "absl/base/macros.h"
0059 #include "absl/log/absl_check.h"
0060 #include "absl/numeric/bits.h"
0061 
0062 // Must be included last.
0063 #include "google/protobuf/port_def.inc"
0064 
0065 namespace google {
0066 namespace protobuf {
0067 namespace internal {
0068 
0069 struct ArenaAlignDefault {
0070   PROTOBUF_EXPORT static constexpr size_t align = 8;  // NOLINT
0071 
0072   static constexpr bool IsAligned(size_t n) { return (n & (align - 1)) == 0U; }
0073 
0074   template <typename T>
0075   static inline PROTOBUF_ALWAYS_INLINE bool IsAligned(T* ptr) {
0076     return (reinterpret_cast<uintptr_t>(ptr) & (align - 1)) == 0U;
0077   }
0078 
0079   static inline PROTOBUF_ALWAYS_INLINE constexpr size_t Ceil(size_t n) {
0080     return (n + align - 1) & ~(align - 1);
0081   }
0082   static inline PROTOBUF_ALWAYS_INLINE constexpr size_t Floor(size_t n) {
0083     return (n & ~(align - 1));
0084   }
0085 
0086   static inline PROTOBUF_ALWAYS_INLINE size_t Padded(size_t n) {
0087     ABSL_ASSERT(IsAligned(n));
0088     return n;
0089   }
0090 
0091   template <typename T>
0092   static inline PROTOBUF_ALWAYS_INLINE T* Ceil(T* ptr) {
0093     uintptr_t intptr = reinterpret_cast<uintptr_t>(ptr);
0094     return reinterpret_cast<T*>((intptr + align - 1) & ~(align - 1));
0095   }
0096 
0097   template <typename T>
0098   static inline PROTOBUF_ALWAYS_INLINE T* CeilDefaultAligned(T* ptr) {
0099     ABSL_ASSERT(IsAligned(ptr));
0100     return ptr;
0101   }
0102 
0103   // Address sanitizer enabled alignment check
0104   template <typename T>
0105   static inline PROTOBUF_ALWAYS_INLINE T* CheckAligned(T* ptr) {
0106     ABSL_ASSERT(IsAligned(ptr));
0107     return ptr;
0108   }
0109 };
0110 
0111 struct ArenaAlign {
0112   static constexpr bool IsDefault() { return false; };
0113 
0114   size_t align;
0115 
0116   constexpr bool IsAligned(size_t n) const { return (n & (align - 1)) == 0U; }
0117 
0118   template <typename T>
0119   bool IsAligned(T* ptr) const {
0120     return (reinterpret_cast<uintptr_t>(ptr) & (align - 1)) == 0U;
0121   }
0122 
0123   constexpr size_t Ceil(size_t n) const {
0124     return (n + align - 1) & ~(align - 1);
0125   }
0126   constexpr size_t Floor(size_t n) const { return (n & ~(align - 1)); }
0127 
0128   constexpr size_t Padded(size_t n) const {
0129     // TODO: there are direct callers of AllocateAligned() that violate
0130     // `size` being a multiple of `align`: that should be an error / assert.
0131     //  ABSL_ASSERT(IsAligned(n));
0132     ABSL_ASSERT(ArenaAlignDefault::IsAligned(align));
0133     return n + align - ArenaAlignDefault::align;
0134   }
0135 
0136   template <typename T>
0137   T* Ceil(T* ptr) const {
0138     uintptr_t intptr = reinterpret_cast<uintptr_t>(ptr);
0139     return reinterpret_cast<T*>((intptr + align - 1) & ~(align - 1));
0140   }
0141 
0142   template <typename T>
0143   T* CeilDefaultAligned(T* ptr) const {
0144     ABSL_ASSERT(ArenaAlignDefault::IsAligned(ptr));
0145     return Ceil(ptr);
0146   }
0147 
0148   // Address sanitizer enabled alignment check
0149   template <typename T>
0150   T* CheckAligned(T* ptr) const {
0151     ABSL_ASSERT(IsAligned(ptr));
0152     return ptr;
0153   }
0154 };
0155 
0156 inline ArenaAlign ArenaAlignAs(size_t align) {
0157   // align must be a non zero power of 2 >= 8
0158   ABSL_DCHECK_NE(align, 0U);
0159   ABSL_DCHECK(absl::has_single_bit(align)) << "Invalid alignment " << align;
0160   return ArenaAlign{align};
0161 }
0162 
0163 template <bool, size_t align>
0164 struct AlignFactory {
0165   static_assert(align > ArenaAlignDefault::align, "Not over-aligned");
0166   static_assert((align & (align - 1)) == 0U, "Not power of 2");
0167   static constexpr ArenaAlign Create() { return ArenaAlign{align}; }
0168 };
0169 
0170 template <size_t align>
0171 struct AlignFactory<true, align> {
0172   static_assert(align <= ArenaAlignDefault::align, "Over-aligned");
0173   static_assert((align & (align - 1)) == 0U, "Not power of 2");
0174   static constexpr ArenaAlignDefault Create() { return ArenaAlignDefault{}; }
0175 };
0176 
0177 // Returns an `ArenaAlignDefault` instance for `align` less than or equal to the
0178 // default alignment, and `AlignAs(align)` for over-aligned values of `align`.
0179 // The purpose is to take advantage of invoking functions accepting a template
0180 // overloaded 'Align align` argument reducing the alignment operations on
0181 // `ArenaAlignDefault` implementations to no-ops.
0182 template <size_t align>
0183 inline constexpr auto ArenaAlignAs() {
0184   return AlignFactory<align <= ArenaAlignDefault::align, align>::Create();
0185 }
0186 
0187 // Returns ArenaAlignAs<alignof(T)>
0188 template <typename T>
0189 inline constexpr auto ArenaAlignOf() {
0190   return ArenaAlignAs<alignof(T)>();
0191 }
0192 
0193 }  // namespace internal
0194 }  // namespace protobuf
0195 }  // namespace google
0196 
0197 #include "google/protobuf/port_undef.inc"
0198 
0199 #endif  // GOOGLE_PROTOBUF_ARENA_ALIGN_H__