RavEngine
Loading...
Searching...
No Matches
OmniPvdChunkAlloc.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 OMNI_PVD_CHUNK_ALLOC_H
30#define OMNI_PVD_CHUNK_ALLOC_H
31
32#include "foundation/PxUserAllocated.h"
33
35// The usage reason for splitting the Chunk allocator into a ChunkPool and a
36// ByteAllocator is so that similarly accessed objects get allocated next to each
37// other within the same Chunk, which is managed by the ByteAllocator. The
38// ChunkPool is basically just a resource pool. Not thread safe anything :-)
39// Locking is bad mkkk...
41
43// Usage example of setting up a ChunkPool, a ByteAllocator and allocating some
44// bytes, for the lulz.
46
47/*
48 OmniPvdChunkPool pool;
49
50 // set the individual Chunks to a size of 100k bytes
51 pool.setChunkSize(100000);
52
53 // all allocations must go through a ByteAllocator, so create one
54 OmniPvdChunkByteAllocator allocator;
55
56 // connect the ByteAllocator to the pool
57 allocator.setPool(&pool);
58
59 // we allocate 100 bytes through the ByteAllocator
60 unsigned char *someBytes=allocator.allocBytes(100);
61*/
62
64// Once we want to cleanup the allocations, return the Chunks to the ChunkPool,
65// which is also done silently in the destructor of the ByteAllocator.
67
68/*
69 allocator.freeChunks();
70*/
71
73// Memory management warning! The ByteAllocator does not deallocate the Chunks,
74// but must return the Chunks to the ChunkPool before going out of scope. The
75// ChunkPool can only deallocate the Chunks that is has in its free Chunks list,
76// so all ByteAllocators must either all go out of scope before the ChunkPool
77// goes out of scope or the ByteAllocators must all call the function freeChunks
78// before the ChunkPool goes out of scope.
80
82{
83public:
86 OmniPvdChunkElement* mNextElement;
87};
88
90{
91public:
94
95 void resetChunk(int nbrBytes);
96 int nbrBytesUSed();
97
98 unsigned char* mData;
99 int mNbrBytesAllocated;
100 int mNbrBytesFree;
101 unsigned char* mBytePtr;
102 OmniPvdChunk* mNextChunk;
103};
104
106{
107public:
110
111 void resetList();
112 void appendList(OmniPvdChunkList* list);
113 void appendChunk(OmniPvdChunk* chunk);
114 OmniPvdChunk* removeFirst();
115
116 OmniPvdChunk* mFirstChunk;
117 OmniPvdChunk* mLastChunk;
118 int mNbrChunks;
119};
120
122{
123public:
126
127 void setChunkSize(int chunkSize);
128 OmniPvdChunk* getChunk();
129
130 OmniPvdChunkList mFreeChunks;
131 int mChunkSize;
132 int mNbrAllocatedChunks;
133};
134
135
137{
138public:
141
142 OmniPvdChunkElement* allocChunkElement(int totalStructSize);
143 void setPool(OmniPvdChunkPool* pool);
144 unsigned char* allocBytes(int nbrBytes);
145 void freeChunks();
146
147 OmniPvdChunkList mUsedChunks;
148 OmniPvdChunkPool* mPool;
149};
150
151#endif
Definition OmniPvdChunkAlloc.h:137
Definition OmniPvdChunkAlloc.h:82
Definition OmniPvdChunkAlloc.h:106
Definition OmniPvdChunkAlloc.h:122
Definition OmniPvdChunkAlloc.h:90
Definition PxUserAllocated.h:43