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