On Tue, 3 Oct 2000, Jeffrey Hsu wrote:
> > I think <struct.h> should have been removed when offsetof() became
> > standard.
>
> At last count, offsetof() is defined in 11 different places in the kernel:
> alpha/alpha/machdep.c, line 234
>...
Some other places include <stddef.h>.
> If we unify it in one header file, I'd be happy to use that for STAILQ_LAST().
<machine/ansi.h> is a good place for it now.
> > strbase() is now used in <sys/queue.h>. It is
> > easy to implement directly using offsetof(). Unfortunately, if it is
> > implemented using offsetof(), then <sys/queue.h> will depend on
> > <stddef.h>.
>
> So, what's the correct solution?
I'm currently using the following. This avoids a bug in fldoff(): casting
to int is wrong and causes warnings on alphas. I use a cast to __uintptr_t
since casting to size_t is normally right (offsetof() depends on it), and
__uintptr_t is normally equivalent to size_t and is easier to get at.
---
diff -c2 queue.h~ queue.h
*** queue.h~ Fri Aug 4 21:38:55 2000
--- queue.h Thu Oct 5 21:22:51 2000
***************
*** 38,43 ****
#define _SYS_QUEUE_H_
- #include <struct.h>
-
/*
* This file defines five types of data structures: singly-linked lists,
--- 38,41 ----
***************
*** 224,231 ****
} while (0)
#define STAILQ_LAST(head, type, field) \
! (STAILQ_EMPTY(head) ? \
! NULL : \
! strbase(type, (head)->stqh_last, field))
#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
--- 222,239 ----
} while (0)
+ /*
+ * XXX auxiliary macros for STAILQ_LAST() adapted from offsetof() in
+ * <stddef.h> and strbase() in <struct.h>.
+ */
+ #include <machine/ansi.h>
+
+ #define __offsetof(type, member) \
+ ((__uintptr_t)(&((type *)0)->member))
+ #define __strbase(name, addr, field) \
+ ((struct name *)((char *)(addr) - __offsetof(struct name, field)))
+
#define STAILQ_LAST(head, type, field) \
! (STAILQ_EMPTY(head) ? NULL : \
! __strbase(type, (head)->stqh_last, field))
#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
---
Bruce
To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-current" in the body of the message