HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dataBuffer.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 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 
25 #ifndef PXR_BASE_TRACE_DATA_BUFFER_H
26 #define PXR_BASE_TRACE_DATA_BUFFER_H
27 
28 #include "pxr/pxr.h"
29 
30 #include "pxr/base/trace/api.h"
31 
32 #include "pxr/base/arch/hints.h"
33 
34 #include <cstddef>
35 #include <cstdint>
36 #include <cstring>
37 #include <deque>
38 #include <memory>
39 #include <type_traits>
40 
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 /// \class TraceDataBuffer
45 ///
46 /// This class stores copies of data that are associated with TraceEvent
47 /// instances.
48 /// Data stored in the buffer must be copy constructible and trivially
49 /// destructible.
50 ///
52 public:
53  constexpr static size_t DefaultAllocSize = 1024;
54 
55  /// Constructor. The buffer will make allocations of \p allocSize.
56  ///
57  TraceDataBuffer(size_t allocSize = DefaultAllocSize) : _alloc(allocSize) {}
58 
59  /// Makes a copy of \p value and returns a pointer to it.
60  ///
61  template <typename T>
62  const T* StoreData(const T& value)
63  {
65  "Must by copy constructible");
67  "No destructors will be called");
68  return new(_alloc.Allocate(alignof(T), sizeof(T))) T(value);
69  }
70 
71  /// Makes a copy of \p str and returns a pointer to it.
72  /// Specialization for c strings.
73  const char* StoreData(const char* str) {
74  const size_t strLen = std::strlen(str) + 1;
75  void* mem = _alloc.Allocate(alignof(char), strLen);
76  char* cstr = reinterpret_cast<char*>(mem);
77  std::memcpy(cstr, str, strLen);
78  return cstr;
79  }
80 
81 private:
82  // Simple Allocator that only supports allocations, but not frees.
83  // Allocated memory is tied to the lifetime of the allocator object.
84  class Allocator {
85  public:
86  Allocator(size_t blockSize)
87  : _desiredBlockSize(blockSize) {}
88  Allocator(Allocator&&) = default;
89  Allocator& operator=(Allocator&&) = default;
90 
91  Allocator(const Allocator&) = delete;
92  Allocator& operator=(const Allocator&) = delete;
93 
94  void* Allocate(const size_t align, const size_t size) {
95  Byte* alignedNext = AlignPointer(_next, align);
96  Byte* end = alignedNext + size;
97  if (ARCH_UNLIKELY(end > _blockEnd)) {
98  AllocateBlock(align, size);
99  alignedNext = AlignPointer(_next, align);
100  end = _next + size;
101  }
102  _next = end;
103  return alignedNext;
104  }
105 
106  private:
107  using Byte = std::uint8_t;
108 
109  static Byte* AlignPointer(Byte* ptr, const size_t align) {
110  const size_t alignMask = align - 1;
111  return reinterpret_cast<Byte*>(
112  reinterpret_cast<uintptr_t>(ptr + alignMask) & ~alignMask);
113  }
114 
115  TRACE_API void AllocateBlock(const size_t align, const size_t desiredSize);
116 
117  Byte* _blockEnd = nullptr;
118  Byte* _next = nullptr;
119  using BlockPtr = std::unique_ptr<Byte[]>;
120  std::deque<BlockPtr> _blocks;
121  size_t _desiredBlockSize;
122  };
123 
124  Allocator _alloc;
125 };
126 
128 
129 #endif // PXR_BASE_TRACE_DATA_BUFFER_H
unsigned char Byte
Definition: zconf.h:365
fallback_uintptr uintptr_t
Definition: format.h:314
static constexpr size_t DefaultAllocSize
Definition: dataBuffer.h:53
GLsizeiptr size
Definition: glcorearb.h:663
#define ARCH_UNLIKELY(x)
Definition: hints.h:47
const char * StoreData(const char *str)
Definition: dataBuffer.h:73
GLuint GLuint end
Definition: glcorearb.h:474
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
const void * ptr(const T *p)
Definition: format.h:3603
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GLsizei const GLfloat * value
Definition: glcorearb.h:823
TraceDataBuffer(size_t allocSize=DefaultAllocSize)
Definition: dataBuffer.h:57
const T * StoreData(const T &value)
Definition: dataBuffer.h:62
#define TRACE_API
Definition: api.h:40