1 /*
2  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>
3  *     economic rights: Technische Universität Dresden (Germany)
4  *
5  * This file is part of TUD:OS and distributed under the terms of the
6  * GNU General Public License 2.
7  * Please see the COPYING-GPL-2 file for details.
8  *
9  * As a special exception, you may use this file as part of a free software
10  * library without restriction.  Specifically, if other files instantiate
11  * templates or use macros or inline functions from this file, or you compile
12  * this file and link it with other files to produce an executable, this
13  * file does not by itself cause the resulting executable to be covered by
14  * the GNU General Public License.  This exception does not however
15  * invalidate any other reasons why the executable file might be covered by
16  * the GNU General Public License.
17  */
18 #include <l4/re/env>
19 #include <l4/re/util/event_buffer>
20 #include <l4/re/c/event_buffer.h>
21 
22 using L4Re::Util::Event_buffer_consumer;
23 
cast(l4re_event_buffer_consumer_t * e)24 static inline Event_buffer_consumer *cast(l4re_event_buffer_consumer_t *e) throw()
25 {
26   (void)sizeof(char[sizeof(e->_obj_buf) - sizeof(Event_buffer_consumer)]);
27   return (Event_buffer_consumer *)e->_obj_buf;
28 }
29 
operator new(size_t,void * a)30 inline void *operator new(size_t, void *a) throw() { return a; }
31 
32 L4_CV long
l4re_event_buffer_attach(l4re_event_buffer_consumer_t * evbuf,l4re_ds_t ds,l4_cap_idx_t rm)33 l4re_event_buffer_attach(l4re_event_buffer_consumer_t *evbuf,
34                          l4re_ds_t ds, l4_cap_idx_t rm) L4_NOTHROW
35 {
36   L4::Cap<L4Re::Dataspace> d(ds);
37   L4::Cap<L4Re::Rm>        r(rm);
38   new (evbuf->_obj_buf) Event_buffer_consumer();
39   return cast(evbuf)->attach(d, r);
40 }
41 
42 L4_CV long
l4re_event_buffer_detach(l4re_event_buffer_consumer_t * evbuf,l4_cap_idx_t rm)43 l4re_event_buffer_detach(l4re_event_buffer_consumer_t *evbuf,
44                          l4_cap_idx_t rm) L4_NOTHROW
45 {
46   L4::Cap<L4Re::Rm>        r(rm);
47   return cast(evbuf)->detach(r);
48 }
49 
50 L4_CV void
l4re_event_free(l4re_event_t * e)51 l4re_event_free(l4re_event_t *e) L4_NOTHROW
52 {
53   L4Re::Event_buffer::Event *ev = (L4Re::Event_buffer::Event *)e;
54 
55   ev->free();
56 }
57 
58 L4_CV l4re_event_t *
l4re_event_buffer_next(l4re_event_buffer_consumer_t * evbuf)59 l4re_event_buffer_next(l4re_event_buffer_consumer_t *evbuf) L4_NOTHROW
60 {
61   return (l4re_event_t*)cast(evbuf)->next();
62 }
63 
64 L4_CV void
l4re_event_buffer_consumer_foreach_available_event(l4re_event_buffer_consumer_t * evbuf,void * data,l4re_event_buffer_cb_t * cb)65 l4re_event_buffer_consumer_foreach_available_event
66   (l4re_event_buffer_consumer_t *evbuf, void *data,
67    l4re_event_buffer_cb_t *cb)
68 {
69   typedef void Cb(L4Re::Event_buffer::Event*, void *);
70 
71   Cb *_cb = (Cb*)cb;
72   cast(evbuf)->foreach_available_event(_cb, data);
73 }
74 
75 L4_CV void
l4re_event_buffer_consumer_process(l4re_event_buffer_consumer_t * evbuf,l4_cap_idx_t irq,l4_cap_idx_t thread,void * data,l4re_event_buffer_cb_t * cb)76 l4re_event_buffer_consumer_process(l4re_event_buffer_consumer_t *evbuf,
77                                    l4_cap_idx_t irq, l4_cap_idx_t thread, void *data,
78                                    l4re_event_buffer_cb_t *cb)
79 {
80   L4::Cap<L4::Irq> i(irq);
81   L4::Cap<L4::Thread> t(thread);
82   void (*_cb)(L4Re::Event_buffer::Event*, void *)
83     = (void (*)(L4Re::Event_buffer::Event*, void *))cb;
84   cast(evbuf)->process(i, t, _cb, data);
85 }
86