GDB (API)
|
00001 /* Support functions for general registry objects. 00002 00003 Copyright (C) 2011-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "registry.h" 00022 #include "gdb_assert.h" 00023 #include "gdb_string.h" 00024 00025 const struct registry_data * 00026 register_data_with_cleanup (struct registry_data_registry *registry, 00027 registry_data_callback save, 00028 registry_data_callback free) 00029 { 00030 struct registry_data_registration **curr; 00031 00032 /* Append new registration. */ 00033 for (curr = ®istry->registrations; 00034 *curr != NULL; 00035 curr = &(*curr)->next) 00036 ; 00037 00038 *curr = XMALLOC (struct registry_data_registration); 00039 (*curr)->next = NULL; 00040 (*curr)->data = XMALLOC (struct registry_data); 00041 (*curr)->data->index = registry->num_registrations++; 00042 (*curr)->data->save = save; 00043 (*curr)->data->free = free; 00044 00045 return (*curr)->data; 00046 } 00047 00048 void 00049 registry_alloc_data (struct registry_data_registry *registry, 00050 struct registry_fields *fields) 00051 { 00052 gdb_assert (fields->data == NULL); 00053 fields->num_data = registry->num_registrations; 00054 fields->data = XCALLOC (fields->num_data, void *); 00055 } 00056 00057 void 00058 registry_clear_data (struct registry_data_registry *data_registry, 00059 registry_callback_adaptor adaptor, 00060 struct registry_container *container, 00061 struct registry_fields *fields) 00062 { 00063 struct registry_data_registration *registration; 00064 int i; 00065 00066 gdb_assert (fields->data != NULL); 00067 00068 /* Process all the save handlers. */ 00069 00070 for (registration = data_registry->registrations, i = 0; 00071 i < fields->num_data; 00072 registration = registration->next, i++) 00073 if (fields->data[i] != NULL && registration->data->save != NULL) 00074 adaptor (registration->data->save, container, fields->data[i]); 00075 00076 /* Now process all the free handlers. */ 00077 00078 for (registration = data_registry->registrations, i = 0; 00079 i < fields->num_data; 00080 registration = registration->next, i++) 00081 if (fields->data[i] != NULL && registration->data->free != NULL) 00082 adaptor (registration->data->free, container, fields->data[i]); 00083 00084 memset (fields->data, 0, fields->num_data * sizeof (void *)); 00085 } 00086 00087 void 00088 registry_container_free_data (struct registry_data_registry *data_registry, 00089 registry_callback_adaptor adaptor, 00090 struct registry_container *container, 00091 struct registry_fields *fields) 00092 { 00093 void ***rdata = &fields->data; 00094 gdb_assert (*rdata != NULL); 00095 registry_clear_data (data_registry, adaptor, container, fields); 00096 xfree (*rdata); 00097 *rdata = NULL; 00098 } 00099 00100 void 00101 registry_set_data (struct registry_fields *fields, 00102 const struct registry_data *data, 00103 void *value) 00104 { 00105 gdb_assert (data->index < fields->num_data); 00106 fields->data[data->index] = value; 00107 } 00108 00109 void * 00110 registry_data (struct registry_fields *fields, 00111 const struct registry_data *data) 00112 { 00113 gdb_assert (data->index < fields->num_data); 00114 return fields->data[data->index]; 00115 }