E-MailRelay
galign.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file galign.h
19///
20
21#ifndef G_ALIGN_H
22#define G_ALIGN_H
23
24#include "gdef.h"
25#include <memory>
26
27namespace G
28{
29 namespace AlignImp /// An implementation namespace for G::align().
30 {
31 template <unsigned int N>
32 struct log2of /// Evaluates the number of bits in the template parameter N.
33 {
34 static constexpr unsigned int value = 1U + log2of<N/2>::value ;
35 } ;
36
37 template <>
38 struct log2of<1U> /// Terminal specialisation of log2of<N>.
39 {
40 static constexpr unsigned int value = 0U ;
41 } ;
42
43 template <typename Talign, typename Tvalue>
44 inline
45 constexpr Tvalue mask( Tvalue n )
46 {
47 return n & (~(Tvalue(0))<<log2of<sizeof(Talign)>::value) ;
48 }
49
50 template <typename Talign, typename Tvalue>
51 inline
52 constexpr Tvalue shift( Tvalue n )
53 {
54 return n >> log2of<sizeof(Talign)>::value ;
55 }
56
57 template <typename Talign, typename Tchar>
58 inline void * align_imp( const Tchar * p , const std::size_t n_in )
59 {
60 void * vp = const_cast<Tchar*>(p) ;
61 std::size_t n = n_in ;
62 return std::align( alignof(Talign) , sizeof(Talign) , vp , n ) ;
63 }
64 }
65
66 /// Returns a pointer inside the given buffer that is aligned for
67 /// values of type T.
68 ///
69 template <typename T>
70 inline void * align( const char * buffer , std::size_t buffer_size )
71 {
72 return AlignImp::align_imp<T>( buffer , buffer_size ) ;
73 }
74
75 /// Returns a pointer inside the given unsigned-char buffer that
76 /// is aligned for values of type T.
77 ///
78 template <typename T>
79 inline void * align( const unsigned char * buffer , std::size_t buffer_size )
80 {
81 return AlignImp::align_imp<T>( buffer , buffer_size ) ;
82 }
83
84 /// Divides the number of bytes in a range to give the number
85 /// of whole Ts.
86 ///
87 template <typename T>
88 inline
89 constexpr std::size_t align_shift( std::size_t n )
90 {
91 return AlignImp::shift<T>( n ) ;
92 }
93
94 /// Rounds down the number of bytes in a range to give a number of
95 /// bytes that will hold an exact number of Ts.
96 ///
97 template <typename T>
98 inline
99 constexpr std::size_t align_mask( std::size_t n )
100 {
101 return AlignImp::mask<T>( n ) ;
102 }
103}
104
105#endif
Low-level classes.
Definition: galign.h:28
constexpr std::size_t align_mask(std::size_t n)
Rounds down the number of bytes in a range to give a number of bytes that will hold an exact number o...
Definition: galign.h:99
constexpr std::size_t align_shift(std::size_t n)
Divides the number of bytes in a range to give the number of whole Ts.
Definition: galign.h:89
void * align(const char *buffer, std::size_t buffer_size)
Returns a pointer inside the given buffer that is aligned for values of type T.
Definition: galign.h:70
void * align(const unsigned char *buffer, std::size_t buffer_size)
Returns a pointer inside the given unsigned-char buffer that is aligned for values of type T.
Definition: galign.h:79
Evaluates the number of bits in the template parameter N.
Definition: galign.h:33