RavEngine
Loading...
Searching...
No Matches
PxProfileMemoryBuffer.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_PROFILE_MEMORY_BUFFER_H
30#define PX_PROFILE_MEMORY_BUFFER_H
31
32#include "foundation/PxAllocator.h"
33#include "foundation/PxMemory.h"
34
35namespace physx { namespace profile {
36
37 template<typename TAllocator = typename PxAllocatorTraits<uint8_t>::Type >
38 class MemoryBuffer : public TAllocator
39 {
40 uint8_t* mBegin;
41 uint8_t* mEnd;
42 uint8_t* mCapacityEnd;
43
44 public:
45 MemoryBuffer( const TAllocator& inAlloc = TAllocator() ) : TAllocator( inAlloc ), mBegin( 0 ), mEnd( 0 ), mCapacityEnd( 0 ) {}
47 {
48 if ( mBegin ) TAllocator::deallocate( mBegin );
49 }
50 uint32_t size() const { return static_cast<uint32_t>( mEnd - mBegin ); }
51 uint32_t capacity() const { return static_cast<uint32_t>( mCapacityEnd - mBegin ); }
52 uint8_t* begin() { return mBegin; }
53 uint8_t* end() { return mEnd; }
54 void setEnd(uint8_t* nEnd) { mEnd = nEnd; }
55 const uint8_t* begin() const { return mBegin; }
56 const uint8_t* end() const { return mEnd; }
57 void clear() { mEnd = mBegin; }
58 uint32_t write( uint8_t inValue )
59 {
60 growBuf( 1 );
61 *mEnd = inValue;
62 ++mEnd;
63 return 1;
64 }
65
66 template<typename TDataType>
67 uint32_t write( const TDataType& inValue )
68 {
69 uint32_t writtenSize = sizeof(TDataType);
70 growBuf(writtenSize);
71 const uint8_t* __restrict readPtr = reinterpret_cast< const uint8_t* >( &inValue );
72 uint8_t* __restrict writePtr = mEnd;
73 for ( uint32_t idx = 0; idx < sizeof(TDataType); ++idx ) writePtr[idx] = readPtr[idx];
74 mEnd += writtenSize;
75 return writtenSize;
76 }
77
78 template<typename TDataType>
79 uint32_t write( const TDataType* inValue, uint32_t inLength )
80 {
81 if ( inValue && inLength )
82 {
83 uint32_t writeSize = inLength * sizeof( TDataType );
84 growBuf( writeSize );
85 PxMemCopy( mBegin + size(), inValue, writeSize );
86 mEnd += writeSize;
87 return writeSize;
88 }
89 return 0;
90 }
91
92 // used by atomic write. Store the data and write the end afterwards
93 // we dont check the buffer size, it should not resize on the fly
94 template<typename TDataType>
95 uint32_t write(const TDataType* inValue, uint32_t inLength, int32_t index)
96 {
97 if (inValue && inLength)
98 {
99 uint32_t writeSize = inLength * sizeof(TDataType);
100 PX_ASSERT(mBegin + index + writeSize < mCapacityEnd);
101 PxMemCopy(mBegin + index, inValue, writeSize);
102 return writeSize;
103 }
104 return 0;
105 }
106
107 void growBuf( uint32_t inAmount )
108 {
109 uint32_t newSize = size() + inAmount;
110 reserve( newSize );
111 }
112 void resize( uint32_t inAmount )
113 {
114 reserve( inAmount );
115 mEnd = mBegin + inAmount;
116 }
117 void reserve( uint32_t newSize )
118 {
119 uint32_t currentSize = size();
120 if ( newSize >= capacity() )
121 {
122 const uint32_t allocSize = mBegin ? newSize * 2 : newSize;
123
124 uint8_t* newData = static_cast<uint8_t*>(TAllocator::allocate(allocSize, __FILE__, __LINE__));
125 memset(newData, 0xf,allocSize);
126 if ( mBegin )
127 {
128 PxMemCopy( newData, mBegin, currentSize );
129 TAllocator::deallocate( mBegin );
130 }
131 mBegin = newData;
132 mEnd = mBegin + currentSize;
133 mCapacityEnd = mBegin + allocSize;
134 }
135 }
136 };
137
138
140 {
141 uint8_t* mBegin;
142 uint8_t* mEnd;
143 uint8_t* mCapacityEnd;
144
145 public:
146 TempMemoryBuffer(uint8_t* data, int32_t size) : mBegin(data), mEnd(data), mCapacityEnd(data + size) {}
148 {
149 }
150 uint32_t size() const { return static_cast<uint32_t>(mEnd - mBegin); }
151 uint32_t capacity() const { return static_cast<uint32_t>(mCapacityEnd - mBegin); }
152 const uint8_t* begin() { return mBegin; }
153 uint8_t* end() { return mEnd; }
154 const uint8_t* begin() const { return mBegin; }
155 const uint8_t* end() const { return mEnd; }
156 uint32_t write(uint8_t inValue)
157 {
158 *mEnd = inValue;
159 ++mEnd;
160 return 1;
161 }
162
163 template<typename TDataType>
164 uint32_t write(const TDataType& inValue)
165 {
166 uint32_t writtenSize = sizeof(TDataType);
167 const uint8_t* __restrict readPtr = reinterpret_cast<const uint8_t*>(&inValue);
168 uint8_t* __restrict writePtr = mEnd;
169 for (uint32_t idx = 0; idx < sizeof(TDataType); ++idx) writePtr[idx] = readPtr[idx];
170 mEnd += writtenSize;
171 return writtenSize;
172 }
173
174 template<typename TDataType>
175 uint32_t write(const TDataType* inValue, uint32_t inLength)
176 {
177 if (inValue && inLength)
178 {
179 uint32_t writeSize = inLength * sizeof(TDataType);
180 PxMemCopy(mBegin + size(), inValue, writeSize);
181 mEnd += writeSize;
182 return writeSize;
183 }
184 return 0;
185 }
186 };
187
188}}
189
190#endif
Definition PxProfileMemoryBuffer.h:39
Definition PxProfileMemoryBuffer.h:140
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_FORCE_INLINE void * PxMemCopy(void *dest, const void *src, PxU32 count)
Copies the bytes of one memory block to another. The memory blocks must not overlap.
Definition PxMemory.h:83