Changed license terms to the plain LGPL thru-out.
[bpt/guile.git] / libguile / symbols.c
index 7805752..c840301 100644 (file)
@@ -1,46 +1,25 @@
-/* Copyright (C) 1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003 Free Software Foundation, Inc.
  * 
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- * 
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this software; see the file COPYING.  If not, write to
- * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307 USA
- *
- * As a special exception, the Free Software Foundation gives permission
- * for additional uses of the text contained in its release of GUILE.
- *
- * The exception is that, if you link the GUILE library with other files
- * to produce an executable, this does not by itself cause the
- * resulting executable to be covered by the GNU General Public License.
- * Your use of that executable is in no way restricted on account of
- * linking the GUILE library code into it.
- *
- * This exception does not however invalidate any other reasons why
- * the executable file might be covered by the GNU General Public License.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This exception applies only to the code released by the
- * Free Software Foundation under the name GUILE.  If you copy
- * code from other Free Software Foundation releases into a copy of
- * GUILE, as the General Public License permits, the exception does
- * not apply to the code that you add in this way.  To avoid misleading
- * anyone as to the status of such modified files, you must delete
- * this exception notice from them.
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * If you write modifications of your own for GUILE, it is your choice
- * whether to permit this exception to apply to your modifications.
- * If you do not wish that, delete this exception notice.  */
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
 
 
 \f
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
 
 #include "libguile/_scm.h"
 #include "libguile/chars.h"
@@ -83,19 +62,40 @@ SCM_DEFINE (scm_sys_symbols, "%symbols", 0, 0, 0,
 /* {Symbols}
  */
 
+/* In order to optimize reading speed, this function breaks part of
+ * the hashtable abstraction.  The optimizations are:
+ *
+ * 1. The argument string can be compared directly to symbol objects
+ *    without first creating an SCM string object.  (This would have
+ *    been necessary if we had used the hashtable API in hashtab.h.)
+ *
+ * 2. We can use the raw hash value stored in SCM_SYMBOL_HASH (sym)
+ *    to speed up lookup.
+ *
+ * Both optimizations might be possible without breaking the
+ * abstraction if the API in hashtab.c is improved.
+ */
+
+unsigned long
+scm_i_hash_symbol (SCM obj, unsigned long n, void *closure)
+{
+  return SCM_SYMBOL_HASH (obj) % n;
+}
 
 SCM
 scm_mem2symbol (const char *name, size_t len)
 {
-  size_t raw_hash = scm_string_hash ((const unsigned char *) name, len)/2;
-  size_t hash = raw_hash % SCM_VECTOR_LENGTH (symbols);
+  size_t raw_hash = scm_string_hash ((const unsigned char *) name, len) / 2;
+  size_t hash = raw_hash % SCM_HASHTABLE_N_BUCKETS (symbols);
 
   {
     /* Try to find the symbol in the symbols table */
 
     SCM l;
 
-    for (l = SCM_VELTS (symbols) [hash]; !SCM_NULLP (l); l = SCM_CDR (l))
+    for (l = SCM_HASHTABLE_BUCKETS (symbols) [hash];
+        !SCM_NULLP (l);
+        l = SCM_CDR (l))
       {
        SCM sym = SCM_CAAR (l);
        if (SCM_SYMBOL_HASH (sym) == raw_hash
@@ -120,20 +120,18 @@ scm_mem2symbol (const char *name, size_t len)
 
   {
     /* The symbol was not found - create it. */
-
-    SCM symbol;
-    SCM cell;
-    SCM slot;
-
-    symbol = scm_double_cell (SCM_MAKE_SYMBOL_TAG (len),
+    SCM symbol = scm_double_cell (SCM_MAKE_SYMBOL_TAG (len),
                              (scm_t_bits) scm_gc_strndup (name, len,
                                                           "symbol"),
                              raw_hash,
                              SCM_UNPACK (scm_cons (SCM_BOOL_F, SCM_EOL)));
 
-    slot = SCM_VELTS (symbols) [hash];
-    cell = scm_cons (symbol, SCM_UNDEFINED);
-    SCM_VELTS (symbols) [hash] = scm_cons (cell, slot);
+    SCM slot = SCM_HASHTABLE_BUCKETS (symbols) [hash];
+    SCM cell = scm_cons (symbol, SCM_UNDEFINED);
+    SCM_SET_HASHTABLE_BUCKET (symbols, hash, scm_cons (cell, slot));
+    SCM_HASHTABLE_INCREMENT (symbols);
+    if (SCM_HASHTABLE_N_ITEMS (symbols) > SCM_HASHTABLE_UPPER (symbols))
+      scm_i_rehash (symbols, scm_i_hash_symbol, 0, "scm_mem2symbol");
 
     return symbol;
   }
@@ -319,7 +317,7 @@ SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
            "Return the contents of @var{symbol}'s @dfn{function slot}.")
 #define FUNC_NAME s_scm_symbol_fref
 {
-  SCM_VALIDATE_SYMBOL (1,s);
+  SCM_VALIDATE_SYMBOL (1, s);
   return SCM_SYMBOL_FUNC (s);
 }
 #undef FUNC_NAME
@@ -330,7 +328,7 @@ SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
            "Return the @dfn{property list} currently associated with @var{symbol}.")
 #define FUNC_NAME s_scm_symbol_pref
 {
-  SCM_VALIDATE_SYMBOL (1,s);
+  SCM_VALIDATE_SYMBOL (1, s);
   return SCM_SYMBOL_PROPS (s);
 }
 #undef FUNC_NAME
@@ -341,7 +339,7 @@ SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
            "Change the binding of @var{symbol}'s function slot.")
 #define FUNC_NAME s_scm_symbol_fset_x
 {
-  SCM_VALIDATE_SYMBOL (1,s);
+  SCM_VALIDATE_SYMBOL (1, s);
   SCM_SET_SYMBOL_FUNC (s, val);
   return SCM_UNSPECIFIED;
 }
@@ -353,7 +351,7 @@ SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
            "Change the binding of @var{symbol}'s property slot.")
 #define FUNC_NAME s_scm_symbol_pset_x
 {
-  SCM_VALIDATE_SYMBOL (1,s);
+  SCM_VALIDATE_SYMBOL (1, s);
   SCM_DEFER_INTS;
   SCM_SET_SYMBOL_PROPS (s, val);
   SCM_ALLOW_INTS;
@@ -388,7 +386,7 @@ scm_c_symbol2str (SCM obj, char *str, size_t *lenp)
     {
       /* FIXME: Should we use exported wrappers for malloc (and free), which
        * allow windows DLLs to call the correct freeing function? */
-      str = (char *) malloc ((len + 1) * sizeof (char));
+      str = (char *) scm_malloc ((len + 1) * sizeof (char));
       if (str == NULL)
        return NULL;
     }
@@ -408,7 +406,7 @@ scm_c_symbol2str (SCM obj, char *str, size_t *lenp)
 void
 scm_symbols_prehistory ()
 {
-  symbols = scm_make_weak_key_hash_table (SCM_MAKINUM (1009));
+  symbols = scm_make_weak_key_hash_table (SCM_MAKINUM (2139));
   scm_permanent_object (symbols);
 }