simple cpplib patch

Zack Weinberg zack@rabi.columbia.edu
Sat Feb 20 07:30:00 GMT 1999


This patch just gets rid of the static macro hash table.

zw

1999-02-20 10:23 -0500  Zack Weinberg  <zack@rabi.columbia.edu>

	* cpphash.c (install): Rename to cpp_install, add cpp_reader*
          first argument.  All callers changed.
	  (hashtab): Removed.
	  (cpp_lookup, cpp_install): Change all refs to hashtab to
          pfile->hashtab.
	  (cpp_hash_cleanup): Removed.
	* cpphash.h: Adjust prototypes.
	* cpplib.h (struct cpp_reader): Add hashtab pointer.
	* cppinit.c (cpp_reader_init): Also allocate space for the
          hashtab.
	  (cpp_cleanup): Delete all macros and free the hashtab.

============================================================
Index: cpplib.c
--- cpplib.c	1999/02/18 15:35:42	1.72
+++ cpplib.c	1999/02/20 15:22:16
@@ -657,8 +657,8 @@
 	 that for this new definition now.  */
       if (CPP_OPTIONS (pfile)->debug_output && keyword)
 	pass_thru_directive (macro, end, pfile, keyword);
-      install (mdef.symnam, mdef.symlen, T_MACRO,
-	       (char *) mdef.defn, hashcode);
+      cpp_install (pfile, mdef.symnam, mdef.symlen, T_MACRO,
+		   (char *) mdef.defn, hashcode);
     }
 
   return 0;
@@ -2845,7 +2845,7 @@
 
   base = cpp_lookup (pfile, sym, baselen, -1);
   if (! base)
-    base = install (sym, baselen, T_ASSERT, 0, -1);
+    base = cpp_install (pfile, sym, baselen, T_ASSERT, 0, -1);
   else if (base->type != T_ASSERT)
   {
     /* Token clash - but with what?! */
@@ -2854,8 +2854,8 @@
     goto error;
   }
 
-  this = install (sym, thislen, T_ASSERT,
-		  (char *)base->value.aschain, -1);
+  this = cpp_install (pfile, sym, thislen, T_ASSERT,
+		      (char *)base->value.aschain, -1);
   base->value.aschain = this;
   
   pfile->limit = sym; /* Pop */
============================================================
Index: cpplib.h
--- cpplib.h	1999/02/18 15:35:43	1.30
+++ cpplib.h	1999/02/20 15:22:16
@@ -181,6 +181,10 @@
   /* Current depth of buffer stack. */
   int buffer_stack_depth;
 
+  /* Hash table of macros and assertions.  See cpphash.c */
+#define HASHSIZE 1403
+  struct hashnode **hashtab;
+  
   /* Hash table of other included files.  See cppfiles.c */
 #define ALL_INCLUDE_HASHSIZE 71
   struct include_hash *all_include_files[ALL_INCLUDE_HASHSIZE];
@@ -245,10 +249,6 @@
 
   /* Number of bytes since the last newline.  */
   int deps_column;
-
-#ifdef __cplusplus
-  ~cpp_reader () { cpp_cleanup (this); }
-#endif
 };
 
 #define CPP_FATAL_LIMIT 1000
============================================================
Index: cpphash.c
--- cpphash.c	1999/02/18 15:35:44	1.14
+++ cpphash.c	1999/02/20 15:22:16
@@ -27,8 +27,6 @@
 #include "cpplib.h"
 #include "cpphash.h"
 
-static HASHNODE *hashtab[HASHSIZE];
-
 static int comp_def_part	 PARAMS ((int, U_CHAR *, int, U_CHAR *,
 					  int, int));
 static int change_newlines	 PARAMS ((U_CHAR *, int));
@@ -104,7 +102,7 @@
 }
 
 /* Find the most recent hash node for name "name" (ending with first
-   non-identifier char) installed by install
+   non-identifier char) installed by cpp_install
 
    If LEN is >= 0, it is the length of the name.
    Otherwise, compute the length by scanning the entire name.
@@ -131,7 +129,7 @@
   if (hash < 0)
     hash = hashf (name, len, HASHSIZE);
 
-  bucket = hashtab[hash];
+  bucket = pfile->hashtab[hash];
   while (bucket)
     {
       if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
@@ -191,7 +189,7 @@
 /* Install a name in the main hash table, even if it is already there.
    Name stops with first non alphanumeric, except leading '#'.
    Caller must check against redefinition if that is desired.
-   delete_macro () removes things installed by install () in fifo order.
+   delete_macro () removes things installed by cpp_install () in fifo order.
    this is important because of the `defined' special symbol used
    in #if, and also if pushdef/popdef directives are ever implemented.
 
@@ -202,7 +200,8 @@
    Otherwise, compute the hash code.  */
 
 HASHNODE *
-install (name, len, type, value, hash)
+cpp_install (pfile, name, len, type, value, hash)
+     cpp_reader *pfile;
      U_CHAR *name;
      int len;
      enum node_type type;
@@ -227,9 +226,9 @@
   i = sizeof (HASHNODE) + len + 1;
   hp = (HASHNODE *) xmalloc (i);
   bucket = hash;
-  hp->bucket_hdr = &hashtab[bucket];
-  hp->next = hashtab[bucket];
-  hashtab[bucket] = hp;
+  hp->bucket_hdr = &pfile->hashtab[bucket];
+  hp->next = pfile->hashtab[bucket];
+  pfile->hashtab[bucket] = hp;
   hp->prev = NULL;
   if (hp->next != NULL)
     hp->next->prev = hp;
@@ -247,11 +246,6 @@
      cpp_reader *pfile ATTRIBUTE_UNUSED;
 {
   register int i;
-  for (i = HASHSIZE; --i >= 0;)
-    {
-      while (hashtab[i])
-	delete_macro (hashtab[i]);
-    }
 }
 
 static int
============================================================
Index: cpphash.h
--- cpphash.h	1999/02/18 15:35:45	1.10
+++ cpphash.h	1999/02/20 15:22:16
@@ -45,18 +45,16 @@
    the hashf () function.  Hashf () only exists for the sake of
    politeness, for use when speed isn't so important. */
 
-#define HASHSIZE 1403
 #define HASHSTEP(old, c) ((old << 2) + c)
 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
 
-extern HASHNODE *install PARAMS ((U_CHAR *, int, enum node_type,
-				  const char *, int));
-extern int hashf PARAMS ((const U_CHAR *, int, int));
-extern void delete_macro PARAMS ((HASHNODE *));
+extern HASHNODE *cpp_install	  PARAMS ((cpp_reader *, U_CHAR *, int,
+					   enum node_type, const char *, int));
+extern int hashf		  PARAMS ((const U_CHAR *, int, int));
+extern void delete_macro	  PARAMS ((HASHNODE *));
 
 extern MACRODEF create_definition PARAMS ((U_CHAR *, U_CHAR *,
 					   cpp_reader *, int));
 extern int compare_defs		  PARAMS ((cpp_reader *, DEFINITION *,
 					   DEFINITION *));
 extern void macroexpand		  PARAMS ((cpp_reader *, HASHNODE *));
-extern void cpp_hash_cleanup	  PARAMS ((cpp_reader *));
============================================================
Index: cppinit.c
--- cppinit.c	1999/02/19 12:50:44	1.4
+++ cppinit.c	1999/02/20 15:22:17
@@ -452,6 +452,8 @@
   pfile->token_buffer_size = 200;
   pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
   CPP_SET_WRITTEN (pfile, 0);
+
+  pfile->hashtab = (HASHNODE **) xcalloc (HASHSIZE, sizeof (HASHNODE *));
 }
 
 /* Free resources used by PFILE.
@@ -500,7 +502,12 @@
       pfile->all_include_files[i] = 0;
     }
 
-  cpp_hash_cleanup (pfile);
+  for (i = HASHSIZE; --i >= 0;)
+    {
+      while (pfile->hashtab[i])
+	delete_macro (pfile->hashtab[i]);
+    }
+  free (pfile->hashtab);
 }
 
 
@@ -510,31 +517,31 @@
      cpp_reader *pfile;
 {
 #define NAME(str) (U_CHAR *)str, sizeof str - 1
-  install (NAME("__TIME__"),		  T_TIME,	0, -1);
-  install (NAME("__DATE__"),		  T_DATE,	0, -1);
-  install (NAME("__FILE__"),		  T_FILE,	0, -1);
-  install (NAME("__BASE_FILE__"),	  T_BASE_FILE,	0, -1);
-  install (NAME("__LINE__"),		  T_SPECLINE,	0, -1);
-  install (NAME("__INCLUDE_LEVEL__"),	  T_INCLUDE_LEVEL, 0, -1);
-  install (NAME("__VERSION__"),		  T_VERSION,	0, -1);
+  cpp_install (pfile, NAME("__TIME__"),		  T_TIME,	0, -1);
+  cpp_install (pfile, NAME("__DATE__"),		  T_DATE,	0, -1);
+  cpp_install (pfile, NAME("__FILE__"),		  T_FILE,	0, -1);
+  cpp_install (pfile, NAME("__BASE_FILE__"),	  T_BASE_FILE,	0, -1);
+  cpp_install (pfile, NAME("__LINE__"),		  T_SPECLINE,	0, -1);
+  cpp_install (pfile, NAME("__INCLUDE_LEVEL__"),  T_INCLUDE_LEVEL, 0, -1);
+  cpp_install (pfile, NAME("__VERSION__"),	  T_VERSION,	0, -1);
 #ifndef NO_BUILTIN_SIZE_TYPE
-  install (NAME("__SIZE_TYPE__"),	  T_CONST, SIZE_TYPE, -1);
+  cpp_install (pfile, NAME("__SIZE_TYPE__"),	  T_CONST, SIZE_TYPE, -1);
 #endif
 #ifndef NO_BUILTIN_PTRDIFF_TYPE
-  install (NAME("__PTRDIFF_TYPE__ "),	  T_CONST, PTRDIFF_TYPE, -1);
+  cpp_install (pfile, NAME("__PTRDIFF_TYPE__ "),  T_CONST, PTRDIFF_TYPE, -1);
 #endif
-  install (NAME("__WCHAR_TYPE__"),	  T_CONST, WCHAR_TYPE, -1);
-  install (NAME("__USER_LABEL_PREFIX__"), T_CONST, user_label_prefix, -1);
-  install (NAME("__REGISTER_PREFIX__"),	  T_CONST, REGISTER_PREFIX, -1);
+  cpp_install (pfile, NAME("__WCHAR_TYPE__"),	  T_CONST, WCHAR_TYPE, -1);
+  cpp_install (pfile, NAME("__USER_LABEL_PREFIX__"), T_CONST, user_label_prefix, -1);
+  cpp_install (pfile, NAME("__REGISTER_PREFIX__"),  T_CONST, REGISTER_PREFIX, -1);
   if (!CPP_TRADITIONAL (pfile))
     {
-      install (NAME("__STDC__"),	  T_STDC,  0, -1);
+      cpp_install (pfile, NAME("__STDC__"),	  T_STDC,  0, -1);
 #if 0
       if (CPP_OPTIONS (pfile)->c9x)
-	install (NAME("__STDC_VERSION__"),T_CONST, "199909L", -1);
+	cpp_install (pfile, NAME("__STDC_VERSION__"),T_CONST, "199909L", -1);
       else
 #endif
-	install (NAME("__STDC_VERSION__"),T_CONST, "199409L", -1);
+	cpp_install (pfile, NAME("__STDC_VERSION__"),T_CONST, "199409L", -1);
     }
 #undef NAME
 


More information about the Gcc-patches mailing list