2013-03-12 Sebastian Huber <sebastian.huber@embedded-brains.de>
[binutils-gdb.git] / gdb / registry.c
blob8055300d445576c50479d1c476729563136fd1fa
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/>. */
20 #include "defs.h"
21 #include "registry.h"
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 = &registry->registrations;
34 *curr != NULL;
35 curr = &(*curr)->next)
38 *curr = XMALLOC (struct registry_data_registration);
39 (*curr)->next = NULL;
40 (*curr)->data = XMALLOC (struct registry_data);
41 (*curr)->data->index = registry->num_registrations++;
42 (*curr)->data->save = save;
43 (*curr)->data->free = free;
45 return (*curr)->data;
48 void
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 *);
57 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;
64 int i;
66 gdb_assert (fields->data != NULL);
68 /* Process all the save handlers. */
70 for (registration = data_registry->registrations, i = 0;
71 i < fields->num_data;
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;
79 i < fields->num_data;
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 *));
87 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);
96 xfree (*rdata);
97 *rdata = NULL;
100 void
101 registry_set_data (struct registry_fields *fields,
102 const struct registry_data *data,
103 void *value)
105 gdb_assert (data->index < fields->num_data);
106 fields->data[data->index] = value;
109 void *
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];