1 /* Copyright 2019 Google LLC. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef RUY_RUY_SYSTEM_ALIGNED_ALLOC_H_
17 #define RUY_RUY_SYSTEM_ALIGNED_ALLOC_H_
18 
19 #include <cstddef>
20 
21 namespace ruy {
22 
23 namespace detail {
24 
25 // Minimum alignment for blocks.
26 //
27 // Considerations:
28 //  - This needs to be at least the alignment of any usual data type.
29 //  - It's useful that this is at least the size of a cache line to limit
30 //    possible cache side effects (if only on performance behavior).
31 //  - It's useful that this is at least the size of SIMD registers, as
32 //    some SIMD instruction sets have at least performance behavior
33 //    differences (e.g. NEON) or even different requirements (e.g. SSE)
34 //    based on that.
35 //  - It's useful that this is at least the size of an "exclusive reservation
36 //    granule" on ARM, meaning that if we use this Allocator to allocate
37 //    an atomic variable, there will be no side effects from other things
38 //    contending for exclusive/atomic memory accesses to it. While the
39 //    ARM reference manual mentions that this granule size may be as large
40 //    as 2048 bytes, in practice we observe it to be 64 bytes. It can
41 //    be queried cheaply, at runtime, from userspace, if needed.
42 constexpr std::ptrdiff_t kMinimumBlockAlignment = 64;
43 
44 // Primitive allocation functions obtaining aligned memory from the
45 // operating system.
46 void* SystemAlignedAlloc(std::ptrdiff_t num_bytes);
47 void SystemAlignedFree(void* ptr);
48 
49 }  // namespace detail
50 
51 }  // namespace ruy
52 
53 #endif  // RUY_RUY_SYSTEM_ALIGNED_ALLOC_H_
54