Nebula
Loading...
Searching...
No Matches
ringallocator.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
14//------------------------------------------------------------------------------
15#include "stdneb.h"
16#include "threading/event.h"
17#include "util/array.h"
18namespace Memory
19{
20
22{
24 byte* data;
25};
26
27template<uint SYNCPOINTS>
69
70//------------------------------------------------------------------------------
73template<uint SYNCPOINTS>
74inline
76 size(size),
77 nextEvent(0)
78{
79 this->lockedIntervals.Resize(SYNCPOINTS);
80 this->events.Resize(SYNCPOINTS);
81 this->states.Resize(SYNCPOINTS);
82
83 for (uint i = 0; i < SYNCPOINTS; i++)
84 {
85 this->states[i] = Reset;
86 }
87 this->buffer = new byte[size];
88
89 this->currentAllocation = 0;
90 this->currentInterval.lower = 0;
91 this->currentInterval.upper = 0;
92 this->freeInterval.lower = 0;
93 this->freeInterval.upper = size;
94}
95
96//------------------------------------------------------------------------------
99template<uint SYNCPOINTS>
100inline
102{
103 delete[] this->buffer;
104}
105
106//------------------------------------------------------------------------------
109template<uint SYNCPOINTS>
110inline
112{
113 // reset intervals and counter
114 this->currentAllocation = 0;
115 this->currentInterval.lower = this->currentInterval.upper = this->freeInterval.lower;
116
117 // get event and wait, they should be checked in order
118 for (IndexT i = 0; i < this->events.Size(); i++)
119 {
120 Threading::Event& ev = this->events[i];
121 if (ev.Peek() && this->freeInterval.lower)
122 {
123 // set interval and reset
124 this->freeInterval.upper = this->lockedIntervals[i].upper;
125 this->states[i] = Signaled;
126 ev.Reset();
127 }
128 }
129}
130
131//------------------------------------------------------------------------------
134template<uint SYNCPOINTS>
135inline bool
137{
138 n_assert(size <= this->size);
139 uint begin = this->currentInterval.upper;
140 uint end = begin + size;
141 if (end > this->size && this->freeInterval.lower >= this->freeInterval.upper)
142 {
143 // wrap around to beginning, which should be okay if the lower is higher than upper, meaning
144 // we popped a lower interval during Start() and can now reuse from the beginning of the buffer
145 alloc.offset = 0;
146 end = this->freeInterval.upper;
147 }
148 else if (end > this->freeInterval.upper)
149 {
150 // if our intervals are not wrapping, we can just check to see if we will go overboard
151 n_warning("Over-allocated RingAllocator!\n");
152 alloc.data = nullptr;
153 alloc.offset = -1;
154 return false;
155 }
156
157 // set allocation structure
158 alloc.data = this->buffer + begin;
159 alloc.offset = begin;
160
161 // move lower interval to the end for the next allocation
162 this->currentInterval.upper = end;
163 this->currentAllocation += size;
164 return true;
165}
166
167//------------------------------------------------------------------------------
170template<uint SYNCPOINTS>
171inline Threading::Event*
173{
174 Threading::Event* ret = nullptr;
175
176 // if we did allocate this frame, use one of the sync events
177 if (this->currentAllocation > 0)
178 {
179 // save current interval as locked
180 this->lockedIntervals[this->nextEvent] = this->currentInterval;
181 this->states[this->nextEvent] = Waiting;
182 ret = &this->events[this->nextEvent];
183 this->nextEvent = (this->nextEvent + 1) % SYNCPOINTS;
184
185 // set the free interval to be offset by the last allocation size
186 this->freeInterval.lower = this->currentInterval.upper;
187 }
188
189 // return event so we can pass it to worker thread
190 return ret;
191}
192} // namespace Memory
Allocates memory up-front, and then allows other systems to grab regions.
Definition ringallocator.h:29
Util::FixedArray< SyncState > states
Definition ringallocator.h:63
Threading::Event ev[SYNCPOINTS]
Definition ringallocator.h:47
Util::FixedArray< Interval > lockedIntervals
Definition ringallocator.h:61
SyncState
Definition ringallocator.h:55
@ Waiting
Definition ringallocator.h:57
@ Reset
Definition ringallocator.h:58
@ Signaled
Definition ringallocator.h:56
Threading::Event * End()
end allocation phase, insert sync points
Definition ringallocator.h:172
uint currentAllocation
Definition ringallocator.h:66
RingAllocator(const SizeT size)
constructor
Definition ringallocator.h:75
bool Allocate(const SizeT size, RingAlloc &alloc)
allocate memory
Definition ringallocator.h:136
uint nextEvent
Definition ringallocator.h:67
uint size
Definition ringallocator.h:46
byte * buffer
Definition ringallocator.h:45
Interval currentInterval
Definition ringallocator.h:64
void Start()
start allocation phase, checks sync points and unlocks regions if they have been signaled
Definition ringallocator.h:111
~RingAllocator()
destructor
Definition ringallocator.h:101
Util::FixedArray< Threading::Event > events
Definition ringallocator.h:62
Interval freeInterval
Definition ringallocator.h:65
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
void Resize(SizeT newSize)
resize array without deleting existing content
Definition fixedarray.h:368
void __cdecl n_warning(const char *msg,...)
This function is called when a warning should be issued which doesn't require abortion of the applica...
Definition debug.cc:161
#define n_assert(exp)
Definition debug.h:50
Allocates memory using the TLSF method (http://www.gii.upv.es/tlsf/files/ecrts04_tlsf....
Definition arenaallocator.h:31
Definition ringallocator.h:22
byte * data
Definition ringallocator.h:24
uint offset
Definition ringallocator.h:23
Definition ringallocator.h:50
uint lower
Definition ringallocator.h:51
uint upper
Definition ringallocator.h:51
int SizeT
Definition types.h:49
unsigned int uint
Definition types.h:31
int IndexT
Definition types.h:48