Re: [PATCH rdma-next] RDMA: Provide safe ib_alloc_device() function

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

 



On Mon, Jan 28, 2019 at 08:27:17AM -0800, Bart Van Assche wrote:
> On Mon, 2019-01-28 at 17:20 +0200, Leon Romanovsky wrote:
> > On Mon, Jan 28, 2019 at 07:01:41AM -0800, Bart Van Assche wrote:
> > > On 1/28/19 6:14 AM, Leon Romanovsky wrote:
> > > > -struct ib_device *ib_alloc_device(size_t size);
> > > > +void *_ib_alloc_device(size_t size);
> > > > +#define ib_alloc_device(drv_struct, member)                                    \
> > > > +	(BUILD_BUG_ON_ZERO(offsetof(struct drv_struct, member)) +              \
> > > > +	 BUILD_BUG_ON_ZERO(!__builtin_types_compatible_p(                      \
> > > > +		 typeof(((struct drv_struct *)NULL)->member),                  \
> > > > +		 struct ib_device)) +                                          \
> > > > +	 _ib_alloc_device(sizeof(struct drv_struct)))
> > > > +
> > >
> > > Hi Leon,
> > >
> > > Have you considered to use a comma expression and BUILD_BUG_ON() instead
> > > of adding the BUILD_BUG_ON_ZERO() results to the _ib_alloc_device()
> > > return value? I think that would result in slightly easier to read code
> > > and also in code that reflects the intent better.
> >
> > I wanted to make sure that macro is used as assignment and returns
> > value, in such case it should be art of expression, hence in success
> > it is evaluated to 0.
> >
> > I will very happy to receive code snippet for the same with BUILD_BUG_ON().
>
> Hi Leon,
>
> How about the following macro definition, which uses BUILD_BUG_ON() instead of
> BUILD_BUG_ON_ZERO() and which uses __same_type() instead of
> __builtin_types_compatible_p() (compile-tested only):

Strange, i tried __same_type() too and the second typeof didn't work for
me. But in your macro it works, maybe I missed some brackets.

>
> #define ib_alloc_device(drv_struct, member)				\
> 	(({BUILD_BUG_ON(offsetof(struct drv_struct, member) != 0);}),	\
> 	 ({BUILD_BUG_ON(!__same_type(((struct drv_struct *)NULL)->member, \
> 				     struct ib_device));}),		\
> 	 _ib_alloc_device(sizeof(struct drv_struct)))
>
> Thanks,
>
> Bart.

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Photo]     [Yosemite News]     [Yosemite Photos]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux