peer.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2006, 2008, 2009 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/peer.c
  18. * @brief peer-ID table that assigns integer IDs to peer-IDs to save memory
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_peer_lib.h"
  23. #define LOG(kind, ...) GNUNET_log_from (kind, "util-peer", __VA_ARGS__)
  24. struct PeerEntry
  25. {
  26. /**
  27. * The identifier itself
  28. */
  29. struct GNUNET_PeerIdentity id;
  30. /**
  31. * Short version of the identifier; if the RC==0, then index of next
  32. * free slot in table, otherwise equal to this slot in the table.
  33. */
  34. GNUNET_PEER_Id pid;
  35. /**
  36. * Reference counter, 0 if this slot is not used.
  37. */
  38. unsigned int rc;
  39. };
  40. /**
  41. * Table with our interned peer IDs.
  42. */
  43. static struct PeerEntry **table;
  44. /**
  45. * Peermap of PeerIdentities to "struct PeerEntry"
  46. * (for fast lookup). NULL until the library
  47. * is actually being used.
  48. */
  49. static struct GNUNET_CONTAINER_MultiPeerMap *map;
  50. /**
  51. * Size of the "table".
  52. */
  53. static unsigned int size;
  54. /**
  55. * Index of the beginning of the free list in the table; set to "size"
  56. * if no slots are free in the table.
  57. */
  58. static unsigned int free_list_start;
  59. /**
  60. * Search for a peer identity. The reference counter is not changed.
  61. *
  62. * @param pid identity to find
  63. * @return the interned identity or 0.
  64. */
  65. GNUNET_PEER_Id
  66. GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
  67. {
  68. struct PeerEntry *e;
  69. if (NULL == pid)
  70. return 0;
  71. if (NULL == map)
  72. return 0;
  73. e = GNUNET_CONTAINER_multipeermap_get (map, pid);
  74. if (NULL == e)
  75. return 0;
  76. GNUNET_assert (e->rc > 0);
  77. return e->pid;
  78. }
  79. /**
  80. * Intern an peer identity. If the identity is already known, its
  81. * reference counter will be increased by one.
  82. *
  83. * @param pid identity to intern
  84. * @return the interned identity.
  85. */
  86. GNUNET_PEER_Id
  87. GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
  88. {
  89. GNUNET_PEER_Id ret;
  90. struct PeerEntry *e;
  91. if (NULL == pid)
  92. return 0;
  93. if (NULL == map)
  94. map = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
  95. e = GNUNET_CONTAINER_multipeermap_get (map, pid);
  96. if (NULL != e)
  97. {
  98. GNUNET_assert (e->rc > 0);
  99. e->rc++;
  100. return e->pid;
  101. }
  102. ret = free_list_start;
  103. if (ret == size)
  104. {
  105. GNUNET_array_grow (table, size, size + 16);
  106. for (unsigned int i = ret; i < size; i++)
  107. {
  108. table[i] = GNUNET_new (struct PeerEntry);
  109. table[i]->pid = i + 1;
  110. }
  111. }
  112. if (0 == ret)
  113. {
  114. memset (&table[0]->id, 0, sizeof(struct GNUNET_PeerIdentity));
  115. table[0]->pid = 0;
  116. table[0]->rc = 1;
  117. ret = 1;
  118. }
  119. GNUNET_assert (ret < size);
  120. GNUNET_assert (0 == table[ret]->rc);
  121. free_list_start = table[ret]->pid;
  122. table[ret]->id = *pid;
  123. table[ret]->rc = 1;
  124. table[ret]->pid = ret;
  125. GNUNET_break (GNUNET_OK ==
  126. GNUNET_CONTAINER_multipeermap_put (map,
  127. &table[ret]->id,
  128. table[ret],
  129. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  130. return ret;
  131. }
  132. /**
  133. * Decrement multiple RCs of peer identities by one.
  134. *
  135. * @param ids array of PIDs to decrement the RCs of
  136. * @param count size of the ids array
  137. */
  138. void
  139. GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
  140. {
  141. int i;
  142. GNUNET_PEER_Id id;
  143. if (0 == count)
  144. return;
  145. for (i = count - 1; i >= 0; i--)
  146. {
  147. id = ids[i];
  148. if (0 == id)
  149. continue;
  150. GNUNET_assert (id < size);
  151. GNUNET_assert (table[id]->rc > 0);
  152. table[id]->rc--;
  153. if (0 == table[id]->rc)
  154. {
  155. GNUNET_break (GNUNET_OK ==
  156. GNUNET_CONTAINER_multipeermap_remove (map,
  157. &table[id]->id,
  158. table[id]));
  159. table[id]->pid = free_list_start;
  160. free_list_start = id;
  161. }
  162. }
  163. }
  164. /**
  165. * Change the reference counter of an interned PID.
  166. *
  167. * @param id identity to change the RC of
  168. * @param delta how much to change the RC
  169. */
  170. void
  171. GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
  172. {
  173. if (0 == id)
  174. return;
  175. GNUNET_assert (id < size);
  176. GNUNET_assert (table[id]->rc > 0);
  177. GNUNET_assert ((delta >= 0) ||
  178. (table[id]->rc >= (unsigned int) (-delta)));
  179. table[id]->rc += delta;
  180. if (0 == table[id]->rc)
  181. {
  182. GNUNET_break (GNUNET_OK ==
  183. GNUNET_CONTAINER_multipeermap_remove (map,
  184. &table[id]->id,
  185. table[id]));
  186. table[id]->pid = free_list_start;
  187. free_list_start = id;
  188. }
  189. }
  190. /**
  191. * Convert an interned PID to a normal peer identity.
  192. *
  193. * @param id interned PID to convert
  194. * @param pid where to write the normal peer identity
  195. */
  196. void
  197. GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
  198. {
  199. if (0 == id)
  200. {
  201. memset (pid, 0, sizeof(struct GNUNET_PeerIdentity));
  202. return;
  203. }
  204. GNUNET_assert (id < size);
  205. GNUNET_assert (table[id]->rc > 0);
  206. *pid = table[id]->id;
  207. }
  208. /**
  209. * Convert an interned PID to a normal peer identity.
  210. *
  211. * @param id interned PID to convert
  212. * @return pointer to peer identity, valid as long 'id' is valid
  213. */
  214. const struct GNUNET_PeerIdentity *
  215. GNUNET_PEER_resolve2 (GNUNET_PEER_Id id)
  216. {
  217. GNUNET_assert (id < size);
  218. GNUNET_assert (table[id]->rc > 0);
  219. return &table[id]->id;
  220. }
  221. /* end of peer.c */