1 /* Support functions for general registry objects.
3 Copyright (C) 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_assert.h"
24 #include "gdb_string.h"
26 const struct registry_data *
27 register_data_with_cleanup (struct registry_data_registry *registry,
28 registry_data_callback save,
29 registry_data_callback free)
31 struct registry_data_registration **curr;
33 /* Append new registration. */
34 for (curr = ®istry->registrations;
36 curr = &(*curr)->next)
39 *curr = XMALLOC (struct registry_data_registration);
41 (*curr)->data = XMALLOC (struct registry_data);
42 (*curr)->data->index = registry->num_registrations++;
43 (*curr)->data->save = save;
44 (*curr)->data->free = free;
50 registry_alloc_data (struct registry_data_registry *registry,
51 struct registry_fields *fields)
53 gdb_assert (fields->data == NULL);
54 fields->num_data = registry->num_registrations;
55 fields->data = XCALLOC (fields->num_data, void *);
59 registry_clear_data (struct registry_data_registry *data_registry,
60 registry_callback_adaptor adaptor,
61 struct registry_container *container,
62 struct registry_fields *fields)
64 struct registry_data_registration *registration;
67 gdb_assert (fields->data != NULL);
69 /* Process all the save handlers. */
71 for (registration = data_registry->registrations, i = 0;
73 registration = registration->next, i++)
74 if (fields->data[i] != NULL && registration->data->save != NULL)
75 adaptor (registration->data->save, container, fields->data[i]);
77 /* Now process all the free handlers. */
79 for (registration = data_registry->registrations, i = 0;
81 registration = registration->next, i++)
82 if (fields->data[i] != NULL && registration->data->free != NULL)
83 adaptor (registration->data->free, container, fields->data[i]);
85 memset (fields->data, 0, fields->num_data * sizeof (void *));
89 registry_container_free_data (struct registry_data_registry *data_registry,
90 registry_callback_adaptor adaptor,
91 struct registry_container *container,
92 struct registry_fields *fields)
94 void ***rdata = &fields->data;
95 gdb_assert (*rdata != NULL);
96 registry_clear_data (data_registry, adaptor, container, fields);
102 registry_set_data (struct registry_fields *fields,
103 const struct registry_data *data,
106 gdb_assert (data->index < fields->num_data);
107 fields->data[data->index] = value;
111 registry_data (struct registry_fields *fields,
112 const struct registry_data *data)
114 gdb_assert (data->index < fields->num_data);
115 return fields->data[data->index];