diff options
author | Christian Grothoff <christian@grothoff.org> | 2009-09-15 15:58:09 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2009-09-15 15:58:09 +0000 |
commit | b7d63eaae6bfb3e72a452d1841e808a0159be173 (patch) | |
tree | fde0992e88eba130b83baa36f984d9718e3ce5e4 /src/util/peer.c | |
parent | 5cccb4380fdeb089860a70df5befe0116fdc875a (diff) | |
download | gnunet-b7d63eaae6bfb3e72a452d1841e808a0159be173.tar.gz gnunet-b7d63eaae6bfb3e72a452d1841e808a0159be173.zip |
move
Diffstat (limited to 'src/util/peer.c')
-rw-r--r-- | src/util/peer.c | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/src/util/peer.c b/src/util/peer.c new file mode 100644 index 000000000..acb1b3922 --- /dev/null +++ b/src/util/peer.c | |||
@@ -0,0 +1,212 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2006, 2008, 2009 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file util/peer.c | ||
23 | * @brief peer-ID table that assigns integer IDs to peer-IDs to save memory | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_peer_lib.h" | ||
29 | |||
30 | |||
31 | struct PeerEntry | ||
32 | { | ||
33 | /** | ||
34 | * the identifier itself | ||
35 | */ | ||
36 | struct GNUNET_PeerIdentity id; | ||
37 | |||
38 | /** | ||
39 | * Short version of the identifier; | ||
40 | * if the RC==0, then index of next | ||
41 | * free slot in table, otherwise | ||
42 | * equal to this slot in the table. | ||
43 | */ | ||
44 | GNUNET_PEER_Id pid; | ||
45 | |||
46 | /** | ||
47 | * Reference counter, 0 if this slot | ||
48 | * is not used. | ||
49 | */ | ||
50 | unsigned int rc; | ||
51 | }; | ||
52 | |||
53 | |||
54 | /** | ||
55 | * Table with our interned peer IDs. | ||
56 | */ | ||
57 | static struct PeerEntry *table; | ||
58 | |||
59 | /** | ||
60 | * Hashmap of PeerIdentities to "struct PeerEntry" | ||
61 | * (for fast lookup). NULL until the library | ||
62 | * is actually being used. | ||
63 | */ | ||
64 | static struct GNUNET_CONTAINER_MultiHashMap *map; | ||
65 | |||
66 | /** | ||
67 | * Size of the "table". | ||
68 | */ | ||
69 | static unsigned int size; | ||
70 | |||
71 | /** | ||
72 | * Index of the beginning of the free list in the table; set to "size" | ||
73 | * if no slots are free in the table. | ||
74 | */ | ||
75 | static unsigned int free_list_start; | ||
76 | |||
77 | |||
78 | /** | ||
79 | * Intern an peer identity. If the identity is already known, its | ||
80 | * reference counter will be increased by one. | ||
81 | * | ||
82 | * @param pid identity to intern | ||
83 | * @return the interned identity. | ||
84 | */ | ||
85 | GNUNET_PEER_Id | ||
86 | GNUNET_PEER_intern (const struct GNUNET_PeerIdentity * pid) | ||
87 | { | ||
88 | GNUNET_PEER_Id ret; | ||
89 | struct PeerEntry *e; | ||
90 | unsigned int i; | ||
91 | |||
92 | if (pid == NULL) | ||
93 | return 0; | ||
94 | if (NULL == map) | ||
95 | map = GNUNET_CONTAINER_multihashmap_create (32); | ||
96 | e = GNUNET_CONTAINER_multihashmap_get (map, | ||
97 | &pid->hashPubKey); | ||
98 | if (e != NULL) | ||
99 | { | ||
100 | GNUNET_assert (e->rc > 0); | ||
101 | e->rc++; | ||
102 | return e->pid; | ||
103 | } | ||
104 | ret = free_list_start; | ||
105 | if (ret == size) | ||
106 | { | ||
107 | GNUNET_array_grow (table, size, size + 16); | ||
108 | for (i=ret;i<size;i++) | ||
109 | table[i].pid = i + 1; | ||
110 | } | ||
111 | if (ret == 0) | ||
112 | { | ||
113 | table[0].pid = 0; | ||
114 | table[0].rc = 1; | ||
115 | ret = 1; | ||
116 | } | ||
117 | GNUNET_assert (ret < size); | ||
118 | GNUNET_assert (table[ret].rc == 0); | ||
119 | free_list_start = table[ret].pid; | ||
120 | table[ret].id = *pid; | ||
121 | table[ret].rc = 1; | ||
122 | table[ret].pid = ret; | ||
123 | GNUNET_CONTAINER_multihashmap_put (map, | ||
124 | &pid->hashPubKey, | ||
125 | &table[ret], | ||
126 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
127 | return ret; | ||
128 | } | ||
129 | |||
130 | |||
131 | /** | ||
132 | * Decrement multiple RCs of peer identities by one. | ||
133 | * | ||
134 | * @param ids array of PIDs to decrement the RCs of | ||
135 | * @param count size of the ids array | ||
136 | */ | ||
137 | void | ||
138 | GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id * ids, | ||
139 | unsigned int count) | ||
140 | { | ||
141 | int i; | ||
142 | GNUNET_PEER_Id id; | ||
143 | |||
144 | if (count == 0) | ||
145 | return; | ||
146 | for (i = count - 1; i >= 0; i--) | ||
147 | { | ||
148 | id = ids[i]; | ||
149 | GNUNET_assert (id < size); | ||
150 | GNUNET_assert (table[id].rc > 0); | ||
151 | table[id].rc--; | ||
152 | if (table[id].rc == 0) | ||
153 | { | ||
154 | GNUNET_CONTAINER_multihashmap_remove (map, | ||
155 | &table[id].id.hashPubKey, | ||
156 | &table[id]); | ||
157 | table[id].pid = free_list_start; | ||
158 | free_list_start = id; | ||
159 | } | ||
160 | } | ||
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 (id == 0) | ||
174 | return; | ||
175 | GNUNET_assert (id < size); | ||
176 | GNUNET_assert (table[id].rc > 0); | ||
177 | GNUNET_assert ((delta >= 0) || (table[id].rc >= -delta)); | ||
178 | table[id].rc += delta; | ||
179 | if (table[id].rc == 0) | ||
180 | { | ||
181 | GNUNET_CONTAINER_multihashmap_remove (map, | ||
182 | &table[id].id.hashPubKey, | ||
183 | &table[id]); | ||
184 | table[id].pid = free_list_start; | ||
185 | free_list_start = id; | ||
186 | } | ||
187 | } | ||
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, | ||
198 | struct GNUNET_PeerIdentity * pid) | ||
199 | { | ||
200 | if (id == 0) | ||
201 | { | ||
202 | memset (pid, 0, sizeof (struct GNUNET_PeerIdentity)); | ||
203 | GNUNET_break (0); | ||
204 | return; | ||
205 | } | ||
206 | GNUNET_assert (id < size); | ||
207 | GNUNET_assert (table[id].rc > 0); | ||
208 | *pid = table[id].id; | ||
209 | } | ||
210 | |||
211 | |||
212 | /* end of peer.c */ | ||