Formatted all .cc and .h code in rtengine, rtexif and rtgui using astyle
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
*
|
||||
* RawTherapee is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
@@ -25,7 +25,8 @@
|
||||
#include "../rtgui/threadutils.h"
|
||||
|
||||
// Aligned buffer that should be faster
|
||||
template <class T> class AlignedBuffer {
|
||||
template <class T> class AlignedBuffer
|
||||
{
|
||||
|
||||
private:
|
||||
void* real ;
|
||||
@@ -37,23 +38,29 @@ public:
|
||||
T* data ;
|
||||
bool inUse;
|
||||
|
||||
/** @brief Allocate aligned memory
|
||||
* @param size Number of elements of size T to allocate, i.e. allocated size will be sizeof(T)*size ; set it to 0 if you want to defer the allocation
|
||||
* @param align Expressed in bytes; SSE instructions need 128 bits alignment, which mean 16 bytes, which is the default value
|
||||
*/
|
||||
AlignedBuffer (size_t size=0, size_t align=16) : real(NULL), alignment(align), allocatedSize(0), unitSize(0), data(NULL), inUse(false) {
|
||||
if (size)
|
||||
/** @brief Allocate aligned memory
|
||||
* @param size Number of elements of size T to allocate, i.e. allocated size will be sizeof(T)*size ; set it to 0 if you want to defer the allocation
|
||||
* @param align Expressed in bytes; SSE instructions need 128 bits alignment, which mean 16 bytes, which is the default value
|
||||
*/
|
||||
AlignedBuffer (size_t size = 0, size_t align = 16) : real(NULL), alignment(align), allocatedSize(0), unitSize(0), data(NULL), inUse(false)
|
||||
{
|
||||
if (size) {
|
||||
resize(size);
|
||||
}
|
||||
}
|
||||
|
||||
~AlignedBuffer () {
|
||||
if (real) free(real);
|
||||
~AlignedBuffer ()
|
||||
{
|
||||
if (real) {
|
||||
free(real);
|
||||
}
|
||||
}
|
||||
|
||||
/** @brief Return true if there's no memory allocated
|
||||
*/
|
||||
bool isEmpty() {
|
||||
return allocatedSize==0;
|
||||
bool isEmpty()
|
||||
{
|
||||
return allocatedSize == 0;
|
||||
}
|
||||
|
||||
/** @brief Allocate the the "size" amount of elements of "structSize" length each
|
||||
@@ -61,39 +68,44 @@ public:
|
||||
* @param structSize if non null, will let you override the default struct's size (unit: byte)
|
||||
* @return True is everything went fine, including freeing memory when size==0, false if the allocation failed
|
||||
*/
|
||||
bool resize(size_t size, int structSize=0) {
|
||||
bool resize(size_t size, int structSize = 0)
|
||||
{
|
||||
if (allocatedSize != size) {
|
||||
if (!size) {
|
||||
// The user want to free the memory
|
||||
if (real) free(real);
|
||||
if (real) {
|
||||
free(real);
|
||||
}
|
||||
|
||||
real = NULL;
|
||||
data = NULL;
|
||||
inUse = false;
|
||||
allocatedSize = 0;
|
||||
unitSize = 0;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
unitSize = structSize ? structSize : sizeof(T);
|
||||
size_t oldAllocatedSize = allocatedSize;
|
||||
allocatedSize = size*unitSize;
|
||||
allocatedSize = size * unitSize;
|
||||
|
||||
// realloc were used here to limit memory fragmentation, specially when the size was smaller than the previous one.
|
||||
// But realloc copies the content to the eventually new location, which is unnecessary. To avoid this performance penalty,
|
||||
// we're freeing the memory and allocate it again if the new size is bigger.
|
||||
|
||||
if (allocatedSize < oldAllocatedSize)
|
||||
real = realloc(real, allocatedSize+alignment);
|
||||
else {
|
||||
if (real) free (real);
|
||||
real = malloc(allocatedSize+alignment);
|
||||
if (allocatedSize < oldAllocatedSize) {
|
||||
real = realloc(real, allocatedSize + alignment);
|
||||
} else {
|
||||
if (real) {
|
||||
free (real);
|
||||
}
|
||||
|
||||
real = malloc(allocatedSize + alignment);
|
||||
}
|
||||
|
||||
if (real) {
|
||||
//data = (T*)( (uintptr_t)real + (alignment-((uintptr_t)real)%alignment) );
|
||||
data = (T*)( ( uintptr_t(real) + uintptr_t(alignment-1)) / alignment * alignment);
|
||||
data = (T*)( ( uintptr_t(real) + uintptr_t(alignment - 1)) / alignment * alignment);
|
||||
inUse = true;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
allocatedSize = 0;
|
||||
unitSize = 0;
|
||||
data = NULL;
|
||||
@@ -102,10 +114,12 @@ public:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void swap(AlignedBuffer<T> &other) {
|
||||
void swap(AlignedBuffer<T> &other)
|
||||
{
|
||||
void *tmpReal = other.real;
|
||||
other.real = real;
|
||||
real = tmpReal;
|
||||
@@ -127,49 +141,57 @@ public:
|
||||
inUse = tmpInUse;
|
||||
}
|
||||
|
||||
unsigned int getSize() {
|
||||
return unitSize ? allocatedSize/unitSize : 0;
|
||||
unsigned int getSize()
|
||||
{
|
||||
return unitSize ? allocatedSize / unitSize : 0;
|
||||
}
|
||||
};
|
||||
|
||||
// Multi processor version, use with OpenMP
|
||||
template <class T> class AlignedBufferMP {
|
||||
template <class T> class AlignedBufferMP
|
||||
{
|
||||
private:
|
||||
MyMutex mtx;
|
||||
std::vector<AlignedBuffer<T>*> buffers;
|
||||
size_t size;
|
||||
|
||||
public:
|
||||
AlignedBufferMP(size_t sizeP) {
|
||||
size=sizeP;
|
||||
AlignedBufferMP(size_t sizeP)
|
||||
{
|
||||
size = sizeP;
|
||||
}
|
||||
|
||||
~AlignedBufferMP() {
|
||||
for (size_t i=0;i<buffers.size();i++) delete buffers[i];
|
||||
~AlignedBufferMP()
|
||||
{
|
||||
for (size_t i = 0; i < buffers.size(); i++) {
|
||||
delete buffers[i];
|
||||
}
|
||||
}
|
||||
|
||||
AlignedBuffer<T>* acquire() {
|
||||
AlignedBuffer<T>* acquire()
|
||||
{
|
||||
MyMutex::MyLock lock(mtx);
|
||||
|
||||
// Find available buffer
|
||||
for (size_t i=0;i<buffers.size();i++) {
|
||||
for (size_t i = 0; i < buffers.size(); i++) {
|
||||
if (!buffers[i]->inUse) {
|
||||
buffers[i]->inUse=true;
|
||||
buffers[i]->inUse = true;
|
||||
return buffers[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Add new buffer if nothing is free
|
||||
AlignedBuffer<T>* buffer=new AlignedBuffer<T>(size);
|
||||
AlignedBuffer<T>* buffer = new AlignedBuffer<T>(size);
|
||||
buffers.push_back(buffer);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void release(AlignedBuffer<T>* buffer) {
|
||||
MyMutex::MyLock lock(mtx);
|
||||
void release(AlignedBuffer<T>* buffer)
|
||||
{
|
||||
MyMutex::MyLock lock(mtx);
|
||||
|
||||
buffer->inUse=false;
|
||||
buffer->inUse = false;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user