lkml.org 
[lkml]   [2018]   [Feb]   [15]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH v2 2/8] mm: Add kvmalloc_ab_c and kvzalloc_struct
On Wed, Feb 14, 2018 at 07:40:50PM -0800, Matthew Wilcox wrote:
> > a_foo = kvzalloc_struct_buf(struct foo, struct bar, nr_bars);
> >
> > or, of course.
> >
> > a_foo = kvzalloc_struct_buf(typeof(*a_foo), typeof(a_foo->bar[0]),
> > nr_bars);
> >
> > or whatever.

This version works, although it's more typing in the callers:

- attr_group = kvzalloc_struct(attr_group, attrs, i + 1,
+ attr_group = kvzalloc_struct(typeof(*attr_group), attrs, i + 1,
GFP_KERNEL);
...
- dev_dax = kvzalloc_struct(dev_dax, res, count, GFP_KERNEL);
+ dev_dax = kvzalloc_struct(struct dev_dax, res, count, GFP_KERNEL);
...
-#define kvzalloc_struct(p, member, n, gfp) \
- (typeof(p))kvzalloc_ab_c(n, \
- sizeof(*(p)->member) + __must_be_array((p)->member), \
- offsetof(typeof(*(p)), member), gfp)
+#define kvzalloc_struct(s, member, n, gfp) ({ \
+ s *__p; \
+ (s *)kvzalloc_ab_c(n, \
+ sizeof(*(__p)->member) + __must_be_array((__p)->member),\
+ offsetof(s, member), gfp); \
+})

Gives all the same checking as the current version, and doesn't involve
passing an uninitialised pointer to the macro.

It also looks pretty similar:

p = kvzalloc(sizeof(*p), GFP_KERNEL);
p = kvzalloc_struct(typeof(*p), array, count, GFP_KERNEL);
p = kvzalloc_array(sizeof(*p), count);

\
 
 \ /
  Last update: 2018-02-15 18:37    [W:0.067 / U:0.088 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site