HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CE_BufferHost.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  */
7 
8 #pragma once
9 
10 #include "CE_API.h"
11 #include <UT/UT_Assert.h>
12 #include <SYS/SYS_Align.h>
13 #include <SYS/SYS_Types.h>
14 #include <string.h>
15 
16 /// The traditional allocator will align to the size of the type, which in this
17 /// case is char - however, in order to maximize the chances of vectorized
18 /// instructions being generated, we'll align to the size of the cache line.
19 ///
20 /// Note: We don't have a cross-platform method to determine the size of the
21 /// cache line, so we'll just assume 64 bytes.
22 ///
23 /// T should be a POD as this is not initialized.
24 ///
25 template <typename T>
27 {
28 public:
29  explicit CE_BufferHost() = default;
30 
31  // Note this is NOT initialized.
32  explicit CE_BufferHost(const exint size)
33  : myData((T*)SYSamalloc(size*sizeof(T), ALIGNMENT))
34  , myDataSize(size)
35  , myStorageMode(StorageMode::STORE_ALIGNED)
36  {
37  }
38 
40  {
42  (T*)SYSamalloc(other.myDataSize*sizeof(T), ALIGNMENT), other.myDataSize*sizeof(T));
43  memcpy(myData, other.myData, other.myDataSize*sizeof(T));
44  }
45 
46  CE_BufferHost(CE_BufferHost &&other) noexcept
47  : myData(other.myData)
48  , myDataSize(other.myDataSize)
49  , myStorageMode(other.myStorageMode)
50  {
51  other.myData = nullptr;
52  other.myDataSize = 0;
53  other.myStorageMode = StorageMode::STORE_UNDEFINED;
54  }
55 
57  {
59  (T*)SYSamalloc(other.myDataSize*sizeof(T), ALIGNMENT), other.myDataSize*sizeof(T));
60  memcpy(myData, other.myData, other.myDataSize*sizeof(T));
61  return *this;
62  }
63 
65  {
66  freeData();
67 
68  myData = (T*) data;
69  myDataSize = size;
70  myStorageMode = StorageMode::STORE_ALIGNED;
71  }
72 
73  void adoptFromMalloc(void *data, const exint size)
74  {
75  freeData();
76 
77  myData = (T*) data;
78  myDataSize = size;
79  myStorageMode = StorageMode::STORE_MALLOC;
80  }
81 
82  void adoptFromNew(void *data, const exint size)
83  {
84  freeData();
85 
86  myData = (T*) data;
87  myDataSize = size;
88  myStorageMode = StorageMode::STORE_NEW;
89  }
90 
92  {
93  freeData();
94  }
95 
96  /// The buffer data.
97  T *data() const { return myData; }
98 
99  /// The size of the buffer data in elements.
100  exint size() const { return myDataSize; }
101 
103  {
104  int64 total = sizeof(*this);
105  total += myDataSize * sizeof(T);
106  return total;
107  }
108 
109  static constexpr int ALIGNMENT = 64; // bytes
110 
111 private:
112  enum class StorageMode
113  {
115  STORE_NEW,
116  STORE_MALLOC,
118  };
119 
120  void freeData()
121  {
122  if (!myData)
123  return;
124 
125  switch (myStorageMode)
126  {
127  case StorageMode::STORE_MALLOC: free(myData); break;
128  case StorageMode::STORE_NEW: delete[] myData; break;
129  case StorageMode::STORE_ALIGNED: SYSafree(myData); break;
131  default: UT_ASSERT(0 && "Unknown storage mode");
132  }
133 
134  myStorageMode = StorageMode::STORE_UNDEFINED;
135  myData = nullptr;
136  myDataSize = 0;
137  }
138 
139  T *myData = nullptr;
140  exint myDataSize = 0; // T elements
141  StorageMode myStorageMode = StorageMode::STORE_UNDEFINED;
142 };
143 
145 
void adoptFromMalloc(void *data, const exint size)
Definition: CE_BufferHost.h:73
CE_BufferHost()=default
T * data() const
The buffer data.
Definition: CE_BufferHost.h:97
int64 exint
Definition: SYS_Types.h:125
void SYSafree(void *p)
Definition: SYS_Align.h:105
void * SYSamalloc(size_t b)
Definition: SYS_Align.h:103
#define SYS_FALLTHROUGH
Definition: SYS_Compiler.h:68
long long int64
Definition: SYS_Types.h:116
CE_BufferHost(const CE_BufferHost &other)
Definition: CE_BufferHost.h:39
exint size() const
The size of the buffer data in elements.
int64 getMemoryUsage() const
void adoptFromAlignedMalloc(void *data, const exint size)
Definition: CE_BufferHost.h:64
GLsizeiptr size
Definition: glcorearb.h:664
CE_BufferHost(CE_BufferHost &&other) noexcept
Definition: CE_BufferHost.h:46
static constexpr int ALIGNMENT
CE_BufferHost(const exint size)
Definition: CE_BufferHost.h:32
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
void adoptFromNew(void *data, const exint size)
Definition: CE_BufferHost.h:82
Definition: format.h:1821
CE_BufferHost & operator=(const CE_BufferHost &other)
Definition: CE_BufferHost.h:56