Removed unused headers.
[cacao.git] / tables.c
index 0d5907521888bf5d10724ecc01535201f531fad2..9cbd95e9ae8a8ea4091834cbc91f074b72abf05b 100644 (file)
--- a/tables.c
+++ b/tables.c
@@ -35,7 +35,7 @@
        - the heap
        - additional support functions
 
-   $Id: tables.c 1185 2004-06-19 12:23:13Z twisti $
+   $Id: tables.c 1482 2004-11-11 14:39:13Z twisti $
 
 */
 
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <unistd.h>
+#include "builtin.h"
+#include "exceptions.h"
 #include "types.h"
-#include "main.h"
+#include "native.h"
+#include "options.h"
 #include "tables.h"
 #include "loader.h"
 #include "asmpart.h"
+#include "statistics.h"
 #include "threads/thread.h"
 #include "threads/locks.h"
 #include "toolbox/logging.h"
 #include "toolbox/memory.h"
 
 
-/* statistics */
-int count_utf_len = 0;         /* size of utf hash                  */
-int count_utf_new = 0;         /* calls of utf_new                  */
-int count_utf_new_found  = 0;  /* calls of utf_new with fast return */
-
 hashtable utf_hash;     /* hashtable for utf8-symbols */
 hashtable string_hash;  /* hashtable for javastrings  */
 hashtable class_hash;   /* hashtable for classes      */
@@ -170,15 +169,21 @@ void tables_close()
 
        write utf symbol to stdout (debugging purposes)
 
-******************************************************************************/
+*******************************************************************************/
 
 void utf_display(utf *u)
 {
-    char *endpos  = utf_end(u);  /* points behind utf string       */
-    char *utf_ptr = u->text;     /* current position in utf text   */
+    char *endpos;                       /* points behind utf string           */
+    char *utf_ptr;                      /* current position in utf text       */
 
-       if (!u)
+       if (!u) {
+               printf("NULL");
+               fflush(stdout);
                return;
+       }
+
+    endpos = utf_end(u);
+    utf_ptr = u->text;
 
     while (utf_ptr < endpos) {
                /* read next unicode character */                
@@ -191,19 +196,25 @@ void utf_display(utf *u)
 }
 
 
-/********************* function: utf_display *********************************
+/* utf_display_classname *******************************************************
 
-       write utf symbol to stdout (debugging purposes)
+   write utf symbol to stdout (debugging purposes)
 
-******************************************************************************/
+*******************************************************************************/
 
 void utf_display_classname(utf *u)
 {
-    char *endpos  = utf_end(u);  /* points behind utf string       */
-    char *utf_ptr = u->text;     /* current position in utf text   */
+    char *endpos;                       /* points behind utf string           */
+    char *utf_ptr;                      /* current position in utf text       */
 
-       if (!u)
+       if (!u) {
+               printf("NULL");
+               fflush(stdout);
                return;
+       }
+
+    endpos = utf_end(u);
+    utf_ptr = u->text;
 
     while (utf_ptr < endpos) {
                /* read next unicode character */                
@@ -245,17 +256,25 @@ void log_plain_utf(utf *u)
 }
 
 
-/************************ function: utf_sprint *******************************
+/* utf_sprint ******************************************************************
        
-    write utf symbol into c-string (debugging purposes)                                                 
+   write utf symbol into c-string (debugging purposes)
 
-******************************************************************************/
+*******************************************************************************/
 
 void utf_sprint(char *buffer, utf *u)
 {
-    char *endpos  = utf_end(u);  /* points behind utf string       */
-    char *utf_ptr = u->text;     /* current position in utf text   */ 
-    u2 pos = 0;                  /* position in c-string           */
+    char *endpos;                       /* points behind utf string           */
+    char *utf_ptr;                      /* current position in utf text       */
+    u2 pos = 0;                         /* position in c-string               */
+
+       if (!u) {
+               memcpy(buffer, "NULL", 5);      /* 4 chars + terminating \0           */
+               return;
+       }
+
+    endpos = utf_end(u);
+    utf_ptr = u->text;
 
     while (utf_ptr < endpos) 
                /* copy next unicode character */       
@@ -266,17 +285,25 @@ void utf_sprint(char *buffer, utf *u)
 }
 
 
-/************************ function: utf_sprint_classname *********************
+/* utf_sprint_classname ********************************************************
        
-    write utf symbol into c-string (debugging purposes)
+   write utf symbol into c-string (debugging purposes)
 
-******************************************************************************
+*******************************************************************************/
 
 void utf_sprint_classname(char *buffer, utf *u)
 {
-    char *endpos  = utf_end(u);  /* points behind utf string       */
-    char *utf_ptr = u->text;     /* current position in utf text   */ 
-    u2 pos = 0;                  /* position in c-string           */
+    char *endpos;                       /* points behind utf string           */
+    char *utf_ptr;                      /* current position in utf text       */
+    u2 pos = 0;                         /* position in c-string               */
+
+       if (!u) {
+               memcpy(buffer, "NULL", 5);      /* 4 chars + terminating \0           */
+               return;
+       }
+
+    endpos = utf_end(u);
+    utf_ptr = u->text;
 
     while (utf_ptr < endpos) {
                /* copy next unicode character */       
@@ -1095,6 +1122,9 @@ classinfo *class_new_intern(utf *classname)
                        }
                }
        }
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+       initObjectLock(&c->header);
+#endif
 
        return c;
 }
@@ -1114,6 +1144,7 @@ classinfo *class_new(utf *classname)
 
        if (opt_eager) {
                classinfo *tc;
+               classinfo *tmp;
 
                list_init(&unlinkedclasses, OFFSET(classinfo, listnode));
 
@@ -1122,29 +1153,30 @@ classinfo *class_new(utf *classname)
 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
                                tables_unlock();
 #endif
-                               return NULL;
+                               return c;
                        }
                }
 
                /* link all referenced classes */
 
-               while ((tc = list_first(&unlinkedclasses))) {
-       printf("tc=%p next=%p prev=%p ", tc, tc->listnode.next, tc->listnode.prev);
-       utf_display(tc->name);
-       printf("\n");
-       fflush(stdout);
+               tc = list_first(&unlinkedclasses);
 
-                       /* skip super class */
+               while (tc) {
+                       /* skip the current loaded/linked class */
                        if (tc != c) {
                                if (!class_link(tc)) {
 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
                                        tables_unlock();
 #endif
-                                       return NULL;
+                                       return c;
                                }
                        }
 
-                       list_remove(&unlinkedclasses, tc);
+                       /* we need a tmp variable here, because list_remove sets prev and
+                          next to NULL */
+                       tmp = list_next(&unlinkedclasses, tc);
+                       list_remove(&unlinkedclasses, tc);
+                       tc = tmp;
                }
 
                if (!c->linked) {
@@ -1152,7 +1184,7 @@ classinfo *class_new(utf *classname)
 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
                                tables_unlock();
 #endif
-                               return NULL;
+                               return c;
                        }
                }
        }
@@ -1343,9 +1375,17 @@ classinfo *class_multiarray_of(int dim, classinfo *element)
 
 u4 utf_strlen(utf *u) 
 {
-    char *endpos  = utf_end(u);  /* points behind utf string       */
-    char *utf_ptr = u->text;     /* current position in utf text   */
-    u4 len = 0;                  /* number of unicode characters   */
+    char *endpos;                   /* points behind utf string       */
+    char *utf_ptr;                  /* current position in utf text   */
+    u4 len = 0;                     /* number of unicode characters   */
+
+       if (!u) {
+               *exceptionptr = new_nullpointerexception();
+               return 0;
+       }
+
+       endpos = utf_end(u);
+       utf_ptr = u->text;
 
     while (utf_ptr < endpos) {
                len++;