1 /* Support functions for general registry objects.
3 Copyright (C) 2011-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 const struct registry_data *
23 register_data_with_cleanup (struct registry_data_registry *registry,
24 registry_data_callback save,
25 registry_data_callback free)
27 struct registry_data_registration **curr;
29 /* Append new registration. */
30 for (curr = ®istry->registrations;
32 curr = &(*curr)->next)
35 *curr = XNEW (struct registry_data_registration);
37 (*curr)->data = XNEW (struct registry_data);
38 (*curr)->data->index = registry->num_registrations++;
39 (*curr)->data->save = save;
40 (*curr)->data->free = free;
46 registry_alloc_data (struct registry_data_registry *registry,
47 struct registry_fields *fields)
49 gdb_assert (fields->data == NULL);
50 fields->num_data = registry->num_registrations;
51 fields->data = XCNEWVEC (void *, fields->num_data);
55 registry_clear_data (struct registry_data_registry *data_registry,
56 registry_callback_adaptor adaptor,
57 struct registry_container *container,
58 struct registry_fields *fields)
60 struct registry_data_registration *registration;
63 gdb_assert (fields->data != NULL);
65 /* Process all the save handlers. */
67 for (registration = data_registry->registrations, i = 0;
69 registration = registration->next, i++)
70 if (fields->data[i] != NULL && registration->data->save != NULL)
71 adaptor (registration->data->save, container, fields->data[i]);
73 /* Now process all the free handlers. */
75 for (registration = data_registry->registrations, i = 0;
77 registration = registration->next, i++)
78 if (fields->data[i] != NULL && registration->data->free != NULL)
79 adaptor (registration->data->free, container, fields->data[i]);
81 memset (fields->data, 0, fields->num_data * sizeof (void *));
85 registry_container_free_data (struct registry_data_registry *data_registry,
86 registry_callback_adaptor adaptor,
87 struct registry_container *container,
88 struct registry_fields *fields)
90 void ***rdata = &fields->data;
91 gdb_assert (*rdata != NULL);
92 registry_clear_data (data_registry, adaptor, container, fields);
98 registry_set_data (struct registry_fields *fields,
99 const struct registry_data *data,
102 gdb_assert (data->index < fields->num_data);
103 fields->data[data->index] = value;
107 registry_data (struct registry_fields *fields,
108 const struct registry_data *data)
110 gdb_assert (data->index < fields->num_data);
111 return fields->data[data->index];