event pool allocation debugging
[ardour2.git] / libs / ardour / session_events.cc
blob9c17c1cd12273dd1045815a1bd9aadb1ade4d56a
1 /*
2 Copyright (C) 1999-2004 Paul 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.
20 #include <cmath>
21 #include <unistd.h>
23 #include "ardour/timestamps.h"
25 #include "pbd/error.h"
26 #include "pbd/enumwriter.h"
28 #include "ardour/ardour.h"
29 #include "ardour/audio_diskstream.h"
30 #include "ardour/butler.h"
31 #include "ardour/debug.h"
32 #include "ardour/session_event.h"
34 #include "i18n.h"
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
40 PerThreadPool* SessionEvent::pool;
42 void
43 SessionEvent::init_event_pool ()
45 pool = new PerThreadPool;
48 void
49 SessionEvent::create_per_thread_pool (const std::string& name, uint32_t nitems)
51 /* this is a per-thread call that simply creates a thread-private ptr to
52 a CrossThreadPool for use by this thread whenever events are allocated/released
53 from SessionEvent::pool()
55 pool->create_per_thread_pool (name, sizeof (SessionEvent), nitems);
58 void *
59 SessionEvent::operator new (size_t)
61 CrossThreadPool* p = pool->per_thread_pool ();
62 SessionEvent* ev = static_cast<SessionEvent*> (p->alloc ());
63 cerr << "Allocating SessionEvent from " << p->name() << " ev @ " << ev << endl;
64 ev->own_pool = p;
65 return ev;
68 void
69 SessionEvent::operator delete (void *ptr, size_t /*size*/)
71 Pool* p = pool->per_thread_pool ();
72 SessionEvent* ev = static_cast<SessionEvent*> (ptr);
74 cerr << "Deleting SessionEvent @ " << ev << " thread pool = " << p->name() << " ev pool = " << ev->own_pool->name() << endl;
75 if (p == ev->own_pool) {
76 p->release (ptr);
77 } else {
78 ev->own_pool->push (ev);
82 void
83 SessionEventManager::add_event (framepos_t frame, SessionEvent::Type type, framepos_t target_frame)
85 SessionEvent* ev = new SessionEvent (type, SessionEvent::Add, frame, target_frame, 0);
86 queue_event (ev);
89 void
90 SessionEventManager::remove_event (framepos_t frame, SessionEvent::Type type)
92 SessionEvent* ev = new SessionEvent (type, SessionEvent::Remove, frame, 0, 0);
93 queue_event (ev);
96 void
97 SessionEventManager::replace_event (SessionEvent::Type type, framepos_t frame, framepos_t target)
99 SessionEvent* ev = new SessionEvent (type, SessionEvent::Replace, frame, target, 0);
100 queue_event (ev);
103 void
104 SessionEventManager::clear_events (SessionEvent::Type type)
106 SessionEvent* ev = new SessionEvent (type, SessionEvent::Clear, 0, 0, 0);
107 queue_event (ev);
111 void
112 SessionEventManager::dump_events () const
114 cerr << "EVENT DUMP" << endl;
115 for (Events::const_iterator i = events.begin(); i != events.end(); ++i) {
116 cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl;
118 cerr << "Next event: ";
120 if ((Events::const_iterator) next_event == events.end()) {
121 cerr << "none" << endl;
122 } else {
123 cerr << "at " << (*next_event)->action_frame << ' '
124 << (*next_event)->type << " target = "
125 << (*next_event)->target_frame << endl;
127 cerr << "Immediate events pending:\n";
128 for (Events::const_iterator i = immediate_events.begin(); i != immediate_events.end(); ++i) {
129 cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl;
131 cerr << "END EVENT_DUMP" << endl;
134 void
135 SessionEventManager::merge_event (SessionEvent* ev)
137 switch (ev->action) {
138 case SessionEvent::Remove:
139 _remove_event (ev);
140 delete ev;
141 return;
143 case SessionEvent::Replace:
144 _replace_event (ev);
145 return;
147 case SessionEvent::Clear:
148 _clear_event_type (ev->type);
149 delete ev;
150 return;
152 case SessionEvent::Add:
153 break;
156 /* try to handle immediate events right here */
158 if (ev->action_frame == 0) {
159 process_event (ev);
160 return;
163 switch (ev->type) {
164 case SessionEvent::AutoLoop:
165 case SessionEvent::StopOnce:
166 _clear_event_type (ev->type);
167 break;
169 default:
170 for (Events::iterator i = events.begin(); i != events.end(); ++i) {
171 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
172 error << string_compose(_("Session: cannot have two events of type %1 at the same frame (%2)."),
173 enum_2_string (ev->type), ev->action_frame) << endmsg;
174 return;
179 events.insert (events.begin(), ev);
180 events.sort (SessionEvent::compare);
181 next_event = events.begin();
182 set_next_event ();
185 /** @return true when @a ev is deleted. */
186 bool
187 SessionEventManager::_replace_event (SessionEvent* ev)
189 bool ret = false;
190 Events::iterator i;
192 /* private, used only for events that can only exist once in the queue */
194 for (i = events.begin(); i != events.end(); ++i) {
195 if ((*i)->type == ev->type) {
196 (*i)->action_frame = ev->action_frame;
197 (*i)->target_frame = ev->target_frame;
198 if ((*i) == ev) {
199 ret = true;
201 delete ev;
202 break;
206 if (i == events.end()) {
207 events.insert (events.begin(), ev);
210 events.sort (SessionEvent::compare);
211 next_event = events.end();
212 set_next_event ();
214 return ret;
217 /** @return true when @a ev is deleted. */
218 bool
219 SessionEventManager::_remove_event (SessionEvent* ev)
221 bool ret = false;
222 Events::iterator i;
224 for (i = events.begin(); i != events.end(); ++i) {
225 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
226 if ((*i) == ev) {
227 ret = true;
230 delete *i;
231 if (i == next_event) {
232 ++next_event;
234 events.erase (i);
235 break;
239 if (i != events.end()) {
240 set_next_event ();
243 return ret;
246 void
247 SessionEventManager::_clear_event_type (SessionEvent::Type type)
249 Events::iterator i, tmp;
251 for (i = events.begin(); i != events.end(); ) {
253 tmp = i;
254 ++tmp;
256 if ((*i)->type == type) {
257 delete *i;
258 if (i == next_event) {
259 ++next_event;
261 events.erase (i);
264 i = tmp;
267 for (i = immediate_events.begin(); i != immediate_events.end(); ) {
269 tmp = i;
270 ++tmp;
272 if ((*i)->type == type) {
273 delete *i;
274 immediate_events.erase (i);
277 i = tmp;
280 set_next_event ();