ptrmap.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Stupid implementation of pointer -> pointer map.
  4. *
  5. * Copyright (c) 2017 Luc Van Oostenryck.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "ptrmap.h"
  26. #include "allocate.h"
  27. #include <stddef.h>
  28. #define MAP_NR 7
  29. struct ptrpair {
  30. void *key;
  31. void *val;
  32. };
  33. struct ptrmap {
  34. struct ptrmap *next;
  35. int nr;
  36. struct ptrpair pairs[MAP_NR];
  37. };
  38. DECLARE_ALLOCATOR(ptrmap);
  39. ALLOCATOR(ptrmap, "ptrmap");
  40. void __ptrmap_add(struct ptrmap **mapp, void *key, void *val)
  41. {
  42. struct ptrmap *head = *mapp;
  43. struct ptrmap *newmap;
  44. struct ptrmap *map;
  45. struct ptrpair *pair;
  46. int nr;
  47. if ((map = head)) {
  48. struct ptrmap *next = map->next;
  49. if (next) // head is full
  50. map = next;
  51. if ((nr = map->nr) < MAP_NR)
  52. goto oldmap;
  53. }
  54. // need a new block
  55. newmap = __alloc_ptrmap(0);
  56. if (!head) {
  57. *mapp = newmap;
  58. } else {
  59. newmap->next = head->next;
  60. head->next = newmap;
  61. }
  62. map = newmap;
  63. nr = 0;
  64. oldmap:
  65. pair = &map->pairs[nr];
  66. pair->key = key;
  67. pair->val = val;
  68. map->nr = ++nr;
  69. }
  70. void *__ptrmap_lookup(struct ptrmap *map, void *key)
  71. {
  72. for (; map; map = map->next) {
  73. int i, n = map->nr;
  74. for (i = 0; i < n; i++) {
  75. struct ptrpair *pair = &map->pairs[i];
  76. if (pair->key == key)
  77. return pair->val;
  78. }
  79. }
  80. return NULL;
  81. }
  82. void __ptrmap_update(struct ptrmap **mapp, void *key, void *val)
  83. {
  84. struct ptrmap *map = *mapp;
  85. for (; map; map = map->next) {
  86. int i, n = map->nr;
  87. for (i = 0; i < n; i++) {
  88. struct ptrpair *pair = &map->pairs[i];
  89. if (pair->key == key) {
  90. if (pair->val != val)
  91. pair->val = val;
  92. return;
  93. }
  94. }
  95. }
  96. __ptrmap_add(mapp, key, val);
  97. }