-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfinger.c
44 lines (34 loc) · 1.03 KB
/
finger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "finger.h"
Finger* finger_init(Node *node, int start) {
Finger *finger = NULL;
if ((finger = malloc(sizeof(Finger))) == NULL) {
BAIL("Failed to allocate memory for Finger");
}
finger->node = node;
finger->start = start;
return finger;
}
FingerTable* finger_table_init(Node *node) {
int i;
Finger *finger = NULL;
FingerTable *finger_table = NULL;
int start;
/* allocate finger table memory */
if ((finger_table = malloc(sizeof(FingerTable))) == NULL) {
BAIL("Failed to allocate memory for FingerTable");
}
finger_table->length = KEY_BITS;
/* allocate fingers in the table */
if ((finger_table->fingers = malloc(sizeof(Finger) * finger_table->length)) == NULL) {
BAIL("Failed to allocate memory for Finger");
}
for (i = 0; i < finger_table->length; i++) {
start = node->key + pow(2, i);
if (start > ring_key_max()) {
start -= ring_key_max();
}
finger = finger_init(node, start);
finger_table->fingers[i] = finger;
}
return finger_table;
}