Statistics
| Branch: | Revision:

grapes / src / ChunkIDSet / chunkids_ops.c @ 176b8de8

History | View | Annotate | Download (2.42 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

    
7
#include <stdlib.h>
8
#include <stdint.h>
9
#include <limits.h>
10
#include <string.h>
11
#include <assert.h>
12

    
13
#include "chunkids_private.h"
14
#include "chunkids_iface.h"
15
#include "chunkidset.h"
16
#include "grapes_config.h"
17

    
18
#define DEFAULT_SIZE_INCREMENT 32
19

    
20
extern struct cids_encoding_iface prio_encoding;
21
extern struct cids_encoding_iface bmap_encoding;
22
extern struct cids_ops_iface list_ops;
23
extern struct cids_ops_iface set_ops;
24

    
25
struct chunkID_set *chunkID_set_init(const char *config)
26
{
27
  struct chunkID_set *p;
28
  struct tag *cfg_tags;
29
  int res;
30
  const char *type;
31

    
32
  p = malloc(sizeof(struct chunkID_set));
33
  if (p == NULL) {
34
    return NULL;
35
  }
36
  p->n_elements = 0;
37
  cfg_tags = grapes_config_parse(config);
38
  if (!cfg_tags) {
39
    free(p);
40
    return NULL;
41
  }
42
  res = grapes_config_value_int(cfg_tags, "size", &p->size);
43
  if (!res) {
44
    p->size = 0;
45
  }
46
  if (p->size) {
47
    p->elements = malloc(p->size * sizeof(int));
48
    if (p->elements == NULL) {
49
      p->size = 0;
50
    }
51
  } else {
52
    p->elements = NULL;
53
  }
54
  p->enc = &prio_encoding;
55
  p->ops = &list_ops;
56
  p->type = CIST_PRIORITY;
57
  type = grapes_config_value_str(cfg_tags, "type");
58
  if (type) {
59
    if (!memcmp(type, "priority", strlen(type) - 1)) {
60
      p->enc = &prio_encoding;
61
      p->ops = &list_ops;
62
      p->type = CIST_PRIORITY;
63
    } else if (!memcmp(type, "bitmap", strlen(type) - 1)) {
64
      p->enc = &bmap_encoding;
65
      p->ops = &set_ops;
66
      p->type = CIST_BITMAP;
67
    } else {
68
      chunkID_set_free(p);
69
      free(cfg_tags);
70

    
71
      return NULL; 
72
    }
73
  }
74
  free(cfg_tags);
75
  assert(p->type == CIST_PRIORITY || p->type == CIST_BITMAP);
76

    
77
  return p;
78
}
79

    
80
int chunkID_set_add_chunk(struct chunkID_set *h, int chunk_id)
81
{
82
  return h->ops->add_chunk(h, chunk_id);
83
}
84

    
85

    
86
int chunkID_set_size(const struct chunkID_set *h)
87
{
88
  return h->n_elements;
89
}
90

    
91
int chunkID_set_get_chunk(const struct chunkID_set *h, int i)
92
{
93
  if (i < h->n_elements) {
94
    return h->elements[i];
95
  }
96

    
97
  return -1;
98
}
99

    
100
int chunkID_set_check(const struct chunkID_set *h, int chunk_id)
101
{
102
  return h->ops->check(h, chunk_id);
103
}
104

    
105
void chunkID_set_clear(struct chunkID_set *h, int size)
106
{
107
  h->n_elements = 0;
108
  h->size = size;
109
  h->elements = realloc(h->elements, size * sizeof(int));
110
  if (h->elements == NULL) {
111
    h->size = 0;
112
  }
113
}
114

    
115
void chunkID_set_free(struct chunkID_set *h)
116
{
117
  chunkID_set_clear(h,0);
118
  free(h->elements);
119
  free(h);
120
}