Loading...
Searching...
No Matches
dataBuffer.h
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
41PXR_NAMESPACE_OPEN_SCOPE
42
52public:
53 constexpr static size_t DefaultAllocSize = 1024;
54
57 TraceDataBuffer(size_t allocSize = DefaultAllocSize) : _alloc(allocSize) {}
58
61 template <typename T>
62 const T* StoreData(const T& value)
63 {
64 static_assert(std::is_copy_constructible<T>::value,
65 "Must by copy constructible");
66 static_assert(std::is_trivially_destructible<T>::value,
67 "No destructors will be called");
68 return new(_alloc.Allocate(alignof(T), sizeof(T))) T(value);
69 }
70
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
81private:
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
127PXR_NAMESPACE_CLOSE_SCOPE
128
129#endif // PXR_BASE_TRACE_DATA_BUFFER_H
This class stores copies of data that are associated with TraceEvent instances.
Definition: dataBuffer.h:51
TraceDataBuffer(size_t allocSize=DefaultAllocSize)
Constructor.
Definition: dataBuffer.h:57
const T * StoreData(const T &value)
Makes a copy of value and returns a pointer to it.
Definition: dataBuffer.h:62
const char * StoreData(const char *str)
Makes a copy of str and returns a pointer to it.
Definition: dataBuffer.h:73
Compiler hints.