Revision 74c838a8 nest/rt-attr.c

View differences:

nest/rt-attr.c
52 52
#include "nest/attrs.h"
53 53
#include "lib/alloca.h"
54 54
#include "lib/hash.h"
55
#include "lib/idm.h"
55 56
#include "lib/resource.h"
56 57
#include "lib/string.h"
57 58

  
......
61 62
static slab *mpnh_slab;
62 63
static slab *rte_src_slab;
63 64

  
64
/* rte source ID bitmap */
65
static u32 *src_ids;
66
static u32 src_id_size, src_id_used, src_id_pos;
65
static struct idm src_ids;
67 66
#define SRC_ID_INIT_SIZE 4
68 67

  
69 68
/* rte source hash */
......
87 86
{
88 87
  rte_src_slab = sl_new(rta_pool, sizeof(struct rte_src));
89 88

  
90
  src_id_pos = 0;
91
  src_id_size = SRC_ID_INIT_SIZE;
92
  src_ids = mb_allocz(rta_pool, src_id_size * sizeof(u32));
93

  
94
 /* ID 0 is reserved */
95
  src_ids[0] = 1;
96
  src_id_used = 1;
89
  idm_init(&src_ids, rta_pool, SRC_ID_INIT_SIZE);
97 90

  
98 91
  HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER);
99 92
}
100 93

  
101
static inline int u32_cto(uint x) { return ffs(~x) - 1; }
102

  
103
static inline u32
104
rte_src_alloc_id(void)
105
{
106
  int i, j;
107
  for (i = src_id_pos; i < src_id_size; i++)
108
    if (src_ids[i] != 0xffffffff)
109
      goto found;
110

  
111
  /* If we are at least 7/8 full, expand */
112
  if (src_id_used > (src_id_size * 28))
113
    {
114
      src_id_size *= 2;
115
      src_ids = mb_realloc(src_ids, src_id_size * sizeof(u32));
116
      bzero(src_ids + i, (src_id_size - i) * sizeof(u32));
117
      goto found;
118
    }
119

  
120
  for (i = 0; i < src_id_pos; i++)
121
    if (src_ids[i] != 0xffffffff)
122
      goto found;
123

  
124
  ASSERT(0);
125

  
126
 found:
127
  ASSERT(i < 0x8000000);
128

  
129
  src_id_pos = i;
130
  j = u32_cto(src_ids[i]);
131

  
132
  src_ids[i] |= (1 << j);
133
  src_id_used++;
134
  return 32 * i + j;
135
}
136

  
137
static inline void
138
rte_src_free_id(u32 id)
139
{
140
  int i = id / 32;
141
  int j = id % 32;
142

  
143
  ASSERT((i < src_id_size) && (src_ids[i] & (1 << j)));
144
  src_ids[i] &= ~(1 << j);
145
  src_id_used--;
146
}
147

  
148 94

  
149 95
HASH_DEFINE_REHASH_FN(RSH, struct rte_src)
150 96

  
......
165 111
  src = sl_alloc(rte_src_slab);
166 112
  src->proto = p;
167 113
  src->private_id = id;
168
  src->global_id = rte_src_alloc_id();
114
  src->global_id = idm_alloc(&src_ids);
169 115
  src->uc = 0;
170 116

  
171 117
  HASH_INSERT2(src_hash, RSH, rta_pool, src);
......
181 127
    if (src->uc == 0)
182 128
    {
183 129
      HASH_DO_REMOVE(src_hash, RSH, sp);
184
      rte_src_free_id(src->global_id);
130
      idm_free(&src_ids, src->global_id);
185 131
      sl_free(rte_src_slab, src);
186 132
    }
187 133
  }

Also available in: Unified diff