HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
align.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_ARCH_ALIGN_H
25 #define PXR_BASE_ARCH_ALIGN_H
26 
27 /// \file arch/align.h
28 /// \ingroup group_arch_Memory
29 /// Provide architecture-specific memory-alignment information.
30 
31 #if !defined(__cplusplus)
32 #error This include file can only be included in C++ programs.
33 #endif
34 
35 #include "pxr/pxr.h"
36 #include "pxr/base/arch/api.h"
37 #include "pxr/base/arch/defines.h"
38 #include <cstddef>
39 #include <cstdint>
40 
42 
43 /// \addtogroup group_arch_Memory
44 ///@{
45 
46 /// Return suitably aligned memory size.
47 ///
48 /// Requests to \c malloc() or \c ::new for a given size are often rounded
49 /// upward. Given a request for \c nBytes bytes of storage, this function
50 /// returns the amount that would actually be consumed by the system to
51 /// satisfy it. This is needed for efficient user-defined memory management.
52 ///
53 inline size_t
54 ArchAlignMemorySize(size_t nBytes) {
55  return (nBytes + 7) & (~0x7);
56 }
57 
58 /// Maximum extra space needed for alignment.
59 ///
60 /// The \c ArchAlignMemorySize() can increase the required memory by no more
61 /// than \c ARCH_MAX_ALIGNMENT_INCREASE.
62 ///
63 /// \hideinitializer
64 #define ARCH_MAX_ALIGNMENT_INCREASE 7
65 
66 /// Align memory to the next "best" alignment value.
67 ///
68 /// This will take a pointer and bump it to the next ideal alignment boundary
69 /// that will work for all data types.
70 ///
71 inline void *
72 ArchAlignMemory(void *base)
73 {
74  return reinterpret_cast<void *>
75  ((reinterpret_cast<uintptr_t>(base) + 7) & ~0x7);
76 }
77 
78 /// The size of a CPU cache line on the current processor architecture in bytes.
79 ///
80 /// \hideinitializer
81 #if defined(ARCH_OS_DARWIN) && defined(ARCH_CPU_ARM)
82 #define ARCH_CACHE_LINE_SIZE 128
83 #else
84 #define ARCH_CACHE_LINE_SIZE 64
85 #endif
86 
87 /// Aligned memory allocation.
89 void *
90 ArchAlignedAlloc(size_t alignment, size_t size);
91 
92 /// Free memory allocated by ArchAlignedAlloc.
94 void
95 ArchAlignedFree(void* ptr);
96 
97 ///@}
98 
100 
101 #endif // PXR_BASE_ARCH_ALIGN_H
fallback_uintptr uintptr_t
Definition: format.h:295
ARCH_API void ArchAlignedFree(void *ptr)
Free memory allocated by ArchAlignedAlloc.
void * ArchAlignMemory(void *base)
Definition: align.h:72
ARCH_API void * ArchAlignedAlloc(size_t alignment, size_t size)
Aligned memory allocation.
GLsizeiptr size
Definition: glcorearb.h:664
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
auto ptr(T p) -> const void *
Definition: format.h:2448
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
size_t ArchAlignMemorySize(size_t nBytes)
Definition: align.h:54
#define ARCH_API
Definition: api.h:40