Ardour  9.0-pre0-582-g084a23a80d
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 #pragma once
23 
24 #include <string>
25 #include <vector>
26 
27 #include <glibmm/threads.h>
28 
29 #include "pbd/libpbd_visibility.h"
30 #include "pbd/ringbuffer.h"
31 
32 namespace PBD {
33 
34 typedef void (*PoolDumpCallback) (size_t, void*);
35 
40 {
41 public:
42  Pool (std::string name, unsigned long item_size, unsigned long nitems, PoolDumpCallback cb = NULL);
43  virtual ~Pool ();
44 
45  virtual void* alloc ();
46  virtual void release (void*);
47 
48  std::string name () const
49  {
50  return _name;
51  }
52  guint available () const
53  {
54  return free_list.read_space ();
55  }
56  guint used () const
57  {
58  return free_list.bufsize () - available ();
59  }
60  guint total () const
61  {
62  return free_list.bufsize ();
63  }
64 
65 protected:
67 
68  std::string _name;
69 
70 private:
71  void* _block;
73 #ifndef NDEBUG
74  unsigned long max_usage;
75 #endif
76 };
77 
79 {
80 public:
81  SingleAllocMultiReleasePool (std::string name, unsigned long item_size, unsigned long nitems);
83 
84  virtual void* alloc ();
85  virtual void release (void*);
86 
87 private:
88  Glib::Threads::Mutex m_lock;
89 };
90 
92 {
93 public:
94  MultiAllocSingleReleasePool (std::string name, unsigned long item_size, unsigned long nitems);
96 
97  virtual void* alloc ();
98  virtual void release (void*);
99 
100 private:
101  Glib::Threads::Mutex m_lock;
102 };
103 
105 
119 {
120 public:
121  CrossThreadPool (std::string n, unsigned long isize, unsigned long nitems, PerThreadPool*, PoolDumpCallback);
122 
123  void* alloc ();
124  void push (void*);
125 
127  {
128  return _parent;
129  }
130 
131  bool empty ();
132  guint pending_size () const
133  {
134  return pending.read_space ();
135  }
136 
137  void flush_pending ();
138  void flush_pending_with_ev (void*);
139 
140 private:
143 };
144 
149 {
150 public:
152 
153  const Glib::Threads::Private<CrossThreadPool>& key () const
154  {
155  return _key;
156  }
157 
158  void create_per_thread_pool (std::string name, unsigned long item_size, unsigned long nitems, PoolDumpCallback cb = NULL);
159 
160  CrossThreadPool* per_thread_pool (bool must_exist = true);
161 
165 
166 private:
167  Glib::Threads::Private<CrossThreadPool> _key;
168  std::string _name;
169 
171  Glib::Threads::Mutex _trash_mutex;
173 };
174 
175 } // namespace PBD
176 
PerThreadPool * parent() const
Definition: pool.h:126
PBD::RingBuffer< void * > pending
Definition: pool.h:141
PerThreadPool * _parent
Definition: pool.h:142
guint pending_size() const
Definition: pool.h:132
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:101
virtual void release(void *)
PBD::RingBuffer< CrossThreadPool * > * _trash
Definition: pool.h:172
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:153
Glib::Threads::Mutex _trash_mutex
Definition: pool.h:171
std::string _name
Definition: pool.h:168
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:167
Definition: pool.h:40
PBD::RingBuffer< void * > free_list
a list of pointers to free items within block
Definition: pool.h:66
guint available() const
Definition: pool.h:52
std::string name() const
Definition: pool.h:48
guint total() const
Definition: pool.h:60
virtual void * alloc()
Pool(std::string name, unsigned long item_size, unsigned long nitems, PoolDumpCallback cb=NULL)
guint used() const
Definition: pool.h:56
virtual void release(void *)
virtual ~Pool()
PoolDumpCallback _dump
callback to print pool contents
Definition: pool.h:72
void * _block
data storage area
Definition: pool.h:71
unsigned long max_usage
Definition: pool.h:74
std::string _name
Definition: pool.h:68
Glib::Threads::Mutex m_lock
Definition: pool.h:88
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:34