5e5d3b2004ef910b911f256ce371dac47813d0a0
[cacao.git] / src / vm / package.cpp
1 /* src/vm/package.cpp - Java boot-package functions
2
3    Copyright (C) 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 #include "config.h"
27
28 #include <stdint.h>
29
30 #include "toolbox/list.h"
31
32 #include "mm/memory.h"
33
34 #include "native/jni.h"
35
36 #include "vm/options.h"
37 #include "vm/package.hpp"
38 #include "vm/string.hpp"
39 #include "vm/utf8.h"
40
41
42 /* internal property structure ************************************************/
43
44 typedef struct list_package_entry_t list_package_entry_t;
45
46 struct list_package_entry_t {
47 /*      java_string_t *packagename; */
48         utf           *packagename;
49         listnode_t     linkage;
50 };
51
52
53 /* global variables ***********************************************************/
54
55 static list_t *list_package = NULL;
56
57
58 /**
59  * Initialize the package list.
60  */
61 void Package::initialize(void)
62 {
63         TRACESUBSYSTEMINITIALIZATION("package_init");
64
65         /* create the properties list */
66
67         list_package = list_create(OFFSET(list_package_entry_t, linkage));
68 }
69
70
71 /**
72  * Add a package to the boot-package list.
73  *
74  * @param packagename Package name as Java string.
75  */
76 /* void package_add(java_handle_t *packagename) */
77 void Package::add(utf *packagename)
78 {
79 /*      java_string_t        *s; */
80         list_package_entry_t *lpe;
81
82         /* Intern the Java string to get a unique address. */
83
84 /*      s = javastring_intern(packagename); */
85
86         /* Check if the package is already stored. */
87
88         if (Package::find(packagename) != NULL)
89                 return;
90
91         /* Add the package. */
92
93 #if !defined(NDEBUG)
94         if (opt_DebugPackage) {
95                 log_start();
96                 log_print("[package_add: packagename=");
97                 utf_display_printable_ascii(packagename);
98                 log_print("]");
99                 log_finish();
100         }
101 #endif
102
103         lpe = NEW(list_package_entry_t);
104
105         lpe->packagename = packagename;
106
107         list_add_last(list_package, lpe);
108 }
109
110
111 /**
112  * Find a package in the list.
113  *
114  * @param packagename Package name as Java string.
115  *
116  * @return Package name as Java string.
117  */
118 /* java_handle_t *package_find(java_handle_t *packagename) */
119 utf* Package::find(utf *packagename)
120 {
121 /*      java_string_t        *s; */
122         list_t               *l;
123         list_package_entry_t *lpe;
124
125         /* Intern the Java string to get a unique address. */
126
127 /*      s = javastring_intern(packagename); */
128
129         /* For convenience. */
130
131         l = list_package;
132
133         for (lpe = (list_package_entry_t*) list_first(l); lpe != NULL; lpe = (list_package_entry_t*) list_next(l, lpe)) {
134 /*              if (lpe->packagename == s) */
135                 if (lpe->packagename == packagename)
136                         return lpe->packagename;
137         }
138
139         return NULL;
140 }
141
142
143 /* Legacy C interface *********************************************************/
144
145 extern "C" {
146
147 void Package_initialize(void) { Package::initialize(); }
148 void Package_add(utf* packagename) { Package::add(packagename); }
149 utf* Package_find(utf *packagename) { return Package::find(packagename); }
150
151 }
152
153
154 /*
155  * These are local overrides for various environment variables in Emacs.
156  * Please do not remove this and leave it at the end of the file, where
157  * Emacs will automagically detect them.
158  * ---------------------------------------------------------------------
159  * Local variables:
160  * mode: c++
161  * indent-tabs-mode: t
162  * c-basic-offset: 4
163  * tab-width: 4
164  * End:
165  * vim:noexpandtab:sw=4:ts=4:
166  */