tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: proposal: some pointer and arithmetic utilities



Urk...sorry to self-reply, but ten-fingered copypasta invariably
tastes wrong.  Let's try asking the computer to make copypasta for me
from the code that I actually tested:

/* typeof-free roundup2/rounddown2 */
#define roundup2(x, m)  (((x) + ((m) - 1)) & ~((m) - 1 + ((x) - (x))))
#define rounddown2(x,m) ((x) & ~((m) - 1 + ((x) - (x))))

/* multiple-evaluation-free roundup2/rounddown2 */
#define roundup2(x, m)  ((((x) - 1) | ((m) - 1)) + 1)
#define rounddown2(x,m) ((x) &~ ((typeof(x))((m) - 1)))

#define offsetin(s, f)  (((const char *)&(s).f) - ((const char *)&(s)))

#define container_of(p, t, f)                                           \
        ((void)sizeof((p) -                                             \
                &((t *)(((char *)(p)) - offsetof(t, f)))->f),           \
            ((t *)(((char *)(p)) - offsetof(t, f))))


Home | Main Index | Thread Index | Old Index