Commit | Line | Data |
---|---|---|
7a28f973 JB |
1 | /* GDB Notifications to Observers. |
2 | Copyright 2003 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | /* An observer is an entity who is interested in being notified when GDB | |
22 | reaches certain states, or certain events occur in GDB. The entity being | |
23 | observed is called the Subject. To receive notifications, the observer | |
24 | attaches a callback to the subject. One subject can have several | |
25 | observers. | |
26 | ||
27 | This file implements an internal generic low-level event notification | |
28 | mechanism based on the Observer paradigm described in the book "Design | |
29 | Patterns". This generic event notification mechansim is then re-used | |
30 | to implement the exported high-level notification management routines | |
31 | for all possible notifications. | |
32 | ||
33 | The current implementation of the generic observer provides support | |
34 | for contextual data. This contextual data is given to the subject | |
35 | when attaching the callback. In return, the subject will provide | |
36 | this contextual data back to the observer as a parameter of the | |
37 | callback. | |
38 | ||
39 | FIXME: The current support for the contextual data is only partial, | |
40 | as it lacks a mechanism that would deallocate this data when the | |
41 | callback is detached. This is not a problem so far, as this contextual | |
42 | data is only used internally to hold a function pointer. Later on, | |
43 | if a certain observer needs to provide support for user-level | |
44 | contextual data, then the generic notification mechanism will need | |
45 | need to be enhanced to allow the observer to provide a routine to | |
46 | deallocate the data when attaching the callback. | |
47 | ||
48 | This file is currently maintained by hand, but the long term plan | |
49 | if the number of different notifications starts growing is to create | |
50 | a new script (observer.sh) that would generate this file, and the | |
51 | associated documentation. */ | |
52 | ||
53 | #include "defs.h" | |
54 | #include "observer.h" | |
2b4855ab AC |
55 | #include "command.h" |
56 | #include "gdbcmd.h" | |
57 | ||
58 | static int observer_debug; | |
7a28f973 JB |
59 | |
60 | /* The internal generic observer. */ | |
61 | ||
62 | typedef void (generic_observer_notification_ftype) (const void *data, | |
63 | const void *args); | |
64 | ||
65 | struct observer | |
66 | { | |
67 | generic_observer_notification_ftype *notify; | |
68 | /* No memory management needed for the following field for now. */ | |
69 | void *data; | |
70 | }; | |
71 | ||
72 | /* A list of observers, maintained by the subject. A subject is | |
73 | actually represented by its list of observers. */ | |
74 | ||
75 | struct observer_list | |
76 | { | |
77 | struct observer_list *next; | |
78 | struct observer *observer; | |
79 | }; | |
80 | ||
81 | /* Allocate a struct observer_list, intended to be used as a node | |
82 | in the list of observers maintained by a subject. */ | |
83 | ||
84 | static struct observer_list * | |
85 | xalloc_observer_list_node (void) | |
86 | { | |
87 | struct observer_list *node = XMALLOC (struct observer_list); | |
88 | node->observer = XMALLOC (struct observer); | |
89 | return node; | |
90 | } | |
91 | ||
92 | /* The opposite of xalloc_observer_list_node, frees the memory for | |
93 | the given node. */ | |
94 | ||
95 | static void | |
96 | xfree_observer_list_node (struct observer_list *node) | |
97 | { | |
98 | xfree (node->observer); | |
99 | xfree (node); | |
100 | } | |
101 | ||
974e8ced JB |
102 | /* Attach the callback NOTIFY to a SUBJECT. The DATA is also stored, |
103 | in order for the subject to provide it back to the observer during | |
104 | a notification. */ | |
7a28f973 JB |
105 | |
106 | static struct observer * | |
107 | generic_observer_attach (struct observer_list **subject, | |
108 | generic_observer_notification_ftype * notify, | |
109 | void *data) | |
110 | { | |
111 | struct observer_list *observer_list = xalloc_observer_list_node (); | |
112 | ||
113 | observer_list->next = *subject; | |
114 | observer_list->observer->notify = notify; | |
115 | observer_list->observer->data = data; | |
116 | *subject = observer_list; | |
117 | ||
118 | return observer_list->observer; | |
119 | } | |
120 | ||
974e8ced JB |
121 | /* Remove the given OBSERVER from the SUBJECT. Once detached, OBSERVER |
122 | should no longer be used, as it is no longer valid. */ | |
123 | ||
7a28f973 JB |
124 | static void |
125 | generic_observer_detach (struct observer_list **subject, | |
126 | const struct observer *observer) | |
127 | { | |
128 | struct observer_list *previous_node = NULL; | |
129 | struct observer_list *current_node = *subject; | |
130 | ||
131 | while (current_node != NULL) | |
132 | { | |
133 | if (current_node->observer == observer) | |
134 | { | |
135 | if (previous_node != NULL) | |
136 | previous_node->next = current_node->next; | |
137 | else | |
138 | *subject = current_node->next; | |
139 | xfree_observer_list_node (current_node); | |
140 | return; | |
141 | } | |
142 | previous_node = current_node; | |
143 | current_node = current_node->next; | |
144 | } | |
145 | ||
146 | /* We should never reach this point. However, this should not be | |
147 | a very serious error, so simply report a warning to the user. */ | |
148 | warning ("Failed to detach observer"); | |
149 | } | |
150 | ||
974e8ced JB |
151 | /* Send a notification to all the observers of SUBJECT. ARGS is passed to |
152 | all observers as an argument to the notification callback. */ | |
7a28f973 JB |
153 | |
154 | static void | |
155 | generic_observer_notify (struct observer_list *subject, const void *args) | |
156 | { | |
157 | struct observer_list *current_node = subject; | |
158 | ||
159 | while (current_node != NULL) | |
160 | { | |
161 | (*current_node->observer->notify) (current_node->observer->data, args); | |
162 | current_node = current_node->next; | |
163 | } | |
164 | } | |
165 | ||
4fbe891e | 166 | |
f82de61c MK |
167 | /* The following code is only used to unit-test the observers from our |
168 | testsuite. DO NOT USE IT within observer.c (or anywhere else for | |
169 | that matter)! */ | |
4fbe891e | 170 | |
f82de61c MK |
171 | /* If we define these variables and functions as `static', the |
172 | compiler will optimize them out. */ | |
4fbe891e | 173 | |
f82de61c MK |
174 | int observer_test_first_observer = 0; |
175 | int observer_test_second_observer = 0; | |
176 | int observer_test_third_observer = 0; | |
4fbe891e | 177 | |
f82de61c | 178 | void |
e0270fd9 | 179 | observer_test_first_notification_function (struct bpstats *bs) |
4fbe891e JB |
180 | { |
181 | observer_test_first_observer++; | |
182 | } | |
183 | ||
f82de61c | 184 | void |
e0270fd9 | 185 | observer_test_second_notification_function (struct bpstats *bs) |
4fbe891e JB |
186 | { |
187 | observer_test_second_observer++; | |
188 | } | |
189 | ||
f82de61c | 190 | void |
e0270fd9 | 191 | observer_test_third_notification_function (struct bpstats *bs) |
4fbe891e JB |
192 | { |
193 | observer_test_third_observer++; | |
194 | } | |
195 | ||
2b4855ab AC |
196 | extern initialize_file_ftype _initialize_observer; /* -Wmissing-prototypes */ |
197 | ||
198 | void | |
199 | _initialize_observer (void) | |
200 | { | |
201 | add_setshow_zinteger_cmd ("observer", class_maintenance, &observer_debug, "\ | |
202 | Set observer debugging.\n\ | |
203 | When non-zero, observer debugging is enabled.", "\ | |
204 | Show observer debugging.\n\ | |
205 | When non-zero, observer debugging is enabled.", | |
206 | NULL, NULL, | |
207 | &setdebuglist, &showdebuglist); | |
208 | } | |
209 | ||
7a464420 | 210 | #include "observer.inc" |