compat: backport memweight()
authorLuis R. Rodriguez <mcgrof@do-not-panic.com>
Fri, 29 Mar 2013 23:13:17 +0000 (16:13 -0700)
committerJohannes Berg <johannes@sipsolutions.net>
Tue, 2 Apr 2013 20:19:35 +0000 (22:19 +0200)
mcgrof@frijol ~/linux-next (git::master)$ git describe --contains 639b9e34
v3.6-rc1~41^2~73

commit 639b9e34f15e4b2c30068a4e4485586af0cdf709
Author: Akinobu Mita <akinobu.mita@gmail.com>
Date:   Mon Jul 30 14:40:55 2012 -0700

    string: introduce memweight()

    memweight() is the function that counts the total number of bits set in
    memory area.  Unlike bitmap_weight(), memweight() takes pointer and size
    in bytes to specify a memory area which does not need to be aligned to
    long-word boundary.

    [akpm@linux-foundation.org: rename `w' to `ret']
Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
Cc: Anders Larsen <al@alarsen.net>
Cc: Alasdair Kergon <agk@redhat.com>
Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Cc: Mark Fasheh <mfasheh@suse.com>
Cc: Joel Becker <jlbec@evilplan.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Andreas Dilger <adilger.kernel@dilger.ca>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: Matthew Wilcox <matthew@wil.cx>
Cc: Mauro Carvalho Chehab <mchehab@infradead.org>
Cc: Tony Luck <tony.luck@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Luis R. Rodriguez <mcgrof@do-not-panic.com>
backport/compat/compat-3.6.c
backport/include/linux/compat-3.6.h

index 113e3a867221898d31208497608b4be2adf6a295..a50fc987f56963a30249de97cefc6bd9ade8d53f 100644 (file)
 
 #include <linux/module.h>
 #include <linux/scatterlist.h>
+#include <linux/export.h>
+#include <linux/bug.h>
+#include <linux/bitmap.h>
+
+/**
+ * memweight - count the total number of bits set in memory area
+ * @ptr: pointer to the start of the area
+ * @bytes: the size of the area
+ */
+size_t memweight(const void *ptr, size_t bytes)
+{
+       size_t ret = 0;
+       size_t longs;
+       const unsigned char *bitmap = ptr;
+
+       for (; bytes > 0 && ((unsigned long)bitmap) % sizeof(long);
+                       bytes--, bitmap++)
+               ret += hweight8(*bitmap);
+
+       longs = bytes / sizeof(long);
+       if (longs) {
+               BUG_ON(longs >= INT_MAX / BITS_PER_LONG);
+               ret += bitmap_weight((unsigned long *)bitmap,
+                               longs * BITS_PER_LONG);
+               bytes -= longs * sizeof(long);
+               bitmap += longs * sizeof(long);
+       }
+       /*
+        * The reason that this last loop is distinct from the preceding
+        * bitmap_weight() call is to compute 1-bits in the last region smaller
+        * than sizeof(long) properly on big-endian systems.
+        */
+       for (; bytes > 0; bytes--, bitmap++)
+               ret += hweight8(*bitmap);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(memweight);
 
 /**
  * sg_alloc_table_from_pages - Allocate and initialize an sg table from
index d9c964ed3f9a5049f63b7543a33399b4a0ee6dba..bef0503aeff96b2a9023a107b7e6da0aa6f5ecf6 100644 (file)
@@ -7,6 +7,9 @@
 
 #include <linux/scatterlist.h>
 
+#define memweight LINUX_BACKPORT(memweight)
+extern size_t memweight(const void *ptr, size_t bytes);
+
 /* backports efc42bc9 */
 #define sg_alloc_table_from_pages LINUX_BACKPORT(sg_alloc_table_from_pages)
 int sg_alloc_table_from_pages(struct sg_table *sgt,