Revision: 698
http://assorted.svn.sourceforge.net/assorted/?rev=698&view=rev
Author: yangzhang
Date: 2008-05-04 00:50:13 -0700 (Sun, 04 May 2008)
Log Message:
-----------
polishing blocks.c
Modified Paths:
--------------
sandbox/trunk/src/c/blocks.c
Modified: sandbox/trunk/src/c/blocks.c
===================================================================
--- sandbox/trunk/src/c/blocks.c 2008-05-04 07:27:22 UTC (rev 697)
+++ sandbox/trunk/src/c/blocks.c 2008-05-04 07:50:13 UTC (rev 698)
@@ -6,17 +6,39 @@
enum { blk = 10 };
enum { datlen = 2 * blk };
+/**
+ * Calculate the number of blocks in a segment of length \a i.
+ * Mathematically, this is \code ceil(i / blk) \endcode.
+ * Equivalent to \code (i - 1) / blk + 1 \endcode, but without the
+ * arithmetic underflow.
+ *
+ * E.g., with a block size of 10:
+ *
+ * \code
+ * nblocks( 0) == 0 &&
+ * nblocks( 1) == 1 && nblocks(10) == 1 &&
+ * nblocks(11) == 2 && ...
+ * \endcode
+ */
+inline uint32_t
+nblocks(uint32_t i)
+{
+ return (i + blk - 1) / blk;
+}
+
void
f(uint32_t off, uint32_t len)
{
uint32_t end = off + len;
- uint32_t fstblk = off / blk, lstblk = (end + blk - 1) / blk;
- printf("f(off %4d, len %4d), fstblk %2d, lstblk %2d\n", off, len, fstblk, lstblk);
+ uint32_t fstblk = off / blk, lstblk = nblocks(end);
+ printf("f(off %4d, len %4d), fstblk %2d, lstblk %2d\n",
+ off, len, fstblk, lstblk);
char *p = 0;
for (uint32_t i = fstblk; i < lstblk; i++) {
uint32_t blkoff = i == fstblk ? off % blk : 0;
uint32_t blklen = min(end - i * blk, blk) - blkoff;
- printf(" i %2d | blkoff %4d | blklen %4d | p %04p\n", i, blkoff, blklen, p);
+ printf(" i %2d | blkoff %4d | blklen %4d | p %04p\n",
+ i, blkoff, blklen, p);
p += blklen;
}
printf("\n");
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|