YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/alignment.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
// Macros for dealing with memory alignment.
33
#ifndef YB_UTIL_ALIGNMENT_H
34
#define YB_UTIL_ALIGNMENT_H
35
36
#include <cstddef>
37
38
namespace yb {
39
40
// Round down 'x' to the nearest 'align' boundary
41
#define YB_ALIGN_DOWN(x, align) ((x) & (-(align)))
42
43
template<class T>
44
class AlignmentTraits {
45
 public:
46
  typedef T value_type;
47
48
20
  static value_type to_int(T x) {
49
20
    return x;
50
20
  }
51
52
20
  static T from_int(value_type value) {
53
20
    return value;
54
20
  }
55
};
56
57
template<class T>
58
class AlignmentTraits<T*> {
59
 public:
60
  typedef size_t value_type;
61
62
575M
  static value_type to_int(T* ptr) {
63
575M
    return reinterpret_cast<value_type>(ptr);
64
575M
  }
65
66
575M
  static T* from_int(value_type value) {
67
575M
    return reinterpret_cast<T*>(value);
68
575M
  }
69
};
70
71
// Round up 'ptr' to the nearest 'align' boundary.
72
// T should be pointer or integer type.
73
template<class T>
74
575M
T align_up(T ptr, typename AlignmentTraits<T>::value_type alignment_bytes) {
75
575M
  typedef AlignmentTraits<T> Traits;
76
575M
  typedef typename Traits::value_type int_type;
77
575M
  static_assert(sizeof(T) == sizeof(int_type), "Invalid source size in align_up");
78
575M
  auto x = Traits::to_int(ptr);
79
575M
  return Traits::from_int((x + alignment_bytes - 1) & -alignment_bytes);
80
575M
}
unsigned char* yb::align_up<unsigned char*>(unsigned char*, yb::AlignmentTraits<unsigned char*>::value_type)
Line
Count
Source
74
575M
T align_up(T ptr, typename AlignmentTraits<T>::value_type alignment_bytes) {
75
575M
  typedef AlignmentTraits<T> Traits;
76
575M
  typedef typename Traits::value_type int_type;
77
575M
  static_assert(sizeof(T) == sizeof(int_type), "Invalid source size in align_up");
78
575M
  auto x = Traits::to_int(ptr);
79
575M
  return Traits::from_int((x + alignment_bytes - 1) & -alignment_bytes);
80
575M
}
unsigned long yb::align_up<unsigned long>(unsigned long, yb::AlignmentTraits<unsigned long>::value_type)
Line
Count
Source
74
20
T align_up(T ptr, typename AlignmentTraits<T>::value_type alignment_bytes) {
75
20
  typedef AlignmentTraits<T> Traits;
76
20
  typedef typename Traits::value_type int_type;
77
20
  static_assert(sizeof(T) == sizeof(int_type), "Invalid source size in align_up");
78
20
  auto x = Traits::to_int(ptr);
79
20
  return Traits::from_int((x + alignment_bytes - 1) & -alignment_bytes);
80
20
}
81
82
} // namespace yb
83
84
#endif // YB_UTIL_ALIGNMENT_H