[ovs-dev] [tests+nxm-ofctl 28/42] util: Improve type-safety of OBJECT_CONTAINING.

Ben Pfaff blp at nicira.com
Fri Dec 3 18:36:51 UTC 2010


On Thu, Dec 02, 2010 at 06:48:08PM -0800, Justin Pettit wrote:
> On Nov 23, 2010, at 2:44 PM, Ben Pfaff wrote:
> 
> > +/* Given a pointer-typed lvalue OBJECT, expands to a pointer type that may be
> > + * assigned to OBJECT. */
> > +#ifdef __GNUC__
> > +#define OVS_TYPEOF(OBJECT) typeof(OBJECT)
> > +#else
> > +#define OVS_TYPEOF(OBJECT) void *
> > +#endif
> > +
> > /* Given POINTER, the address of the given MEMBER in a STRUCT object, returns
> >    the STRUCT object. */
> > #define CONTAINER_OF(POINTER, STRUCT, MEMBER)                           \
> >         ((STRUCT *) (void *) ((char *) (POINTER) - offsetof (STRUCT, MEMBER)))
> > 
> > /* Given POINTER, the address of the given MEMBER within an object of the type
> > - * that that OBJECT points to, returns OBJECT as a "void *" pointer.  OBJECT
> > - * must be an lvalue.
> > + * that that OBJECT points to, returns OBJECT as an assignment-compatible
> > + * pointer type (either the correct pointer type or "void *").  OBJECT must be
> > + * an lvalue.
> >  *
> >  * This is the same as CONTAINER_OF except that it infers the structure type
> >  * from the type of '*OBJECT'. */
> > #define OBJECT_CONTAINING(POINTER, OBJECT, MEMBER)                      \
> > -        ((void *) ((char *) (POINTER)                                   \
> > -                   - ((char *) &(OBJECT)->MEMBER - (char *) (OBJECT))))
> > +    ((OVS_TYPEOF(OBJECT)) (void *)                                      \
> > +     ((char *) (POINTER) - ((char *) &(OBJECT)->MEMBER - (char *) (OBJECT))))
> 
> Did you want "((OVS_TYPEOF(OBJECT)) (void *)"?  I would have thought
> it wouldn't have that (void *) in it anymore.

The extra cast to (void *) suppresses a GCC warning on RISC systems
about casting to a more strictly aligned type.  Here's the commit that
originally introduced it:

--8<--------------------------cut here-------------------------->8--

>From 26adc8ddd75df99c17bf4bbe8dac5562213866bc Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp at nicira.com>
Date: Thu, 6 May 2010 17:04:11 -0700
Subject: [PATCH] util: Fix GCC false-positive warning for CONTAINER_OF.

On sparc, GCC would issue the following warning for every use of
CONTAINER_OF:

    warning: cast increases required alignment of target type

This is a false positive: assuming that the data structure that it is
applied to was allocated properly, the use of CONTAINER_OF to reach it is
valid.  And if it was not allocated properly, then code that accesses it
other ways will have trouble too.
---
 lib/util.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/lib/util.h b/lib/util.h
index 9df6db5..0e9353d 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -80,7 +80,7 @@ extern const char *program_name;
 /* Given POINTER, the address of the given MEMBER in a STRUCT object, returns
    the STRUCT object. */
 #define CONTAINER_OF(POINTER, STRUCT, MEMBER)                           \
-        ((STRUCT *) ((char *) (POINTER) - offsetof (STRUCT, MEMBER)))
+        ((STRUCT *) (void *) ((char *) (POINTER) - offsetof (STRUCT, MEMBER)))
 
 #ifdef  __cplusplus
 extern "C" {
-- 
1.7.1





More information about the dev mailing list