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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_ARCH_ALIGN_H
8 #define PXR_BASE_ARCH_ALIGN_H
9 
10 /// \file arch/align.h
11 /// \ingroup group_arch_Memory
12 /// Provide architecture-specific memory-alignment information.
13 
14 #if !defined(__cplusplus)
15 #error This include file can only be included in C++ programs.
16 #endif
17 
18 #include "pxr/pxr.h"
19 #include "pxr/base/arch/api.h"
20 #include "pxr/base/arch/defines.h"
21 #include <cstddef>
22 #include <cstdint>
23 
25 
26 /// \addtogroup group_arch_Memory
27 ///@{
28 
29 /// Return suitably aligned memory size.
30 ///
31 /// Requests to \c malloc() or \c ::new for a given size are often rounded
32 /// upward. Given a request for \c nBytes bytes of storage, this function
33 /// returns the amount that would actually be consumed by the system to
34 /// satisfy it. This is needed for efficient user-defined memory management.
35 ///
36 inline size_t
37 ArchAlignMemorySize(size_t nBytes) {
38  return (nBytes + 7) & (~0x7);
39 }
40 
41 /// Maximum extra space needed for alignment.
42 ///
43 /// The \c ArchAlignMemorySize() can increase the required memory by no more
44 /// than \c ARCH_MAX_ALIGNMENT_INCREASE.
45 ///
46 /// \hideinitializer
47 #define ARCH_MAX_ALIGNMENT_INCREASE 7
48 
49 /// Align memory to the next "best" alignment value.
50 ///
51 /// This will take a pointer and bump it to the next ideal alignment boundary
52 /// that will work for all data types.
53 ///
54 inline void *
55 ArchAlignMemory(void *base)
56 {
57  return reinterpret_cast<void *>
58  ((reinterpret_cast<uintptr_t>(base) + 7) & ~0x7);
59 }
60 
61 /// The size of a CPU cache line on the current processor architecture in bytes.
62 ///
63 /// \hideinitializer
64 #if defined(ARCH_OS_DARWIN) && defined(ARCH_CPU_ARM)
65 #define ARCH_CACHE_LINE_SIZE 128
66 #else
67 #define ARCH_CACHE_LINE_SIZE 64
68 #endif
69 
70 /// Aligned memory allocation.
72 void *
73 ArchAlignedAlloc(size_t alignment, size_t size);
74 
75 /// Free memory allocated by ArchAlignedAlloc.
77 void
78 ArchAlignedFree(void* ptr);
79 
80 ///@}
81 
83 
84 #endif // PXR_BASE_ARCH_ALIGN_H
uint128_t uintptr_t
Definition: format.h:479
ARCH_API void ArchAlignedFree(void *ptr)
Free memory allocated by ArchAlignedAlloc.
void * ArchAlignMemory(void *base)
Definition: align.h:55
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:1425
auto ptr(T p) -> const void *
Definition: format.h:4331
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
size_t ArchAlignMemorySize(size_t nBytes)
Definition: align.h:37
#define ARCH_API
Definition: api.h:23