e8286e050d066a1de38dc5112c08d6c08f47a813
[cacao.git] / src / vm / classcache.h
1 /* src/vm/classcache.h - loaded class cache and loading constraints
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _CLASSCACHE_H
27 #define _CLASSCACHE_H
28
29 #include "config.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 #include "vm/types.h"
36
37 #include <stdio.h>  /* for FILE */
38
39 #if defined(ENABLE_JVMTI)
40 # include "native/jni.h"
41 #endif
42
43 #include "toolbox/hashtable.h"
44
45 #include "vm/class.h"
46 #include "vm/global.h"
47 #include "vm/loader.h"
48 #include "vm/references.h"
49
50
51 /* forward declarations *******************************************************/
52
53 typedef struct classcache_name_entry classcache_name_entry;
54 typedef struct classcache_class_entry classcache_class_entry;
55 typedef struct classcache_loader_entry classcache_loader_entry;
56
57 /* global variables ***********************************************************/
58
59 extern hashtable hashtable_classcache;
60
61
62 /* structs ********************************************************************/
63
64 /*----------------------------------------------------------------------------*/
65 /* The Loaded Class Cache                                                     */
66 /*                                                                            */
67 /* The loaded class cache is implemented as a two-level data structure.       */
68 /*                                                                            */
69 /* The first level is a hash table indexed by class names. For each class     */
70 /* name in the cache there is a classcache_name_entry, which collects all     */
71 /* information about classes with this class name.                            */
72 /*                                                                            */
73 /* Second level: For each classcache_name_entry there is a list of            */
74 /* classcache_class_entry:s representing the possible different resolutions   */
75 /* of the class name.                                                         */
76 /*                                                                            */
77 /* A classcache_class_entry records the following:                            */
78 /*                                                                            */
79 /* - the loaded class object, if this entry has been resolved, otherwise NULL */
80 /* - the list of initiating loaders which have resolved the class name to     */
81 /*   this class object                                                        */
82 /* - the list of initiating loaders which are constrained to resolve this     */
83 /*   class name to this class object in the future                            */
84 /*                                                                            */
85 /* The classcache_class_entry:s approximate the equivalence classes created   */
86 /* by the loading constraints and the equivalence of loaded classes.          */
87 /*                                                                            */
88 /* When a loading constraint (loaderA,loaderB,NAME) is added, then the        */
89 /* classcache_class_entry:s for NAME containing loaderA and loaderB resp.     */
90 /* must be merged into one entry. If this is impossible, because the entries  */
91 /* have already been resolved to different class objects, then the constraint */
92 /* is violated and an expception must be thrown.                              */
93 /*----------------------------------------------------------------------------*/
94
95
96 /* classcache_name_entry
97  *
98  * For each classname a classcache_name_entry struct is created.
99  */
100
101 struct classcache_name_entry
102 {
103         utf                     *name;        /* class name                       */
104         classcache_name_entry   *hashlink;    /* link for external chaining       */
105         classcache_class_entry  *classes;     /* equivalence classes for this name*/
106 };
107
108 struct classcache_class_entry
109 {
110         classinfo               *classobj;    /* the loaded class object, or NULL */
111         classcache_loader_entry *loaders;
112         classcache_loader_entry *constraints;
113         classcache_class_entry  *next;        /* next class entry for same name   */
114 };
115
116 struct classcache_loader_entry
117 {
118         classloader_t            *loader;     /* class loader object              */
119         classcache_loader_entry  *next;       /* next loader entry in the list    */
120 };
121
122
123 /* callback function type for  classcache_foreach_loaded_class */
124
125 typedef void (*classcache_foreach_functionptr_t)(classinfo *, void *);
126
127
128 /* function prototypes ********************************************************/
129
130 /* initialize the loaded class cache */
131 bool classcache_init(void);
132 void classcache_free(void);
133
134 classinfo * classcache_lookup(classloader_t *initloader,utf *classname);
135 classinfo * classcache_lookup_defined(classloader_t *defloader,utf *classname);
136 classinfo * classcache_lookup_defined_or_initiated(classloader_t *loader,utf *classname);
137
138 bool classcache_store_unique(classinfo *cls);
139 classinfo * classcache_store(classloader_t *initloader,classinfo *cls,bool mayfree);
140 classinfo * classcache_store_defined(classinfo *cls);
141
142 #if defined(ENABLE_VERIFIER)
143 bool classcache_add_constraint(classloader_t *a,classloader_t *b,utf *classname);
144 bool classcache_add_constraints_for_params(classloader_t *a,classloader_t *b,
145                                                                                    methodinfo *m);
146 #endif
147
148 s4 classcache_get_loaded_class_count(void);
149
150 void classcache_foreach_loaded_class(classcache_foreach_functionptr_t func,
151                                                                          void *data);
152
153 #if defined(ENABLE_JVMTI)
154 void classcache_get_loaded_classes(s4 *class_count_ptr,
155                                                                    classinfo ***classes_ptr);
156 #endif
157
158 #ifndef NDEBUG
159 void classcache_debug_dump(FILE *file,utf *only);
160 #endif
161         
162 #ifdef __cplusplus
163 }
164 #endif
165
166 #endif /* _CLASSCACHE_H */
167
168 /*
169  * These are local overrides for various environment variables in Emacs.
170  * Please do not remove this and leave it at the end of the file, where
171  * Emacs will automagically detect them.
172  * ---------------------------------------------------------------------
173  * Local variables:
174  * mode: c
175  * indent-tabs-mode: t
176  * c-basic-offset: 4
177  * tab-width: 4
178  * End:
179  * vim:noexpandtab:sw=4:ts=4:
180  */
181