registry.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Support functions for general registry objects.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "registry.h"
  16. const struct registry_data *
  17. register_data_with_cleanup (struct registry_data_registry *registry,
  18. registry_data_callback save,
  19. registry_data_callback free)
  20. {
  21. struct registry_data_registration **curr;
  22. /* Append new registration. */
  23. for (curr = &registry->registrations;
  24. *curr != NULL;
  25. curr = &(*curr)->next)
  26. ;
  27. *curr = XNEW (struct registry_data_registration);
  28. (*curr)->next = NULL;
  29. (*curr)->data = XNEW (struct registry_data);
  30. (*curr)->data->index = registry->num_registrations++;
  31. (*curr)->data->save = save;
  32. (*curr)->data->free = free;
  33. return (*curr)->data;
  34. }
  35. void
  36. registry_alloc_data (struct registry_data_registry *registry,
  37. struct registry_fields *fields)
  38. {
  39. gdb_assert (fields->data == NULL);
  40. fields->num_data = registry->num_registrations;
  41. fields->data = XCNEWVEC (void *, fields->num_data);
  42. }
  43. void
  44. registry_clear_data (struct registry_data_registry *data_registry,
  45. registry_callback_adaptor adaptor,
  46. struct registry_container *container,
  47. struct registry_fields *fields)
  48. {
  49. struct registry_data_registration *registration;
  50. int i;
  51. gdb_assert (fields->data != NULL);
  52. /* Process all the save handlers. */
  53. for (registration = data_registry->registrations, i = 0;
  54. i < fields->num_data;
  55. registration = registration->next, i++)
  56. if (fields->data[i] != NULL && registration->data->save != NULL)
  57. adaptor (registration->data->save, container, fields->data[i]);
  58. /* Now process all the free handlers. */
  59. for (registration = data_registry->registrations, i = 0;
  60. i < fields->num_data;
  61. registration = registration->next, i++)
  62. if (fields->data[i] != NULL && registration->data->free != NULL)
  63. adaptor (registration->data->free, container, fields->data[i]);
  64. memset (fields->data, 0, fields->num_data * sizeof (void *));
  65. }
  66. void
  67. registry_container_free_data (struct registry_data_registry *data_registry,
  68. registry_callback_adaptor adaptor,
  69. struct registry_container *container,
  70. struct registry_fields *fields)
  71. {
  72. void ***rdata = &fields->data;
  73. gdb_assert (*rdata != NULL);
  74. registry_clear_data (data_registry, adaptor, container, fields);
  75. xfree (*rdata);
  76. *rdata = NULL;
  77. }
  78. void
  79. registry_set_data (struct registry_fields *fields,
  80. const struct registry_data *data,
  81. void *value)
  82. {
  83. gdb_assert (data->index < fields->num_data);
  84. fields->data[data->index] = value;
  85. }
  86. void *
  87. registry_data (struct registry_fields *fields,
  88. const struct registry_data *data)
  89. {
  90. gdb_assert (data->index < fields->num_data);
  91. return fields->data[data->index];
  92. }