callback.c
1 /*
2  * $Id: callback.c,v 1.5 2003/12/01 09:10:14 troth Exp $
3  *
4  ****************************************************************************
5  *
6  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
7  * Copyright (C) 2001, 2002, 2003 Theodore A. Roth
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  ****************************************************************************
24  */
25 
26 #include <config.h>
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 
31 #include "avrerror.h"
32 #include "avrmalloc.h"
33 #include "avrclass.h"
34 #include "utils.h"
35 #include "callback.h"
36 
37 /****************************************************************************\
38  *
39  * Clock Call Back methods
40  *
41 \****************************************************************************/
42 
43 #ifndef DOXYGEN /* don't expose to doxygen */
44 
45 struct _CallBack
46 {
47  AvrClass parent;
48  CallBack_FP func; /* the callback function */
49  AvrClass *data; /* user data to be passed to callback
50  function */
51 };
52 
53 #endif /* DOXYGEN */
54 
55 CallBack *
56 callback_new (CallBack_FP func, AvrClass *data)
57 {
58  CallBack *cb;
59 
60  cb = avr_new (CallBack, 1);
61  callback_construct (cb, func, data);
62  class_overload_destroy ((AvrClass *)cb, callback_destroy);
63 
64  return cb;
65 }
66 
67 void
68 callback_construct (CallBack *cb, CallBack_FP func, AvrClass *data)
69 {
70  if (cb == NULL)
71  avr_error ("passed null ptr");
72 
73  class_construct ((AvrClass *)cb);
74 
75  cb->func = func;
76 
77  cb->data = data;
78 
79  if (data)
80  class_ref (data);
81 }
82 
83 void
84 callback_destroy (void *cb)
85 {
86  CallBack *_cb = (CallBack *)cb;
87 
88  if (cb == NULL)
89  return;
90 
91  if (_cb->data)
92  class_unref (_cb->data);
93 
94  class_destroy (cb);
95 }
96 
97 DList *
98 callback_list_add (DList *head, CallBack *cb)
99 {
100  return dlist_add (head, (AvrClass *)cb, NULL);
101 }
102 
103 static int
104 callback_execute (AvrClass *data, void *user_data)
105 {
106  CallBack *cb = (CallBack *)data;
107  uint64_t time = *(uint64_t *) user_data;
108 
109  return cb->func (time, cb->data);
110 }
111 
112 DList *
113 callback_list_execute_all (DList *head, uint64_t time)
114 {
115  return dlist_iterator (head, callback_execute, &time);
116 }

Automatically generated by Doxygen 1.8.2 on Tue Mar 12 2013.