Statistics
| Branch: | Revision:

grapes / som / ChunkIDSet / chunkids_ops.c @ d99e397d

History | View | Annotate | Download (2.39 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 "chunkidset.h"
15
#include "config.h"
16

    
17
#define DEFAULT_SIZE_INCREMENT 32
18

    
19
struct chunkID_set *chunkID_set_init(const char *config)
20
{
21
  struct chunkID_set *p;
22
  struct tag *cfg_tags;
23
  int res;
24
  const char *type;
25

    
26
  p = malloc(sizeof(struct chunkID_set));
27
  if (p == NULL) {
28
    return NULL;
29
  }
30
  p->n_elements = 0;
31
  cfg_tags = config_parse(config);
32
  res = config_value_int(cfg_tags, "size", &p->size);
33
  if (!res) {
34
    p->size = 0;
35
  }
36
  if (p->size) {
37
    p->elements = malloc(p->size * sizeof(int));
38
    if (p->elements == NULL) {
39
      p->size = 0;
40
    }
41
  } else {
42
    p->elements = NULL;
43
  }
44
  p->type = CIST_PRIORITY;
45
  type = config_value_str(cfg_tags, "type");
46
  if (type) {
47
    if (!memcmp(type, "priority", strlen(type) - 1)) {
48
      p->type = CIST_PRIORITY;
49
    } else if (!memcmp(type, "bitmap", strlen(type) - 1)) {
50
      p->type = CIST_BITMAP;
51
    } else {
52
      chunkID_set_free(p);
53
      free(cfg_tags);
54

    
55
      return NULL; 
56
    }
57
  }
58
  free(cfg_tags);
59
  assert(p->type == CIST_PRIORITY || p->type == CIST_BITMAP);
60

    
61
  return p;
62
}
63

    
64
int chunkID_set_add_chunk(struct chunkID_set *h, int chunk_id)
65
{
66
  if (chunkID_set_check(h, chunk_id) >= 0) {
67
    return 0;
68
  }
69

    
70
  if (h->n_elements == h->size) {
71
    int *res;
72

    
73
    res = realloc(h->elements, (h->size + DEFAULT_SIZE_INCREMENT) * sizeof(int));
74
    if (res == NULL) {
75
      return -1;
76
    }
77
    h->size += DEFAULT_SIZE_INCREMENT;
78
    h->elements = res;
79
  }
80
  h->elements[h->n_elements++] = chunk_id;
81

    
82
  return h->n_elements;
83
}
84

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

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

    
96
  return -1;
97
}
98

    
99
int chunkID_set_check(const struct chunkID_set *h, int chunk_id)
100
{
101
  int i;
102

    
103
  for (i = 0; i < h->n_elements; i++) {
104
    if (h->elements[i] == chunk_id) {
105
      return i;
106
    }
107
  }
108

    
109
  return -1;
110
}
111

    
112
void chunkID_set_clear(struct chunkID_set *h, int size)
113
{
114
  h->n_elements = 0;
115
  h->size = size;
116
  h->elements = realloc(h->elements, size * sizeof(int));
117
  if (h->elements == NULL) {
118
    h->size = 0;
119
  }
120
}
121

    
122
void chunkID_set_free(struct chunkID_set *h)
123
{
124
  chunkID_set_clear(h,0);
125
  free(h->elements);
126
  free(h);
127
}