1 /* Support functions for general registry objects.
3 Copyright (C) 2011-2014 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/>. */
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
)
29 struct registry_data_registration
**curr
;
31 /* Append new registration. */
32 for (curr
= ®istry
->registrations
;
34 curr
= &(*curr
)->next
)
37 *curr
= XNEW (struct registry_data_registration
);
39 (*curr
)->data
= XNEW (struct registry_data
);
40 (*curr
)->data
->index
= registry
->num_registrations
++;
41 (*curr
)->data
->save
= save
;
42 (*curr
)->data
->free
= free
;
48 registry_alloc_data (struct registry_data_registry
*registry
,
49 struct registry_fields
*fields
)
51 gdb_assert (fields
->data
== NULL
);
52 fields
->num_data
= registry
->num_registrations
;
53 fields
->data
= XCNEWVEC (void *, fields
->num_data
);
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
)
62 struct registry_data_registration
*registration
;
65 gdb_assert (fields
->data
!= NULL
);
67 /* Process all the save handlers. */
69 for (registration
= data_registry
->registrations
, i
= 0;
71 registration
= registration
->next
, i
++)
72 if (fields
->data
[i
] != NULL
&& registration
->data
->save
!= NULL
)
73 adaptor (registration
->data
->save
, container
, fields
->data
[i
]);
75 /* Now process all the free handlers. */
77 for (registration
= data_registry
->registrations
, i
= 0;
79 registration
= registration
->next
, i
++)
80 if (fields
->data
[i
] != NULL
&& registration
->data
->free
!= NULL
)
81 adaptor (registration
->data
->free
, container
, fields
->data
[i
]);
83 memset (fields
->data
, 0, fields
->num_data
* sizeof (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
)
92 void ***rdata
= &fields
->data
;
93 gdb_assert (*rdata
!= NULL
);
94 registry_clear_data (data_registry
, adaptor
, container
, fields
);
100 registry_set_data (struct registry_fields
*fields
,
101 const struct registry_data
*data
,
104 gdb_assert (data
->index
< fields
->num_data
);
105 fields
->data
[data
->index
] = value
;
109 registry_data (struct registry_fields
*fields
,
110 const struct registry_data
*data
)
112 gdb_assert (data
->index
< fields
->num_data
);
113 return fields
->data
[data
->index
];