peer.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. unsigned int i;
  92. if (NULL == pid)
  93. return 0;
  94. if (NULL == map)
  95. map = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
  96. e = GNUNET_CONTAINER_multipeermap_get (map, pid);
  97. if (NULL != e)
  98. {
  99. GNUNET_assert (e->rc > 0);
  100. e->rc++;
  101. return e->pid;
  102. }
  103. ret = free_list_start;
  104. if (ret == size)
  105. {
  106. GNUNET_array_grow (table, size, size + 16);
  107. for (i = ret; i < size; i++)
  108. {
  109. table[i] = GNUNET_new (struct PeerEntry);
  110. table[i]->pid = i + 1;
  111. }
  112. }
  113. if (0 == ret)
  114. {
  115. memset (&table[0]->id, 0, sizeof (struct GNUNET_PeerIdentity));
  116. table[0]->pid = 0;
  117. table[0]->rc = 1;
  118. ret = 1;
  119. }
  120. GNUNET_assert (ret < size);
  121. GNUNET_assert (0 == table[ret]->rc);
  122. free_list_start = table[ret]->pid;
  123. table[ret]->id = *pid;
  124. table[ret]->rc = 1;
  125. table[ret]->pid = ret;
  126. GNUNET_break (GNUNET_OK ==
  127. GNUNET_CONTAINER_multipeermap_put (map,
  128. &table[ret]->id,
  129. table[ret],
  130. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  131. return ret;
  132. }
  133. /**
  134. * Decrement multiple RCs of peer identities by one.
  135. *
  136. * @param ids array of PIDs to decrement the RCs of
  137. * @param count size of the ids array
  138. */
  139. void
  140. GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
  141. {
  142. int i;
  143. GNUNET_PEER_Id id;
  144. if (0 == count)
  145. return;
  146. for (i = count - 1; i >= 0; i--)
  147. {
  148. id = ids[i];
  149. if (0 == id)
  150. continue;
  151. GNUNET_assert (id < size);
  152. GNUNET_assert (table[id]->rc > 0);
  153. table[id]->rc--;
  154. if (0 == table[id]->rc)
  155. {
  156. GNUNET_break (GNUNET_OK ==
  157. GNUNET_CONTAINER_multipeermap_remove (map,
  158. &table[id]->id,
  159. table[id]));
  160. table[id]->pid = free_list_start;
  161. free_list_start = id;
  162. }
  163. }
  164. }
  165. /**
  166. * Change the reference counter of an interned PID.
  167. *
  168. * @param id identity to change the RC of
  169. * @param delta how much to change the RC
  170. */
  171. void
  172. GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
  173. {
  174. if (0 == id)
  175. return;
  176. GNUNET_assert (id < size);
  177. GNUNET_assert (table[id]->rc > 0);
  178. GNUNET_assert ( (delta >= 0) ||
  179. (table[id]->rc >= (unsigned int) (-delta)) );
  180. table[id]->rc += delta;
  181. if (0 == table[id]->rc)
  182. {
  183. GNUNET_break (GNUNET_OK ==
  184. GNUNET_CONTAINER_multipeermap_remove (map,
  185. &table[id]->id,
  186. table[id]));
  187. table[id]->pid = free_list_start;
  188. free_list_start = id;
  189. }
  190. }
  191. /**
  192. * Convert an interned PID to a normal peer identity.
  193. *
  194. * @param id interned PID to convert
  195. * @param pid where to write the normal peer identity
  196. */
  197. void
  198. GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
  199. {
  200. if (0 == id)
  201. {
  202. memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
  203. return;
  204. }
  205. GNUNET_assert (id < size);
  206. GNUNET_assert (table[id]->rc > 0);
  207. *pid = table[id]->id;
  208. }
  209. /**
  210. * Convert an interned PID to a normal peer identity.
  211. *
  212. * @param id interned PID to convert
  213. * @return pointer to peer identity, valid as long 'id' is valid
  214. */
  215. const struct GNUNET_PeerIdentity *
  216. GNUNET_PEER_resolve2 (GNUNET_PEER_Id id)
  217. {
  218. GNUNET_assert (id < size);
  219. GNUNET_assert (table[id]->rc > 0);
  220. return &table[id]->id;
  221. }
  222. /* end of peer.c */