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

View File

@ -20,17 +20,16 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
#include <cassert>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <vector> #include <vector>
#include <cstdlib>
#include <cassert>
#include "pbd/compose.h"
#include "pbd/debug.h"
#include "pbd/error.h"
#include "pbd/pool.h" #include "pbd/pool.h"
#include "pbd/pthread_utils.h" #include "pbd/pthread_utils.h"
#include "pbd/error.h"
#include "pbd/debug.h"
#include "pbd/compose.h"
#include "pbd/stacktrace.h" #include "pbd/stacktrace.h"
using namespace std; using namespace std;
@ -55,10 +54,10 @@ Pool::Pool (string n, unsigned long item_size, unsigned long nitems, PoolDumpCal
_block = malloc (nitems * item_size); _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++) { 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); free_list.write (ptrlist, nitems);
@ -67,17 +66,17 @@ Pool::Pool (string n, unsigned long item_size, unsigned long nitems, PoolDumpCal
Pool::~Pool () 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); free (_block);
} }
/** Allocate an item's worth of memory in the Pool by taking one from the free list. /** Allocate an item's worth of memory in the Pool by taking one from the free list.
* @return Pointer to free item. * @return Pointer to free item.
*/ */
void * void*
Pool::alloc () Pool::alloc ()
{ {
void *ptr; void* ptr;
#ifndef NDEBUG #ifndef NDEBUG
if (used () > max_usage) { if (used () > max_usage) {
@ -97,7 +96,7 @@ Pool::alloc ()
} }
fatal << "CRITICAL: " << _name << " POOL OUT OF MEMORY - RECOMPILE WITH LARGER SIZE!!" << endmsg; fatal << "CRITICAL: " << _name << " POOL OUT OF MEMORY - RECOMPILE WITH LARGER SIZE!!" << endmsg;
abort(); /*NOTREACHED*/ abort (); /*NOTREACHED*/
return 0; return 0;
} else { } else {
return ptr; return ptr;
@ -106,7 +105,7 @@ Pool::alloc ()
/** Release an item's memory by writing its location to the free list */ /** Release an item's memory by writing its location to the free list */
void void
Pool::release (void *ptr) Pool::release (void* ptr)
{ {
free_list.write (&ptr, 1); free_list.write (&ptr, 1);
} }
@ -134,10 +133,8 @@ SingleAllocMultiReleasePool::~SingleAllocMultiReleasePool ()
void* void*
MultiAllocSingleReleasePool::alloc () MultiAllocSingleReleasePool::alloc ()
{ {
void *ptr;
Glib::Threads::Mutex::Lock guard (m_lock); Glib::Threads::Mutex::Lock guard (m_lock);
ptr = Pool::alloc (); return Pool::alloc ();
return ptr;
} }
void void
@ -171,7 +168,7 @@ free_per_thread_pool (void* ptr)
CrossThreadPool* cp = static_cast<CrossThreadPool*> (ptr); CrossThreadPool* cp = static_cast<CrossThreadPool*> (ptr);
assert (cp); assert (cp);
if (cp->empty()) { if (cp->empty ()) {
/* This CrossThreadPool is already empty, and the thread is finishing so nothing /* This CrossThreadPool is already empty, and the thread is finishing so nothing
* more can be added to it. We can just delete the pool. * 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. * 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. * 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 bool
PerThreadPool::has_per_thread_pool () PerThreadPool::has_per_thread_pool ()
{ {
CrossThreadPool* p = _key.get(); CrossThreadPool* p = _key.get ();
if (p) { if (p) {
return true; return true;
} }
return false; return false;
} }
/** @return CrossThreadPool for the current thread, which must previously have been created by /** @return CrossThreadPool for the current thread, which must previously have been created by
* calling create_per_thread_pool in the current thread. * calling create_per_thread_pool in the current thread.
*/ */
CrossThreadPool* CrossThreadPool*
PerThreadPool::per_thread_pool (bool must_exist) PerThreadPool::per_thread_pool (bool must_exist)
{ {
CrossThreadPool* p = _key.get(); CrossThreadPool* p = _key.get ();
if (!p && must_exist) { if (!p && must_exist) {
fatal << "programming error: no per-thread pool \"" << _name << "\" for thread " << pthread_name() << endmsg; fatal << "programming error: no per-thread pool \"" << _name << "\" for thread " << pthread_name () << endmsg;
abort(); /*NOTREACHED*/ abort (); /*NOTREACHED*/
} }
return p; return p;
} }
@ -244,7 +240,7 @@ PerThreadPool::add_to_trash (CrossThreadPool* p)
Glib::Threads::Mutex::Lock lm (_trash_mutex); Glib::Threads::Mutex::Lock lm (_trash_mutex);
if (!_trash) { 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; return;
} }
@ -255,16 +251,15 @@ PerThreadPool::add_to_trash (CrossThreadPool* p)
_trash->write (&p, 1); _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) : Pool (n, isize, nitems, cb)
, pending (nitems) , pending (nitems)
, _parent (p) , _parent (p)
{ {
} }
void void
CrossThreadPool::flush_pending_with_ev (void *ptr) CrossThreadPool::flush_pending_with_ev (void* ptr)
{ {
push (ptr); push (ptr);
flush_pending (); flush_pending ();
@ -274,19 +269,19 @@ void
CrossThreadPool::flush_pending () CrossThreadPool::flush_pending ()
{ {
void* ptr; 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(), 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())); total (), available (), used ()));
while (pending.read (&ptr, 1) == 1) { 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); free_list.write (&ptr, 1);
did_release = true; did_release = true;
} }
if (did_release) { 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 bool
CrossThreadPool::empty () CrossThreadPool::empty ()
{ {
return (free_list.write_space() == pending.read_space()); return (free_list.write_space () == pending.read_space ());
} }