Ardour  8.12
pool.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2015 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2009-2014 David Robillard <d@drobilla.net>
4  * Copyright (C) 2010 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2015-2022 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifndef __qm_pool_h__
23 #define __qm_pool_h__
24 
25 #include <string>
26 #include <vector>
27 
28 #include <glibmm/threads.h>
29 
30 #include "pbd/libpbd_visibility.h"
31 #include "pbd/ringbuffer.h"
32 
33 namespace PBD {
34 
35 typedef void (*PoolDumpCallback) (size_t, void*);
36 
41 {
42 public:
43  Pool (std::string name, unsigned long item_size, unsigned long nitems, PoolDumpCallback cb = NULL);
44  virtual ~Pool ();
45 
46  virtual void* alloc ();
47  virtual void release (void*);
48 
49  std::string name () const
50  {
51  return _name;
52  }
53  guint available () const
54  {
55  return free_list.read_space ();
56  }
57  guint used () const
58  {
59  return free_list.bufsize () - available ();
60  }
61  guint total () const
62  {
63  return free_list.bufsize ();
64  }
65 
66 protected:
68 
69  std::string _name;
70 
71 private:
72  void* _block;
74 #ifndef NDEBUG
75  unsigned long max_usage;
76 #endif
77 };
78 
80 {
81 public:
82  SingleAllocMultiReleasePool (std::string name, unsigned long item_size, unsigned long nitems);
84 
85  virtual void* alloc ();
86  virtual void release (void*);
87 
88 private:
89  Glib::Threads::Mutex m_lock;
90 };
91 
93 {
94 public:
95  MultiAllocSingleReleasePool (std::string name, unsigned long item_size, unsigned long nitems);
97 
98  virtual void* alloc ();
99  virtual void release (void*);
100 
101 private:
102  Glib::Threads::Mutex m_lock;
103 };
104 
106 
120 {
121 public:
122  CrossThreadPool (std::string n, unsigned long isize, unsigned long nitems, PerThreadPool*, PoolDumpCallback);
123 
124  void* alloc ();
125  void push (void*);
126 
128  {
129  return _parent;
130  }
131 
132  bool empty ();
133  guint pending_size () const
134  {
135  return pending.read_space ();
136  }
137 
138  void flush_pending ();
139  void flush_pending_with_ev (void*);
140 
141 private:
144 };
145 
150 {
151 public:
153 
154  const Glib::Threads::Private<CrossThreadPool>& key () const
155  {
156  return _key;
157  }
158 
159  void create_per_thread_pool (std::string name, unsigned long item_size, unsigned long nitems, PoolDumpCallback cb = NULL);
160 
161  CrossThreadPool* per_thread_pool (bool must_exist = true);
162 
166 
167 private:
168  Glib::Threads::Private<CrossThreadPool> _key;
169  std::string _name;
170 
172  Glib::Threads::Mutex _trash_mutex;
174 };
175 
176 } // namespace PBD
177 
178 #endif // __qm_pool_h__
PerThreadPool * parent() const
Definition: pool.h:127
PBD::RingBuffer< void * > pending
Definition: pool.h:142
PerThreadPool * _parent
Definition: pool.h:143
guint pending_size() const
Definition: pool.h:133
CrossThreadPool(std::string n, unsigned long isize, unsigned long nitems, PerThreadPool *, PoolDumpCallback)
void flush_pending_with_ev(void *)
MultiAllocSingleReleasePool(std::string name, unsigned long item_size, unsigned long nitems)
Glib::Threads::Mutex m_lock
Definition: pool.h:102
virtual void release(void *)
PBD::RingBuffer< CrossThreadPool * > * _trash
Definition: pool.h:173
bool has_per_thread_pool()
void create_per_thread_pool(std::string name, unsigned long item_size, unsigned long nitems, PoolDumpCallback cb=NULL)
const Glib::Threads::Private< CrossThreadPool > & key() const
Definition: pool.h:154
Glib::Threads::Mutex _trash_mutex
Definition: pool.h:172
std::string _name
Definition: pool.h:169
void add_to_trash(CrossThreadPool *)
void set_trash(PBD::RingBuffer< CrossThreadPool * > *t)
CrossThreadPool * per_thread_pool(bool must_exist=true)
Glib::Threads::Private< CrossThreadPool > _key
Definition: pool.h:168
Definition: pool.h:41
PBD::RingBuffer< void * > free_list
a list of pointers to free items within block
Definition: pool.h:67
guint available() const
Definition: pool.h:53
std::string name() const
Definition: pool.h:49
guint total() const
Definition: pool.h:61
virtual void * alloc()
Pool(std::string name, unsigned long item_size, unsigned long nitems, PoolDumpCallback cb=NULL)
guint used() const
Definition: pool.h:57
virtual void release(void *)
virtual ~Pool()
PoolDumpCallback _dump
callback to print pool contents
Definition: pool.h:73
void * _block
data storage area
Definition: pool.h:72
unsigned long max_usage
Definition: pool.h:75
std::string _name
Definition: pool.h:69
Glib::Threads::Mutex m_lock
Definition: pool.h:89
SingleAllocMultiReleasePool(std::string name, unsigned long item_size, unsigned long nitems)
virtual void release(void *)
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBPBD_API
Definition: axis_view.h:42
void(* PoolDumpCallback)(size_t, void *)
Definition: pool.h:35