Sort includes for files gdb/[a-f]*.[chyl].
[external/binutils.git] / gdb / continuations.c
1 /* Continuations for GDB, the GNU debugger.
2
3    Copyright (C) 1986-2019 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21
22 /* Local non-gdb includes.  */
23 #include "continuations.h"
24 #include "gdbthread.h"
25 #include "inferior.h"
26
27 struct continuation
28 {
29   struct continuation *next;
30   continuation_ftype *function;
31   continuation_free_arg_ftype *free_arg;
32   void *arg;
33 };
34
35 /* Add a new continuation to the continuation chain.  Args are
36    FUNCTION to run the continuation up with, and ARG to pass to
37    it.  */
38
39 static void
40 make_continuation (struct continuation **pmy_chain,
41                    continuation_ftype *function,
42                    void *arg,  void (*free_arg) (void *))
43 {
44   struct continuation *newobj = XNEW (struct continuation);
45
46   newobj->next = *pmy_chain;
47   newobj->function = function;
48   newobj->free_arg = free_arg;
49   newobj->arg = arg;
50   *pmy_chain = newobj;
51 }
52
53 static void
54 do_my_continuations_1 (struct continuation **pmy_chain, int err)
55 {
56   struct continuation *ptr;
57
58   while ((ptr = *pmy_chain) != NULL)
59     {
60       *pmy_chain = ptr->next;   /* Do this first in case of recursion.  */
61       (*ptr->function) (ptr->arg, err);
62       if (ptr->free_arg)
63         (*ptr->free_arg) (ptr->arg);
64       xfree (ptr);
65     }
66 }
67
68 static void
69 do_my_continuations (struct continuation **list, int err)
70 {
71   struct continuation *continuations;
72
73   if (*list == NULL)
74     return;
75
76   /* Copy the list header into another pointer, and set the global
77      list header to null, so that the global list can change as a side
78      effect of invoking the continuations and the processing of the
79      preexisting continuations will not be affected.  */
80
81   continuations = *list;
82   *list = NULL;
83
84   /* Work now on the list we have set aside.  */
85   do_my_continuations_1 (&continuations, err);
86 }
87
88 static void
89 discard_my_continuations_1 (struct continuation **pmy_chain)
90 {
91   struct continuation *ptr;
92
93   while ((ptr = *pmy_chain) != NULL)
94     {
95       *pmy_chain = ptr->next;
96       if (ptr->free_arg)
97         (*ptr->free_arg) (ptr->arg);
98       xfree (ptr);
99     }
100 }
101
102 static void
103 discard_my_continuations (struct continuation **list)
104 {
105   discard_my_continuations_1 (list);
106   *list = NULL;
107 }
108
109 /* Add a continuation to the continuation list of INFERIOR.  The new
110    continuation will be added at the front.  */
111
112 void
113 add_inferior_continuation (continuation_ftype *hook, void *args,
114                            continuation_free_arg_ftype *free_arg)
115 {
116   struct inferior *inf = current_inferior ();
117
118   make_continuation (&inf->continuations, hook, args, free_arg);
119 }
120
121 /* Do all continuations of the current inferior.  */
122
123 void
124 do_all_inferior_continuations (int err)
125 {
126   struct inferior *inf = current_inferior ();
127   do_my_continuations (&inf->continuations, err);
128 }
129
130 /* Get rid of all the inferior-wide continuations of INF.  */
131
132 void
133 discard_all_inferior_continuations (struct inferior *inf)
134 {
135   discard_my_continuations (&inf->continuations);
136 }