gensetdyn_iter_nocancel.c (871B)
1 /* ISC license. */ 2 3 #include <stddef.h> 4 #include <stdint.h> 5 6 #include <skalibs/bitarray.h> 7 #include <skalibs/gensetdyn.h> 8 9 uint32_t gensetdyn_iter_nocancel (gensetdyn *g, uint32_t n, iter_func_ref f, void *stuff) 10 { 11 /* 12 XXX: we may be called by a freeing function, so we cannot alloc - 13 XXX: so pray that the bitarray fits in the stack. 14 */ 15 unsigned char bits[bitarray_div8(g->storage.len) ? bitarray_div8(g->storage.len) : 1] ; 16 size_t i = 0 ; 17 uint32_t j = 0 ; 18 uint32_t *fl = genalloc_s(uint32_t, &g->freelist) ; 19 size_t sp = genalloc_len(uint32_t, &g->freelist) ; 20 bitarray_setn(bits, 0, g->storage.len) ; 21 22 for (; i < sp ; i++) if (fl[i] < g->storage.len) bitarray_clear(bits, fl[i]) ; 23 for (i = 0 ; (i < g->storage.len) && (j < n) ; i++) if (bitarray_peek(bits, i)) 24 { 25 j++ ; 26 if (!(*f)(gensetdyn_p(g, i), stuff)) break ; 27 } 28 return j ; 29 }