Added "idlist": maange a list of used ids
This commit is contained in:
parent
48bf0071e4
commit
7c27d1b342
|
@ -0,0 +1,38 @@
|
|||
#ifndef _LIGHTTPD_IDLIST_H_
|
||||
#define _LIGHTTPD_IDLIST_H_
|
||||
|
||||
#include <lighttpd/settings.h>
|
||||
|
||||
typedef struct idlist idlist;
|
||||
|
||||
struct idlist {
|
||||
/* used ids are marked with a "1" in the bitvector (represented as array of gulong) */
|
||||
GArray *bitvector;
|
||||
|
||||
/* all ids are in the range [0, max_ids[, i.e. 0 <= id < max_ids
|
||||
* although the type is guint, it has to fit in a gint too, as we
|
||||
* use gint for the ids in the interface, so we can use -1 as a special value.
|
||||
*/
|
||||
guint max_ids;
|
||||
|
||||
/* if all ids in [0, used_ids-1] are used, next_free_id is -1
|
||||
* if not, then all available ids are >= next_free_id,
|
||||
* so we can start at next_free_id for searching the next free id
|
||||
*/
|
||||
gint next_free_id;
|
||||
guint used_ids;
|
||||
};
|
||||
|
||||
/* create new idlist; the parameter max_ids is "signed" on purpose */
|
||||
LI_API idlist* idlist_new(gint max_ids);
|
||||
|
||||
/* free idlist */
|
||||
LI_API void idlist_free(idlist *l);
|
||||
|
||||
/* requst new id; return -1 if no id is available, valid ids are always > 0 */
|
||||
LI_API gint idlist_get(idlist *l);
|
||||
|
||||
/* release id. never release a id more than once! */
|
||||
LI_API void idlist_put(idlist *l, gint id);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,105 @@
|
|||
|
||||
#include <lighttpd/idlist.h>
|
||||
|
||||
#define UL_BITS (sizeof(ulong) * 8)
|
||||
|
||||
/* There are often no explicit bit shifts used in this code. This is on purpose, the
|
||||
* code looks much cleaner without them, the correct constant for *, / and % is easier to calculate
|
||||
* as constant (UL_BITS) and the compiler should know how to optimize the operations; as UL_BITS is hopefully
|
||||
* of the form 2^n this should result in bit shifts in the executable code.
|
||||
*/
|
||||
|
||||
idlist* idlist_new(gint max_ids) {
|
||||
idlist *l = g_slice_new0(idlist);
|
||||
g_assert(max_ids > 0);
|
||||
l->bitvector = g_array_new(FALSE, TRUE, sizeof(gulong));
|
||||
l->max_ids = max_ids;
|
||||
l->next_free_id = -1;
|
||||
l->used_ids = 0;
|
||||
return l;
|
||||
}
|
||||
|
||||
void idlist_free(idlist *l) {
|
||||
if (!l) return;
|
||||
g_array_free(l->bitvector, TRUE);
|
||||
g_slice_free(idlist, l);
|
||||
}
|
||||
|
||||
static void mark_bit(GArray *a, gint id) {
|
||||
guint ndx = id / UL_BITS, bndx = id % UL_BITS;
|
||||
gulong bmask = 1 << bndx;
|
||||
g_assert(id >= 0 && ndx < a->len);
|
||||
|
||||
g_assert(0 == (g_array_index(a, gulong, ndx) & (bmask))); /* bit musn't be set */
|
||||
g_array_index(a, gulong, ndx) |= (bmask);
|
||||
}
|
||||
|
||||
static void clear_bit(GArray *a, gint id) {
|
||||
guint ndx = id / UL_BITS, bndx = id % UL_BITS;
|
||||
gulong bmask = 1 << bndx;
|
||||
g_assert(id >= 0 && ndx < a->len);
|
||||
|
||||
g_assert(0 != (g_array_index(a, gulong, ndx) & (bmask))); /* bit must be set */
|
||||
g_array_index(a, gulong, ndx) &= ~(bmask);
|
||||
}
|
||||
|
||||
static void idlist_reserve(GArray *a, guint id) {
|
||||
guint ndx = id / UL_BITS;
|
||||
if (ndx >= a->len) g_array_set_size(a, ndx+1);
|
||||
}
|
||||
|
||||
gint idlist_get(idlist *l) {
|
||||
guint fndx, ndx;
|
||||
gint newid, bndx;
|
||||
gulong u = -1;
|
||||
GArray *a = l->bitvector;
|
||||
if (l->used_ids >= l->max_ids) return -1;
|
||||
|
||||
if (l->next_free_id < 0) { /* all ids in use */
|
||||
newid = l->used_ids++;
|
||||
idlist_reserve(a, newid);
|
||||
mark_bit(a, newid);
|
||||
return newid;
|
||||
}
|
||||
|
||||
/* search for an array entry which doesn't have all bits set (i.e. != (gulong) -1)
|
||||
* start with the entry of next_free_id, all below are in use anyway
|
||||
*/
|
||||
fndx = l->next_free_id / UL_BITS;
|
||||
for (ndx = fndx; ndx < a->len && ((gulong) -1 == (u = g_array_index(a, gulong, ndx))); ndx++) ;
|
||||
|
||||
if (ndx == a->len) { /* again: all ids are in use */
|
||||
l->next_free_id = -1;
|
||||
newid = l->used_ids++;
|
||||
idlist_reserve(a, newid);
|
||||
mark_bit(a, newid);
|
||||
return newid;
|
||||
}
|
||||
|
||||
/* array entry != -1, search for free bit */
|
||||
if (fndx == ndx) bndx = (l->next_free_id / UL_BITS) - 1;
|
||||
else bndx = -1;
|
||||
bndx = g_bit_nth_lsf(~u, bndx);
|
||||
|
||||
/* no free bit found; should never happen as u != -1 and next_free_id should be correct, i.e. all bits <= the bit start index should be set */
|
||||
g_assert(bndx != -1);
|
||||
|
||||
newid = ndx * UL_BITS + bndx;
|
||||
if (newid == (gint) l->used_ids) {
|
||||
l->next_free_id = -1;
|
||||
} else {
|
||||
l->next_free_id = newid+1;
|
||||
}
|
||||
|
||||
l->used_ids++;
|
||||
mark_bit(a, newid);
|
||||
|
||||
return newid;
|
||||
}
|
||||
|
||||
void idlist_put(idlist *l, gint id) {
|
||||
clear_bit(l->bitvector, id);
|
||||
|
||||
l->used_ids--;
|
||||
if ((l->next_free_id < 0 && (guint) id < l->used_ids) || id < l->next_free_id) l->next_free_id = id;
|
||||
}
|
Loading…
Reference in New Issue