All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dataBuffer.h
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
24PXR_NAMESPACE_OPEN_SCOPE
25
35public:
36 constexpr static size_t DefaultAllocSize = 1024;
37
40 TraceDataBuffer(size_t allocSize = DefaultAllocSize) : _alloc(allocSize) {}
41
44 template <typename T>
45 const T* StoreData(const T& value)
46 {
47 static_assert(std::is_copy_constructible<T>::value,
48 "Must by copy constructible");
49 static_assert(std::is_trivially_destructible<T>::value,
50 "No destructors will be called");
51 return new(_alloc.Allocate(alignof(T), sizeof(T))) T(value);
52 }
53
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
64private:
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
110PXR_NAMESPACE_CLOSE_SCOPE
111
112#endif // PXR_BASE_TRACE_DATA_BUFFER_H
This class stores copies of data that are associated with TraceEvent instances.
Definition: dataBuffer.h:34
TraceDataBuffer(size_t allocSize=DefaultAllocSize)
Constructor.
Definition: dataBuffer.h:40
const T * StoreData(const T &value)
Makes a copy of value and returns a pointer to it.
Definition: dataBuffer.h:45
const char * StoreData(const char *str)
Makes a copy of str and returns a pointer to it.
Definition: dataBuffer.h:56
Compiler hints.