24#ifndef _GEMA_APPEND_BUFFER_H_
25#define _GEMA_APPEND_BUFFER_H_
30#include <QAtomicInteger>
31#include <QVarLengthArray>
38#ifndef Q_ATOMIC_INT64_IS_SUPPORTED
39#error No 64 bit support for atomic operations
47static_assert(
sizeof(size_t) == 8,
"Unexpected size_t size");
88 virtual void append(
const T& val) = 0;
94 virtual size_t size()
const = 0;
181 Q_UNUSED(resizeFactor);
184 assert(initSize >= 0);
219 for(
int i = 0, nb =
_data.
size(); i < nb; i++)
249 size_t s = (bsize +
_nt-1)/
_nt;
251 for(
int i = 1; i <
_nt; i++)
286 for(
int i = 0, nb =
_data.
size(); i < nb; i++)
314 for(
int i = 0, nb =
_data.
size(); i < nb; i++)
319 size_t n = buffer.
size();
350 for(
int i = 0, nb =
_data.
size(); i < nb; i++)
352 return s *
sizeof(T);
420 assert(resizeFactor > 1.0);
421 assert(initSize >= 0);
499 qint64 index = pos - p->
_offset;
501 if(index < (qint64)p->
_size)
504 while(index < 0) { assert(p->
_prev); p = p->
_prev; index += p->
_size; }
506 p->
_data[index] = val;
527 size_t newsize = qMax((
size_t)(cursize *
_resizeFactor), pos + 1);
631 return (p->_offset + p->_size) *
sizeof(T);
639 while((p =
_head) != NULL)
A virtual class representing a buffer of T objects that can be appended in a thread-safe way,...
Definition gmAppendBuffer.h:66
virtual size_t usedMemory() const =0
Returns an estimative of the total memory used by the buffer in bytes.
virtual size_t size() const =0
Returns the number of entries in the buffer. Must be called from the main thread only.
virtual T * data()=0
Returns a vector filled with the buffer data. After this call, NO calls to append() can be made witho...
virtual void clear()=0
Releases the memory used by the buffer, returning it to a recently constructed state....
virtual void append(const T &val)=0
Appends val to the buffer in a thread safe way. Can grow the buffer if needed.
virtual void appendFromThread(int tid, const T &val)=0
Appends val to the buffer in a thread safe way, using the given tid to access TLS storage....
virtual void reserve(size_t size)=0
Informs the buffer of the expected number of entries that will be filled by (concurrent) calls to app...
virtual ~GmAppendBuffer()
Virtual destructor.
Definition gmAppendBuffer.h:69
An implementation of the GmAppendBuffer interface based on a "per thread" growing buffer.
Definition gmAppendBuffer.h:153
virtual T * data()
Returns a vector filled with the buffer data. After this call, NO calls to append() can be made witho...
Definition gmAppendBuffer.h:292
virtual void append(const T &val)
Appends val to the buffer in a thread safe way. Can grow the buffer if needed.
Definition gmAppendBuffer.h:257
T * _dataBuffer
The single buffer after a call to data()
Definition gmAppendBuffer.h:359
virtual size_t usedMemory() const
Returns an estimative of the total memory used by the buffer in bytes.
Definition gmAppendBuffer.h:336
GmTLS< QVarLengthArray< T >, false > _data
Per thread buffers.
Definition gmAppendBuffer.h:356
size_t _dataSize
The size in _dataBuffer when _dataBuffer is not NULL.
Definition gmAppendBuffer.h:360
virtual void clear()
Releases the memory used by the buffer, returning it to a recently constructed state....
Definition gmAppendBuffer.h:209
GmPerThreadAppendBuffer(size_t initSize, double resizeFactor=2.0, int numThreads=-1)
Buffer constructor. Can optionally pre allocate the buffer with initSize entries. If initSize is zero...
Definition gmAppendBuffer.h:178
virtual void appendFromThread(int tid, const T &val)
Appends val to the buffer in a thread safe way, using the given tid to access TLS storage....
Definition gmAppendBuffer.h:266
bool _globalOnly
Flag set to true if the user passed zero as the number of threads for the constructor.
Definition gmAppendBuffer.h:357
int _nt
Number of threads considered for buffer pre-allocation & parallel memcopy.
Definition gmAppendBuffer.h:358
virtual void reserve(size_t bsize)
Pre allocates buffer sizes. See basic description on the base class Should not be called if the size ...
Definition gmAppendBuffer.h:232
virtual size_t size() const
Returns the number of entries in the buffer. Must be called from the main thread only.
Definition gmAppendBuffer.h:276
~GmPerThreadAppendBuffer()
Destructor.
Definition gmAppendBuffer.h:199
An implementation of the GmAppendBuffer interface based on synchoronized access to a shared buffer.
Definition gmAppendBuffer.h:403
virtual size_t size() const
Returns the number of entries in the buffer. Must be called from the main thread only.
Definition gmAppendBuffer.h:558
QAtomicPointer< ControllData > _controll
The controll block pointing to the current buffer.
Definition gmAppendBuffer.h:677
~GmSingleAppendBuffer()
Destructor. Releases the allocated memory.
Definition gmAppendBuffer.h:436
virtual void clear()
Releases the memory used by the buffer, returning it to a recently constructed state....
Definition gmAppendBuffer.h:443
virtual size_t usedMemory() const
Returns an estimative of the total memory used by the buffer in bytes.
Definition gmAppendBuffer.h:622
virtual void reserve(size_t bsize)
See comments on the base class. Should not be called if the size was given in the constructor.
Definition gmAppendBuffer.h:456
ControllData * _head
Pointer to the first allocated buffer.
Definition gmAppendBuffer.h:674
double _resizeFactor
The resize factor.
Definition gmAppendBuffer.h:675
int _nt
Number of threads considered for parallel memcopy.
Definition gmAppendBuffer.h:680
T * _dataBuffer
The single buffer after a call to data()
Definition gmAppendBuffer.h:679
GmSpinLock _controllLock
The lock controlling changes to _controll.
Definition gmAppendBuffer.h:678
virtual void appendFromThread(int tid, const T &val)
Appends val to the buffer in a thread safe way, using the given tid to access TLS storage....
Definition gmAppendBuffer.h:550
virtual T * data()
Returns a vector filled with the buffer data. After this call, NO calls to append() can be made witho...
Definition gmAppendBuffer.h:561
GmSingleAppendBuffer(size_t initSize, double resizeFactor=2.0, int numThreads=-1)
Buffer constructor. Can optionally pre allocate the buffer with initSize entries. If initSize is zero...
Definition gmAppendBuffer.h:415
virtual void append(const T &val)
Appends val to the buffer in a thread safe way. Can grow the buffer if needed.
Definition gmAppendBuffer.h:468
QAtomicInteger< size_t > _nextIndex
The next free index in the global vector.
Definition gmAppendBuffer.h:676
A simple spin lock implementation based on a loop using test and set over an atomic int to change its...
Definition gmSpinLock.h:37
bool tryLock()
Try to lock. Returns true if the lock was aquired, false otherwise.
Definition gmSpinLock.h:46
void unlock()
Releases the lock.
Definition gmSpinLock.h:49
A class that works together with GmThreadManager to provide thread local storage.
Definition gmThreadLocalStorage.h:132
T & localData(int tid)
Returns the given thread local data as a modifiable reference.
Definition gmThreadLocalStorage.h:163
int size() const
Returns the number of values stored in the TLS object. Equal to the max number of threads + 1 (for th...
Definition gmThreadLocalStorage.h:160
static bool inMainThread()
Is the current thread the main thread? Equivalent to comparing the currentId() with 0.
Definition gmThreadManager.h:174
static int maxWorkerThreads()
Returns the maximum number of allowed working threads.
Definition gmThreadManager.h:158
void * GmPmemcpy(void *dst, const void *src, size_t n, int nt=0, size_t min=10 *1024 *1024)
Parallel (thread enabled) version of memcpy using OpenMP.
Definition gmMemory.h:183
int GmOmpAdjustNumThreads(int nt)
Adjusts the given number of threads. If nt <= 0 or if nt > maximum number of omp threads,...
Definition gmOmp.h:53
Declaration of the GmSpinLock class.
Declaration of the GmTLS class.
Declaration of the GmThreadManager class.
Auxiliary configuration file used to enable or disable compiling the GeMA tools with support for usin...
#define S_TRACE()
Macro for run time stack tracking at release build.
Definition gmTrace.h:44
T fetchAndAddAcquire(T valueToAdd)
Aux controll structure storing a buffer.
Definition gmAppendBuffer.h:649
T * _data
This data buffer.
Definition gmAppendBuffer.h:666
ControllData * _next
The next buffer.
Definition gmAppendBuffer.h:671
~ControllData()
Destructor.
Definition gmAppendBuffer.h:664
ControllData(size_t size, ControllData *prev)
Constructor: Initializes the buffer with the given size. Allocation errors should be cought by the ca...
Definition gmAppendBuffer.h:653
size_t _size
The size of this data buffer.
Definition gmAppendBuffer.h:667
ControllData * _prev
The previous buffer.
Definition gmAppendBuffer.h:670
size_t _offset
The offset of the first entry in data in the global buffer reference.
Definition gmAppendBuffer.h:668