1 /* Support functions for general registry objects.
3 Copyright (C) 2011-2013 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 #include "gdb_assert.h"
23 #include "gdb_string.h"
25 const struct registry_data *
26 register_data_with_cleanup (struct registry_data_registry *registry,
27 registry_data_callback save,
28 registry_data_callback free)
30 struct registry_data_registration **curr;
32 /* Append new registration. */
33 for (curr = ®istry->registrations;
35 curr = &(*curr)->next)
38 *curr = XMALLOC (struct registry_data_registration);
40 (*curr)->data = XMALLOC (struct registry_data);
41 (*curr)->data->index = registry->num_registrations++;
42 (*curr)->data->save = save;
43 (*curr)->data->free = free;
49 registry_alloc_data (struct registry_data_registry *registry,
50 struct registry_fields *fields)
52 gdb_assert (fields->data == NULL);
53 fields->num_data = registry->num_registrations;
54 fields->data = XCALLOC (fields->num_data, void *);
58 registry_clear_data (struct registry_data_registry *data_registry,
59 registry_callback_adaptor adaptor,
60 struct registry_container *container,
61 struct registry_fields *fields)
63 struct registry_data_registration *registration;
66 gdb_assert (fields->data != NULL);
68 /* Process all the save handlers. */
70 for (registration = data_registry->registrations, i = 0;
72 registration = registration->next, i++)
73 if (fields->data[i] != NULL && registration->data->save != NULL)
74 adaptor (registration->data->save, container, fields->data[i]);
76 /* Now process all the free handlers. */
78 for (registration = data_registry->registrations, i = 0;
80 registration = registration->next, i++)
81 if (fields->data[i] != NULL && registration->data->free != NULL)
82 adaptor (registration->data->free, container, fields->data[i]);
84 memset (fields->data, 0, fields->num_data * sizeof (void *));
88 registry_container_free_data (struct registry_data_registry *data_registry,
89 registry_callback_adaptor adaptor,
90 struct registry_container *container,
91 struct registry_fields *fields)
93 void ***rdata = &fields->data;
94 gdb_assert (*rdata != NULL);
95 registry_clear_data (data_registry, adaptor, container, fields);
101 registry_set_data (struct registry_fields *fields,
102 const struct registry_data *data,
105 gdb_assert (data->index < fields->num_data);
106 fields->data[data->index] = value;
110 registry_data (struct registry_fields *fields,
111 const struct registry_data *data)
113 gdb_assert (data->index < fields->num_data);
114 return fields->data[data->index];