Re: seperator error in __mask_snprintf_len

From: Joe Korty
Date: Sun Jan 11 2004 - 19:11:13 EST


Paul & Andrew,
This version of __mask_snprintf_len should work with all mixes of big
vs little endian and 32 vs 64 bit longs. I tested it in user-land on
a 32 bit big and 32 bit little endian machine. I tested the 64 bit
long issues by setting MASK_CHUNKSZ == 16 on both of my 32 bit processors.
This should be equivalent to MASK_CHUNKSZ == 32 test on a 64 bit
processor.

This patch preserves Paul's ideas of how a cpumask_t should be printed
out even though I do not agree with those ideas. At a minimum I prefer
a constant-width display so that columns of cpumasks will be readable.

I expect __mask_parse_len will need similar fixes; I have not looked
as this patch is enough for a Sunday evening's worth of work.

Regards,
Joe

Against 2.6.1


diff -Nua 2.6/lib/mask.c.0 2.6/lib/mask.c
--- 2.6/lib/mask.c.0 2004-01-11 18:53:18.000000000 -0500
+++ 2.6/lib/mask.c 2004-01-11 18:52:43.000000000 -0500
@@ -85,21 +85,30 @@
* __mask_snprintf_len(buf, buflen, cpus_addr(mask), sizeof(mask))
*/

+#define MASK_CHUNKSZ (32) /* in bits. legal values: 4, 8, 16, 32 */
+
int __mask_snprintf_len(char *buf, unsigned int buflen,
const unsigned long *maskp, unsigned int maskbytes)
{
- u32 *wordp = (u32 *)maskp;
- int i = maskbytes/sizeof(u32) - 1;
- int len = 0;
+ int i, word, bit, len = 0;
+ unsigned long val;
char *sep = "";

- while (i >= 1 && wordp[i] == 0)
- i--;
- while (i >= 0) {
- len += snprintf(buf+len, buflen-len, "%s%x", sep, wordp[i]);
- sep = ",";
- i--;
+ if (buflen <= 0)
+ return 0;
+ buflen--;
+
+ i = (maskbytes * 8 - 1) & ~(MASK_CHUNKSZ - 1);
+ for (; i >= 0; i -= MASK_CHUNKSZ) {
+ word = i / (sizeof(unsigned long) * 8);
+ bit = i % (sizeof(unsigned long) * 8);
+ val = (maskp[word] >> bit) & ((1ULL << MASK_CHUNKSZ) - 1);
+ if (!i || val || *sep == ',') {
+ len += snprintf(buf+len, buflen-len, "%s%lx", sep, val);
+ sep = ",";
+ }
}
+ buf[len++] = 0;
return len;
}

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/