summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2015-02-19 16:39:56 +0100
committerRichard Braun <rbraun@sceen.net>2015-02-19 16:39:56 +0100
commit97c3e15455de116b0849db93eb0e983e9ea8aa94 (patch)
treead52e50bc156ebbf1bc345f19ffae930237721c0
parentbedbd788577ee2361a1e95a9aad020c88de9994c (diff)
bitmap: new module
Imported from the X15 microkernel and relicensed.
-rw-r--r--Makefile.am3
-rw-r--r--bitmap.c116
-rw-r--r--bitmap.h185
-rw-r--r--bitmap_i.h68
4 files changed, 372 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am
index 7018878..b5d255d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -18,6 +18,9 @@ librbraun_la_SOURCES = \
avltree.c \
avltree.h \
avltree_i.h \
+ bitmap.c \
+ bitmap.h \
+ bitmap_i.h \
cpu.h \
error.c \
error.h \
diff --git a/bitmap.c b/bitmap.c
new file mode 100644
index 0000000..7ac1662
--- /dev/null
+++ b/bitmap.c
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2013-2015 Richard Braun.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <limits.h>
+#include <stddef.h>
+#include <string.h>
+
+#include "bitmap.h"
+#include "bitmap_i.h"
+
+int
+bitmap_cmp(const unsigned long *a, const unsigned long *b, int nr_bits)
+{
+ unsigned long last_a, last_b, mask;
+ int n, rv;
+
+ n = BITMAP_LONGS(nr_bits) - 1;
+
+ if (n != 0) {
+ rv = memcmp(a, b, n * sizeof(unsigned long));
+
+ if (rv != 0)
+ return rv;
+
+ nr_bits -= n * LONG_BIT;
+ }
+
+ last_a = a[n];
+ last_b = b[n];
+
+ if (nr_bits != LONG_BIT) {
+ mask = (1UL << nr_bits) - 1;
+ last_a &= mask;
+ last_b &= mask;
+ }
+
+ if (last_a == last_b)
+ return 0;
+ else if (last_a < last_b)
+ return -1;
+ else
+ return 1;
+}
+
+static inline unsigned long
+bitmap_find_next_compute_complement(unsigned long word, int nr_bits)
+{
+ if (nr_bits < LONG_BIT)
+ word |= (((unsigned long)-1) << nr_bits);
+
+ return ~word;
+}
+
+int
+bitmap_find_next_bit(const unsigned long *bm, int nr_bits, int bit,
+ int complement)
+{
+ const unsigned long *start, *end;
+ unsigned long word;
+
+ start = bm;
+ end = bm + BITMAP_LONGS(nr_bits);
+
+ if (bit >= LONG_BIT) {
+ bitmap_lookup(bm, bit);
+ nr_bits -= ((bm - start) * LONG_BIT);
+ }
+
+ word = *bm;
+
+ if (complement)
+ word = bitmap_find_next_compute_complement(word, nr_bits);
+
+ if (bit < LONG_BIT)
+ word &= ~(bitmap_mask(bit) - 1);
+
+ for (;;) {
+ bit = __builtin_ffsl(word);
+
+ if (bit != 0)
+ return ((bm - start) * LONG_BIT) + bit - 1;
+
+ bm++;
+
+ if (bm >= end)
+ return -1;
+
+ nr_bits -= LONG_BIT;
+ word = *bm;
+
+ if (complement)
+ word = bitmap_find_next_compute_complement(word, nr_bits);
+ }
+}
diff --git a/bitmap.h b/bitmap.h
new file mode 100644
index 0000000..9e6efad
--- /dev/null
+++ b/bitmap.h
@@ -0,0 +1,185 @@
+/*
+ * Copyright (c) 2013-2015 Richard Braun.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ *
+ * Arbitrary-length bit arrays.
+ *
+ * Most functions do not check whether the given parameters are valid. This
+ * is the responsibility of the caller.
+ */
+
+#ifndef _BITMAP_H
+#define _BITMAP_H
+
+#include <limits.h>
+#include <string.h>
+
+#include "bitmap_i.h"
+#include "x86/atomic.h"
+
+#define BITMAP_DECLARE(name, nr_bits) unsigned long name[BITMAP_LONGS(nr_bits)]
+
+int bitmap_cmp(const unsigned long *a, const unsigned long *b, int nr_bits);
+
+static inline void
+bitmap_zero(unsigned long *bm, int nr_bits)
+{
+ int n;
+
+ n = BITMAP_LONGS(nr_bits);
+ memset(bm, 0, n * sizeof(unsigned long));
+}
+
+static inline void
+bitmap_fill(unsigned long *bm, int nr_bits)
+{
+ int n;
+
+ n = BITMAP_LONGS(nr_bits);
+ memset(bm, 0xff, n * sizeof(unsigned long));
+}
+
+static inline void
+bitmap_copy(unsigned long *dest, const unsigned long *src, int nr_bits)
+{
+ int n;
+
+ n = BITMAP_LONGS(nr_bits);
+ memcpy(dest, src, n * sizeof(unsigned long));
+}
+
+static inline void
+bitmap_set(unsigned long *bm, int bit)
+{
+ if (bit >= LONG_BIT)
+ bitmap_lookup(bm, bit);
+
+ *bm |= bitmap_mask(bit);
+}
+
+static inline void
+bitmap_set_atomic(unsigned long *bm, int bit)
+{
+ if (bit >= LONG_BIT)
+ bitmap_lookup(bm, bit);
+
+ atomic_or_ulong(bm, bitmap_mask(bit));
+}
+
+static inline void
+bitmap_clear(unsigned long *bm, int bit)
+{
+ if (bit >= LONG_BIT)
+ bitmap_lookup(bm, bit);
+
+ *bm &= ~bitmap_mask(bit);
+}
+
+static inline void
+bitmap_clear_atomic(unsigned long *bm, int bit)
+{
+ if (bit >= LONG_BIT)
+ bitmap_lookup(bm, bit);
+
+ atomic_and_ulong(bm, ~bitmap_mask(bit));
+}
+
+static inline int
+bitmap_test(const unsigned long *bm, int bit)
+{
+ if (bit >= LONG_BIT)
+ bitmap_lookup(bm, bit);
+
+ return ((*bm & bitmap_mask(bit)) != 0);
+}
+
+static inline void
+bitmap_and(unsigned long *a, const unsigned long *b, int nr_bits)
+{
+ int i, n;
+
+ n = BITMAP_LONGS(nr_bits);
+
+ for (i = 0; i < n; i++)
+ a[i] &= b[i];
+}
+
+static inline void
+bitmap_or(unsigned long *a, const unsigned long *b, int nr_bits)
+{
+ int i, n;
+
+ n = BITMAP_LONGS(nr_bits);
+
+ for (i = 0; i < n; i++)
+ a[i] |= b[i];
+}
+
+static inline void
+bitmap_xor(unsigned long *a, const unsigned long *b, int nr_bits)
+{
+ int i, n;
+
+ n = BITMAP_LONGS(nr_bits);
+
+ for (i = 0; i < n; i++)
+ a[i] ^= b[i];
+}
+
+static inline int
+bitmap_find_next(const unsigned long *bm, int nr_bits, int bit)
+{
+ return bitmap_find_next_bit(bm, nr_bits, bit, 0);
+}
+
+static inline int
+bitmap_find_first(const unsigned long *bm, int nr_bits)
+{
+ return bitmap_find_next(bm, nr_bits, 0);
+}
+
+static inline int
+bitmap_find_next_zero(const unsigned long *bm, int nr_bits, int bit)
+{
+ return bitmap_find_next_bit(bm, nr_bits, bit, 1);
+}
+
+static inline int
+bitmap_find_first_zero(const unsigned long *bm, int nr_bits)
+{
+ return bitmap_find_next_zero(bm, nr_bits, 0);
+}
+
+#define bitmap_for_each(bm, nr_bits, bit) \
+for ((bit) = 0; \
+ ((bit) < nr_bits) \
+ && (((bit) = bitmap_find_next(bm, nr_bits, bit)) != -1); \
+ (bit)++)
+
+#define bitmap_for_each_zero(bm, nr_bits, bit) \
+for ((bit) = 0; \
+ ((bit) < nr_bits) \
+ && (((bit) = bitmap_find_next_zero(bm, nr_bits, bit)) != -1); \
+ (bit)++)
+
+#endif /* _BITMAP_H */
diff --git a/bitmap_i.h b/bitmap_i.h
new file mode 100644
index 0000000..45208f3
--- /dev/null
+++ b/bitmap_i.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2013-2015 Richard Braun.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _BITMAP_I_H
+#define _BITMAP_I_H
+
+#include <limits.h>
+
+#include "macros.h"
+
+#ifndef LONG_BIT
+#define LONG_BIT ((int)(CHAR_BIT * sizeof(long)))
+#endif /* LONG_BIT */
+
+#define BITMAP_LONGS(nr_bits) DIV_CEIL(nr_bits, LONG_BIT)
+
+/*
+ * Adjust the bitmap pointer and the bit index so that the latter refers
+ * to a bit inside the word pointed by the former.
+ *
+ * Implemented as a macro for const-correctness.
+ */
+#define bitmap_lookup(bm, bit) \
+MACRO_BEGIN \
+ int i; \
+ \
+ i = BITMAP_LONGS((bit) + 1) - 1; \
+ (bm) += i; \
+ (bit) -= i * LONG_BIT; \
+MACRO_END
+
+static inline unsigned long
+bitmap_mask(int bit)
+{
+ return (1UL << bit);
+}
+
+/*
+ * Return the index of the next set bit in the bitmap, starting (and
+ * including) the given bit index, or -1 if the bitmap is empty. If
+ * complement is true, bits are toggled before searching so that the
+ * result is the index of the next zero bit.
+ */
+int bitmap_find_next_bit(const unsigned long *bm, int nr_bits, int bit,
+ int complement);
+
+#endif /* _BITMAP_I_H */