summaryrefslogtreecommitdiff
path: root/idlist.c
diff options
context:
space:
mode:
authorStefan Bühler <stbuehler@web.de>2010-09-09 14:26:43 +0200
committerStefan Bühler <stbuehler@web.de>2010-09-09 14:26:43 +0200
commit7a88d7a2887dcfea8371e7e84bb905a2527f4133 (patch)
tree3c3b23528af46391a45209b6b2f187dd76ebd3f0 /idlist.c
parent87bb51239cb3ecd64407c4e06fb14e35da21db8c (diff)
downloadlibmanda-7a88d7a2887dcfea8371e7e84bb905a2527f4133.tar.gz
libmanda-7a88d7a2887dcfea8371e7e84bb905a2527f4133.zip
Add idlist, code basic message handling
Diffstat (limited to 'idlist.c')
-rw-r--r--idlist.c114
1 files changed, 114 insertions, 0 deletions
diff --git a/idlist.c b/idlist.c
new file mode 100644
index 0000000..56e3e68
--- /dev/null
+++ b/idlist.c
@@ -0,0 +1,114 @@
+
+#include "idlist.h"
+
+#define UL_BITS (sizeof(gulong) * 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.
+ */
+
+manda_IDList* manda_idlist_new(gint max_ids) {
+ manda_IDList *l = g_slice_new0(manda_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 manda_idlist_free(manda_IDList *l) {
+ if (!l) return;
+ g_array_free(l->bitvector, TRUE);
+ g_slice_free(manda_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 manda_idlist_get(manda_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;
+}
+
+gboolean manda_idlist_is_used(manda_IDList *l, gint id) {
+ GArray *a = l->bitvector;
+ guint ndx = id / UL_BITS, bndx = id % UL_BITS;
+ gulong bmask = 1 << bndx;
+ if (id < 0 || ndx >= a->len) return FALSE;
+
+ return (0 != (g_array_index(a, gulong, ndx) & (bmask)));
+}
+
+void manda_idlist_put(manda_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;
+}