AddrSet.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  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. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "util/Bits.h"
  16. #include "subnode/AddrSet.h"
  17. #include "util/Identity.h"
  18. struct Elem
  19. {
  20. struct Address addr;
  21. struct Allocator* alloc;
  22. Identity
  23. };
  24. #define ArrayList_TYPE struct Elem
  25. #define ArrayList_NAME OfAddrs
  26. #include "util/ArrayList.h"
  27. struct AddrSet_pvt
  28. {
  29. struct AddrSet pub;
  30. struct ArrayList_OfAddrs* addrList;
  31. struct Allocator* alloc;
  32. Identity
  33. };
  34. static int indexOf(struct AddrSet_pvt* ap, struct Address* addr, enum AddrSet_Match m)
  35. {
  36. for (int i = 0; i < ap->addrList->length; i++) {
  37. struct Elem* el = ArrayList_OfAddrs_get(ap->addrList, i);
  38. // We will just match on the same IP, even if the path is not the same
  39. if (m == AddrSet_Match_ADDRESS_ONLY) {
  40. if (Address_isSameIp(addr, &el->addr)) { return i; }
  41. } else if (m == AddrSet_Match_LABEL_ONLY) {
  42. if (addr->path == el->addr.path) { return i; }
  43. } else if (m == AddrSet_Match_BOTH) {
  44. if (Address_isSame(addr, &el->addr)) { return i; }
  45. } else {
  46. Assert_failure("wtf");
  47. }
  48. }
  49. return -1;
  50. }
  51. int AddrSet_indexOf(struct AddrSet* as, struct Address* addr, enum AddrSet_Match m)
  52. {
  53. struct AddrSet_pvt* ap = Identity_check((struct AddrSet_pvt*) as);
  54. return indexOf(ap, addr, m);
  55. }
  56. void AddrSet_add(struct AddrSet* as, struct Address* addr, enum AddrSet_Match m)
  57. {
  58. struct AddrSet_pvt* ap = Identity_check((struct AddrSet_pvt*) as);
  59. int idx = indexOf(ap, addr, m);
  60. if (idx != -1) { return; }
  61. struct Allocator* alloc = Allocator_child(ap->alloc);
  62. struct Elem* el = Allocator_calloc(alloc, sizeof(struct Elem), 1);
  63. el->alloc = alloc;
  64. Bits_memcpy(&el->addr, addr, sizeof(struct Address));
  65. Identity_set(el);
  66. ArrayList_OfAddrs_add(ap->addrList, el);
  67. ap->pub.length = ap->addrList->length;
  68. }
  69. void AddrSet_remove(struct AddrSet* as, struct Address* addr, enum AddrSet_Match m)
  70. {
  71. struct AddrSet_pvt* ap = Identity_check((struct AddrSet_pvt*) as);
  72. for (;;) {
  73. int idx = indexOf(ap, addr, m);
  74. if (idx == -1) { return; }
  75. struct Elem* el = ArrayList_OfAddrs_get(ap->addrList, idx);
  76. ArrayList_OfAddrs_remove(ap->addrList, idx);
  77. Allocator_free(el->alloc);
  78. ap->pub.length = ap->addrList->length;
  79. }
  80. }
  81. void AddrSet_flush(struct AddrSet* as)
  82. {
  83. struct AddrSet_pvt* ap = Identity_check((struct AddrSet_pvt*) as);
  84. for (int i = 0; i < ap->addrList->length; i++) {
  85. struct Elem* el = ArrayList_OfAddrs_get(ap->addrList, i);
  86. ArrayList_OfAddrs_remove(ap->addrList, i);
  87. Allocator_free(el->alloc);
  88. }
  89. ap->pub.length = 0;
  90. }
  91. struct Address* AddrSet_get(struct AddrSet* as, int i)
  92. {
  93. struct AddrSet_pvt* ap = Identity_check((struct AddrSet_pvt*) as);
  94. struct Elem* el = ArrayList_OfAddrs_get(ap->addrList, i);
  95. if (el) { return &el->addr; }
  96. return NULL;
  97. }
  98. struct AddrSet* AddrSet_new(struct Allocator* allocator)
  99. {
  100. struct Allocator* alloc = Allocator_child(allocator);
  101. struct AddrSet_pvt* out = Allocator_calloc(alloc, sizeof(struct AddrSet_pvt), 1);
  102. out->alloc = alloc;
  103. out->addrList = ArrayList_OfAddrs_new(alloc);
  104. Identity_set(out);
  105. return &out->pub;
  106. }