Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
alignment.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Roc authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_core/alignment.h
10 //! @brief Alignment.
11 
12 #ifndef ROC_CORE_ALIGNMENT_H_
13 #define ROC_CORE_ALIGNMENT_H_
14 
15 #include "roc_core/stddefs.h"
16 
17 namespace roc {
18 namespace core {
19 
20 //! A union with maximum possible allignment.
21 union MaxAlign {
22  double d; //!< Double.
23  void (*fp)(); //!< Function pointer.
24 };
25 
26 //! Adjust the given size to be maximum aligned.
27 inline size_t max_align(size_t sz) {
28  enum { Align = sizeof(MaxAlign) };
29  if (sz % Align != 0) {
30  sz += Align - sz % Align;
31  }
32  return sz;
33 }
34 
35 //! Calculate padding required for given alignment.
36 inline size_t padding(size_t size, size_t alignment) {
37  if (alignment == 0) {
38  return 0;
39  }
40  size_t new_size = size / alignment * alignment;
41  if (new_size < size) {
42  new_size += alignment;
43  }
44  return (new_size - size);
45 }
46 
47 } // namespace core
48 } // namespace roc
49 
50 #endif // ROC_CORE_ALIGNMENT_H_
size_t padding(size_t size, size_t alignment)
Calculate padding required for given alignment.
Definition: alignment.h:36
A union with maximum possible allignment.
Definition: alignment.h:21
Root namespace.
Commonly used types and functions.
size_t max_align(size_t sz)
Adjust the given size to be maximum aligned.
Definition: alignment.h:27
void(* fp)()
Function pointer.
Definition: alignment.h:23
double d
Double.
Definition: alignment.h:22