2 Copyright (C) 1998-99 Paul Barton-Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <glibmm/threads.h>
28 #include "pbd/ringbuffer.h"
30 /** A pool of data items that can be allocated, read from and written to
31 * without system memory allocation or locking.
36 Pool (std::string name, unsigned long item_size, unsigned long nitems);
39 virtual void *alloc ();
40 virtual void release (void *);
42 std::string name() const { return _name; }
45 RingBuffer<void*> free_list; ///< a list of pointers to free items within block
49 void *block; ///< data storage area
52 class SingleAllocMultiReleasePool : public Pool
55 SingleAllocMultiReleasePool (std::string name, unsigned long item_size, unsigned long nitems);
56 ~SingleAllocMultiReleasePool ();
58 virtual void *alloc ();
59 virtual void release (void *);
62 Glib::Threads::Mutex m_lock;
66 class MultiAllocSingleReleasePool : public Pool
69 MultiAllocSingleReleasePool (std::string name, unsigned long item_size, unsigned long nitems);
70 ~MultiAllocSingleReleasePool ();
72 virtual void *alloc ();
73 virtual void release (void *);
76 Glib::Threads::Mutex m_lock;
81 /** Management of a per-thread pool of data that is allocated by one thread and
82 * freed by one other thread. Not safe for use when there is more than 1
83 * reader and 1 writer.
85 * This is basically a wrapper around a thread-local storage instance of a
86 * ringbuffer, made safe for use in the case where multiple threads allocate
87 * from the ringbuffer and a single thread "frees" the allocations.
89 * Rather than using locks, each thread has its own ringbuffer (and associated
90 * data), and so it calls alloc(), passes a pointer to the result of the alloc
91 * to another thread, which later calls push() to "free" it.
93 class CrossThreadPool : public Pool
96 CrossThreadPool (std::string n, unsigned long isize, unsigned long nitems, PerThreadPool *);
101 PerThreadPool* parent () const {
108 RingBuffer<void*> pending;
109 PerThreadPool* _parent;
112 /** A class to manage per-thread pools of memory. One object of this class is instantiated,
113 * and then it is used to create per-thread pools for 1 or more threads as required.
120 const Glib::Threads::Private<CrossThreadPool>& key() const { return _key; }
122 void create_per_thread_pool (std::string name, unsigned long item_size, unsigned long nitems);
123 CrossThreadPool* per_thread_pool ();
125 void set_trash (RingBuffer<CrossThreadPool*>* t);
126 void add_to_trash (CrossThreadPool *);
129 Glib::Threads::Private<CrossThreadPool> _key;
131 unsigned long _item_size;
132 unsigned long _nitems;
134 /** mutex to protect either changes to the _trash variable, or writes to the RingBuffer */
135 Glib::Threads::Mutex _trash_mutex;
136 RingBuffer<CrossThreadPool*>* _trash;
139 #endif // __qm_pool_h__