This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Alignment.hpp
102 lines (89 loc) · 3.07 KB
/
Alignment.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**/
#ifndef QUICKSTEP_UTILITY_ALIGNMENT_HPP_
#define QUICKSTEP_UTILITY_ALIGNMENT_HPP_
#include <cstddef>
#include <cstdint>
#include "utility/Macros.hpp"
#include "utility/UtilityConfig.h"
#include "glog/logging.h"
#ifdef QUICKSTEP_HAVE_STD_ALIGN
#include <memory>
#endif
#if defined(QUICKSTEP_HAVE_TCMALLOC)
#include "gperftools/include/gperftools/tcmalloc.h"
#elif defined(QUICKSTEP_HAVE_ALIGNED_ALLOC) || defined(QUICKSTEP_HAVE_POSIX_MEMALIGN)
#include <stdlib.h>
#elif defined(QUICKSTEP_HAVE_MEMALIGN) || defined(QUICKSTEP_HAVE_WIN_ALIGNED_MALLOC)
#include <malloc.h>
#endif
namespace quickstep {
/** \addtogroup Utility
* @{
*/
/**
* @brief Fallback for std::align (works exactly the same way) with an
* implementation for systems where std::align isn't available.
**/
inline void* align(std::size_t alignment,
std::size_t size,
void *&ptr, // NOLINT(runtime/references)
std::size_t &space) { // NOLINT(runtime/references)
#ifdef QUICKSTEP_HAVE_STD_ALIGN
return std::align(alignment, size, ptr, space);
#else
std::uintptr_t ptr_as_uint = reinterpret_cast<std::uintptr_t>(ptr);
std::uintptr_t adjustment = alignment - (ptr_as_uint % alignment);
if (adjustment == alignment) {
adjustment = 0;
}
if (adjustment + size <= space) {
ptr = reinterpret_cast<void*>(ptr_as_uint + adjustment);
space -= adjustment;
return ptr;
} else {
return nullptr;
}
#endif
}
/**
* @brief Allocate size bytes of memory aligned to the specified alignment
* using a platform-specific allocator.
**/
inline void* malloc_with_alignment(const std::size_t size,
const std::size_t alignment) {
#if defined(QUICKSTEP_HAVE_TCMALLOC)
void *ptr;
CHECK_EQ(0, tc_posix_memalign(&ptr, alignment, size));
return ptr;
#elif defined(QUICKSTEP_HAVE_ALIGNED_ALLOC)
return aligned_alloc(alignment, size);
#elif defined(QUICKSTEP_HAVE_POSIX_MEMALIGN)
void *ptr;
CHECK_EQ(0, posix_memalign(&ptr, alignment, size));
return ptr;
#elif defined(QUICKSTEP_HAVE_MEMALIGN)
return memalign(alignment, size);
#elif defined(QUICKSTEP_HAVE_WIN_ALIGNED_MALLOC)
return _aligned_malloc(size, alignment);
#endif
}
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_UTILITY_ALIGNMENT_HPP_