| 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 "gdb_assert.h" |
| 23 | #include <string.h> |
| 24 | |
| 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) |
| 29 | { |
| 30 | struct registry_data_registration **curr; |
| 31 | |
| 32 | /* Append new registration. */ |
| 33 | for (curr = ®istry->registrations; |
| 34 | *curr != NULL; |
| 35 | curr = &(*curr)->next) |
| 36 | ; |
| 37 | |
| 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; |
| 44 | |
| 45 | return (*curr)->data; |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | registry_alloc_data (struct registry_data_registry *registry, |
| 50 | struct registry_fields *fields) |
| 51 | { |
| 52 | gdb_assert (fields->data == NULL); |
| 53 | fields->num_data = registry->num_registrations; |
| 54 | fields->data = XCALLOC (fields->num_data, void *); |
| 55 | } |
| 56 | |
| 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) |
| 62 | { |
| 63 | struct registry_data_registration *registration; |
| 64 | int i; |
| 65 | |
| 66 | gdb_assert (fields->data != NULL); |
| 67 | |
| 68 | /* Process all the save handlers. */ |
| 69 | |
| 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]); |
| 75 | |
| 76 | /* Now process all the free handlers. */ |
| 77 | |
| 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]); |
| 83 | |
| 84 | memset (fields->data, 0, fields->num_data * sizeof (void *)); |
| 85 | } |
| 86 | |
| 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) |
| 92 | { |
| 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; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | registry_set_data (struct registry_fields *fields, |
| 102 | const struct registry_data *data, |
| 103 | void *value) |
| 104 | { |
| 105 | gdb_assert (data->index < fields->num_data); |
| 106 | fields->data[data->index] = value; |
| 107 | } |
| 108 | |
| 109 | void * |
| 110 | registry_data (struct registry_fields *fields, |
| 111 | const struct registry_data *data) |
| 112 | { |
| 113 | gdb_assert (data->index < fields->num_data); |
| 114 | return fields->data[data->index]; |
| 115 | } |