Skip to contentSkip to conversation

Re-organization of PIDMAP_ENTRIES macro expansion

PATCH In review Ratnadeep Joshi 2 participants 2 messages Jan 14, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Re-organization of PIDMAP_ENTRIES macro expansion

This patch tries to re-organize the macro expansion of PIDMAP_ENTRIES (possibly) to a more clear one.

Thanks,
- Ratnadeep Joshi

(quoted diff) +0 -0

b/include/linux/pid_namespace.h
index 1689e28..06e3e99 100644

include/linux/pid_namespace.h +0 -1
@@ -12,7 +12,7 @@ struct pidmap {
        void *page;
 };
 
-#define PIDMAP_ENTRIES         ((PID_MAX_LIMIT + 8*PAGE_SIZE -

1)/PAGE_SIZE/8)
+#define PIDMAP_ENTRIES ((PID_MAX_LIMIT - 1)/PAGE_SIZE/8 + 1)

struct pid_namespace {
struct kref kref;

#1 lore raw
Quoted 20 lines · On Mon, 14 Jan 2008 16:20:35 +0530 Ratnadeep Joshi wrote:

This patch tries to re-organize the macro expansion of PIDMAP_ENTRIES (possibly) to a more clear one.

Thanks,
- Ratnadeep Joshi

include/linux/pid_namespace.h +1 -1
index 1689e28..06e3e99 100644
@@ -12,7 +12,7 @@ struct pidmap {
        void *page;
 };
 
-#define PIDMAP_ENTRIES         ((PID_MAX_LIMIT + 8*PAGE_SIZE -
 1)/PAGE_SIZE/8)
+#define PIDMAP_ENTRIES         ((PID_MAX_LIMIT - 1)/PAGE_SIZE/8 + 1)
 
 struct pid_namespace {
Quoted 1 line

struct kref kref;

I beg to disagree. There is a very common (idiomatic) way for doing this kind of calculation and the first/original expression is in that form, although it's a little difficult to recognize.

This common formula lives in linux/kernel.h:

#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))

where n is (AFAIK) numerator and d is divisor.

Example: One use of this is in linux/bitops.h:

#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_LONG)

This macro returns the number of longs that it takes to hold 'nr' bits. Expanded, it looks like:

nr_longs = (nr_bits + BITS_PER_LONG - 1) / BITS_PER_LONG;

The idiomatic formula handles boundary conditions very nicely.

Now for PIDMAP_ENTRIES, use 8 * PAGE_SIZE as the (d) part of DIV_ROUND_UP() and you can see that it is done correctly.

---
~Randy