Include gdb_assert.h in common-defs.h
[deliverable/binutils-gdb.git] / gdb / registry.c
1 /* Support functions for general registry objects.
2
3 Copyright (C) 2011-2014 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 #include "registry.h"
22 #include <string.h>
23
24 const struct registry_data *
25 register_data_with_cleanup (struct registry_data_registry *registry,
26 registry_data_callback save,
27 registry_data_callback free)
28 {
29 struct registry_data_registration **curr;
30
31 /* Append new registration. */
32 for (curr = &registry->registrations;
33 *curr != NULL;
34 curr = &(*curr)->next)
35 ;
36
37 *curr = XNEW (struct registry_data_registration);
38 (*curr)->next = NULL;
39 (*curr)->data = XNEW (struct registry_data);
40 (*curr)->data->index = registry->num_registrations++;
41 (*curr)->data->save = save;
42 (*curr)->data->free = free;
43
44 return (*curr)->data;
45 }
46
47 void
48 registry_alloc_data (struct registry_data_registry *registry,
49 struct registry_fields *fields)
50 {
51 gdb_assert (fields->data == NULL);
52 fields->num_data = registry->num_registrations;
53 fields->data = XCNEWVEC (void *, fields->num_data);
54 }
55
56 void
57 registry_clear_data (struct registry_data_registry *data_registry,
58 registry_callback_adaptor adaptor,
59 struct registry_container *container,
60 struct registry_fields *fields)
61 {
62 struct registry_data_registration *registration;
63 int i;
64
65 gdb_assert (fields->data != NULL);
66
67 /* Process all the save handlers. */
68
69 for (registration = data_registry->registrations, i = 0;
70 i < fields->num_data;
71 registration = registration->next, i++)
72 if (fields->data[i] != NULL && registration->data->save != NULL)
73 adaptor (registration->data->save, container, fields->data[i]);
74
75 /* Now process all the free handlers. */
76
77 for (registration = data_registry->registrations, i = 0;
78 i < fields->num_data;
79 registration = registration->next, i++)
80 if (fields->data[i] != NULL && registration->data->free != NULL)
81 adaptor (registration->data->free, container, fields->data[i]);
82
83 memset (fields->data, 0, fields->num_data * sizeof (void *));
84 }
85
86 void
87 registry_container_free_data (struct registry_data_registry *data_registry,
88 registry_callback_adaptor adaptor,
89 struct registry_container *container,
90 struct registry_fields *fields)
91 {
92 void ***rdata = &fields->data;
93 gdb_assert (*rdata != NULL);
94 registry_clear_data (data_registry, adaptor, container, fields);
95 xfree (*rdata);
96 *rdata = NULL;
97 }
98
99 void
100 registry_set_data (struct registry_fields *fields,
101 const struct registry_data *data,
102 void *value)
103 {
104 gdb_assert (data->index < fields->num_data);
105 fields->data[data->index] = value;
106 }
107
108 void *
109 registry_data (struct registry_fields *fields,
110 const struct registry_data *data)
111 {
112 gdb_assert (data->index < fields->num_data);
113 return fields->data[data->index];
114 }
This page took 0.049986 seconds and 5 git commands to generate.