Re: [PATCH 05/12] drm/i915: Add DEFINE_SNPRINTF_ARRAY()

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

 



On Thu, 11 Oct 2018, Ville Syrjälä <ville.syrjala@xxxxxxxxxxxxxxx> wrote:
> On Thu, Oct 11, 2018 at 03:14:41PM +0300, Jani Nikula wrote:
>> On Wed, 10 Oct 2018, Ville Syrjala <ville.syrjala@xxxxxxxxxxxxxxx> wrote:
>> > From: Ville Syrjälä <ville.syrjala@xxxxxxxxxxxxxxx>
>> >
>> > Templatize snprintf_int_array() to allow us to print
>> > different kinds of arrays without having to type all
>> > the boilerplate for the snprintf() loop.
>> 
>> I might just feel happier about duplicating the boilerplate...
>
> How about when the third user appears? :)
>
> Not sure I have a third user for this actually.
> snprintf_output_types() is pretty close, and there are other
> bitmask I'd probably like to decode. But I couldn't immediately
> think of a nice way to handle bitmasks and arrays in the same
> function.

So here's the non-macro generic approach. It's not perfect, it just has
different wrinkles:

static int snprintf_int(char *str, size_t len, const void *elem)
{
	return snprintf(str, len, "%d", *((const int *)elem));
}

static void snprintf_array(char *str, size_t len, const void *array, int nelem,
			   size_t size, const char *sep,
			   int (*print)(char *str, size_t len, const void *elem))
{
	int i, r;

	if (len)
		str[0] = '\0';

	for (i = 0; i < nelem; i++) {
		const void *elem = array + i * size;

		if (i) {
			r = snprintf(str, len, "%s", sep);
			if (r >= len)
				return;
			str += r;
			len -= r;
		}

		r = print(str, len, elem);
		if (r >= len)
			return;
		str += r;
		len -= r;
	}
}

static void snprintf_int_array(char *str, size_t len, const int *array, int nelem)
{
	snprintf_array(str, len, array, nelem, sizeof(array[0]), ", ",
		       snprintf_int);
}

Of course, this doesn't help with bitmasks either. You'd first have to
split the bitmask into an array.


BR,
Jani.



>
>> 
>> BR,
>> Jani.
>> 
>> 
>> 
>> >
>> > Signed-off-by: Ville Syrjälä <ville.syrjala@xxxxxxxxxxxxxxx>
>> > ---
>> >  drivers/gpu/drm/i915/i915_utils.h | 16 ++++++++++++++++
>> >  drivers/gpu/drm/i915/intel_dp.c   | 17 ++---------------
>> >  2 files changed, 18 insertions(+), 15 deletions(-)
>> >
>> > diff --git a/drivers/gpu/drm/i915/i915_utils.h b/drivers/gpu/drm/i915/i915_utils.h
>> > index 5858a43e19da..079aefa20bee 100644
>> > --- a/drivers/gpu/drm/i915/i915_utils.h
>> > +++ b/drivers/gpu/drm/i915/i915_utils.h
>> > @@ -161,4 +161,20 @@ static inline const char *enableddisabled(bool v)
>> >  	return v ? "enabled" : "disabled";
>> >  }
>> >  
>> > +#define DEFINE_SNPRINTF_ARRAY(name, type, values, index, fmt, ...) \
>> > +void name(char *_str, size_t _len, const type *values, int _nelems) \
>> > +{ \
>> > +	int index; \
>> > +	if (_len) \
>> > +		_str[0] = '\0'; \
>> > +	for (index = 0; index < _nelems; index++) { \
>> > +		int _r = snprintf(_str, _len, "%s" fmt, \
>> > +				  index ? ", " : "", __VA_ARGS__); \
>> > +		if (_r >= _len) \
>> > +			return; \
>> > +		_str += _r; \
>> > +		_len -= _r; \
>> > +	} \
>> > +}
>> > +
>> >  #endif /* !__I915_UTILS_H */
>> > diff --git a/drivers/gpu/drm/i915/intel_dp.c b/drivers/gpu/drm/i915/intel_dp.c
>> > index 13ff89be6ad6..dd8634b40179 100644
>> > --- a/drivers/gpu/drm/i915/intel_dp.c
>> > +++ b/drivers/gpu/drm/i915/intel_dp.c
>> > @@ -1774,21 +1774,8 @@ intel_dp_set_clock(struct intel_encoder *encoder,
>> >  	}
>> >  }
>> >  
>> > -static void snprintf_int_array(char *str, size_t len,
>> > -			       const int *array, int nelem)
>> > -{
>> > -	int i;
>> > -
>> > -	str[0] = '\0';
>> > -
>> > -	for (i = 0; i < nelem; i++) {
>> > -		int r = snprintf(str, len, "%s%d", i ? ", " : "", array[i]);
>> > -		if (r >= len)
>> > -			return;
>> > -		str += r;
>> > -		len -= r;
>> > -	}
>> > -}
>> > +static DEFINE_SNPRINTF_ARRAY(snprintf_int_array,
>> > +			     int, array, i, "%d", array[i]);
>> >  
>> >  static void intel_dp_print_rates(struct intel_dp *intel_dp)
>> >  {
>> 
>> -- 
>> Jani Nikula, Intel Open Source Graphics Center

-- 
Jani Nikula, Intel Open Source Graphics Center
_______________________________________________
Intel-gfx mailing list
Intel-gfx@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/intel-gfx




[Index of Archives]     [AMD Graphics]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux