public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
To: Irina Tirdea <irina.tirdea@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	LKML <linux-kernel@vger.kernel.org>,
	Paul Mackerras <paulus@samba.org>,
	David Ahern <dsahern@gmail.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Pekka Enberg <penberg@kernel.org>, Jiri Olsa <jolsa@redhat.com>,
	Irina Tirdea <irina.tirdea@intel.com>
Subject: Re: [PATCH v3 8/8] perf stat: implement --big-num grouping
Date: Mon, 8 Oct 2012 14:11:03 -0700	[thread overview]
Message-ID: <20121008211103.GK2631@ghostprotocols.net> (raw)
In-Reply-To: <1349678613-7045-9-git-send-email-irina.tirdea@gmail.com>

Em Mon, Oct 08, 2012 at 09:43:33AM +0300, Irina Tirdea escreveu:
>  
> +/* Group the digits according to the grouping rules of the current locale.
> +   The interpretation of GROUPING is as in `struct lconv' from <locale.h>.  */
> +static int group_number_locale(char *number, char **gnumber)
> +{
> +	const char *thousands_sep = NULL, *grouping = NULL;
> +	int glen, tlen, dest_alloc_size, src_size, ret = 0, cnt;

Please set ret to -ENOMEM; and...

> +	char *dest_alloc_ptr, *dest_end, *src_start, *src_end;
> +

When we have else clauses, I think its better to use

#ifdef LOCALE_SUPPORT
	struct lconv *lc = localeconv();
	if (lc != NULL) {
		thousands_sep = lc->thousands_sep;
		grouping = lc->grouping;
	}
#else
	thousands_sep = ",";
	grouping = "\x3";
#endif


> +#ifndef LOCALE_SUPPORT
> +	thousands_sep = ",";
> +	grouping = "\x3";
> +#else
> +	struct lconv *lc = localeconv();
> +	if (lc != NULL) {
> +		thousands_sep = lc->thousands_sep;
> +		grouping = lc->grouping;
> +	}
> +#endif
> +
> +	*gnumber = NULL;
> +	/* No grouping */
> +	if (thousands_sep == NULL || grouping == NULL ||
> +	    *thousands_sep == '\0' || *grouping == CHAR_MAX || *grouping <= 0) {
> +		*gnumber = strdup(number);
> +		if (*gnumber == NULL)
> +			ret = -ENOMEM;
> +		goto out;

Humm, so we bail out unconditionally? :-)

	this should be:

		if (*gnumber == NULL)
			goto out;
> +	}
> +
> +	glen = *grouping++;
> +	tlen = strlen(thousands_sep);
> +
> +	src_size = strlen(number);
> +	/* Worst case scenario we have 1-character grouping */
> +	dest_alloc_size = (src_size + src_size * tlen) * sizeof(char);
> +	dest_alloc_ptr = zalloc(dest_alloc_size);
> +	if (dest_alloc_ptr == NULL) {
> +		ret = -ENOMEM;

remove the above

> +		goto out;
> +	}
> +	/* -1 for '\0' */
> +	dest_end = dest_alloc_ptr + dest_alloc_size - 1;
> +
> +	src_start = number;
> +	src_end = number + src_size;
> +
> +	while (src_end > src_start) {
> +		*--dest_end = *--src_end;
> +		if (--glen == 0 && src_end > src_start) {
> +			/* A new group */
> +			cnt = tlen;
> +			do
> +				*--dest_end = thousands_sep[--cnt];
> +			while (cnt > 0);
> +
> +			if (*grouping == CHAR_MAX || *grouping < 0) {
> +				/* No further grouping to be done.
> +				   Copy the rest of the number. */
> +				do
> +					*--dest_end = *--src_end;
> +				while (src_end > src_start);
> +				break;
> +			} else if (*grouping != '\0') {
> +				glen = *grouping++;
> +			} else {
> +				/* The previous grouping repeats ad infinitum */
> +				glen = grouping[-1];
> +			}
> +		}
> +	}
> +
> +	/* Make a copy with the exact needed size of the grouped number */
> +	*gnumber = strdup(dest_end);
> +	if (*gnumber == NULL) {
> +		ret = -ENOMEM;

	ditto
> +		goto out_free_dest;
> +	}
> +
> +	/* fall through */

Why the "fall through" comment? This is a common construct and this
comment mostly applies where one would expect a break in a switch
statement :-)

Ah, here you do:

	ret = 0;

Since all went well.

> +out_free_dest:
> +	free(dest_alloc_ptr);
> +out:
> +	return ret;
> +}

  reply	other threads:[~2012-10-08 21:11 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-08  6:43 [PATCH 0/8] perf tools: fixes for Android Irina Tirdea
2012-10-08  6:43 ` [PATCH 1/8] perf tools: add on_exit implementation Irina Tirdea
2012-10-09 17:41   ` [tip:perf/core] perf tools: Add " tip-bot for Bernhard Rosenkraenzer
2012-10-08  6:43 ` [PATCH 2/8] perf tools: update Makefile for Android Irina Tirdea
2012-10-08 20:43   ` Arnaldo Carvalho de Melo
2012-10-09 17:42   ` [tip:perf/core] perf tools: Update " tip-bot for Irina Tirdea
2012-10-08  6:43 ` [PATCH 3/8] Documentation: add documentation on compiling " Irina Tirdea
2012-10-09 17:43   ` [tip:perf/core] " tip-bot for Irina Tirdea
2012-10-08  6:43 ` [PATCH v3 4/8] perf tools: configure tmp path at build time Irina Tirdea
2012-10-08 20:50   ` Arnaldo Carvalho de Melo
2012-10-08  6:43 ` [PATCH v3 5/8] perf tools: configure shell path at compile time Irina Tirdea
2012-10-08  6:43 ` [PATCH v3 6/8] perf tools: Try to find cross-built objdump path Irina Tirdea
2012-10-08 21:03   ` Arnaldo Carvalho de Melo
2012-10-15 22:59     ` Irina Tirdea
2012-10-08  6:43 ` [PATCH v3 7/8] perf tools: configure addr2line for cross-compiling Irina Tirdea
2012-10-08 21:04   ` Arnaldo Carvalho de Melo
2012-10-08  6:43 ` [PATCH v3 8/8] perf stat: implement --big-num grouping Irina Tirdea
2012-10-08 21:11   ` Arnaldo Carvalho de Melo [this message]
2012-10-15 23:05     ` Irina Tirdea

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20121008211103.GK2631@ghostprotocols.net \
    --to=acme@ghostprotocols.net \
    --cc=a.p.zijlstra@chello.nl \
    --cc=dsahern@gmail.com \
    --cc=irina.tirdea@gmail.com \
    --cc=irina.tirdea@intel.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=paulus@samba.org \
    --cc=penberg@kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox