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/thread.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 *);
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 *);
81 /** A per-thread pool of data */
82 class CrossThreadPool : public Pool
85 CrossThreadPool (std::string n, unsigned long isize, unsigned long nitems, PerThreadPool *);
90 PerThreadPool* parent () const {
97 RingBuffer<void*> pending;
98 PerThreadPool* _parent;
101 /** A class to manage per-thread pools of memory. One object of this class is instantiated,
102 * and then it is used to create per-thread pools as required.
109 GPrivate* key() const { return _key; }
111 void create_per_thread_pool (std::string name, unsigned long item_size, unsigned long nitems);
112 CrossThreadPool* per_thread_pool ();
114 void set_trash (RingBuffer<CrossThreadPool*>* t);
115 void add_to_trash (CrossThreadPool *);
120 unsigned long _item_size;
121 unsigned long _nitems;
123 /** mutex to protect either changes to the _trash variable, or writes to the RingBuffer */
124 Glib::Mutex _trash_mutex;
125 RingBuffer<CrossThreadPool*>* _trash;
128 #endif // __qm_pool_h__