gwnavruntime/database/stitchdatamanagerutils.h Source File

stitchdatamanagerutils.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 #pragma once
8 
10 
11 namespace Kaim
12 {
13 
14 class MemoryManagerUtils
15 {
16 public:
17  template<class T>
18  static bool IsMemoryAlignedForClass(T* memory)
19  {
20  return IsMemoryAlignedForClassFromClassSize<(KyUInt32)sizeof(T)>((char*)memory);
21  }
22 
23  template<class T>
24  static KyUInt32 GetPaddingForMemory(char* memory)
25  {
26  return GetPaddingForMemoryFromClassSize<(KyUInt32)sizeof(T)>(memory);
27  }
28 
29  template<class T>
30  static KyUInt32 GetPaddingForBytesCount(KyUInt32 currentBytesCount)
31  {
32  return GetPaddingForBytesCountFromClassSize<(KyUInt32)sizeof(T)>((UPInt)currentBytesCount);
33  }
34 
35  template<KyUInt32 sizeOfClass>
36  static inline bool IsMemoryAlignedForClassFromClassSize(char* memory)
37  {
38  return GetPaddingForMemoryFromClassSize<sizeOfClass>(memory) == 0;
39  }
40 
41  template<KyUInt32 sizeOfClass>
42  static inline KyUInt32 GetPaddingForMemoryFromClassSize(char* memory)
43  {
44  return GetPaddingForBytesCountFromClassSize<sizeOfClass>((UPInt)memory);
45  }
46 
47  template<KyUInt32 sizeOfClass>
48  static inline KyUInt32 GetPaddingForBytesCountFromClassSize(UPInt currentBytesCount)
49  {
50  if ((sizeOfClass & 0x1) != 0)
51  // no padding
52  return 0;
53  else if ((sizeOfClass & 0x2) != 0)
54  // padding to be 2 bytes aligned
55  // need one 1 byte more if currentBytesCount is not already even
56  return (KyUInt32)(currentBytesCount & 0x1);
57  else
58  {
59 #ifndef KY_64BIT_POINTERS
60  // padding to be 4 bytes aligned
61  // add
62  return (KyUInt32)((4 - (currentBytesCount & 0x3)) & 0x3);
63 #else
64  if ((sizeOfClass & 0x4) != 0)
65  // padding to be 4 bytes aligned
66  return (KyUInt32)((4 - (currentBytesCount & 0x3)) & 0x3);
67  else
68  // padding to be 8 bytes aligned
69  return (KyUInt32)((8 - (currentBytesCount & 0x7)) & 0x7);
70 #endif
71  }
72 
73  }
74 
75 
76 };
77 
78 
79 }
80 
81 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17