Revision 2843a018

View differences:

chunklock.c
4 4
 *
5 5
 *  This is free software; see gpl-3.0.txt
6 6
 */
7
#include <stdlib.h>
8
#include <stdio.h>
9
#include <string.h>
10
#include <stdint.h>
11

  
7 12
#include "chunklock.h"
8 13

  
9
static struct chunkID_set *lock_set;
14
#include "net_helper.h"
10 15

  
11
void chunk_lock(int chunkid,struct peer *from){
12
  if (!lock_set) lock_set = chunkID_set_init(16);
16
struct lock {
17
  int chunkid;
18
  struct nodeID *peer;
19
  struct timeval timestamp;
20
};
21

  
22
struct lock *locks;
23
static size_t lsize, lcount=0;
24
#define LSIZE_INCREMENT 10
25

  
26
void locks_init()
27
{
28
  if (!locks) {
29
    lsize = LSIZE_INCREMENT;
30
    locks = malloc(sizeof(struct lock) * lsize);
31
    lcount = 0;
32
  }
33

  
34
  if (lcount == lsize) {
35
    lsize += LSIZE_INCREMENT;
36
    locks = realloc(locks , sizeof(struct lock) * lsize);
37
  }
13 38

  
14
  chunkID_set_add_chunk(lock_set, chunkid);
39
  if (!locks) {
40
    fprintf(stderr, "Error allocating memory for locks!\n");
41
    exit(EXIT_FAILURE);
42
  }
43
}
44

  
45

  
46
void chunk_lock(int chunkid,struct peer *from){
47
  locks_init();
48
  locks[lcount].chunkid = chunkid;
49
  locks[lcount].peer = nodeid_dup(from->id);
50
  gettimeofday(&locks[lcount].timestamp,NULL);
51
  lcount++;
15 52
}
16 53

  
17 54
void chunk_unlock(int chunkid){
18
  if (!lock_set) return;
19
  chunkID_set_remove_chunk(lock_set, chunkid);
55
  int i;
56

  
57
  for (i=0; i<lcount; i++) {
58
    if (locks[i].chunkid == chunkid) {
59
      nodeID_free(locks[i].peer);
60
      memmove(locks+i, locks+i+1, sizeof(struct lock) * (lcount-i-1));
61
      lcount--;
62
      break;
63
    }
64
  }
20 65
}
21 66

  
22 67
int chunk_islocked(int chunkid){
23
  int r;
68
  int i;
24 69

  
25
  if (!lock_set) return 0;
26
  r = chunkID_set_check(lock_set, chunkid);
27
  return (r >= 0);
70
  for (i=0; i<lcount; i++) {
71
    if (locks[i].chunkid == chunkid) {
72
      return 1;
73
    }
74
  }
75
  return 0;
28 76
}

Also available in: Unified diff