Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Alexey Makhalov <makhaloff@gmail.com>
To: Tom Tromey <tromey@redhat.com>
Cc: gdb-patches@sourceware.org
Subject: Re: Sim hangs on new target at dup_arg_p() in infinite loop.
Date: Fri, 20 Sep 2013 05:02:00 -0000	[thread overview]
Message-ID: <CALd+sge-JGnmCc3Sj1iExukM=J9n=QM2ZgUmBPZa9FKvJwyBfg@mail.gmail.com> (raw)
In-Reply-To: <CALd+sgeSJz46ja1xbbFYO12kiF=LjVCaF2Xy_TzeMUkPkwZVmw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2709 bytes --]

Tom, thanks for advice.
Now it works fine.


Index: sim/common/sim-options.c
===================================================================
RCS file: /cvs/src/src/sim/common/sim-options.c,v
retrieving revision 1.31
diff -u -p -r1.31 sim-options.c
--- sim/common/sim-options.c 3 Sep 2013 20:45:08 -0000 1.31
+++ sim/common/sim-options.c 20 Sep 2013 04:57:09 -0000
@@ -35,6 +35,7 @@ along with this program.  If not, see <h
 #include "sim-assert.h"

 #include "bfd.h"
+#include "hashtab.h"

 /* Add a set of options to the simulator.
    TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry.
@@ -485,37 +486,40 @@ standard_install (SIM_DESC sd)
   return SIM_RC_OK;
 }

+/* A simple comparison function with opposite semantics to strcmp.  */
+
+static int
+streq (const char *lhs, const char *rhs)
+{
+  return !strcmp (lhs, rhs);
+}
+
 /* Return non-zero if arg is a duplicate argument.
    If ARG is NULL, initialize.  */

-#define ARG_HASH_SIZE 97
-#define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char)
a[1]) % ARG_HASH_SIZE)
+#define ARG_HASH_SIZE 256

 static int
 dup_arg_p (const char *arg)
 {
-  int hash;
-  static const char **arg_table = NULL;
+  void **slot;
+  static htab_t arg_table = NULL;

   if (arg == NULL)
     {
       if (arg_table == NULL)
- arg_table = (const char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
-      memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
+        arg_table = htab_create (ARG_HASH_SIZE, htab_hash_string,
+               (int (*)(const void *, const void *)) streq, free);
+      else
+        htab_empty (arg_table);
       return 0;
     }

-  hash = ARG_HASH (arg);
-  while (arg_table[hash] != NULL)
-    {
-      if (strcmp (arg, arg_table[hash]) == 0)
- return 1;
-      /* We assume there won't be more than ARG_HASH_SIZE arguments so we
- don't check if the table is full.  */
-      if (++hash == ARG_HASH_SIZE)
- hash = 0;
-    }
-  arg_table[hash] = arg;
+  slot = htab_find_slot (arg_table, arg, INSERT);
+  if (*slot != NULL)
+    return 1;
+
+  *slot = strdup(arg);
   return 0;
 }

On Tue, Sep 17, 2013 at 2:52 PM, Alexey Makhalov <makhaloff@gmail.com> wrote:
> I'll try.
>
>
> On Tue, Sep 17, 2013 at 10:29 AM, Tom Tromey <tromey@redhat.com> wrote:
>>
>> >>>>> "Alexey" == Alexey Makhalov <makhaloff@gmail.com> writes:
>>
>> Alexey> I've added checking for hash table overflow. It's better to have
>> Alexey> internal error message instead of looping.
>>
>> Alexey> 2013-09-09 Alexey Makhalov <makhaloff@gmail.com>
>> Alexey>         * sim-options.c (dup_arg_p) Check for hash table overflow.
>>
>> Is it possible to just use the libiberty hashtab code and not have it
>> ever overflow?
>>
>> Tom
>
>

[-- Attachment #2: htab.patch --]
[-- Type: application/octet-stream, Size: 2052 bytes --]

Index: sim/common/sim-options.c
===================================================================
RCS file: /cvs/src/src/sim/common/sim-options.c,v
retrieving revision 1.31
diff -u -p -r1.31 sim-options.c
--- sim/common/sim-options.c	3 Sep 2013 20:45:08 -0000	1.31
+++ sim/common/sim-options.c	20 Sep 2013 04:57:09 -0000
@@ -35,6 +35,7 @@ along with this program.  If not, see <h
 #include "sim-assert.h"
 
 #include "bfd.h"
+#include "hashtab.h"
 
 /* Add a set of options to the simulator.
    TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry.
@@ -485,37 +486,40 @@ standard_install (SIM_DESC sd)
   return SIM_RC_OK;
 }
 
+/* A simple comparison function with opposite semantics to strcmp.  */
+
+static int
+streq (const char *lhs, const char *rhs)
+{
+  return !strcmp (lhs, rhs);
+}
+
 /* Return non-zero if arg is a duplicate argument.
    If ARG is NULL, initialize.  */
 
-#define ARG_HASH_SIZE 97
-#define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
+#define ARG_HASH_SIZE 256
 
 static int
 dup_arg_p (const char *arg)
 {
-  int hash;
-  static const char **arg_table = NULL;
+  void **slot;
+  static htab_t arg_table = NULL;
 
   if (arg == NULL)
     {
       if (arg_table == NULL)
-	arg_table = (const char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
-      memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
+        arg_table = htab_create (ARG_HASH_SIZE, htab_hash_string, 
+               (int (*)(const void *, const void *)) streq, free);
+      else
+        htab_empty (arg_table);
       return 0;
     }
 
-  hash = ARG_HASH (arg);
-  while (arg_table[hash] != NULL)
-    {
-      if (strcmp (arg, arg_table[hash]) == 0)
-	return 1;
-      /* We assume there won't be more than ARG_HASH_SIZE arguments so we
-	 don't check if the table is full.  */
-      if (++hash == ARG_HASH_SIZE)
-	hash = 0;
-    }
-  arg_table[hash] = arg;
+  slot = htab_find_slot (arg_table, arg, INSERT);
+  if (*slot != NULL)
+    return 1;
+
+  *slot = strdup(arg);
   return 0;
 }

  parent reply	other threads:[~2013-09-20  5:02 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-09 20:13 Alexey Makhalov
2013-09-11 22:28 ` Mike Frysinger
2013-09-17 18:11 ` Tom Tromey
     [not found]   ` <CALd+sgeSJz46ja1xbbFYO12kiF=LjVCaF2Xy_TzeMUkPkwZVmw@mail.gmail.com>
2013-09-20  5:02     ` Alexey Makhalov [this message]
2013-09-20 17:33       ` Tom Tromey
2013-09-21  2:59         ` Alexey Makhalov
2013-11-07 15:20           ` Tom Tromey
2013-11-08  7:25             ` Alexey Makhalov
2013-11-27 11:28               ` Mike Frysinger
2013-11-27 12:22                 ` Joel Brobecker
2013-11-27 18:58                   ` Mike Frysinger

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='CALd+sge-JGnmCc3Sj1iExukM=J9n=QM2ZgUmBPZa9FKvJwyBfg@mail.gmail.com' \
    --to=makhaloff@gmail.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@redhat.com \
    /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