From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jan Kara Subject: Re: [PATCH v2 01/10] string: introduce memweight Date: Mon, 4 Jun 2012 15:35:09 +0200 Message-ID: <20120604133509.GA11010@quack.suse.cz> References: <1338644416-11417-1-git-send-email-akinobu.mita@gmail.com> <20120604101237.GD7670@quack.suse.cz> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Jan Kara , linux-kernel@vger.kernel.org, akpm@linux-foundation.org, Anders Larsen , Alasdair Kergon , dm-devel@redhat.com, linux-fsdevel@vger.kernel.org, Laurent Pinchart , linux-media@vger.kernel.org, Mark Fasheh , Joel Becker , ocfs2-devel@oss.oracle.com, linux-ext4@vger.kernel.org, Andreas Dilger , Theodore Ts'o , Matthew Wilcox To: Akinobu Mita Return-path: Content-Disposition: inline In-Reply-To: Sender: linux-ext4-owner@vger.kernel.org List-Id: linux-fsdevel.vger.kernel.org On Mon 04-06-12 20:46:14, Akinobu Mita wrote: > 2012/6/4 Jan Kara : > > On Sat 02-06-12 22:40:07, Akinobu Mita wrote: > >> memweight() is the function that counts the total number of bits s= et > >> in memory area. =A0Unlike bitmap_weight(), memweight() takes point= er > >> and size in bytes to specify a memory area which does not need to = be > >> aligned to long-word boundary. > >> > >> Signed-off-by: Akinobu Mita > >> Cc: Anders Larsen > >> Cc: Alasdair Kergon > >> Cc: dm-devel@redhat.com > >> Cc: linux-fsdevel@vger.kernel.org > >> Cc: Laurent Pinchart > >> Cc: linux-media@vger.kernel.org > >> Cc: Mark Fasheh > >> Cc: Joel Becker > >> Cc: ocfs2-devel@oss.oracle.com > >> Cc: Jan Kara > >> Cc: linux-ext4@vger.kernel.org > >> Cc: Andrew Morton > >> Cc: Andreas Dilger > >> Cc: "Theodore Ts'o" > >> Cc: Matthew Wilcox > >> --- > >> > >> v2: simplify memweight(), adviced by Jan Kara > >> > >> =A0include/linux/string.h | =A0 =A03 +++ > >> =A0lib/string.c =A0 =A0 =A0 =A0 =A0 | =A0 32 +++++++++++++++++++++= +++++++++++ > >> =A02 files changed, 35 insertions(+), 0 deletions(-) > >> > >> diff --git a/include/linux/string.h b/include/linux/string.h > >> index e033564..ffe0442 100644 > >> --- a/include/linux/string.h > >> +++ b/include/linux/string.h > >> @@ -145,4 +145,7 @@ static inline bool strstarts(const char *str, = const char *prefix) > >> =A0 =A0 =A0 return strncmp(str, prefix, strlen(prefix)) =3D=3D 0; > >> =A0} > >> =A0#endif > >> + > >> +extern size_t memweight(const void *ptr, size_t bytes); > >> + > >> =A0#endif /* _LINUX_STRING_H_ */ > >> diff --git a/lib/string.c b/lib/string.c > >> index e5878de..bf4d5a8 100644 > >> --- a/lib/string.c > >> +++ b/lib/string.c > >> @@ -26,6 +26,7 @@ > >> =A0#include > >> =A0#include > >> =A0#include > >> +#include > >> > >> =A0#ifndef __HAVE_ARCH_STRNICMP > >> =A0/** > >> @@ -824,3 +825,34 @@ void *memchr_inv(const void *start, int c, si= ze_t bytes) > >> =A0 =A0 =A0 return check_bytes8(start, value, bytes % 8); > >> =A0} > >> =A0EXPORT_SYMBOL(memchr_inv); > >> + > >> +/** > >> + * 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) > >> +{ > >> + =A0 =A0 size_t w =3D 0; > >> + =A0 =A0 size_t longs; > >> + =A0 =A0 const unsigned char *bitmap =3D ptr; > >> + > >> + =A0 =A0 for (; bytes > 0 && ((unsigned long)bitmap) % sizeof(lon= g); > >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 bytes--, bitmap++) > >> + =A0 =A0 =A0 =A0 =A0 =A0 w +=3D hweight8(*bitmap); > >> + > >> + =A0 =A0 longs =3D bytes / sizeof(long); > >> + =A0 =A0 if (longs) { > >> + =A0 =A0 =A0 =A0 =A0 =A0 BUG_ON(longs >=3D INT_MAX / BITS_PER_LON= G); > >> + =A0 =A0 =A0 =A0 =A0 =A0 w +=3D bitmap_weight((unsigned long *)bi= tmap, > >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 longs * = BITS_PER_LONG); > >> + =A0 =A0 =A0 =A0 =A0 =A0 bytes -=3D longs * sizeof(long); > >> + =A0 =A0 =A0 =A0 =A0 =A0 bitmap +=3D longs * sizeof(long); > >> + =A0 =A0 } > >> + > >> + =A0 =A0 for (; bytes > 0; bytes--, bitmap++) > >> + =A0 =A0 =A0 =A0 =A0 =A0 w +=3D hweight8(*bitmap); > > =A0Looking at bitmap_weight() it seems this last loop is not needed= =2E Just > > pass to bitmap_weight() bytes*BITS_PER_BYTE. Also generally this fu= nction > > doesn't seem necessary at all at least for ext2 & ext3 (sorry for n= ot > > noticing this earlier...). >=20 > This last loop is necessary for big-endian architecture. > if bytes % sizeof(long) !=3D 0, bitmap_weight() counts one-bits in wr= ong > byte-field > of the last long word. Ah, right. OK. Add this to the comment before the loop please. You sa= ve yourself some explanations :) Honza --=20 Jan Kara SUSE Labs, CR -- To unsubscribe from this list: send the line "unsubscribe linux-ext4" i= n the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html