ALT Linux Team development discussions
 help / color / mirror / Atom feed
From: "Kirill A. Shutsemov" <kirill@shutemov.name>
To: "Dmitry V. Levin" <ldv@altlinux.org>, Alexey Tourbin <at@altlinux.ru>
Cc: "Alexey I. Froloff" <raorn@altlinux.org>,
	devel@lists.altlinux.org, Alexey Gladkov <legion@altlinux.ru>,
	"Kirill A. Shutemov" <kirill@shutemov.name>
Subject: [devel] [PATCH 5/8] set.c: do not mix declarations and code
Date: Tue, 16 Nov 2010 17:56:39 +0200
Message-ID: <1289923002-14132-6-git-send-email-kirill@shutemov.name> (raw)
In-Reply-To: <1289923002-14132-1-git-send-email-kirill@shutemov.name>

From: Kirill A. Shutemov <kirill@shutemov.name>

Let's move variable declarations to the begin of blocks.

Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
---
 lib/set.c |  111 +++++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 65 insertions(+), 46 deletions(-)

diff --git a/lib/set.c b/lib/set.c
index 9dd3eb4..2a8d8b3 100644
--- a/lib/set.c
+++ b/lib/set.c
@@ -11,6 +11,7 @@
 #include <stdio.h>
 #endif
 
+#include <alloca.h>
 #include <string.h>
 #include <stdlib.h>
 #include <assert.h>
@@ -280,11 +281,13 @@ int log2i(int n)
 static
 int encode_golomb_Mshift(int c, int bpp)
 {
+    int Mshift;
+
     /*
      * XXX Slightly better Mshift estimations are probably possible.
      * Recheck "Compression and coding algorithms" by Moffat & Turpin.
      */
-    int Mshift = bpp - log2i(c) - 1;
+    Mshift = bpp - log2i(c) - 1;
 
     /* Adjust out-of-range values. */
     if (Mshift < 7)
@@ -315,20 +318,20 @@ int encode_golomb(int c, const unsigned *v, int Mshift, char *bitv)
     char *bitv_start = bitv;
     const unsigned mask = (1 << Mshift) - 1;
 
-    while (c > 0) {
-	c--;
-	unsigned v0 = *v++;
+    while (c-- > 0) {
+	unsigned v0, q, r;
 	int i;
-	/* first part: variable-length sequence */
-	unsigned q = v0 >> Mshift;
 
-	for (i = 0; i < (int)q; i++)
+	v0 = *v++;
+
+	/* first part: variable-length sequence */
+	q = v0 >> Mshift;
+	for (i = 0; i < (int) q; i++)
 	    *bitv++ = 0;
 	*bitv++ = 1;
 
 	/* second part: lower Mshift bits */
-	unsigned r = v0 & mask;
-
+	r = v0 & mask;
 	for (i = 0; i < Mshift; i++)
 	    *bitv++ = (r >> i) & 1;
     }
@@ -355,10 +358,13 @@ int decode_golomb(int bitc, const char *bitv, int Mshift, unsigned *v)
 
     /* next value */
     while (bitc > 0) {
-	/* first part */
-	unsigned q = 0;
-	char bit = 0;
+	unsigned q, r;
+	char bit;
+	int i;
 
+	/* first part */
+	q = 0;
+	bit = 0;
 	while (bitc > 0) {
 	    bitc--;
 	    bit = *bitv++;
@@ -377,14 +383,13 @@ int decode_golomb(int bitc, const char *bitv, int Mshift, unsigned *v)
 	    return -1;
 
 	/* second part */
-	unsigned r = 0;
-	int i;
-
+	r = 0;
 	for (i = 0; i < Mshift; i++) {
 	    bitc--;
 	    if (*bitv++)
 		r |= (1 << i);
 	}
+
 	/* the value */
 	*v++ = (q << Mshift) | r;
     }
@@ -586,6 +591,7 @@ int encode_set(int c, unsigned *v, int bpp, char *base62)
     /* XXX v is non-const due to encode_delta */
     int Mshift = encode_golomb_Mshift(c, bpp);
     int bitc = encode_golomb_size(c, Mshift);
+    int len;
     char bitv[bitc];
 
     /* bpp */
@@ -612,7 +618,7 @@ int encode_set(int c, unsigned *v, int bpp, char *base62)
 	return -3;
 
     /* base62 */
-    int len = encode_base62(bitc, bitv, base62);
+    len = encode_base62(bitc, bitv, base62);
     if (len < 0)
 	return -4;
 
@@ -622,15 +628,15 @@ int encode_set(int c, unsigned *v, int bpp, char *base62)
 static
 int decode_set_init(const char *str, int *pbpp, int *pMshift)
 {
-    /* 7..32 values encoded with 'a'..'z' */
-    int bpp = *str++ + 7 - 'a';
+    int bpp, Mshift;
 
+    /* 7..32 values encoded with 'a'..'z' */
+    bpp = *str++ + 7 - 'a';
     if (bpp < 10 || bpp > 32)
 	return -1;
 
     /* golomb parameter */
-    int Mshift = *str++ + 7 - 'a';
-
+    Mshift = *str++ + 7 - 'a';
     if (Mshift < 7 || Mshift > 31)
 	return -2;
 
@@ -649,24 +655,32 @@ int decode_set_init(const char *str, int *pbpp, int *pMshift)
 static inline
 int decode_set_size(const char *str, int Mshift)
 {
+    int bitc;
+
     str += 2;
-    int bitc = decode_base62_size(str);
+    bitc = decode_base62_size(str);
+
     return decode_golomb_size(bitc, Mshift);
 }
 
 static
 int decode_set(const char *str, int Mshift, unsigned *v)
 {
+    char *bitv;
+    int bitc;
+    int c;
+
     str += 2;
+
     /* base62 */
-    char bitv[decode_base62_size(str)];
-    int bitc = decode_base62(str, bitv);
+    bitv = alloca(decode_base62_size(str));
+    bitc = decode_base62(str, bitv);
 
     if (bitc < 0)
 	return -1;
 
     /* golomb */
-    int c = decode_golomb(bitc, bitv, Mshift, v);
+    c = decode_golomb(bitc, bitv, Mshift, v);
     if (c < 0)
 	return -2;
 
@@ -731,30 +745,32 @@ void test_set(void)
 /* main API routine */
 int rpmsetcmp(const char *str1, const char *str2)
 {
+	int bpp1, Mshift1, c1, i1;
+	int bpp2, Mshift2, c2, i2;
+	int ge, le;
+	unsigned *v1, *v2;
+
 	if (strncmp(str1, "set:", 4) == 0)
 	    str1 += 4;
 	if (strncmp(str2, "set:", 4) == 0)
 	    str2 += 4;
 
 	/* initialize decoding */
-	int bpp1, Mshift1;
-	int bpp2, Mshift2;
-
 	if (decode_set_init(str1, &bpp1, &Mshift1) < 0)
 	    return -3;
 	if (decode_set_init(str2, &bpp2, &Mshift2) < 0)
 	    return -4;
 
 	/* make room for hash values */
-	unsigned v1[decode_set_size(str1, Mshift1)];
-	unsigned v2[decode_set_size(str2, Mshift2)];
+	v1 = alloca(sizeof(*v1) * decode_set_size(str1, Mshift1));
+	v2 = alloca(sizeof(*v2) * decode_set_size(str2, Mshift2));
 
 	/* decode hash values */
-	int c1 = decode_set(str1, Mshift1, v1);
+	c1 = decode_set(str1, Mshift1, v1);
 	if (c1 < 0)
 	    return -3;
 
-	int c2 = decode_set(str2, Mshift2, v2);
+	c2 = decode_set(str2, Mshift2, v2);
 	if (c2 < 0)
 	    return -4;
 
@@ -769,9 +785,8 @@ int rpmsetcmp(const char *str1, const char *str2)
 	}
 
 	/* compare */
-	int ge = 1;
-	int le = 1;
-	int i1 = 0, i2 = 0;
+	ge = 1; le = 1;
+	i1 = 0; i2 = 0;
 	while (i1 < c1 && i2 < c2) {
 	    if (v1[i1] < v2[i2]) {
 		le = 0;
@@ -840,14 +855,15 @@ void set_add(struct set *set, const char *sym)
 
 struct set *set_free(struct set *set)
 {
-    if (set) {
-	int i;
+    int i;
 
-	for (i = 0; i < set->c; i++)
-	    set->sv[i].s = _free(set->sv[i].s);
+    if (!set)
+	return NULL;
+
+    for (i = 0; i < set->c; i++)
+	set->sv[i].s = _free(set->sv[i].s);
+    set->sv = _free(set->sv);
 
-	set->sv = _free(set->sv);
-    }
     return NULL;
 }
 
@@ -889,16 +905,20 @@ int cmp_sv(const void *arg1, const void *arg2)
 /* This routine does the whole job. */
 const char *set_fini(struct set *set, int bpp)
 {
+    unsigned mask;
+    unsigned v[set->c];
+    char *base62;
+    int c, len, i;
+
     if (set->c < 1)
 	return NULL;
     if (bpp < 10)
 	return NULL;
     if (bpp > 32)
 	return NULL;
-    unsigned mask = (bpp < 32) ? (1u << bpp) - 1 : ~0u;
+    mask = (bpp < 32) ? (1u << bpp) - 1 : ~0u;
 
     /* hash sv strings */
-    int i;
     for (i = 0; i < set->c; i++)
 	set->sv[i].v = jenkins_hash(set->sv[i].s) & mask;
 
@@ -916,13 +936,12 @@ const char *set_fini(struct set *set, int bpp)
     }
 
     /* encode */
-    unsigned v[set->c];
     for (i = 0; i < set->c; i++)
 	v[i] = set->sv[i].v;
 
-    int c = uniqv(set->c, v);
-    char base62[encode_set_size(c, bpp)];
-    int len = encode_set(c, v, bpp, base62);
+    c = uniqv(set->c, v);
+    base62 = alloca(encode_set_size(c, bpp));
+    len = encode_set(c, v, bpp, base62);
     if (len < 0)
 	return NULL;
 
-- 
1.7.3.2



  parent reply	other threads:[~2010-11-16 15:56 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-16 15:56 [devel] [PATCH 0/8] rpm: cleanup set.c and set.h Kirill A. Shutsemov
2010-11-16 15:56 ` [devel] [PATCH 1/8] set.c, set.h: get rid of C++-style comments Kirill A. Shutsemov
2010-11-16 15:56 ` [devel] [PATCH 2/8] set.c: get rid of nested functions Kirill A. Shutsemov
2010-11-16 22:14   ` Dmitry V. Levin
2010-11-16 22:54     ` Kirill A. Shutemov
2010-11-16 23:07       ` Dmitry V. Levin
2010-11-16 23:10         ` Kirill A. Shutemov
2010-11-17 17:55           ` Dmitry V. Levin
2010-11-16 15:56 ` [devel] [PATCH 3/8] set.c: fixup self-test functions declaration Kirill A. Shutsemov
2010-11-16 15:56 ` [devel] [PATCH 4/8] set.c: slightly reformat code to increase its readability Kirill A. Shutsemov
2010-11-16 15:56 ` Kirill A. Shutsemov [this message]
2010-11-16 15:56 ` [devel] [PATCH 6/8] set.c: use function-like syntax for sizeof Kirill A. Shutsemov
2010-11-16 15:56 ` [devel] [PATCH 7/8] set.c: cleanup self-tests Kirill A. Shutsemov
2010-11-16 15:56 ` [devel] [PATCH 8/8] set.c: update copyright notice Kirill A. Shutsemov
2010-11-22  5:49   ` Alexey Tourbin
2010-11-23  0:48       ` Alexey Tourbin
2010-11-23  0:56         ` Dmitry V. Levin
2010-11-23  1:38           ` Alexey Tourbin
2010-11-23  6:42             ` Kirill A. Shutemov
2010-11-23 11:50               ` Alexey Tourbin
2010-11-25 22:02                 ` Kirill A. Shutemov
2010-11-26 18:03                 ` Michael Shigorin
2010-11-17  6:59 ` [devel] [PATCH 0/8] rpm: cleanup set.c and set.h REAL
2010-11-22  5:40 ` Alexey Tourbin
2010-11-22  7:14   ` Kirill A. Shutemov
2010-11-22  7:38     ` Alexey Tourbin

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=1289923002-14132-6-git-send-email-kirill@shutemov.name \
    --to=kirill@shutemov.name \
    --cc=at@altlinux.ru \
    --cc=devel@lists.altlinux.org \
    --cc=ldv@altlinux.org \
    --cc=legion@altlinux.ru \
    --cc=raorn@altlinux.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

ALT Linux Team development discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/devel/0 devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 devel devel/ http://lore.altlinux.org/devel \
		devel@altlinux.org devel@altlinux.ru devel@lists.altlinux.org devel@lists.altlinux.ru devel@linux.iplabs.ru mandrake-russian@linuxteam.iplabs.ru sisyphus@linuxteam.iplabs.ru
	public-inbox-index devel

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git