summaryrefslogtreecommitdiffstats
path: root/common/tlsfbits.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/tlsfbits.h')
-rw-r--r--common/tlsfbits.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/common/tlsfbits.h b/common/tlsfbits.h
new file mode 100644
index 0000000000..93466e46a8
--- /dev/null
+++ b/common/tlsfbits.h
@@ -0,0 +1,55 @@
+#ifndef INCLUDED_tlsfbits
+#define INCLUDED_tlsfbits
+
+#include <linux/bitops.h>
+
+/*
+** Architecture-specific bit manipulation routines.
+**
+** TLSF achieves O(1) cost for malloc and free operations by limiting
+** the search for a free block to a free list of guaranteed size
+** adequate to fulfill the request, combined with efficient free list
+** queries using bitmasks and architecture-specific bit-manipulation
+** routines.
+**
+** Most modern processors provide instructions to count leading zeroes
+** in a word, find the lowest and highest set bit, etc. These
+** specific implementations will be used when available, falling back
+** to a reasonably efficient generic implementation.
+**
+** NOTE: TLSF spec relies on ffs/fls returning value 0..31.
+** ffs/fls return 1-32 by default, returning 0 for error.
+*/
+
+static int tlsf_ffs(unsigned int word)
+{
+ return ffs(word) - 1;
+}
+
+static int tlsf_fls(unsigned int word)
+{
+ return fls(word) - 1;
+}
+
+/* Possibly 64-bit version of tlsf_fls. */
+#if defined (TLSF_64BIT)
+tlsf_decl int tlsf_fls_sizet(size_t size)
+{
+ int high = (int)(size >> 32);
+ int bits = 0;
+ if (high)
+ {
+ bits = 32 + tlsf_fls(high);
+ }
+ else
+ {
+ bits = tlsf_fls((int)size & 0xffffffff);
+
+ }
+ return bits;
+}
+#else
+#define tlsf_fls_sizet tlsf_fls
+#endif
+
+#endif