cap.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1995,1997, Andrew Cagney <cagney@highland.com.au>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _CAP_C_
  15. #define _CAP_C_
  16. #include "cap.h"
  17. typedef struct _cap_mapping cap_mapping;
  18. struct _cap_mapping {
  19. unsigned_cell external;
  20. void *internal;
  21. cap_mapping *next;
  22. };
  23. struct _cap {
  24. int nr_mappings;
  25. cap_mapping *mappings;
  26. };
  27. INLINE_CAP\
  28. (cap *)
  29. cap_create(const char *key)
  30. {
  31. return ZALLOC(cap);
  32. }
  33. INLINE_CAP\
  34. (void)
  35. cap_init(cap *db)
  36. {
  37. cap_mapping *current_map = db->mappings;
  38. if (current_map != NULL) {
  39. db->nr_mappings = db->mappings->external;
  40. /* verify that the mappings that were not removed are in sequence
  41. down to nr 1 */
  42. while (current_map->next != NULL) {
  43. if (current_map->external != current_map->next->external + 1)
  44. error("cap: cap database possibly corrupt");
  45. current_map = current_map->next;
  46. }
  47. ASSERT(current_map->next == NULL);
  48. if (current_map->external != 1)
  49. error("cap: cap database possibly currupt");
  50. }
  51. else {
  52. db->nr_mappings = 0;
  53. }
  54. }
  55. INLINE_CAP\
  56. (void *)
  57. cap_internal(cap *db,
  58. signed_cell external)
  59. {
  60. cap_mapping *current_map = db->mappings;
  61. while (current_map != NULL) {
  62. if (current_map->external == external)
  63. return current_map->internal;
  64. current_map = current_map->next;
  65. }
  66. return (void*)0;
  67. }
  68. INLINE_CAP\
  69. (signed_cell)
  70. cap_external(cap *db,
  71. void *internal)
  72. {
  73. cap_mapping *current_map = db->mappings;
  74. while (current_map != NULL) {
  75. if (current_map->internal == internal)
  76. return current_map->external;
  77. current_map = current_map->next;
  78. }
  79. return 0;
  80. }
  81. INLINE_CAP\
  82. (void)
  83. cap_add(cap *db,
  84. void *internal)
  85. {
  86. if (cap_external(db, internal) != 0) {
  87. error("cap: attempting to add an object already in the data base");
  88. }
  89. else {
  90. /* insert at the front making things in decending order */
  91. cap_mapping *new_map = ZALLOC(cap_mapping);
  92. new_map->next = db->mappings;
  93. new_map->internal = internal;
  94. db->nr_mappings += 1;
  95. new_map->external = db->nr_mappings;
  96. db->mappings = new_map;
  97. }
  98. }
  99. INLINE_CAP\
  100. (void)
  101. cap_remove(cap *db,
  102. void *internal)
  103. {
  104. cap_mapping **current_map = &db->mappings;
  105. while (*current_map != NULL) {
  106. if ((*current_map)->internal == internal) {
  107. cap_mapping *delete = *current_map;
  108. *current_map = delete->next;
  109. free(delete);
  110. return;
  111. }
  112. current_map = &(*current_map)->next;
  113. }
  114. error("cap: attempt to remove nonexistant internal object");
  115. }
  116. #endif