c01929b36532e504768da1682b0bcca079128725
[platform/upstream/binutils.git] / gdb / registry.c
1 /* Support functions for general registry objects.
2
3    Copyright (C) 2011, 2012
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
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.
12
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.
17
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/>.  */
20
21 #include "defs.h"
22 #include "registry.h"
23 #include "gdb_assert.h"
24 #include "gdb_string.h"
25
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)
30 {
31   struct registry_data_registration **curr;
32
33   /* Append new registration.  */
34   for (curr = &registry->registrations;
35        *curr != NULL;
36        curr = &(*curr)->next)
37     ;
38
39   *curr = XMALLOC (struct registry_data_registration);
40   (*curr)->next = NULL;
41   (*curr)->data = XMALLOC (struct registry_data);
42   (*curr)->data->index = registry->num_registrations++;
43   (*curr)->data->save = save;
44   (*curr)->data->free = free;
45
46   return (*curr)->data;
47 }
48
49 void
50 registry_alloc_data (struct registry_data_registry *registry,
51                        struct registry_fields *fields)
52 {
53   gdb_assert (fields->data == NULL);
54   fields->num_data = registry->num_registrations;
55   fields->data = XCALLOC (fields->num_data, void *);
56 }
57
58 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)
63 {
64   struct registry_data_registration *registration;
65   int i;
66
67   gdb_assert (fields->data != NULL);
68
69   /* Process all the save handlers.  */
70
71   for (registration = data_registry->registrations, i = 0;
72        i < fields->num_data;
73        registration = registration->next, i++)
74     if (fields->data[i] != NULL && registration->data->save != NULL)
75       adaptor (registration->data->save, container, fields->data[i]);
76
77   /* Now process all the free handlers.  */
78
79   for (registration = data_registry->registrations, i = 0;
80        i < fields->num_data;
81        registration = registration->next, i++)
82     if (fields->data[i] != NULL && registration->data->free != NULL)
83       adaptor (registration->data->free, container, fields->data[i]);
84
85   memset (fields->data, 0, fields->num_data * sizeof (void *));
86 }
87
88 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)
93 {
94   void ***rdata = &fields->data;
95   gdb_assert (*rdata != NULL);
96   registry_clear_data (data_registry, adaptor, container, fields);
97   xfree (*rdata);
98   *rdata = NULL;
99 }
100
101 void
102 registry_set_data (struct registry_fields *fields,
103                    const struct registry_data *data,
104                    void *value)
105 {
106   gdb_assert (data->index < fields->num_data);
107   fields->data[data->index] = value;
108 }
109
110 void *
111 registry_data (struct registry_fields *fields,
112                const struct registry_data *data)
113 {
114   gdb_assert (data->index < fields->num_data);
115   return fields->data[data->index];
116 }