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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_BASE_TRACE_DATA_BUFFER_H
9 #define PXR_BASE_TRACE_DATA_BUFFER_H
10 
11 #include "pxr/pxr.h"
12 
13 #include "pxr/base/trace/api.h"
14 
15 #include "pxr/base/arch/hints.h"
16 
17 #include <cstddef>
18 #include <cstdint>
19 #include <cstring>
20 #include <deque>
21 #include <memory>
22 #include <type_traits>
23 
25 
26 ///////////////////////////////////////////////////////////////////////////////
27 /// \class TraceDataBuffer
28 ///
29 /// This class stores copies of data that are associated with TraceEvent
30 /// instances.
31 /// Data stored in the buffer must be copy constructible and trivially
32 /// destructible.
33 ///
35 public:
36  constexpr static size_t DefaultAllocSize = 1024;
37 
38  /// Constructor. The buffer will make allocations of \p allocSize.
39  ///
40  TraceDataBuffer(size_t allocSize = DefaultAllocSize) : _alloc(allocSize) {}
41 
42  /// Makes a copy of \p value and returns a pointer to it.
43  ///
44  template <typename T>
45  const T* StoreData(const T& value)
46  {
48  "Must by copy constructible");
50  "No destructors will be called");
51  return new(_alloc.Allocate(alignof(T), sizeof(T))) T(value);
52  }
53 
54  /// Makes a copy of \p str and returns a pointer to it.
55  /// Specialization for c strings.
56  const char* StoreData(const char* str) {
57  const size_t strLen = std::strlen(str) + 1;
58  void* mem = _alloc.Allocate(alignof(char), strLen);
59  char* cstr = reinterpret_cast<char*>(mem);
60  std::memcpy(cstr, str, strLen);
61  return cstr;
62  }
63 
64 private:
65  // Simple Allocator that only supports allocations, but not frees.
66  // Allocated memory is tied to the lifetime of the allocator object.
67  class Allocator {
68  public:
69  Allocator(size_t blockSize)
70  : _desiredBlockSize(blockSize) {}
71  Allocator(Allocator&&) = default;
72  Allocator& operator=(Allocator&&) = default;
73 
74  Allocator(const Allocator&) = delete;
75  Allocator& operator=(const Allocator&) = delete;
76 
77  void* Allocate(const size_t align, const size_t size) {
78  Byte* alignedNext = AlignPointer(_next, align);
79  Byte* end = alignedNext + size;
80  if (ARCH_UNLIKELY(end > _blockEnd)) {
81  AllocateBlock(align, size);
82  alignedNext = AlignPointer(_next, align);
83  end = _next + size;
84  }
85  _next = end;
86  return alignedNext;
87  }
88 
89  private:
90  using Byte = std::uint8_t;
91 
92  static Byte* AlignPointer(Byte* ptr, const size_t align) {
93  const size_t alignMask = align - 1;
94  return reinterpret_cast<Byte*>(
95  reinterpret_cast<uintptr_t>(ptr + alignMask) & ~alignMask);
96  }
97 
98  TRACE_API void AllocateBlock(const size_t align, const size_t desiredSize);
99 
100  Byte* _blockEnd = nullptr;
101  Byte* _next = nullptr;
102  using BlockPtr = std::unique_ptr<Byte[]>;
103  std::deque<BlockPtr> _blocks;
104  size_t _desiredBlockSize;
105  };
106 
107  Allocator _alloc;
108 };
109 
111 
112 #endif // PXR_BASE_TRACE_DATA_BUFFER_H
unsigned char Byte
Definition: zconf.h:124
uint128_t uintptr_t
Definition: format.h:479
GLsizei const GLfloat * value
Definition: glcorearb.h:824
static constexpr size_t DefaultAllocSize
Definition: dataBuffer.h:36
#define ARCH_UNLIKELY(x)
Definition: hints.h:30
const char * StoreData(const char *str)
Definition: dataBuffer.h:56
GLuint GLuint end
Definition: glcorearb.h:475
GLsizeiptr size
Definition: glcorearb.h:664
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
LeafData & operator=(const LeafData &)=delete
auto ptr(T p) -> const void *
Definition: format.h:4331
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR_14 uint8_t blockSize(VULKAN_HPP_NAMESPACE::Format format)
TraceDataBuffer(size_t allocSize=DefaultAllocSize)
Definition: dataBuffer.h:40
const T * StoreData(const T &value)
Definition: dataBuffer.h:45
#define TRACE_API
Definition: api.h:23