Re: [PATCH 1/6] kernel-shark: Add KS_DOUBLE_SIZE macro

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

 



On Wed, 6 Jan 2021 12:02:08 -0500
Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:

> On Wed,  6 Jan 2021 18:11:15 +0200
> "Yordan Karadzhov (VMware)" <y.karadz@xxxxxxxxx> wrote:
> 
> > The macro is useful for resizing of dynamic arrays. It is currently
> > used to resize the Data stream descriptor array, owned by the session
> > context. We will later use the macro with the arrays of data fields and
> > plugin contexts.
> > 
> > Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@xxxxxxxxx>
> > ---
> >  src/libkshark-plugin.h | 14 ++++++++++++++
> >  src/libkshark.c        | 15 +++++++--------
> >  2 files changed, 21 insertions(+), 8 deletions(-)
> > 
> > diff --git a/src/libkshark-plugin.h b/src/libkshark-plugin.h
> > index 1a642ad..f3c724f 100644
> > --- a/src/libkshark-plugin.h
> > +++ b/src/libkshark-plugin.h
> > @@ -346,6 +346,20 @@ int kshark_handle_dpi(struct kshark_data_stream *stream,
> >  int kshark_handle_all_dpis(struct kshark_data_stream *stream,
> >  			   enum kshark_plugin_actions  task_id);
> >  
> > +/** General purpose macro for resizing dynamic arrays. */  
> 
> Perhaps make this into a static inline function?

Never mind, I see you need the macro due to the "type" field. But we can
shorten this with:

   #define KS_DOUBLE_SIZE(array, size)				\
   ({								\
	ssize_t n = size;					\
	typeof(array) _tmp_ = (typeof(array)) realloc(array, 2 * n * \
                               sizeof(*_tmp_));			\
	if (_tmp_) {						\
		memset(_tmp_ + n, 0, n * sizeof(*_tmp_));	\
		size = 2 * n;					\
		array = _tmp_;					\
		true;						\
	}							\
	false;							\
   })

> FYI, as a macro, to return a value you have:
> 
> #define foo() ({ return true; })

The above is wrong, it should have been:

  #define foo() ({ true; })

-- Steve



[Index of Archives]     [Linux USB Development]     [Linux USB Development]     [Linux Audio Users]     [Yosemite Hiking]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux