Lines Matching full:refcount
22 * reference counts. Once the refcount has dropped to zero, it is
38 void ea_refcount_free(ext2_refcount_t refcount)
40 if (!refcount)
43 if (refcount->list)
44 ext2fs_free_mem(&refcount->list);
45 ext2fs_free_mem(&refcount);
50 ext2_refcount_t refcount;
54 retval = ext2fs_get_mem(sizeof(struct ea_refcount), &refcount);
57 memset(refcount, 0, sizeof(struct ea_refcount));
61 refcount->size = size;
64 printf("Refcount allocated %d entries, %d bytes.\n",
65 refcount->size, bytes);
67 retval = ext2fs_get_mem(bytes, &refcount->list);
70 memset(refcount->list, 0, bytes);
72 refcount->count = 0;
73 refcount->cursor = 0;
75 *ret = refcount;
79 ea_refcount_free(refcount);
84 * collapse_refcount() --- go through the refcount array, and get rid
87 static void refcount_collapse(ext2_refcount_t refcount)
92 list = refcount->list;
93 for (i = 0, j = 0; i < refcount->count; i++) {
102 refcount->count, j);
104 refcount->count = j;
112 static struct ea_refcount_el *insert_refcount_el(ext2_refcount_t refcount,
120 if (refcount->count >= refcount->size) {
121 new_size = refcount->size + 100;
123 printf("Reallocating refcount %d entries...\n", new_size);
125 retval = ext2fs_resize_mem((size_t) refcount->size *
129 &refcount->list);
132 refcount->size = new_size;
134 num = (int) refcount->count - pos;
138 memmove(&refcount->list[pos+1], &refcount->list[pos],
141 refcount->count++;
142 el = &refcount->list[pos];
150 * get_refcount_el() --- given an block number, try to find refcount
154 static struct ea_refcount_el *get_refcount_el(ext2_refcount_t refcount,
161 if (!refcount || !refcount->list)
165 high = (int) refcount->count-1;
166 if (create && ((refcount->count == 0) ||
167 (blk > refcount->list[high].ea_blk))) {
168 if (refcount->count >= refcount->size)
169 refcount_collapse(refcount);
171 return insert_refcount_el(refcount, blk,
172 (unsigned) refcount->count);
174 if (refcount->count == 0)
177 if (refcount->cursor >= refcount->count)
178 refcount->cursor = 0;
179 if (blk == refcount->list[refcount->cursor].ea_blk)
180 return &refcount->list[refcount->cursor++];
192 lowval = refcount->list[low].ea_blk;
193 highval = refcount->list[high].ea_blk;
205 if (blk == refcount->list[mid].ea_blk) {
206 refcount->cursor = mid+1;
207 return &refcount->list[mid];
209 if (blk < refcount->list[mid].ea_blk)
219 if (refcount->count >= refcount->size) {
220 refcount_collapse(refcount);
221 if (refcount->count < refcount->size)
224 return insert_refcount_el(refcount, blk, low);
229 errcode_t ea_refcount_fetch(ext2_refcount_t refcount, blk_t blk,
234 el = get_refcount_el(refcount, blk, 0);
243 errcode_t ea_refcount_increment(ext2_refcount_t refcount, blk_t blk, int *ret)
247 el = get_refcount_el(refcount, blk, 1);
257 errcode_t ea_refcount_decrement(ext2_refcount_t refcount, blk_t blk, int *ret)
261 el = get_refcount_el(refcount, blk, 0);
272 errcode_t ea_refcount_store(ext2_refcount_t refcount, blk_t blk, int count)
277 * Get the refcount element
279 el = get_refcount_el(refcount, blk, count ? 1 : 0);
286 blk_t ext2fs_get_refcount_size(ext2_refcount_t refcount)
288 if (!refcount)
291 return refcount->size;
294 void ea_refcount_intr_begin(ext2_refcount_t refcount)
296 refcount->cursor = 0;
300 blk_t ea_refcount_intr_next(ext2_refcount_t refcount,
306 if (refcount->cursor >= refcount->count)
308 list = refcount->list;
309 if (list[refcount->cursor].ea_count) {
311 *ret = list[refcount->cursor].ea_count;
312 return list[refcount->cursor++].ea_blk;
314 refcount->cursor++;
321 errcode_t ea_refcount_validate(ext2_refcount_t refcount, FILE *out)
325 const char *bad = "bad refcount";
327 if (refcount->count > refcount->size) {
331 for (i=1; i < refcount->count; i++) {
332 if (refcount->list[i-1].ea_blk >= refcount->list[i].ea_blk) {
334 bad, i-1, refcount->list[i-1].ea_blk,
335 i, refcount->list[i].ea_blk);
391 ext2_refcount_t refcount;
402 retval = ea_refcount_create(size, &refcount);
408 printf("Creating refcount with size %d\n",
412 ea_refcount_free(refcount);
413 refcount = 0;
414 printf("Freeing refcount\n");
419 retval = ea_refcount_store(refcount, blk, arg);
426 retval = ea_refcount_fetch(refcount, blk, &arg);
435 retval = ea_refcount_increment(refcount, blk,
446 retval = ea_refcount_decrement(refcount, blk,
456 retval = ea_refcount_validate(refcount, stderr);
461 printf("Refcount validation OK.\n");
464 ea_refcount_intr_begin(refcount);
466 blk = ea_refcount_intr_next(refcount,
475 refcount_collapse(refcount);