Merge commit 'ca5e0414e96886177d883a249edd957d2331db65'
[bpt/guile.git] / libguile / symbols.c
index 9dd2044..f93833b 100644 (file)
@@ -1,5 +1,5 @@
 /* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2003, 2004,
- *   2006, 2009, 2011 Free Software Foundation, Inc.
+ *   2006, 2009, 2011, 2013 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -33,7 +33,6 @@
 #include "libguile/variable.h"
 #include "libguile/alist.h"
 #include "libguile/fluids.h"
-#include "libguile/threads.h"
 #include "libguile/strings.h"
 #include "libguile/vectors.h"
 #include "libguile/weak-set.h"
@@ -168,7 +167,7 @@ utf8_string_equals_wide_string (const scm_t_uint8 *narrow, size_t nlen,
       nbytes = u8_mbtouc (&c, narrow + byte_idx, nlen - byte_idx);
       if (nbytes == 0)
         break;
-      else if (nbytes < 0)
+      else if (c == 0xfffd)
         /* Bad UTF-8.  */
         return 0;
       else if (c != wide[char_idx])
@@ -379,9 +378,7 @@ SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0,
 /* The default prefix for `gensym'd symbols.  */
 static SCM default_gensym_prefix;
 
-#define GENSYM_LENGTH      22  /* bytes */
-#define GENSYM_RADIX_BITS  6
-#define GENSYM_RADIX       (1 << (GENSYM_RADIX_BITS))
+#define MAX_PREFIX_LENGTH 30
 
 SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
             (SCM prefix),
@@ -392,47 +389,22 @@ SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
            "resetting the counter.")
 #define FUNC_NAME s_scm_gensym
 {
-  static const char base64[GENSYM_RADIX] =
-    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$@";
-  static const char base4[4] = "_.-~";
-
-  unsigned char *digit_buf = SCM_I_CURRENT_THREAD->gensym_counter;
-  char char_buf[GENSYM_LENGTH];
+  static int gensym_counter = 0;
+  
   SCM suffix, name;
-  int i;
+  int n, n_digits;
+  char buf[SCM_INTBUFLEN];
 
   if (SCM_UNBNDP (prefix))
     prefix = default_gensym_prefix;
 
-  if (SCM_UNLIKELY (digit_buf == NULL))
-    {
-      /* This is the first time gensym has been called in this thread.
-         Allocate and randomize our new thread-local gensym counter */
-      digit_buf = (unsigned char *)
-        scm_gc_malloc_pointerless (GENSYM_LENGTH, "gensym-counter");
-      scm_i_random_bytes_from_platform (digit_buf, GENSYM_LENGTH);
-      for (i = (GENSYM_LENGTH - 1); i >= 0; --i)
-        digit_buf[i] &= (GENSYM_RADIX - 1);
-      SCM_I_CURRENT_THREAD->gensym_counter = digit_buf;
-    }
-
-  /* Increment our thread-local gensym_counter. */
-  for (i = (GENSYM_LENGTH - 1); i >= 0; --i)
-    {
-      if (SCM_LIKELY (++(digit_buf[i]) < GENSYM_RADIX))
-        break;
-      else
-        digit_buf[i] = 0;
-    }
-
-  /* Encode digit_buf as base64, except for the first character where we
-     use the sparse glyphs "_.-~" (base 4) to provide some visual
-     separation between the prefix and the dense base64 block. */
-  for (i = (GENSYM_LENGTH - 1); i > 0; --i)
-    char_buf[i] = base64[digit_buf[i]];
-  char_buf[0] = base4[digit_buf[0] & 3];
+  /* mutex in case another thread looks and incs at the exact same moment */
+  scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
+  n = gensym_counter++;
+  scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
 
-  suffix = scm_from_latin1_stringn (char_buf, GENSYM_LENGTH);
+  n_digits = scm_iint2str (n, 10, buf);
+  suffix = scm_from_latin1_stringn (buf, n_digits);
   name = scm_string_append (scm_list_2 (prefix, suffix));
   return scm_string_to_symbol (name);
 }
@@ -450,7 +422,7 @@ SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
 
 SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0, 
            (SCM s),
-           "Return the contents of @var{symbol}'s @dfn{function slot}.")
+           "Return the contents of the symbol @var{s}'s @dfn{function slot}.")
 #define FUNC_NAME s_scm_symbol_fref
 {
   SCM_VALIDATE_SYMBOL (1, s);
@@ -461,7 +433,8 @@ SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
 
 SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0, 
            (SCM s),
-           "Return the @dfn{property list} currently associated with @var{symbol}.")
+           "Return the @dfn{property list} currently associated with the\n"
+           "symbol @var{s}.")
 #define FUNC_NAME s_scm_symbol_pref
 {
   SCM_VALIDATE_SYMBOL (1, s);
@@ -472,7 +445,7 @@ SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
 
 SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0, 
            (SCM s, SCM val),
-           "Change the binding of @var{symbol}'s function slot.")
+           "Change the binding of the symbol @var{s}'s function slot.")
 #define FUNC_NAME s_scm_symbol_fset_x
 {
   SCM_VALIDATE_SYMBOL (1, s);
@@ -484,7 +457,7 @@ SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
 
 SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
            (SCM s, SCM val),
-           "Change the binding of @var{symbol}'s property slot.")
+           "Change the binding of the symbol @var{s}'s property slot.")
 #define FUNC_NAME s_scm_symbol_pset_x
 {
   SCM_VALIDATE_SYMBOL (1, s);