13
0

NO-OP: clang-format

This commit is contained in:
Robin Gareus 2022-07-07 04:47:08 +02:00
parent a8a4695466
commit 8418e7c954
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
2 changed files with 80 additions and 68 deletions

View File

@ -22,42 +22,53 @@
#ifndef __qm_pool_h__
#define __qm_pool_h__
#include <vector>
#include <string>
#include <vector>
#include <glibmm/threads.h>
#include "pbd/libpbd_visibility.h"
#include "pbd/ringbuffer.h"
typedef void (*PoolDumpCallback)(size_t, void*);
typedef void (*PoolDumpCallback) (size_t, void*);
/** A pool of data items that can be allocated, read from and written to
* without system memory allocation or locking.
*/
class LIBPBD_API Pool
{
public:
public:
Pool (std::string name, unsigned long item_size, unsigned long nitems, PoolDumpCallback cb = NULL);
virtual ~Pool ();
virtual void *alloc ();
virtual void release (void *);
virtual void* alloc ();
virtual void release (void*);
std::string name() const { return _name; }
guint available() const { return free_list.read_space(); }
guint used() const { return free_list.bufsize() - available(); }
guint total() const { return free_list.bufsize(); }
protected:
std::string name () const
{
return _name;
}
guint available () const
{
return free_list.read_space ();
}
guint used () const
{
return free_list.bufsize () - available ();
}
guint total () const
{
return free_list.bufsize ();
}
protected:
PBD::RingBuffer<void*> free_list; ///< a list of pointers to free items within block
std::string _name;
private:
private:
void* _block; ///< data storage area
PoolDumpCallback _dump; ///< callback to print pool contents
PoolDumpCallback _dump; ///< callback to print pool contents
#ifndef NDEBUG
unsigned long max_usage;
#endif
@ -65,29 +76,28 @@ class LIBPBD_API Pool
class LIBPBD_API SingleAllocMultiReleasePool : public Pool
{
public:
public:
SingleAllocMultiReleasePool (std::string name, unsigned long item_size, unsigned long nitems);
~SingleAllocMultiReleasePool ();
virtual void *alloc ();
virtual void release (void *);
virtual void* alloc ();
virtual void release (void*);
private:
Glib::Threads::Mutex m_lock;
private:
Glib::Threads::Mutex m_lock;
};
class LIBPBD_API MultiAllocSingleReleasePool : public Pool
{
public:
public:
MultiAllocSingleReleasePool (std::string name, unsigned long item_size, unsigned long nitems);
~MultiAllocSingleReleasePool ();
virtual void *alloc ();
virtual void release (void *);
virtual void* alloc ();
virtual void release (void*);
private:
Glib::Threads::Mutex m_lock;
private:
Glib::Threads::Mutex m_lock;
};
class LIBPBD_API PerThreadPool;
@ -106,25 +116,29 @@ class LIBPBD_API PerThreadPool;
*/
class LIBPBD_API CrossThreadPool : public Pool
{
public:
public:
CrossThreadPool (std::string n, unsigned long isize, unsigned long nitems, PerThreadPool*, PoolDumpCallback);
void* alloc ();
void push (void *);
void push (void*);
PerThreadPool* parent () const {
PerThreadPool* parent () const
{
return _parent;
}
bool empty ();
guint pending_size() const { return pending.read_space(); }
bool empty ();
guint pending_size () const
{
return pending.read_space ();
}
void flush_pending ();
void flush_pending_with_ev (void*);
private:
private:
PBD::RingBuffer<void*> pending;
PerThreadPool* _parent;
PerThreadPool* _parent;
};
/** A class to manage per-thread pools of memory. One object of this class is instantiated,
@ -135,21 +149,25 @@ class LIBPBD_API PerThreadPool
public:
PerThreadPool ();
const Glib::Threads::Private<CrossThreadPool>& key() const { return _key; }
const Glib::Threads::Private<CrossThreadPool>& key () const
{
return _key;
}
void create_per_thread_pool (std::string name, unsigned long item_size, unsigned long nitems, PoolDumpCallback cb = NULL);
CrossThreadPool* per_thread_pool (bool must_exist = true);
bool has_per_thread_pool ();
void set_trash (PBD::RingBuffer<CrossThreadPool*>* t);
void add_to_trash (CrossThreadPool *);
void add_to_trash (CrossThreadPool*);
private:
Glib::Threads::Private<CrossThreadPool> _key;
std::string _name;
std::string _name;
/** mutex to protect either changes to the _trash variable, or writes to the RingBuffer */
Glib::Threads::Mutex _trash_mutex;
Glib::Threads::Mutex _trash_mutex;
PBD::RingBuffer<CrossThreadPool*>* _trash;
};

View File

@ -20,17 +20,16 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstdlib>
#include <cassert>
#include "pbd/compose.h"
#include "pbd/debug.h"
#include "pbd/error.h"
#include "pbd/pool.h"
#include "pbd/pthread_utils.h"
#include "pbd/error.h"
#include "pbd/debug.h"
#include "pbd/compose.h"
#include "pbd/stacktrace.h"
using namespace std;
@ -55,10 +54,10 @@ Pool::Pool (string n, unsigned long item_size, unsigned long nitems, PoolDumpCal
_block = malloc (nitems * item_size);
void **ptrlist = (void **) calloc (nitems, sizeof (void *));
void** ptrlist = (void**)calloc (nitems, sizeof (void*));
for (unsigned long i = 0; i < nitems; i++) {
ptrlist[i] = static_cast<void *> (static_cast<char*>(_block) + (i * item_size));
ptrlist[i] = static_cast<void*> (static_cast<char*> (_block) + (i * item_size));
}
free_list.write (ptrlist, nitems);
@ -67,17 +66,17 @@ Pool::Pool (string n, unsigned long item_size, unsigned long nitems, PoolDumpCal
Pool::~Pool ()
{
DEBUG_TRACE (DEBUG::Pool, string_compose ("Pool: '%1' max: %2 / %3\n", name(), max_usage, total()));
DEBUG_TRACE (DEBUG::Pool, string_compose ("Pool: '%1' max: %2 / %3\n", name (), max_usage, total ()));
free (_block);
}
/** Allocate an item's worth of memory in the Pool by taking one from the free list.
* @return Pointer to free item.
*/
void *
void*
Pool::alloc ()
{
void *ptr;
void* ptr;
#ifndef NDEBUG
if (used () > max_usage) {
@ -97,7 +96,7 @@ Pool::alloc ()
}
fatal << "CRITICAL: " << _name << " POOL OUT OF MEMORY - RECOMPILE WITH LARGER SIZE!!" << endmsg;
abort(); /*NOTREACHED*/
abort (); /*NOTREACHED*/
return 0;
} else {
return ptr;
@ -106,7 +105,7 @@ Pool::alloc ()
/** Release an item's memory by writing its location to the free list */
void
Pool::release (void *ptr)
Pool::release (void* ptr)
{
free_list.write (&ptr, 1);
}
@ -134,10 +133,8 @@ SingleAllocMultiReleasePool::~SingleAllocMultiReleasePool ()
void*
MultiAllocSingleReleasePool::alloc ()
{
void *ptr;
Glib::Threads::Mutex::Lock guard (m_lock);
ptr = Pool::alloc ();
return ptr;
return Pool::alloc ();
}
void
@ -171,7 +168,7 @@ free_per_thread_pool (void* ptr)
CrossThreadPool* cp = static_cast<CrossThreadPool*> (ptr);
assert (cp);
if (cp->empty()) {
if (cp->empty ()) {
/* This CrossThreadPool is already empty, and the thread is finishing so nothing
* more can be added to it. We can just delete the pool.
*/
@ -181,7 +178,7 @@ free_per_thread_pool (void* ptr)
* which another thread may yet read, so we can't delete the pool just yet.
* Put it in the trash and hope someone deals with it at some stage.
*/
cp->parent()->add_to_trash (cp);
cp->parent ()->add_to_trash (cp);
}
}
@ -208,24 +205,23 @@ PerThreadPool::create_per_thread_pool (string n, unsigned long isize, unsigned l
bool
PerThreadPool::has_per_thread_pool ()
{
CrossThreadPool* p = _key.get();
CrossThreadPool* p = _key.get ();
if (p) {
return true;
}
return false;
}
/** @return CrossThreadPool for the current thread, which must previously have been created by
* calling create_per_thread_pool in the current thread.
*/
CrossThreadPool*
PerThreadPool::per_thread_pool (bool must_exist)
{
CrossThreadPool* p = _key.get();
CrossThreadPool* p = _key.get ();
if (!p && must_exist) {
fatal << "programming error: no per-thread pool \"" << _name << "\" for thread " << pthread_name() << endmsg;
abort(); /*NOTREACHED*/
fatal << "programming error: no per-thread pool \"" << _name << "\" for thread " << pthread_name () << endmsg;
abort (); /*NOTREACHED*/
}
return p;
}
@ -244,7 +240,7 @@ PerThreadPool::add_to_trash (CrossThreadPool* p)
Glib::Threads::Mutex::Lock lm (_trash_mutex);
if (!_trash) {
warning << "Pool " << p->name() << " has no trash collector; a memory leak has therefore occurred" << endmsg;
warning << "Pool " << p->name () << " has no trash collector; a memory leak has therefore occurred" << endmsg;
return;
}
@ -255,16 +251,15 @@ PerThreadPool::add_to_trash (CrossThreadPool* p)
_trash->write (&p, 1);
}
CrossThreadPool::CrossThreadPool (string n, unsigned long isize, unsigned long nitems, PerThreadPool* p, PoolDumpCallback cb)
CrossThreadPool::CrossThreadPool (string n, unsigned long isize, unsigned long nitems, PerThreadPool* p, PoolDumpCallback cb)
: Pool (n, isize, nitems, cb)
, pending (nitems)
, _parent (p)
{
}
void
CrossThreadPool::flush_pending_with_ev (void *ptr)
CrossThreadPool::flush_pending_with_ev (void* ptr)
{
push (ptr);
flush_pending ();
@ -274,19 +269,19 @@ void
CrossThreadPool::flush_pending ()
{
void* ptr;
bool did_release = false;
bool did_release = false;
DEBUG_TRACE (DEBUG::Pool, string_compose ("%1 %2 has %3 pending free entries waiting, status size %4 free %5 used %6\n", pthread_name(), name(), pending.read_space(),
total(), available(), used()));
DEBUG_TRACE (DEBUG::Pool, string_compose ("%1 %2 has %3 pending free entries waiting, status size %4 free %5 used %6\n", pthread_name (), name (), pending.read_space (),
total (), available (), used ()));
while (pending.read (&ptr, 1) == 1) {
DEBUG_TRACE (DEBUG::Pool, string_compose ("%1 %2 pushes back a pending free list entry before allocating\n", pthread_name(), name()));
DEBUG_TRACE (DEBUG::Pool, string_compose ("%1 %2 pushes back a pending free list entry before allocating\n", pthread_name (), name ()));
free_list.write (&ptr, 1);
did_release = true;
}
if (did_release) {
DEBUG_TRACE (DEBUG::Pool, string_compose ("Pool size: %1 free %2 used %3 pending now %4\n", total(), available(), used(), pending_size()));
DEBUG_TRACE (DEBUG::Pool, string_compose ("Pool size: %1 free %2 used %3 pending now %4\n", total (), available (), used (), pending_size ()));
}
}
@ -309,6 +304,5 @@ CrossThreadPool::push (void* t)
bool
CrossThreadPool::empty ()
{
return (free_list.write_space() == pending.read_space());
return (free_list.write_space () == pending.read_space ());
}