harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r294974 [18/25] - in /incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm: ./ jchevm/ jchevm/doc/ jchevm/etc/ jchevm/include/ jchevm/java/ jchevm/java/org/ jchevm/java/org/dellroad/ jchevm/java/org/dellroad/jc/ jchevm/java/org/dellroad...
Date Wed, 05 Oct 2005 02:20:10 GMT
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/libjc.h
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/libjc.h?rev=294974&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/libjc.h (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/libjc.h Tue Oct  4 19:19:16 2005
@@ -0,0 +1,605 @@
+
+/*
+ * Copyright 2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ * 
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ * $Id: libjc.h,v 1.33 2005/07/10 21:03:54 archiecobbs Exp $
+ */
+
+#ifndef _LIBJC_H_
+#define _LIBJC_H_
+
+#include "config.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+#include <sys/time.h>
+#include <sys/utsname.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <ucontext.h>
+#include <limits.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <signal.h>
+#include <ctype.h>
+#include <errno.h>
+#include <dlfcn.h>
+#include <sched.h>
+#include <math.h>
+#include <pwd.h>
+#include <elf.h>
+
+#include <zlib.h>
+
+#include <openssl/md5.h>
+
+#define _JC_VIRTUAL_MACHINE		1	/* for jni.h */
+
+#include "queue.h"
+#include "jni.h"
+#include "jc_defs.h"
+
+#include "arch_definitions.h"
+#include "definitions.h"
+#include "arch_structures.h"
+#include "structures.h"
+#include "arch_libjc.h"
+
+/* array.c */
+extern jint		_jc_setup_array_types(_jc_env *env);
+
+/* bootstrap.c */
+extern jint		_jc_bootstrap_classes(_jc_env *env);
+
+/* class_bytes.c */
+extern _jc_classbytes	*_jc_bootcl_find_classbytes(_jc_env *env,
+				const char *name, int *indexp);
+extern _jc_classbytes	*_jc_copy_classbytes(_jc_env *env, const void *data,
+				size_t len);
+extern _jc_classbytes	*_jc_read_classbytes(_jc_env *env, _jc_cpath_entry *ent,
+				const char *name);
+extern jint		_jc_read_classbytes_dir(_jc_env *env,
+				_jc_cpath_entry *ent, const char *name,
+				_jc_classbytes **bytesp);
+extern jint		_jc_read_classbytes_zip(_jc_env *env,
+				_jc_cpath_entry *ent, const char *name,
+				_jc_classbytes **bytesp);
+extern _jc_classbytes	*_jc_dup_classbytes(_jc_classbytes *bytes);
+extern void		_jc_free_classbytes(_jc_classbytes **bytesp);
+
+/* class_file.c */
+extern _jc_class_node	*_jc_get_class_node(_jc_env *env,
+				_jc_class_loader *loader, const char *name);
+extern _jc_class_node	*_jc_ref_class_node(_jc_env *env, const char *name,
+				jlong hash, _jc_classbytes *cbytes);
+extern void		_jc_unref_class_node(_jc_jvm *vm,
+				_jc_class_node **nodep);
+extern void		_jc_unref_class_deps(_jc_jvm *vm,
+				_jc_class_depend *deps, int num_deps);
+
+/* class_loader.c */
+extern _jc_class_loader	*_jc_get_loader(_jc_env *env, _jc_object *cl);
+extern _jc_class_loader	*_jc_get_jni_loader(_jc_env *env);
+extern _jc_class_loader	*_jc_create_loader(_jc_env *env);
+extern void		_jc_destroy_loader(_jc_jvm *vm,
+				_jc_class_loader **loaderp);
+extern void		_jc_loader_wait(_jc_env *env, _jc_class_loader *loader);
+extern jint		_jc_merge_implicit_refs(_jc_env *env,
+				_jc_resolve_info *info);
+
+/* class_object.c */
+extern jint		_jc_load_object(_jc_env *env, _jc_class_loader *loader,
+				const char *name);
+extern jint		_jc_generate_object(_jc_env *env,
+				_jc_class_loader *loader, const char *name);
+
+/* cl_alloc.c */
+extern void		_jc_uni_alloc_init(_jc_uni_mem *uni, int min_pages,
+				volatile _jc_word *avail_pages);
+extern void		_jc_uni_alloc_free(_jc_uni_mem *uni);
+extern void		*_jc_uni_alloc(_jc_env *env, _jc_uni_mem *uni,
+				size_t size);
+extern void		_jc_uni_unalloc(_jc_uni_mem *uni, void *ptrp,
+				size_t size);
+extern void		*_jc_uni_zalloc(_jc_env *env, _jc_uni_mem *uni,
+				size_t size);
+extern char		*_jc_uni_strdup(_jc_env *env, _jc_uni_mem *uni,
+				const char *s);
+extern jboolean		_jc_uni_contains(_jc_uni_mem *uni, const void *ptr);
+extern void		*_jc_uni_mark(_jc_uni_mem *uni);
+extern void		_jc_uni_reset(_jc_uni_mem *uni, void *mem);
+extern void		*_jc_cl_alloc(_jc_env *env, _jc_class_loader *loader,
+				size_t size);
+extern void		_jc_cl_unalloc(_jc_class_loader *loader, void *ptrp,
+				size_t size);
+extern void		*_jc_cl_zalloc(_jc_env *env, _jc_class_loader *loader,
+				size_t size);
+extern char		*_jc_cl_strdup(_jc_env *env, _jc_class_loader *loader,
+				const char *s);
+
+/* debug_line.c */
+extern jint		_jc_debug_line_stabs(_jc_env *env, _jc_elf *elf,
+				_jc_splay_tree *tree);
+extern jint		_jc_debug_line_dwarf2(_jc_env *env, _jc_elf *elf,
+				_jc_splay_tree *tree);
+extern int		_jc_exec_pc_to_jline(_jc_method *method,
+				const void *pc);
+
+/* deps.c */
+extern int		_jc_gen_deplist(_jc_env *env, _jc_classfile *cfile,
+				_jc_class_ref **arrayp);
+
+/* derive.c */
+extern _jc_type		*_jc_derive_type_from_classfile(_jc_env *env,
+				_jc_class_loader *loader, const char *name,
+				_jc_classbytes *cbytes);
+extern jint		_jc_derive_types_from_object(_jc_env *env,
+				_jc_elf *elf);
+extern _jc_type		*_jc_derive_array_type(_jc_env *env,
+				_jc_class_loader *loader, const char *name);
+extern void		_jc_initialize_lockword(_jc_env *env, _jc_type *type,
+				_jc_type *stype);
+extern void		_jc_initialize_bsi(_jc_jvm *vm, _jc_type *type);
+
+/* derive2.c */
+extern _jc_type		*_jc_derive_type_interp(_jc_env *env,
+				_jc_class_loader *loader,
+				_jc_classbytes *cbytes);
+
+/* elf.c */
+extern _jc_elf		*_jc_elf_load(_jc_env *env, _jc_class_loader *loader,
+				const char *path);
+extern jint		_jc_elf_link(_jc_env *env, _jc_elf *elf,
+				_jc_elf_resolver *resolver, void *arg);
+extern void		_jc_elf_link_cleanup(_jc_elf *elf);
+extern void		_jc_elf_addref(_jc_elf *elf);
+extern void		_jc_elf_unref(_jc_elf **elfp);
+
+/* elf_reloc.c */
+extern jint		_jc_elf_arch_reloc(_jc_env *env, const char *path,
+				char *target_base, Elf_Addr target_offset,
+				Elf_Word type, Elf_Addr value, Elf_Off addend);
+
+/* exception.c */
+extern void		_jc_post_exception_object(_jc_env *env,
+				_jc_object *obj);
+extern void		_jc_post_exception(_jc_env *env, int num);
+extern void		_jc_post_exception_msg(_jc_env *env, int num,
+				const char *fmt, ...)
+#ifdef __FreeBSD__
+				__printf0like(3, 4)
+#endif
+;
+extern void		_jc_post_exception_info(_jc_env *env);
+extern void		_jc_post_exception_params(_jc_env *env, int num,
+				_jc_word *params);
+extern void		_jc_throw_exception(_jc_env *env)
+				__attribute__ ((noreturn));
+extern _jc_object	*_jc_retrieve_exception(_jc_env *env, _jc_type *type);
+extern _jc_object	*_jc_retrieve_cross_exception(_jc_env *env);
+extern jboolean		_jc_unpost_exception(_jc_env *env, int num);
+extern void		_jc_fprint_exception_headline(_jc_env *env,
+				FILE *fp, _jc_object *e);
+
+/* gc_final.c */
+extern jint		_jc_gc_finalize(_jc_env *env);
+
+/* gc_root.c */
+extern int		_jc_root_walk(_jc_env *env, _jc_object ***refsp);
+
+/* gc_scan.c */
+extern jint		_jc_gc(_jc_env *env, jboolean urgent);
+
+/* heap.c */
+extern jint		_jc_heap_init(_jc_env *env, _jc_jvm *vm);
+extern void		_jc_heap_destroy(_jc_jvm *vm);
+extern void		*_jc_heap_alloc_small_block(_jc_env *env, int bsi);
+extern void		*_jc_heap_alloc_pages(_jc_env *env, int npages);
+extern int		_jc_heap_block_size(_jc_jvm *vm, size_t size);
+#ifndef NDEBUG
+extern void		_jc_heap_check(_jc_jvm *vm);
+#endif
+
+/* init.c */
+extern jint		_jc_init(void);
+
+/* initialize.c */
+extern jint		_jc_initialize_type(_jc_env *env, _jc_type *type);
+
+/* instance.c */
+extern int		_jc_instance_of(_jc_env *env, _jc_object *obj,
+				_jc_type *type);
+extern int		_jc_assignable_from(_jc_env *env, _jc_type *from,
+				_jc_type *to);
+extern jboolean		_jc_subclass_of(_jc_object *obj, _jc_type *type);
+
+/* interp.c */
+extern jboolean		_jc_interp_z(_jc_env *env, ...);
+extern jbyte		_jc_interp_b(_jc_env *env, ...);
+extern jchar		_jc_interp_c(_jc_env *env, ...);
+extern jshort		_jc_interp_s(_jc_env *env, ...);
+extern jint		_jc_interp_i(_jc_env *env, ...);
+extern jlong		_jc_interp_j(_jc_env *env, ...);
+extern jfloat		_jc_interp_f(_jc_env *env, ...);
+extern jdouble		_jc_interp_d(_jc_env *env, ...);
+extern _jc_object	*_jc_interp_l(_jc_env *env, ...);
+extern void		_jc_interp_v(_jc_env *env, ...);
+extern jboolean		_jc_interp_native_z(_jc_env *env, ...);
+extern jbyte		_jc_interp_native_b(_jc_env *env, ...);
+extern jchar		_jc_interp_native_c(_jc_env *env, ...);
+extern jshort		_jc_interp_native_s(_jc_env *env, ...);
+extern jint		_jc_interp_native_i(_jc_env *env, ...);
+extern jlong		_jc_interp_native_j(_jc_env *env, ...);
+extern jfloat		_jc_interp_native_f(_jc_env *env, ...);
+extern jdouble		_jc_interp_native_d(_jc_env *env, ...);
+extern _jc_object	*_jc_interp_native_l(_jc_env *env, ...);
+extern void		_jc_interp_native_v(_jc_env *env, ...);
+extern int		_jc_interp_pc_to_jline(_jc_method *method, int index);
+
+/* invoke.c */
+extern jint		_jc_invoke_nonvirtual(_jc_env *env,
+				_jc_method *method, _jc_object *this, ...);
+extern jint		_jc_invoke_unwrap_nonvirtual(_jc_env *env,
+				_jc_method *method, jobject this, ...);
+extern jint		_jc_invoke_nonvirtual_v(_jc_env *env,
+				_jc_method *method, _jc_object *this,
+				va_list args);
+extern jint		_jc_invoke_unwrap_nonvirtual_v(_jc_env *env,
+				_jc_method *method, jobject this, va_list args);
+extern jint		_jc_invoke_nonvirtual_a(_jc_env *env,
+				_jc_method *method, _jc_object *this,
+				_jc_word *params);
+extern jint		_jc_invoke_unwrap_nonvirtual_a(_jc_env *env,
+				_jc_method *method, jobject this,
+				jvalue *params);
+extern jint		_jc_invoke_virtual(_jc_env *env,
+				_jc_method *method, _jc_object *this, ...);
+extern jint		_jc_invoke_unwrap_virtual(_jc_env *env,
+				_jc_method *method, jobject this, ...);
+extern jint		_jc_invoke_virtual_v(_jc_env *env,
+				_jc_method *method, _jc_object *this,
+				va_list args);
+extern jint		_jc_invoke_unwrap_virtual_v(_jc_env *env,
+				_jc_method *method, jobject this, va_list args);
+extern jint		_jc_invoke_virtual_a(_jc_env *env, _jc_method *method,
+				_jc_object *this, _jc_word *params);
+extern jint		_jc_invoke_unwrap_virtual_a(_jc_env *env,
+				_jc_method *method, jobject this,
+				jvalue *params);
+extern jint		_jc_invoke_static(_jc_env *env,
+				_jc_method *method, ...);
+extern jint		_jc_invoke_unwrap_static(_jc_env *env,
+				_jc_method *method, ...);
+extern jint		_jc_invoke_static_v(_jc_env *env,
+				_jc_method *method, va_list args);
+extern jint		_jc_invoke_unwrap_static_v(_jc_env *env,
+				_jc_method *method, va_list args);
+extern jint		_jc_invoke_static_a(_jc_env *env, _jc_method *method,
+				_jc_word *params);
+extern jint		_jc_invoke_unwrap_static_a(_jc_env *env,
+				_jc_method *method, jvalue *params);
+extern jint		_jc_invoke_v(_jc_env *env, _jc_method *method,
+				const void *func, _jc_object *obj, va_list args,
+				jboolean jni);
+extern jint		_jc_invoke_jcni_a(_jc_env *env, _jc_method *method,
+				const void *func, _jc_object *obj,
+				_jc_word *params);
+extern jint		_jc_invoke_jni_a(_jc_env *env, _jc_method *method,
+				const void *func, _jc_object *obj,
+				_jc_word *params);
+
+extern const _jc_method	_jc_invoke_jcni_a$method_info;
+
+/* jni_invoke.c */
+extern const		struct JNIInvokeInterface _jc_invoke_interface;
+
+/* jni_native.c */
+extern const		struct JNINativeInterface _jc_native_interface;
+
+/* load.c */
+extern _jc_type		*_jc_load_type(_jc_env *env, _jc_class_loader *loader,
+				const char *name);
+extern _jc_type		*_jc_load_type2(_jc_env *env, _jc_class_loader *loader,
+				const char *name, size_t len);
+extern _jc_type		*_jc_load_primitive_type(_jc_env *env, int ptype);
+extern jint		_jc_create_class_instance(_jc_env *env, _jc_type *type);
+
+/* lock.c */
+extern jint		_jc_lock_object(_jc_env *env, _jc_object *obj);
+extern jint		_jc_unlock_object(_jc_env *env, _jc_object *obj);
+extern jint		_jc_inflate_lock(_jc_env *env, _jc_object *object);
+extern void		_jc_lock_contention(_jc_env *env, _jc_object *obj);
+extern void		_jc_free_lock(_jc_jvm *vm, _jc_object *obj);
+extern void		_jc_destroy_lock(_jc_fat_lock **lockp);
+extern jboolean		_jc_lock_held(_jc_env *env, _jc_object *obj);
+
+/* misc.c */
+extern void		_jc_fatal_error(_jc_jvm *vm, const char *fmt, ...)
+				__attribute__ ((noreturn));
+extern void		_jc_fatal_error_v(_jc_jvm *vm, const char *fmt,
+				va_list args) __attribute__ ((noreturn));
+extern jint		_jc_create_subdir(_jc_env *env, const char *root,
+				const char *pathname);
+extern size_t		_jc_name_encode(const char *name, char *buf,
+				jboolean pass_slash);
+extern char		*_jc_name_decode(const char *name, char *buf);
+extern const char	*_jc_parse_class_ref(const char *s, _jc_class_ref *rc,
+				int cc, u_char *ptype);
+extern char		**_jc_parse_searchpath(_jc_env *env, const char *path);
+extern jint		_jc_parse_classpath(_jc_env *env, const char *path,
+				_jc_cpath_entry **listp, int *lenp);
+extern int		_jc_parse_objpath(_jc_env *env, const char *path,
+				_jc_objpath_entry **listp);
+extern void		_jc_jni_encode(char **bufp, const char *s);
+extern size_t		_jc_jni_encode_length(const char *s);
+extern int		_jc_field_compare(const void *v1, const void *v2);
+extern int		_jc_method_compare(const void *v1, const void *v2);
+
+extern _jc_splay_cmp_t	_jc_method_node_compare;
+extern _jc_splay_cmp_t	_jc_classfile_tree_compare;
+extern _jc_splay_cmp_t	_jc_method_tree_compare;
+extern _jc_splay_cmp_t	_jc_class_ref_compare;
+extern _jc_splay_cmp_t	_jc_type_cmp;
+extern _jc_splay_cmp_t	_jc_node_cmp;
+
+/* mutex.c */
+extern jint		_jc_mutex_init(_jc_env *env, pthread_mutex_t *mutex);
+extern void		_jc_mutex_destroy(pthread_mutex_t *mutex);
+extern jint		_jc_cond_init(_jc_env *env, pthread_cond_t *cond);
+extern void		_jc_cond_destroy(pthread_cond_t *cond);
+
+/* native_lib.c */
+extern jint		_jc_invoke_native_method(_jc_env *env,
+				_jc_method *method, int values, ...);
+extern jint		_jc_load_native_library(_jc_env *env,
+				_jc_class_loader *loader, const char *name);
+extern void		_jc_unload_native_libraries(_jc_jvm *vm,
+				_jc_class_loader *loader);
+extern jint		_jc_resolve_native_method(_jc_env *env,
+				_jc_method *method);
+
+/* native_ref.c */
+extern jobject		_jc_new_local_native_ref(_jc_env *env, _jc_object *obj);
+extern _jc_object	*_jc_free_local_native_ref(jobject *obj);
+extern void		_jc_free_all_native_local_refs(_jc_env *env);
+extern jobject		_jc_new_global_native_ref(_jc_env *env,
+				_jc_object *obj);
+extern _jc_object	*_jc_free_global_native_ref(jobject *obj);
+extern void		_jc_free_all_native_global_refs(_jc_jvm *vm);
+extern jint		_jc_push_local_native_frame(_jc_env *env, int num_refs);
+extern void		_jc_push_stack_local_native_frame(_jc_env *env,
+			    _jc_native_frame *frame);
+extern jint		_jc_extend_local_native_frame(_jc_env *env,
+				int num_refs);
+extern jobject		_jc_pop_local_native_frame(_jc_env *env,
+				_jc_object *obj);
+extern _jc_native_frame	*_jc_add_native_frame(_jc_env *env,
+				_jc_native_frame_list *list);
+
+/* new.c */
+extern _jc_object	*_jc_new_object(_jc_env *env, _jc_type *type);
+extern _jc_object	*_jc_init_object(_jc_env *env, void *mem,
+				_jc_type *type);
+extern _jc_object	*_jc_initialize_class_object(_jc_env *env, void *mem);
+extern _jc_array	*_jc_new_array(_jc_env *env, _jc_type *type, jint len);
+extern _jc_array *	_jc_init_array(_jc_env *env, void *mem,
+				_jc_type *type, jint len);
+extern _jc_array	*_jc_new_multiarray(_jc_env *env, _jc_type *type,
+				jint num_sizes, const jint *sizes);
+extern _jc_array	*_jc_init_multiarray(_jc_env *env, void *mem,
+				_jc_type *type, jint num_sizes,
+				const jint *sizes);
+
+/* os_functions.c */
+extern int		_jc_num_cpus(_jc_env *env);
+
+/* prepare.c */
+extern jint		_jc_prepare_type(_jc_env *env, _jc_type *type);
+
+/* printf.c */
+extern int		_jc_printf(_jc_jvm *vm, const char *fmt, ...);
+extern int		_jc_eprintf(_jc_jvm *vm, const char *fmt, ...);
+extern int		_jc_fprintf(_jc_jvm *vm, FILE *fp,
+				const char *fmt, ...);
+extern void		_jc_fprint_noslash(_jc_jvm *vm, FILE *fp,
+				const char *s);
+
+/* properties.c */
+extern jint		_jc_set_system_properties(_jc_env *env);
+extern jint		_jc_set_property(_jc_env *env, const char *name,
+				const char *value);
+extern jint		_jc_set_property2(_jc_env *env, const char *name,
+				size_t name_len, const char *value);
+extern jint		_jc_digest_properties(_jc_env *vm);
+extern void		_jc_destroy_properties(_jc_jvm *vm);
+
+/* reflect.c */
+extern _jc_field	*_jc_get_declared_field(_jc_env *env, _jc_type *type,
+				const char *name, const char *sig,
+				int is_static);
+extern _jc_method	*_jc_get_declared_method(_jc_env *env, _jc_type *type,
+				const char *name, const char *sig,
+				int flags_mask, int flags);
+extern _jc_object	*_jc_get_reflected_field(_jc_env *env,
+				_jc_field *field);
+extern _jc_object	*_jc_get_reflected_method(_jc_env *env,
+				_jc_method *method);
+extern _jc_object	*_jc_get_reflected_constructor(_jc_env *env,
+				_jc_method *method);
+extern _jc_field	*_jc_get_field(_jc_env *env, _jc_object *obj);
+extern _jc_method	*_jc_get_method(_jc_env *env, _jc_object *obj);
+extern _jc_method	*_jc_get_constructor(_jc_env *env, _jc_object *obj);
+extern jint		_jc_reflect_invoke(_jc_env *env, _jc_method *method,
+				_jc_object *this, _jc_object_array *pobjs);
+extern _jc_object_array	*_jc_get_parameter_types(_jc_env *env,
+				_jc_method *method);
+extern _jc_object_array	*_jc_get_exception_types(_jc_env *env,
+				_jc_method *method);
+extern _jc_object	*_jc_wrap_primitive(_jc_env *env, int type,
+				_jc_value *value);
+extern int		_jc_unwrap_primitive(_jc_env *env, _jc_object *obj,
+				_jc_value *value);
+extern jint		_jc_convert_primitive(_jc_env *env, int dtype,
+				int stype, _jc_value *value);
+extern int		_jc_reflect_accessible(_jc_env *env,
+				_jc_type *member_class, uint16_t access,
+				_jc_type **calling_classp);
+
+/* resolve.c */
+extern jint		_jc_resolve_type(_jc_env *env, _jc_type *type);
+extern _jc_method	*_jc_resolve_method(_jc_env *env, _jc_type *type,
+				const char *name, const char *sig);
+extern _jc_field	*_jc_resolve_field(_jc_env *env, _jc_type *type,
+				const char *name, const char *sig,
+				int is_static);
+extern jint		_jc_resolve_add_ref(_jc_env *env,
+				_jc_resolve_info *info, _jc_object *ref);
+extern jint		_jc_resolve_add_loader_ref(_jc_env *env,
+				_jc_resolve_info *info,
+				_jc_class_loader *loader);
+
+/* resolve2.c */
+extern jint		_jc_resolve_interp(_jc_env *env, _jc_type *type,
+				_jc_resolve_info *info);
+extern int		_jc_resolve_signature(_jc_env *env, _jc_method *method,
+				_jc_resolve_info *info);
+extern _jc_method	*_jc_method_lookup(_jc_type *type, _jc_method *key);
+
+/* signals.c */
+extern jint		_jc_init_signals(void);
+extern void		_jc_restore_signals(void);
+
+/* splay.c */
+extern void		_jc_splay_init(_jc_splay_tree *tree,
+				_jc_splay_cmp_t *compare, size_t offset);
+extern void		*_jc_splay_find(_jc_splay_tree *tree, void *item);
+extern void		_jc_splay_insert(_jc_splay_tree *tree, void *item);
+extern void		_jc_splay_remove(_jc_splay_tree *tree, void *item);
+extern void		_jc_splay_list(_jc_splay_tree *tree, void **items);
+
+/* stack.c */
+extern void		_jc_print_stack_trace(_jc_env *env, FILE *fp);
+extern int		_jc_save_stack_frames(_jc_env *env, _jc_env *thread,
+				int max, _jc_saved_frame *frames);
+extern void		_jc_print_stack_frames(_jc_env *env, FILE *fp,
+				int num_frames, _jc_saved_frame *frames);
+extern void		_jc_stack_crawl_first(_jc_env *env,
+				_jc_stack_crawl *crawl);
+extern void		_jc_stack_crawl_next(_jc_jvm *vm,
+				_jc_stack_crawl *crawl);
+extern void		_jc_stack_crawl_skip(_jc_jvm *vm,
+				_jc_stack_crawl *crawl);
+extern jboolean		_jc_stack_clip(_jc_env *env);
+extern jboolean		_jc_stack_clip_ctx(_jc_env *env, const mcontext_t *ctx);
+extern void		_jc_stack_unclip(_jc_env *env);
+
+/* string.c */
+extern _jc_object	*_jc_new_string(_jc_env *env,
+				const void *utf, size_t len);
+extern _jc_object	*_jc_new_intern_string(_jc_env *env,
+				const void *utf, size_t len);
+extern jint		_jc_decode_string_chars(_jc_env *env,
+				_jc_object *string, jchar *chars);
+extern size_t		_jc_decode_string_utf8(_jc_env *env,
+				_jc_object *string, char *buf);
+
+/* thread.c */
+extern jint		_jc_thread_init(void);
+extern _jc_env		*_jc_allocate_thread(_jc_env *env);
+extern void		_jc_free_thread(_jc_env **envp, int cachable);
+extern void		_jc_free_thread_stacks(_jc_jvm *vm);
+extern void		_jc_destroy_thread(_jc_env **envp);
+extern _jc_env		*_jc_attach_thread(_jc_jvm *vm, _jc_ex_info *ex);
+extern void		_jc_detach_thread(_jc_env **envp);
+extern jint		_jc_thread_create_instance(_jc_env *env,
+				_jc_object *group, const char *name,
+				jint priority, jboolean daemon);
+extern _jc_env		*_jc_get_current_env(void);
+extern void		_jc_stop_the_world(_jc_env *env);
+extern void		_jc_resume_the_world(_jc_env *env);
+extern void		_jc_halt_if_requested(_jc_env *env);
+extern void		_jc_stopping_java(_jc_env *env, const char *fmt, ...);
+extern void		_jc_resuming_java(_jc_env *env);
+extern void		*_jc_thread_start(void *arg);
+extern jobject		_jc_internal_thread(_jc_env *env, const char *class);
+extern jint		_jc_thread_check(_jc_env *env);
+extern void		_jc_thread_interrupt(_jc_jvm *vm, _jc_env *thread);
+extern void		_jc_thread_interrupt_instance(_jc_jvm *vm,
+				_jc_object *instance);
+extern void		_jc_thread_shutdown(_jc_env **envp);
+
+/* tables.c */
+extern const char	_jc_prim_chars[_JC_TYPE_MAX];
+extern const char	*const _jc_vmex_names[_JC_VMEXCEPTION_MAX];
+extern const char	*const _jc_prim_names[_JC_TYPE_MAX];
+extern const char	*const _jc_prim_wrapper_class[_JC_TYPE_MAX];
+extern const void	*const _jc_interp_funcs[_JC_TYPE_MAX];
+extern const void	*const _jc_interp_native_funcs[_JC_TYPE_MAX];
+extern const u_char	_jc_sig_types[0x100];
+extern const u_char	_jc_dword_type[_JC_TYPE_MAX];
+extern const size_t	_jc_type_sizes[_JC_TYPE_MAX];
+extern const size_t	_jc_type_align[_JC_TYPE_MAX];
+extern const int	_jc_field_type_sort[_JC_TYPE_MAX];
+extern const size_t	_jc_array_head_sizes[_JC_TYPE_MAX];
+extern const jlong	_jc_type_max_array_length[_JC_TYPE_MAX];
+extern const char	_jc_hex_chars[16];
+extern const char	*const _jc_verbose_names[_JC_VERBOSE_MAX];
+extern const char	*const _jc_bytecode_names[0x100];
+
+/* utf.c */
+extern jint		_jc_utf_decode(const u_char *utf,
+				jint ulen, jchar *buf);
+extern size_t		_jc_utf_encode(const jchar *chars,
+				jint clen, u_char *buf);
+
+/* verify.c */
+extern jint		_jc_verify_type(_jc_env *env, _jc_type *type);
+
+/* vm.c */
+extern jint		_jc_create_vm(void *args, _jc_jvm **vmp,
+				_jc_env **envp);
+extern void		_jc_free_vm(_jc_jvm **vmp);
+
+/* vm_alloc.c */
+extern void		*_jc_vm_alloc(_jc_env *env, size_t size);
+extern void		*_jc_vm_zalloc(_jc_env *env, size_t size);
+extern void		*_jc_vm_realloc(_jc_env *env, void *mem, size_t size);
+extern char		*_jc_vm_strdup(_jc_env *env, const char *s);
+extern char		*_jc_vm_strndup(_jc_env *env, const char *s,
+				size_t len);
+extern void		_jc_vm_free(void *pointerp);
+
+/* zip.c */
+extern _jc_zip		*_jc_zip_open(_jc_env *env, const char *path);
+extern void		_jc_zip_close(_jc_zip **zipp);
+extern int		_jc_zip_search(_jc_zip *zip, const char *name);
+extern jint		_jc_zip_read(_jc_env *env, _jc_zip *zip,
+				int indx, u_char *data);
+
+/* java_lang_VMClassLoader.c */
+extern _jc_object	*_jc_internal_load_class(_jc_env *env,
+				_jc_object *name_string, _jc_object *loader_obj,
+				jboolean resolve);
+
+#include "inline.h"
+
+#endif	/* _LIBJC_H_ */
+

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/load.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/load.c?rev=294974&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/load.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/load.c Tue Oct  4 19:19:16 2005
@@ -0,0 +1,461 @@
+
+/*
+ * Copyright 2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ * 
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ * $Id: load.c,v 1.7 2005/03/18 04:17:48 archiecobbs Exp $
+ */
+
+#include "libjc.h"
+
+/* Internal functions */
+static _jc_type	*_jc_usercl_load_type(_jc_env *env, _jc_class_loader *loader,
+			const char *name);
+static _jc_type	*_jc_bootcl_load_type(_jc_env *env, const char *name);
+
+/*
+ * Load a primitive type.
+ *
+ * This does not create the Class instance; for primitive types,
+ * that's done manually at a later point during bootstrapping.
+ *
+ * Also, we do not add primitive types to the bootstrap loader's
+ * initiated or derived type trees, because they are always "found"
+ * explicitly and never by searching in these trees by name (what
+ * name would you use anyway?).
+ *
+ * No need for locking, there is always only one thread because
+ * this happens early during VM bootstrapping.
+ *
+ * If unsuccessful an exception is stored.
+ */
+_jc_type *
+_jc_load_primitive_type(_jc_env *env, int ptype)
+{
+	_jc_jvm *const vm = env->vm;
+	_jc_type *type;
+
+	/* Sanity check */
+	_JC_ASSERT(ptype >= _JC_TYPE_BOOLEAN && ptype <= _JC_TYPE_VOID);
+	_JC_ASSERT(vm->initialization != NULL);
+
+	/* Verbosity */
+	VERBOSE(CLASS, vm, "loading primitive type `%s' (%c)",
+	    _jc_prim_names[ptype], _jc_prim_chars[ptype]);
+
+	/* Lock boot loader */
+	_JC_MUTEX_LOCK(env, vm->boot.loader->mutex);
+
+	/* Create new type descriptor */
+	if ((type = _jc_cl_zalloc(env, vm->boot.loader, sizeof(*type))) == NULL)
+		goto fail;
+	if ((type->name = _jc_cl_strdup(env,
+	    vm->boot.loader, _jc_prim_names[ptype])) == NULL) {
+		_jc_cl_unalloc(vm->boot.loader, &type, sizeof(*type));
+		goto fail;
+	}
+
+	type->loader = vm->boot.loader;
+	type->access_flags = _JC_ACC_PUBLIC | _JC_ACC_FINAL;
+	type->flags = ptype | _JC_TYPE_RESOLVED | _JC_TYPE_VERIFIED
+	    | _JC_TYPE_PREPARED | _JC_TYPE_INITIALIZED | _JC_TYPE_LOADED;
+	type->initial_lockword = (_JC_TYPE_INVALID << _JC_LW_TYPE_SHIFT)
+	    | _JC_LW_ODD_BIT;		/* the lockword should never be used */
+
+	/* Unlock boot loader */
+	_JC_MUTEX_UNLOCK(env, vm->boot.loader->mutex);
+
+	/* Done */
+	return type;
+
+fail:
+	/* Clean up after failure */
+	_JC_MUTEX_UNLOCK(env, vm->boot.loader->mutex);
+	return NULL;
+}
+
+/*
+ * Same as _jc_load_type() but with a non-NUL-terminated name.
+ */
+_jc_type *
+_jc_load_type2(_jc_env *env, _jc_class_loader *loader,
+	const char *name, size_t len)
+{
+	char *buf;
+
+	if ((buf = _JC_STACK_ALLOC(env, len + 1)) == NULL) {
+		_jc_post_exception_info(env);
+		return NULL;
+	}
+	memcpy(buf, name, len);
+	buf[len] = '\0';
+	return _jc_load_type(env, loader, buf);
+}
+
+/*
+ * Load an array or non-array reference type using the supplied class loader.
+ *
+ * This expects non-array class names to be encoded "like/This",
+ * not "Llike/This;".
+ */
+_jc_type *
+_jc_load_type(_jc_env *env, _jc_class_loader *loader, const char *name)
+{
+	_jc_jvm *const vm = env->vm;
+
+	/* Sanity check */
+	if (strchr(name, '.') != NULL) {
+		_jc_post_exception_msg(env, _JC_NoClassDefFoundError,
+		    "class name `%s' is not in internal format", name);
+		return NULL;
+	}
+
+	/* Do bootstrap or user-defined class loading as appropriate */
+	return (loader == vm->boot.loader) ?
+	    _jc_bootcl_load_type(env, name) :
+	    _jc_usercl_load_type(env, loader, name);
+}
+
+/*
+ * Load a type using the bootstrap class loader.
+ *
+ * If successful, the class is added to the boot loader's initiating
+ * and derived types trees.
+ *
+ * We use the vm->boot.loading_types tree to handle the race between
+ * two threads trying to load the same class simultaneously (we don't
+ * need to explicitly handle that case for user-defined class loaders,
+ * as the ClassLoader instance itself is expected to do that).
+ */
+static _jc_type *
+_jc_bootcl_load_type(_jc_env *env, const char *name)
+{
+	_jc_jvm *const vm = env->vm;
+	_jc_class_loader *const loader = vm->boot.loader;
+	jboolean loader_locked = JNI_FALSE;
+	_jc_type_node loading_node;
+	_jc_type_node node_key;
+	_jc_type_node *node;
+	_jc_type *type_key;
+	_jc_type *type = NULL;
+
+	/* Sanity check */
+	_JC_ASSERT(strchr(name, '.') == NULL);
+
+	/*
+	 * Set up a fake type to use as the key for searching trees.
+	 * This works because 'name' is the only field in _jc_type
+	 * that is accessed when searching the splay trees.
+	 */
+	type_key = (_jc_type *)((char *)&name - _JC_OFFSETOF(_jc_type, name));
+	node_key.type = type_key;
+
+	/* Lock loader */
+	_JC_MUTEX_LOCK(env, loader->mutex);
+	loader_locked = JNI_TRUE;
+
+retry:
+	/* Check if loader already been recorded as an initiating loader */
+	if ((node = _jc_splay_find(&loader->initiated_types,
+	    &node_key)) != NULL) {
+		_JC_MUTEX_UNLOCK(env, loader->mutex);
+		_JC_ASSERT(node->type != NULL);
+		return node->type;
+	}
+
+	/*
+	 * If another thread is loading this type, wait for it and try again.
+	 * The only way this thread could already be loading this type is if
+	 * we're about to throw a ClassCircularityError.
+	 */
+	if ((node = _jc_splay_find(&vm->boot.loading_types,
+	    &node_key)) != NULL) {
+		if (node->thread == env)
+			goto not_loading;
+		_jc_loader_wait(env, loader);
+		goto retry;
+	}
+
+	/* Add type to the loading types tree */
+	node = &loading_node;
+	memset(node, 0, sizeof(*node));
+	node->type = type_key;
+	node->thread = env;
+	_jc_splay_insert(&vm->boot.loading_types, node);
+
+not_loading:
+	/* Unlock loader */
+	_JC_MUTEX_UNLOCK(env, loader->mutex);
+	loader_locked = JNI_FALSE;
+
+	/* Verbosity */
+	VERBOSE(CLASS, vm, "loading `%s' (via bootstrap loader)", name);
+
+	/* Handle array types specially */
+	if (*name == '[') {
+		type = _jc_derive_array_type(env, loader, name);
+		goto done;
+	}
+
+	/* Handle two cases: with or without class files */
+	if (vm->without_classfiles) {
+
+		/* Try to load object file for this class */
+		if (_jc_load_object(env, loader, name) != JNI_OK) {
+
+			/* Convert LinkageError -> NoClassDefFoundError */
+			if (env->ex.num == _JC_LinkageError)
+				env->ex.num = _JC_NoClassDefFoundError;
+
+			/* Post exception */
+			_jc_post_exception_info(env);
+			goto done;
+		}
+
+		/* Lock loader */
+		_JC_MUTEX_LOCK(env, loader->mutex);
+		loader_locked = JNI_TRUE;
+
+		/* Look for loaded type */
+		type = _jc_splay_find(&loader->defined_types, type_key);
+	} else {
+		_jc_classbytes *cbytes;
+		int index;
+
+		/* Find the class file bytes in the filesystem */
+		if ((cbytes = _jc_bootcl_find_classbytes(env,
+		    name, &index)) == NULL) {
+			_jc_post_exception_info(env);
+			goto done;
+		}
+
+		/* Attempt to derive the type from the class file bytes */
+		type = _jc_derive_type_from_classfile(env,
+		    loader, name, cbytes);
+
+		/* Verbosity */
+		if (type != NULL) {
+			VERBOSE(CLASS, vm, "found `%s' classfile in `%s'",
+			    type->name, vm->boot.class_path[index].pathname);
+		}
+
+		/* Free class file bytes */
+		_jc_free_classbytes(&cbytes);
+	}
+
+done:
+	/* Lock loader */
+	if (!loader_locked)
+		_JC_MUTEX_LOCK(env, loader->mutex);
+
+	/* Remove type from loading types tree and notify waiting threads */
+	if (node == &loading_node) {
+		_jc_splay_remove(&vm->boot.loading_types, node);
+		if (loader->waiters) {
+			loader->waiters = JNI_FALSE;
+			_JC_COND_BROADCAST(loader->cond);
+		}
+	}
+
+	/* Unlock loader */
+	_JC_MUTEX_UNLOCK(env, loader->mutex);
+
+	/* Done */
+	return type;
+}
+
+/*
+ * Load a type using a user-defined class loader.
+ *
+ * If successful, the class is added to the supplied loader's initiating
+ * types tree and the defining loader's defined types tree.
+ */
+static _jc_type *
+_jc_usercl_load_type(_jc_env *env, _jc_class_loader *loader, const char *name)
+{
+	_jc_jvm *const vm = env->vm;
+	_jc_type *type = NULL;
+	_jc_type_node *node;
+	_jc_type_node *temp;
+	jobject sref = NULL;
+
+	/* Lock loader */
+	_JC_MUTEX_LOCK(env, loader->mutex);
+
+	/* Create a type node for this class; set up fake type for search */
+	if ((node = _jc_cl_zalloc(env, loader, sizeof(*node))) == NULL) {
+		_JC_MUTEX_UNLOCK(env, loader->mutex);
+		_jc_post_exception_info(env);
+		return NULL;
+	}
+	node->type = (_jc_type *)((char *)&name - _JC_OFFSETOF(_jc_type, name));
+
+	/* Check if loader already been recorded as an initiating loader */
+	if ((temp = _jc_splay_find(&loader->initiated_types, node)) != NULL) {
+		_jc_cl_unalloc(loader, &node, sizeof(*node));
+		_JC_MUTEX_UNLOCK(env, loader->mutex);
+		_JC_ASSERT(temp->type != NULL);
+		return temp->type;
+	}
+
+	/* Unlock loader */
+	_JC_MUTEX_UNLOCK(env, loader->mutex);
+
+	/* Verbosity */
+	VERBOSE(CLASS, vm, "loading `%s' (via %s@%p)", name,
+	    loader->instance->type->name, loader->instance);
+
+	/* We handle array types but use ClassLoader for non-array types */
+	if (*name == '[') {
+		if ((type = _jc_derive_array_type(env, loader, name)) == NULL)
+			goto fail;
+	} else {
+		const size_t name_len = strlen(name);
+		char *ncopy;
+		int i;
+
+		/* Convert "/" in name back to "." */
+		if ((ncopy = _JC_STACK_ALLOC(env, name_len)) == NULL) {
+			_jc_post_exception_info(env);
+			goto fail;
+		}
+		memcpy(ncopy, name, name_len);
+		for (i = 0; i < name_len; i++) {
+			if (ncopy[i] == '/')
+				ncopy[i] = '.';
+		}
+
+		/* Create String object from name */
+		if ((sref = _jc_new_local_native_ref(env,
+		    _jc_new_string(env, ncopy, name_len))) == NULL)
+			goto fail;
+
+		/* Invoke loadClass() */
+		if (_jc_invoke_virtual(env,
+		    vm->boot.methods.ClassLoader.loadClass,
+		    loader->instance, *sref) != JNI_OK)
+			goto fail;
+
+		/* Check for null return value */
+		if (env->retval.l == NULL) {
+			_jc_post_exception(env, _JC_NullPointerException);
+			goto fail;
+		}
+
+		/* Extract pointer from Class.vmdata field */
+		type = _jc_get_vm_pointer(env->retval.l,
+		    vm->boot.fields.Class.vmdata);
+
+		/* Sanity check */
+		_JC_ASSERT((type->flags & (_JC_TYPE_ARRAY|_JC_TYPE_MASK))
+		    == _JC_TYPE_REFERENCE);
+
+		/* Make sure that the correct class was returned */
+		if (strcmp(type->name, name) != 0) {
+			_jc_post_exception_msg(env, _JC_NoClassDefFoundError,
+			    "asked for `%s' from `%s' but got `%s' instead",
+			    name, loader->instance->type->name, type->name);
+			goto fail;
+		}
+	}
+
+	/* Lock loader */
+	_JC_MUTEX_LOCK(env, loader->mutex);
+
+	/* Add type to the loader's initiated types tree if not already there */
+	node->type = type;
+	if (_jc_splay_find(&loader->initiated_types, node) == NULL)
+		_jc_splay_insert(&loader->initiated_types, node);
+	else
+		_jc_cl_unalloc(loader, &node, sizeof(*node));
+
+	/* Unlock loader */
+	_JC_MUTEX_UNLOCK(env, loader->mutex);
+
+	/* Free local reference */
+	_jc_free_local_native_ref(&sref);
+
+	/* Verbosity */
+	VERBOSE(CLASS, vm, "loaded `%s' via %s@%p", type->name,
+	    loader->instance->type->name, loader->instance);
+
+	/* Done */
+	return type;
+
+fail:
+	/* Give back node */
+	_JC_MUTEX_LOCK(env, loader->mutex);
+	_jc_cl_unalloc(loader, &node, sizeof(*node));
+	_JC_MUTEX_UNLOCK(env, loader->mutex);
+
+	/* Free local reference */
+	_jc_free_local_native_ref(&sref);
+
+	/* Done */
+	return NULL;
+}
+
+/*
+ * Create the Class object associated with 'type'. Class objects
+ * are allocated from the class loader's memory rather than the heap.
+ *
+ * If unsuccessful an exception is stored.
+ *
+ * NOTE: We never invoke constructors for Class instances.
+ *
+ * NOTE: This assumes the type's class loader mutex is locked.
+ */
+jint
+_jc_create_class_instance(_jc_env *env, _jc_type *type)
+{
+	_jc_jvm *const vm = env->vm;
+	_jc_class_loader *const loader = type->loader;
+	_jc_type *const class_type = vm->boot.types.Class;
+	void *mem;
+
+	/* Sanity check */
+	_JC_ASSERT(type->instance == NULL);
+	_JC_ASSERT(vm->boot.types.Class != NULL);
+	_JC_ASSERT(vm->boot.fields.Class.vmdata != NULL);
+	_JC_ASSERT(vm->initialization == NULL
+	    || vm->initialization->create_class);
+	_JC_MUTEX_ASSERT(env, loader->mutex);
+
+	/* Allocate the class object from the loader's memory area */
+	if ((mem = _jc_cl_alloc(env, loader,
+	    class_type->u.nonarray.instance_size)) == NULL)
+		goto fail;
+
+	/* Initialize new Class object */
+	type->instance = _jc_initialize_class_object(env, mem);
+
+	/* Set 'vmdata' pointer in Class object */
+	_jc_set_vm_pointer(type->instance, vm->boot.fields.Class.vmdata, type);
+
+	/* Done */
+	return JNI_OK;
+
+fail:
+	/* Give back Class object memory */
+	if (mem != NULL) {
+		_jc_cl_unalloc(loader, &mem,
+		    class_type->u.nonarray.instance_size);
+		type->instance = NULL;
+	}
+
+	/* Done */
+	return JNI_ERR;
+}
+

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/lock.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/lock.c?rev=294974&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/lock.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/lock.c Tue Oct  4 19:19:16 2005
@@ -0,0 +1,555 @@
+
+/*
+ * Copyright 2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ * 
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ * $Id: lock.c,v 1.6 2005/03/19 19:40:45 archiecobbs Exp $
+ */
+
+#include "libjc.h"
+
+/*
+ * Enter an object monitor.
+ *
+ * It is assumed that 'object' has already been verified != null
+ * and that the caller ensures a reference to 'obj' will be
+ * maintained somehow (i.e., either as a parameter on the Java
+ * stack or via an explicit native reference).
+ */
+jint
+_jc_lock_object(_jc_env *env, _jc_object *obj)
+{
+	_jc_jvm *const vm = env->vm;
+	jboolean clipped_stack_top;
+	_jc_word old_lockword;
+	_jc_fat_lock *lock;
+	jint status;
+
+	/* Sanity check */
+	_JC_ASSERT(obj != NULL);
+
+retry:
+	/* Get current lockword */
+	old_lockword = obj->lockword;
+
+	/* Sanity check */
+	_JC_ASSERT(env->status == _JC_THRDSTAT_RUNNING_NORMAL
+	    || env->status == _JC_THRDSTAT_HALTING_NORMAL);
+
+	/* Try to acquire the thinlock by glomming in our thinlock ID */
+	if (_jc_compare_and_swap(&obj->lockword,
+	    old_lockword & _JC_LW_INFO_MASK,
+	    env->thinlock_id | (old_lockword & _JC_LW_INFO_MASK)))
+		return JNI_OK;			/* thinlock acquired! */
+
+	/*
+	 * Either the object is already locked, or the lock is inflated.
+	 * First, handle the case of an already-locked thinlock.
+	 */
+	if (!_JC_LW_TEST(old_lockword, FAT)) {
+		_jc_word old_flag;
+		jboolean notified;
+		_jc_env *owner;
+
+		/*
+		 * Check if we are already the owner of the thinlock.
+		 * If so, just increment the recursion count.
+		 */
+		if ((old_lockword & _JC_LW_THIN_TID_MASK) == env->thinlock_id) {
+			jint count = _JC_LW_EXTRACT(old_lockword, THIN_COUNT);
+
+			/* Sanity check */
+			_JC_ASSERT(count < _JC_MAX_THIN_RECURSION);
+
+			/*
+			 * Increment the recursion counter. If the counter
+			 * doesn't overflow, we're done. Otherwise, we have
+			 * to inflate the lock.
+			 */
+			if (++count < _JC_MAX_THIN_RECURSION) {
+				obj->lockword = env->thinlock_id
+				    | (count << _JC_LW_THIN_COUNT_SHIFT)
+				    | (old_lockword & _JC_LW_INFO_MASK);
+				return JNI_OK;
+			}
+
+			/*
+			 * Overflow: we have to inflate the thinlock.
+			 */
+			if ((status = _jc_inflate_lock(env, obj)) != JNI_OK)
+				return status;
+
+			/* OK, now retry locking with the new fat lock */
+			goto retry;
+		}
+
+		/*
+		 * Contention: the thinlock is owned by another thread.
+		 */
+		owner = vm->threads.by_id[
+		    _JC_LW_EXTRACT(old_lockword, THIN_TID)];
+		_JC_ASSERT(owner->jni_interface == &_jc_native_interface);
+
+		/* Acquire mutex associated with the owner's waiter queue */
+		_JC_MUTEX_LOCK(env, owner->lock.owner.mutex);
+
+		/* Update the contention flag */
+		old_flag = owner->lock.owner.contention;
+		owner->lock.owner.contention = 1;
+
+		/*
+		 * If the thinlock owner has not changed, then this thread
+		 * has just notified it that there is contention. Add this
+		 * thread to the waiting list. Otherwise, the owner has
+		 * changed, so restore the contention flag to its original
+		 * value.
+		 */
+		if ((obj->lockword & _JC_LW_THIN_TID_MASK)
+		    == owner->thinlock_id) {
+			SLIST_INSERT_HEAD(&owner->lock.owner.waiters,
+			    env, lock.waiter.link);
+			env->lock.waiter.object = obj;
+			notified = JNI_TRUE;
+		} else {
+			owner->lock.owner.contention = old_flag;
+			notified = JNI_FALSE;
+		}
+
+		/* Release mutex associated with the owner's waiter queue */
+		_JC_MUTEX_UNLOCK(env, owner->lock.owner.mutex);
+
+		/* If the owner changed, retry from scratch */
+		if (!notified)
+			goto retry;
+
+		/* Clip the current top of the Java stack if not already */
+		clipped_stack_top = _jc_stack_clip(env);
+
+		/* Enter non-Java mode */
+		_jc_stopping_java(env, "waiting for thinlock on %s@%p",
+		    obj->type->name, obj);
+
+		/* Transition made; reacquire lock */
+		_JC_MUTEX_LOCK(env, owner->lock.owner.mutex);
+
+		/* Sleep as long as we're on the waiting list */
+		while (JNI_TRUE) {
+			_jc_env *thread;
+
+			/* Look for this thread in the waiters list */
+			SLIST_FOREACH(thread, &owner->lock.owner.waiters,
+			    lock.waiter.link) {
+				if (thread == env)
+					break;
+			}
+			if (thread == NULL)
+				break;		/* no longer in list! */
+
+			/* Sanity check */
+			_JC_ASSERT(owner->lock.owner.contention == 1);
+
+			/* Sleep some more */
+			_JC_COND_WAIT(env, env->lock.waiter.cond,
+			    owner->lock.owner.mutex);
+		}
+
+		/* Release mutex associated with the fat lock */
+		_JC_MUTEX_UNLOCK(env, owner->lock.owner.mutex);
+
+		/* Back to running normal Java */
+		_jc_resuming_java(env);
+
+		/* Unclip stack */
+		if (clipped_stack_top)
+			_jc_stack_unclip(env);
+
+		/* Retry locking */
+		env->lock.waiter.object = NULL;
+		goto retry;
+	}
+
+	/* The object lock is already inflated, i.e., it's a fat lock */
+	lock = vm->fat_locks.by_id[_JC_LW_EXTRACT(old_lockword, FAT_ID)];
+	_JC_ASSERT(lock != NULL);
+
+	/* Clip the current top of the Java stack if not already */
+	clipped_stack_top = _jc_stack_clip(env);
+
+	/* Enter non-Java mode */
+	_jc_stopping_java(env, "waiting for fatlock on %s@%p",
+	    obj->type->name, obj);
+
+	/* Acquire mutex associated with the fat lock */
+	_JC_MUTEX_LOCK(env, lock->mutex);
+
+	/* Wait until no other thread owns the lock */
+	while (lock->recursion_count != 0 && lock->u.owner != env)
+		_JC_COND_WAIT(env, lock->cond, lock->mutex);
+
+	/* Bump recursion count */
+	status = JNI_OK;
+	if (lock->recursion_count == 0) {
+		lock->recursion_count = 1;
+		lock->u.owner = env;
+	} else {
+      
+		/* Sanity check */
+		_JC_ASSERT(lock->u.owner == env);
+
+		/* Bump counter but check for overflow */
+		if (++lock->recursion_count < 0) {
+		      lock->recursion_count--;
+		      status = JNI_ERR;
+		}
+	}
+
+	/* Release the mutex associated with the fat lock */
+	_JC_MUTEX_UNLOCK(env, lock->mutex);
+
+	/* Return to normal java */
+	_jc_resuming_java(env);
+
+	/* Unclip stack */
+	if (clipped_stack_top)
+		_jc_stack_unclip(env);
+
+	/* Throw an error if recursion count overflowed */
+	if (status != JNI_OK) {
+		_jc_post_exception_msg(env, _JC_InternalError,
+		    "max locking recursion (%u) exceeded",
+		    lock->recursion_count);
+	}
+
+	/* Done */
+	return status;
+}
+
+/*
+ * Exit an object monitor.
+ *
+ * NOTE: It is assumed that 'object' has already been verified != null.
+ */
+jint
+_jc_unlock_object(_jc_env *env, _jc_object *obj)
+{
+	_jc_jvm *const vm = env->vm;
+	_jc_word old_lockword;
+	_jc_fat_lock *lock;
+	int released;
+
+	/* Sanity check */
+	_JC_ASSERT(obj != NULL);
+
+	/* Save current lockword */
+	old_lockword = obj->lockword;
+
+	/*
+	 * Handle the case where the lock is a thinlock
+	 */
+	if (!_JC_LW_TEST(old_lockword, FAT)) {
+
+		/* Confirm that this thread actually owns the lock */
+		if ((old_lockword & _JC_LW_THIN_TID_MASK) != env->thinlock_id) {
+			_jc_post_exception(env,
+			    _JC_IllegalMonitorStateException);
+			return JNI_ERR;
+		}
+
+		/* If recursion count is zero, we're releasing the lock */
+		if (_JC_LW_EXTRACT(old_lockword, THIN_COUNT) == 0) {
+			obj->lockword = old_lockword & _JC_LW_INFO_MASK;
+			goto handle_contention;
+		}
+
+		/* Just decrement the recursion count */
+		obj->lockword = old_lockword - (1 << _JC_LW_THIN_COUNT_SHIFT);
+
+		/* Done */
+		return JNI_OK;
+	}
+
+	/*
+	 * Handle the case where the lock is a fat lock.
+	 */
+	lock = vm->fat_locks.by_id[_JC_LW_EXTRACT(old_lockword, FAT_ID)];
+	_JC_ASSERT(lock != NULL);
+
+	/* Acquire mutex associated with the fat lock */
+	_JC_MUTEX_LOCK(env, lock->mutex);
+
+	/* Sanity check */
+	_JC_ASSERT(lock->recursion_count >= 0);
+
+	/* Confirm that this thread actually owns the lock */
+	if (lock->recursion_count == 0 || lock->u.owner != env)
+		released = -1;
+	else if ((released = (--lock->recursion_count == 0))) {
+		_JC_COND_BROADCAST(lock->cond);
+		lock->u.owner = NULL;
+	}
+
+	/* Release the mutex associated with the fat lock */
+	_JC_MUTEX_UNLOCK(env, lock->mutex);
+
+	/* Check for non-acquired lock */
+	if (released == -1) {
+		_jc_post_exception(env, _JC_IllegalMonitorStateException);
+		return JNI_ERR;
+	}
+
+	/* If we didn't actually release the lock, we're done */
+	if (!released)
+		return JNI_OK;
+
+handle_contention:
+	/*
+	 * Handle the case where we are actually releasing the lock,
+	 * not just decrementing the number of times we've re-locked it.
+	 */
+
+	/* If there is nobody else waiting for the lock, we're done */
+	if (!env->lock.owner.contention)
+		return JNI_OK;
+
+	/* Handle contention */
+	_jc_lock_contention(env, obj);
+
+	/* Done */
+	return JNI_OK;
+}
+
+/*
+ * Handle contention on a lock.
+ *
+ * The current thread must own the lock.
+ */
+void
+_jc_lock_contention(_jc_env *env, _jc_object *obj)
+{
+	_jc_env *thread;
+
+	/* Acquire mutex associated with this thread's waiter queue */
+	_JC_MUTEX_LOCK(env, env->lock.owner.mutex);
+
+	/* Iterate through waiters list and wake them up */
+	SLIST_FOREACH(thread, &env->lock.owner.waiters, lock.waiter.link) {
+		_jc_object *const wobj = thread->lock.waiter.object;
+
+		/*
+		 * If the thread is waiting on another object that
+		 * we have locked, then try to inflate that lock.
+		 */
+		if (wobj != obj && !_JC_LW_TEST(wobj->lockword, FAT)) {
+
+			/* Sanity check that we actually own that lock too */
+			_JC_ASSERT((wobj->lockword & _JC_LW_THIN_TID_MASK)
+			    == env->thinlock_id);
+
+			/*
+			 * Try to inflate the other lock, but if we fail,
+			 * don't worry. The other thread will inflate the
+			 * lock when it wakes up.
+			 */
+			if (_jc_inflate_lock(env, wobj) != JNI_OK)
+				_jc_retrieve_exception(env, NULL);
+		}
+
+		/* Wake up the other thread */
+		_JC_COND_SIGNAL(thread->lock.waiter.cond);
+	}
+
+	/* Empty the wait list */
+	SLIST_INIT(&env->lock.owner.waiters);
+
+	/* No more contention */
+	env->lock.owner.contention = 0;
+
+	/* Release mutex associated with this thread's lock owner info */
+	_JC_MUTEX_UNLOCK(env, env->lock.owner.mutex);
+}
+
+/*
+ * Inflate a thin lock, turning it into a fat lock.
+ */
+jint
+_jc_inflate_lock(_jc_env *env, _jc_object *obj)
+{
+	_jc_jvm *const vm = env->vm;
+	jboolean tried_gc = JNI_FALSE;
+	_jc_fat_lock *lock;
+
+try_again:
+	/* Acquire global mutex */
+	_JC_MUTEX_LOCK(env, vm->mutex);
+
+	/* Get a fat lock off the free list if possible */
+	if ((lock = SLIST_FIRST(&vm->fat_locks.free_list)) != NULL) {
+		SLIST_REMOVE_HEAD(&vm->fat_locks.free_list, u.link);
+		goto got_lock;
+	}
+
+	/* Can we create any more fat locks? */
+	if (vm->fat_locks.next_id >= _JC_MAX_FATLOCKS) {
+
+		/* All id's are allocated: try a GC cycle to free up some */
+		if (!tried_gc) {
+			_JC_MUTEX_UNLOCK(env, vm->mutex);
+			if (_jc_gc(env, JNI_TRUE) != JNI_OK) {
+				_jc_post_exception_info(env);
+				return JNI_ERR;
+			}
+			tried_gc = JNI_TRUE;
+			goto try_again;
+		}
+
+		/* All id's allocated and no free locks: throw an error */
+		_jc_post_exception_msg(env, _JC_InternalError,
+		    "max number of fat locks (%d) exceeded", _JC_MAX_FATLOCKS);
+		goto fail;
+	}
+
+	/* Create a new fat lock */
+	if ((lock = _jc_vm_zalloc(env, sizeof(*lock))) == NULL) {
+		_jc_post_exception_info(env);
+		goto fail;
+	}
+	lock->id = vm->fat_locks.next_id;
+
+	/* Initialize fat lock synchronization objects */
+	if (_jc_mutex_init(env, &lock->mutex) != JNI_OK) {
+		_jc_post_exception_info(env);
+		goto fail;
+	}
+	if (_jc_cond_init(env, &lock->cond) != JNI_OK) {
+		_jc_post_exception_info(env);
+		_jc_mutex_destroy(&lock->mutex);
+		goto fail;
+	}
+	if (_jc_cond_init(env, &lock->notify) != JNI_OK) {
+		_jc_post_exception_info(env);
+		_jc_cond_destroy(&lock->cond);
+		_jc_mutex_destroy(&lock->mutex);
+		goto fail;
+	}
+
+	/* Map in this lock's ID */
+	vm->fat_locks.by_id[lock->id] = lock;
+	vm->fat_locks.next_id++;
+
+got_lock:
+	/* Release global mutex */
+	_JC_MUTEX_UNLOCK(env, vm->mutex);
+
+	/* Sanity check */
+	_JC_ASSERT(lock->recursion_count == 0);
+
+	/* Acquire the mutex associated with the fat lock */
+	_JC_MUTEX_LOCK(env, lock->mutex);
+
+	/* Mark this thread as the lock's owner */
+	lock->u.owner = env;
+
+	/* Get recursion count, adjusted by one for thin -> fat transition */
+	lock->recursion_count = _JC_LW_EXTRACT(obj->lockword, THIN_COUNT) + 1;
+
+	/* Convert object lockword to reflect fat lock status */
+	obj->lockword = _JC_LW_FAT_BIT
+	    | (lock->id << _JC_LW_FAT_ID_SHIFT)
+	    | (obj->lockword & _JC_LW_INFO_MASK);
+
+	/* Release the fat lock mutex */
+	_JC_MUTEX_UNLOCK(env, lock->mutex);
+
+	/* Done */
+	return JNI_OK;
+
+fail:
+	/* Clean up after failing to create a new fat lock */
+	_jc_vm_free(&lock);
+	_JC_MUTEX_UNLOCK(env, vm->mutex);
+	return JNI_ERR;
+}
+
+/*
+ * Put a fat lock back on the free list.
+ *
+ * NOTE: This should only be called when the world is stopped.
+ */
+void
+_jc_free_lock(_jc_jvm *vm, _jc_object *obj)
+{
+	const _jc_word lockword = obj->lockword;
+	_jc_fat_lock *lock;
+
+	/* Sanity check */
+	_JC_ASSERT(_JC_LW_TEST(lockword, FAT));
+	_JC_ASSERT(vm->world_stopped);
+
+	/* Get the lock encoded by the lockword */
+	lock = vm->fat_locks.by_id[_JC_LW_EXTRACT(lockword, FAT_ID)];
+	_JC_ASSERT(lock != NULL);
+	_JC_ASSERT(lock->recursion_count == 0);
+
+	/* Return it to the free list */
+	SLIST_INSERT_HEAD(&vm->fat_locks.free_list, lock, u.link);
+}
+
+/*
+ * Destroy a fat lock.
+ *
+ * This is only done during VM shutdown; during normal operation unused
+ * fat locks are always returned to the free list.
+ */
+void
+_jc_destroy_lock(_jc_fat_lock **lockp)
+{
+	_jc_fat_lock *lock = *lockp;
+
+	/* Sanity check */
+	if (lock == NULL)
+		return;
+	*lockp = NULL;
+	_JC_ASSERT(lock->recursion_count == 0);
+
+	/* Destroy lock */
+	_jc_cond_destroy(&lock->cond);
+	_jc_cond_destroy(&lock->notify);
+	_jc_mutex_destroy(&lock->mutex);
+	_jc_vm_free(&lock);
+}
+
+/*
+ * Check whether the current thread holds the object's lock.
+ */
+jboolean
+_jc_lock_held(_jc_env *env, _jc_object *obj)
+{
+	_jc_jvm *const vm = env->vm;
+	_jc_word lockword;
+	_jc_fat_lock *lock;
+
+	/* Get object lockword */
+	lockword = obj->lockword;
+
+	/* Do the thin lock case */
+	if (!_JC_LW_TEST(lockword, FAT))
+		return (lockword & _JC_LW_THIN_TID_MASK) == env->thinlock_id;
+
+	/* It's a fat lock */
+	lock = vm->fat_locks.by_id[_JC_LW_EXTRACT(lockword, FAT_ID)];
+	_JC_ASSERT(lock != NULL);
+	return lock->recursion_count > 0 && lock->u.owner == env;
+}
+

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/misc.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/misc.c?rev=294974&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/misc.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/misc.c Tue Oct  4 19:19:16 2005
@@ -0,0 +1,582 @@
+
+/*
+ * Copyright 2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ * 
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ * $Id: misc.c,v 1.9 2005/07/04 19:56:16 archiecobbs Exp $
+ */
+
+#include "libjc.h"
+
+/*
+ * Create a subdirectory under 'root' if it doesn't already exist.
+ * The pathname is the name of any file in the subdirectory.
+ */
+jint
+_jc_create_subdir(_jc_env *env, const char *root, const char *pathname)
+{
+	const size_t rlen = strlen(root);
+	const char *error_path;
+	struct stat info;
+	char *next;
+	char *buf;
+	char *s;
+
+	/* The root directory must exist */
+	error_path = root;
+	if (stat(root, &info) == -1)
+		goto io_err;
+	if ((info.st_mode & S_IFMT) != S_IFDIR) {
+		errno = ENOTDIR;
+		goto io_err;
+	}
+
+	/* Check successive subdirectories */
+	if ((buf = _JC_FORMAT_STRING(env, "%s%s%s",
+	    root, _JC_FILE_SEPARATOR, pathname)) == NULL) {
+		_jc_post_exception_info(env);
+		return JNI_ERR;
+	}
+	error_path = buf;
+	for (s = buf + rlen + 1; ; s = next) {
+
+		/* Another directory in path? If not, we're done */
+		if ((next = strstr(s, _JC_FILE_SEPARATOR)) == NULL)
+			return JNI_OK;
+
+		/* Check directory status; create if not found */
+		*next = '\0';
+		if (stat(buf, &info) == -1) {
+			if (errno != ENOENT)
+				goto io_err;
+			if (mkdir(buf, 0755) == -1)
+				goto io_err;
+		} else if ((info.st_mode & S_IFMT) != S_IFDIR) {
+			errno = ENOTDIR;
+			goto io_err;
+		}
+		strcpy(next, _JC_FILE_SEPARATOR);
+		next += strlen(_JC_FILE_SEPARATOR);
+	}
+
+io_err:
+	/* Failed; throw an error */
+	_jc_post_exception_msg(env, _JC_InternalError,
+	    "%s: %s", error_path, strerror(errno));
+	return JNI_ERR;
+}
+
+/*
+ * Comparator for _jc_class_node that sorts by name.
+ */
+int
+_jc_classfile_tree_compare(const void *v1, const void *v2)
+{
+	const _jc_class_node *const node1 = v1;
+	const _jc_class_node *const node2 = v2;
+
+	return strcmp(node1->name, node2->name);
+}
+
+/*
+ * Method comparison function that sorts by function address.
+ *
+ * This has a special case for function == function_end, which
+ * is used for searching. In this case, the address is a return
+ * address after a function call, so the beginning of the
+ * other function is exclusive while its end is inclusive.
+ */
+int
+_jc_method_tree_compare(const void *v1, const void *v2)
+{
+	const _jc_method *const method1 = v1;
+	const _jc_method *const method2 = v2;
+
+	/* Handle case where method1 is a search key */
+	if (method1->function == method1->u.exec.function_end) {
+		_JC_ASSERT(method2->function != method2->u.exec.function_end);
+		_JC_ASSERT(!_JC_ACC_TEST(method2, INTERP));
+		return (method1->function > method2->u.exec.function_end)
+		    - (method1->function <= method2->function);
+	}
+
+	/* Handle case where method2 is a search key */
+	if (method2->function == method2->u.exec.function_end) {
+		_JC_ASSERT(!_JC_ACC_TEST(method1, INTERP));
+		return (method2->function <= method1->function)
+		    - (method2->function > method1->u.exec.function_end);
+	}
+
+	/* Sanity check: we only do this for executable methods */
+	_JC_ASSERT(!_JC_ACC_TEST(method1, INTERP));
+	_JC_ASSERT(!_JC_ACC_TEST(method2, INTERP));
+
+	/* Sanity check: if they overlap, they must be the same method */
+	_JC_ASSERT(method1->u.exec.function_end <= method2->function
+	    || method2->u.exec.function_end <= method1->function
+	    || method1 == method2);
+
+	/* Both methods are normal */
+	return (method1->function > method2->function)
+	    - (method1->function < method2->function);
+}
+
+/*
+ * Compare two class references.
+ */
+int
+_jc_class_ref_compare(const void *v1, const void *v2)
+{
+	const _jc_class_ref *const cref1 = v1;
+	const _jc_class_ref *const cref2 = v2;
+	int diff;
+
+	if (cref1->len < cref2->len)
+		diff = strncmp(cref1->name, cref2->name, cref1->len);
+	else
+		diff = strncmp(cref1->name, cref2->name, cref2->len);
+	if (diff != 0)
+		return diff;
+	return (cref1->len > cref2->len) - (cref1->len < cref2->len);
+}
+
+/*
+ * JC-encode a Java name into the supplied buffer. Returns then length
+ * of the encoded name. The 'buf' may be NULL to just compute the length.
+ *
+ * If 'pass_slash' is true, then pass '/' characters through unencoded.
+ */
+size_t
+_jc_name_encode(const char *name, char *buf, jboolean pass_slash)
+{
+	static const char hexdigs[] = "0123456789abcdef";
+	size_t len = 0;
+	const char *s;
+
+	for (s = name; *s != '\0'; s++) {
+		const char ch = *s;
+
+		if (isalnum(ch) || (ch == '/' && pass_slash)) {
+			if (buf != NULL)
+				buf[len] = ch;
+			len++;
+		} else if (ch == '/' || ch == '.') {
+			if (buf != NULL)
+				buf[len] = '_';
+			len++;
+		} else {
+			if (buf != NULL) {
+				buf[len + 0] = '_';
+				buf[len + 1] = '_';
+				buf[len + 2] = hexdigs[(ch >> 4) & 0x0f];
+				buf[len + 3] = hexdigs[ch & 0x0f];
+			}
+			len += 4;
+		}
+	}
+	if (buf != NULL)
+		buf[len] = '\0';
+	return len;
+}
+
+/*
+ * Decode a JC-encoded Java name, stopping at the first '$' or end of string.
+ * The supplied buffer is assumed to be big enough; it need not be bigger
+ * than strlen(name) + 1 in all cases.
+ *
+ * Returns NULL if the string was not validly encoded, otherwise a pointer
+ * to the terminating '\0' byte in the decoded buffer.
+ */
+char *
+_jc_name_decode(const char *name, char *buf)
+{
+	const char *t;
+	char *p;
+	int i;
+
+	for (p = buf, t = name; *t != '\0' && *t != '$'; ) {
+		switch (*t) {
+		case '_':
+
+			// Try to decode an escaped character
+			if (*++t == '_' && isxdigit(t[1]) && isxdigit(t[2])) {
+				for (*p = '\0', i = 1; i <= 2; i++)
+					*p = (*p << 4) | _JC_HEXVAL(t[i]);
+				t += 3;
+				p++;
+				break;
+			}
+
+			// It's just a slash
+			*p++ = '/';
+			break;
+		default:
+			*p++ = *t++;
+			break;
+		}
+	}
+	*p = '\0';
+
+	/* Done */
+	return p;
+}
+
+/*
+ * Parse a base class name out of a signature, stripping off array dimensions.
+ *
+ * Returns a pointer to the character after the parsed class name.
+ * If 'cc' is false then non-array class names are expected to be encoded
+ * "Llike/This;", otherwise "like/This" (the latter is the case for
+ * CONSTANT_Class constant pool entries).
+ *
+ * Upon return '*ptype' will be set to the appropriate _JC_TYPE_XXX value
+ * for the base type (possibly _JC_TYPE_INVALID or _JC_TYPE_VOID).
+ */
+const char *
+_jc_parse_class_ref(const char *s, _jc_class_ref *rc, int cc, u_char *ptype)
+{
+	int dims;
+
+	for (dims = 0; s[dims] == '['; dims++);
+	s += dims;
+	if (dims == 0 && cc) {			/* can't be primitive */
+		rc->name = s;
+		rc->len = strlen(s);
+		*ptype = _JC_TYPE_REFERENCE;
+		return s + rc->len;
+	}
+	if (*s != 'L') {
+		rc->name = s;
+		rc->len = 1;
+		*ptype = _jc_sig_types[(u_char)*s];
+		return s + 1;
+	}
+	rc->name = ++s;
+	while (*s != '\0' && *s != ';')
+		s++;
+	rc->len = s - rc->name;
+	*ptype = _JC_TYPE_REFERENCE;
+	return *s == ';' ? s + 1 : s;
+}
+
+/*
+ * Parse a directory search path into _jc_cpath_entry structures.
+ *
+ * If unsuccessful an exception is stored.
+ */
+jint
+_jc_parse_classpath(_jc_env *env, const char *path,
+	_jc_cpath_entry **listp, int *lenp)
+{
+	_jc_cpath_entry *list;
+	char **pathnames;
+	int len;
+	int i;
+
+	/* Chop up classpath into components and count them */
+	if ((pathnames = _jc_parse_searchpath(env, path)) == NULL)
+		return JNI_ERR;
+	for (len = 0; pathnames[len] != NULL; len++);
+
+	/* Allocate class path entry array */
+	if ((list = _jc_vm_realloc(env, *listp,
+	    (*lenp + len) * sizeof(*list))) == NULL) {
+		while (len > 0)
+			_jc_vm_free(&pathnames[--len]);
+		_jc_vm_free(&pathnames);
+		return JNI_ERR;
+	}
+	memset(list + *lenp, 0, len * sizeof(*list));
+
+	/* Initialize and append entries */
+	for (i = 0; i < len; i++) {
+		_jc_cpath_entry *const ent = &list[*lenp + i];
+
+		ent->type = _JC_CPATH_UNKNOWN;
+		ent->pathname = pathnames[i];
+	}
+
+	/* Return updated list */
+	*listp = list;
+	*lenp += len;
+
+	/* Done */
+	_jc_vm_free(&pathnames);
+	return JNI_OK;
+}
+
+/*
+ * Parse object search path into _jc_parse_objpath structures.
+ *
+ * If unsuccessful an exception is stored.
+ */
+int
+_jc_parse_objpath(_jc_env *env, const char *path, _jc_objpath_entry **listp)
+{
+	_jc_objpath_entry *list;
+	char **pathnames;
+	int len;
+	int i;
+
+	/* Chop up object path into components and count them */
+	if ((pathnames = _jc_parse_searchpath(env, path)) == NULL)
+		return -1;
+	for (len = 0; pathnames[len] != NULL; len++);
+
+	/* Allocate object path entry array */
+	if ((list = _jc_vm_zalloc(env, len * sizeof(*list))) == NULL) {
+		while (len > 0)
+			_jc_vm_free(&pathnames[--len]);
+		_jc_vm_free(&pathnames);
+		return -1;
+	}
+
+	/* Initialize entries */
+	for (i = 0; i < len; i++) {
+		_jc_objpath_entry *const ent = &list[i];
+
+		ent->type = _JC_OBJPATH_UNKNOWN;
+		ent->pathname = pathnames[i];
+	}
+
+	/* Done */
+	_jc_vm_free(&pathnames);
+	*listp = list;
+	return len;
+}
+
+/*
+ * Parse a directory search path into component directories.
+ *
+ * If unsuccessful an exception is stored.
+ */
+char **
+_jc_parse_searchpath(_jc_env *env, const char *path)
+{
+	const size_t ps_len = strlen(_JC_PATH_SEPARATOR);
+	char **array = NULL;
+	const char *s;
+	int num;
+
+again:
+	for (s = path, num = 0; JNI_TRUE; num++) {
+		const char *t;
+		int len;
+
+		while (strncmp(s, _JC_PATH_SEPARATOR, ps_len) == 0)
+			s += ps_len;
+		if (*s == '\0')
+			break;
+		if ((t = strstr(s, _JC_PATH_SEPARATOR)) == NULL)
+			t = s + strlen(s);
+		len = t - s;
+		if (array != NULL
+		    && (array[num] = _jc_vm_strndup(env, s, len)) == NULL) {
+			while (num > 0)
+				_jc_vm_free(&array[--num]);
+			_jc_vm_free(&array);
+			return NULL;
+		}
+		s += len;
+		if (array != NULL) {
+			while (len > 0
+			    && array[num][len - 1] == _JC_FILE_SEPARATOR[0])
+				array[num][--len] = '\0';
+		}
+	}
+	if (array == NULL) {
+		if ((array = _jc_vm_zalloc(env,
+		    (num + 1) * sizeof(*array))) == NULL)
+			return NULL;
+		goto again;
+	}
+	array[num] = NULL;
+	return array;
+}
+
+/*
+ * Compare two nodes in a loader's defining loader type tree.
+ */
+int
+_jc_type_cmp(const void *item1, const void *item2)
+{
+	const _jc_type *const type1 = item1;
+	const _jc_type *const type2 = item2;
+
+	return strcmp(type1->name, type2->name);
+}
+
+/*
+ * Compare two nodes in a loader's initiated type tree
+ * or partially derived type tree.
+ */
+int
+_jc_node_cmp(const void *item1, const void *item2)
+{
+	const _jc_type_node *const node1 = item1;
+	const _jc_type_node *const node2 = item2;
+
+	return strcmp(node1->type->name, node2->type->name);
+}
+
+/*
+ * Compare two _jc_method_node's, sorting on the class and method name.
+ */
+int
+_jc_method_node_compare(const void *v1, const void *v2)
+{
+	const _jc_method_node *const info1 = v1;
+	const _jc_method_node *const info2 = v2;
+	int diff;
+
+	if ((diff = info1->mlen - info2->mlen) != 0)
+		return diff;
+	if ((diff = info1->clen - info2->clen) != 0)
+		return diff;
+	if ((diff = strncmp(info1->mname, info2->mname, info1->mlen)) != 0)
+		return diff;
+	return strncmp(info1->cname, info2->cname, info1->clen);
+}
+
+/*
+ * Compare two _jc_field **'s, sorting in the same way as
+ * org.dellroad.jc.cgen.Util.fieldComparator.
+ */
+int
+_jc_field_compare(const void *v1, const void *v2)
+{
+	_jc_field *const field1 = *((_jc_field **)v1);
+	_jc_field *const field2 = *((_jc_field **)v2);
+	const u_char ptype1 = _jc_sig_types[(u_char)*field1->signature];
+	const u_char ptype2 = _jc_sig_types[(u_char)*field2->signature];
+	int diff;
+
+	if ((diff = !_JC_ACC_TEST(field1, STATIC)
+	    - !_JC_ACC_TEST(field2, STATIC)) != 0)
+		return diff;
+	if ((diff = _jc_field_type_sort[ptype1]
+	    - _jc_field_type_sort[ptype2]) != 0)
+		return diff;
+	if ((diff = strcmp(field1->name, field2->name)) != 0)
+		return diff;
+	return strcmp(field1->signature, field2->signature);
+}
+
+/*
+ * Compare two _jc_method **'s, sorting in the same way as
+ * org.dellroad.jc.cgen.Util.methodComparator.
+ */
+int
+_jc_method_compare(const void *v1, const void *v2)
+{
+	_jc_method *const method1 = *((_jc_method **)v1);
+	_jc_method *const method2 = *((_jc_method **)v2);
+	int diff;
+
+	if ((diff = strcmp(method1->name, method2->name)) != 0)
+		return diff;
+	return strcmp(method1->signature, method2->signature);
+}
+
+/*
+ * Determine the length of JNI encoding a method name or signature.
+ */
+size_t
+_jc_jni_encode_length(const char *s)
+{
+	size_t len;
+
+	for (len = 0; *s != '\0'; s++) {
+		const u_char ch = *s;
+
+		if (isalnum(ch) || ch == '/')
+			len++;
+		else if (ch == '_' || ch == ';' || ch == '[')
+			len += 2;
+		else if (ch == '(')
+			/* skip */;
+		else if (ch == ')')
+			break;
+		else if ((ch & 0x80) == 0x00)
+			len += 6;
+		else if ((ch & 0xe0) == 0xc0) {
+			len += 6;
+			s++;
+		} else {
+			_JC_ASSERT((ch & 0xf0) == 0xe0);
+			len += 6;
+			s += 2;
+		}
+	}
+	return len;
+}
+
+/*
+ * JNI encode a string.
+ */
+void
+_jc_jni_encode(char **bufp, const char *s)
+{
+	while (*s != '\0') {
+		u_char ch = *s++;
+
+		if (isalnum(ch))
+			*(*bufp)++ = ch;
+		else if (ch == '/')
+			*(*bufp)++ = '_';
+		else if (ch == '_') {
+			*(*bufp)++ = '_';
+			*(*bufp)++ = '1';
+		} else if (ch == ';') {
+			*(*bufp)++ = '_';
+			*(*bufp)++ = '2';
+		} else if (ch == '[') {
+			*(*bufp)++ = '_';
+			*(*bufp)++ = '3';
+		} else if (ch == '(')
+			/* skip */;
+		else if (ch == ')')
+			break;
+		else {
+			static const char hexdigits[] = "0123456789abcdef";
+			jchar jc;
+
+			*(*bufp)++ = '_';
+			*(*bufp)++ = '0';
+			if ((ch & 0x80) == 0x00)
+				jc = ch & 0x7f;
+			else if ((ch & 0xe0) == 0xc0) {
+				jc = (ch & 0x1f) << 6;
+				ch = *s++;
+				_JC_ASSERT((ch & 0xc0) == 0x80);
+				jc |= (ch & 0x3f);
+			} else {
+				_JC_ASSERT(((ch >> 4) & 0x0f) == 0x0e);
+				jc = (ch & 0x0f) << 12;
+				ch = *s++;
+				_JC_ASSERT((ch & 0xc0) == 0x80);
+				jc |= (ch & 0x3f) << 6;
+				ch = *s++;
+				_JC_ASSERT((ch & 0xc0) == 0x80);
+				jc |= (ch & 0x3f);
+			}
+			*(*bufp)++ = hexdigits[(jc >> 12) & 0x0f];
+			*(*bufp)++ = hexdigits[(jc >>  8) & 0x0f];
+			*(*bufp)++ = hexdigits[(jc >>  4) & 0x0f];
+			*(*bufp)++ = hexdigits[(jc      ) & 0x0f];
+		}
+	}
+}
+

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/mutex.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/mutex.c?rev=294974&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/mutex.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/mutex.c Tue Oct  4 19:19:16 2005
@@ -0,0 +1,109 @@
+
+/*
+ * Copyright 2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ * 
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ * $Id: mutex.c,v 1.2 2004/07/05 21:03:27 archiecobbs Exp $
+ */
+
+#include "libjc.h"
+
+/*
+ * Initialize a mutex
+ *
+ * If unsuccessful an exception is stored.
+ */
+jint
+_jc_mutex_init(_jc_env *env, pthread_mutex_t *mutex)
+{
+	pthread_mutexattr_t attr;
+	int error;
+
+	/* Initialize mutex attributes */
+	if ((error = pthread_mutexattr_init(&attr)) != 0) {
+		_JC_EX_STORE(env, InternalError,
+		    "%s: %s", "pthread_mutexattr_init", strerror(error));
+		return JNI_ERR;
+	}
+
+#if NDEBUG
+	/* Enable debug checks */
+	if ((error = pthread_mutexattr_settype(&attr,
+	    PTHREAD_MUTEX_ERRORCHECK)) != 0) {
+		_JC_EX_STORE(env, InternalError,
+		    "%s: %s", "pthread_mutexattr_settype", strerror(error));
+		pthread_mutexattr_destroy(&attr);
+		return JNI_ERR;
+	}
+#endif
+
+	/* Initialize mutex */
+	if ((error = pthread_mutex_init(mutex, &attr)) != 0) {
+		_JC_EX_STORE(env, InternalError,
+		    "%s: %s", "pthread_mutex_init", strerror(error));
+		pthread_mutexattr_destroy(&attr);
+		return JNI_ERR;
+	}
+
+	/* Clean up */
+	pthread_mutexattr_destroy(&attr);
+	return JNI_OK;
+}
+
+/*
+ * Destroy a mutex
+ */
+void
+_jc_mutex_destroy(pthread_mutex_t *mutex)
+{
+	int r;
+
+	r = pthread_mutex_destroy(mutex);
+	_JC_ASSERT(r == 0);
+}
+
+/*
+ * Initialize a condition variable.
+ *
+ * If unsuccessful an exception is stored.
+ */
+jint
+_jc_cond_init(_jc_env *env, pthread_cond_t *cond)
+{
+	int error;
+
+	/* Initialize mutex attributes */
+	if ((error = pthread_cond_init(cond, NULL)) != 0) {
+		_JC_EX_STORE(env, InternalError,
+		    "%s: %s", "pthread_cond_init", strerror(error));
+		return JNI_ERR;
+	}
+
+	/* Done */
+	return JNI_OK;
+}
+
+/*
+ * Destroy a condition variable.
+ */
+void
+_jc_cond_destroy(pthread_cond_t *cond)
+{
+	int r;
+
+	r = pthread_cond_destroy(cond);
+	_JC_ASSERT(r == 0);
+}
+

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/Makefile.am
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/Makefile.am?rev=294974&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/Makefile.am (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/Makefile.am Tue Oct  4 19:19:16 2005
@@ -0,0 +1,113 @@
+## $Id: Makefile.am,v 1.8 2005/05/15 21:41:01 archiecobbs Exp $
+
+jcbindir=	$(bindir)
+jcdir=		$(datadir)/jc
+cpdir=		$(CLASSPATH_HOME)/share/classpath
+
+EXTRA_DIST=	gnu_classpath_VMStackWalker.c \
+		gnu_classpath_VMStackWalker.h \
+		gnu_classpath_VMSystemProperties.c \
+		gnu_classpath_VMSystemProperties.h \
+		java_io_VMObjectStreamClass.c \
+		java_io_VMObjectStreamClass.h \
+		java_lang_Thread.h \
+		java_lang_VMClass.c \
+		java_lang_VMClass.h \
+		java_lang_VMClassLoader.c \
+		java_lang_VMClassLoader.h \
+		java_lang_VMCompiler.c \
+		java_lang_VMCompiler.h \
+		java_lang_VMObject.c \
+		java_lang_VMObject.h \
+		java_lang_VMRuntime.c \
+		java_lang_VMRuntime.h \
+		java_lang_VMSystem.c \
+		java_lang_VMSystem.h \
+		java_lang_VMThread.c \
+		java_lang_VMThread.h \
+		java_lang_VMThrowable.c \
+		java_lang_VMThrowable.h \
+		java_lang_reflect_Constructor.c \
+		java_lang_reflect_Constructor.h \
+		java_lang_reflect_Field.c \
+		java_lang_reflect_Field.h \
+		java_lang_reflect_Method.c \
+		java_lang_reflect_Method.h \
+		org_dellroad_jc_JCFinder.c \
+		org_dellroad_jc_JCFinder.h \
+		org_dellroad_jc_cgen_SootCodeGenerator.c \
+		org_dellroad_jc_cgen_SootCodeGenerator.h \
+		org_dellroad_jc_vm_DebugThread.c \
+		org_dellroad_jc_vm_DebugThread.h \
+		org_dellroad_jc_vm_FinalizerThread.c \
+		org_dellroad_jc_vm_FinalizerThread.h
+
+##
+## Generate JCNI header files automatically using jcjavah
+##
+
+JAVAH=		$(jcbindir)/jcjavah
+JAVAH_CMD=	$(JAVAH) -classpath $(jcdir)/jc.zip:$(cpdir)/glibj.zip \
+		    -d $(srcdir)
+
+HFILES=		gnu_classpath_VMStackWalker.h \
+		gnu_classpath_VMSystemProperties.h \
+		java_io_VMObjectStreamClass.h \
+		java_lang_Thread.h \
+		java_lang_VMClass.h \
+		java_lang_VMClassLoader.h \
+		java_lang_VMCompiler.h \
+		java_lang_VMObject.h \
+		java_lang_VMRuntime.h \
+		java_lang_VMSystem.h \
+		java_lang_VMThread.h \
+		java_lang_VMThrowable.h \
+		java_lang_reflect_Constructor.h \
+		java_lang_reflect_Field.h \
+		java_lang_reflect_Method.h \
+		org_dellroad_jc_JCFinder.h \
+		org_dellroad_jc_cgen_SootCodeGenerator.h \
+		org_dellroad_jc_vm_DebugThread.h \
+		org_dellroad_jc_vm_FinalizerThread.h
+
+hfiles:			${HFILES}
+
+gnu_classpath_VMStackWalker.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+gnu_classpath_VMSystemProperties.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_io_VMObjectStreamClass.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_lang_Thread.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_lang_VMClass.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_lang_VMClassLoader.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_lang_VMCompiler.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_lang_VMObject.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_lang_VMRuntime.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_lang_VMSystem.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_lang_VMThread.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_lang_VMThrowable.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_lang_reflect_Constructor.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_lang_reflect_Field.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+java_lang_reflect_Method.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+org_dellroad_jc_JCFinder.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+org_dellroad_jc_cgen_SootCodeGenerator.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+org_dellroad_jc_vm_DebugThread.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+org_dellroad_jc_vm_FinalizerThread.h:
+	$(JAVAH_CMD) `echo $@ | sed -e 's/\.h$$//g' -e 's/_/./g'`
+

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/gnu_classpath_VMStackWalker.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/gnu_classpath_VMStackWalker.c?rev=294974&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/gnu_classpath_VMStackWalker.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/gnu_classpath_VMStackWalker.c Tue Oct  4 19:19:16 2005
@@ -0,0 +1,184 @@
+
+/*
+ * Copyright 2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ * 
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ * $Id: gnu_classpath_VMStackWalker.c,v 1.4 2005/05/15 21:41:01 archiecobbs Exp $
+ */
+
+#include "libjc.h"
+#include "gnu_classpath_VMStackWalker.h"
+
+/* Internal functions */
+static jboolean	_jc_poppable_method(_jc_jvm *vm, _jc_method *method);
+
+static jboolean
+_jc_poppable_method(_jc_jvm *vm, _jc_method *method)
+{
+	/* Check for VMStackWalker methods */
+	if (method->class == vm->boot.types.VMStackWalker)
+		return JNI_TRUE;
+
+	/* Check for Method.invoke() */
+	if (method == vm->boot.methods.Method.invoke)
+		return JNI_TRUE;
+
+	/* Not poppable */
+	return JNI_FALSE;
+}
+
+/*
+ * public static final native Class getCallingClass()
+ */
+_jc_object * _JC_JCNI_ATTR
+JCNI_gnu_classpath_VMStackWalker_getCallingClass(_jc_env *env)
+{
+	_jc_jvm *const vm = env->vm;
+	_jc_object *result = NULL;
+	_jc_stack_crawl crawl;
+	jboolean top;
+
+	/* Lock VM */
+	_JC_MUTEX_LOCK(env, vm->mutex);
+
+	/* Crawl up the stack */
+	for (top = JNI_TRUE, _jc_stack_crawl_first(env, &crawl);
+	    crawl.method != NULL; _jc_stack_crawl_next(vm, &crawl)) {
+
+		/* Ignore non-Java stack frames */
+		if (crawl.method->class == NULL)
+			continue;
+
+		/* Ignore internal methods on top of stack */
+		if (top && _jc_poppable_method(vm, crawl.method))
+			continue;
+		top = JNI_FALSE;
+
+		/* Pop one additional frame */
+		do
+		    _jc_stack_crawl_next(vm, &crawl);
+		while (crawl.method != NULL && crawl.method->class == NULL);
+
+		/* Done */
+		if (crawl.method != NULL && crawl.method->class != NULL)
+			result = crawl.method->class->instance;
+		break;
+	}
+
+	/* Unlock VM */
+	_JC_MUTEX_UNLOCK(env, vm->mutex);
+
+	/* Done */
+	return result;
+}
+
+/*
+ * public static final native ClassLoader getCallingClassLoader()
+ */
+_jc_object * _JC_JCNI_ATTR
+JCNI_gnu_classpath_VMStackWalker_getCallingClassLoader(_jc_env *env)
+{
+	_jc_jvm *const vm = env->vm;
+	_jc_object *result = NULL;
+	_jc_stack_crawl crawl;
+	jboolean top;
+
+	/* Lock VM */
+	_JC_MUTEX_LOCK(env, vm->mutex);
+
+	/* Crawl up the stack */
+	for (top = JNI_TRUE, _jc_stack_crawl_first(env, &crawl);
+	    crawl.method != NULL; _jc_stack_crawl_next(vm, &crawl)) {
+
+		/* Ignore non-Java stack frames */
+		if (crawl.method->class == NULL)
+			continue;
+
+		/* Ignore internal methods on top of stack */
+		if (top && _jc_poppable_method(vm, crawl.method))
+			continue;
+		top = JNI_FALSE;
+
+		/* Pop one additional frame */
+		do
+		    _jc_stack_crawl_next(vm, &crawl);
+		while (crawl.method != NULL && crawl.method->class == NULL);
+
+		/* Done */
+		if (crawl.method != NULL && crawl.method->class != NULL)
+			result = crawl.method->class->loader->instance;
+		break;
+	}
+
+	/* Unlock VM */
+	_JC_MUTEX_UNLOCK(env, vm->mutex);
+
+	/* Done */
+	return result;
+}
+
+/*
+ * public static final native Class[] getClassContext()
+ */
+_jc_object_array * _JC_JCNI_ATTR
+JCNI_gnu_classpath_VMStackWalker_getClassContext(_jc_env *env)
+{
+	_jc_jvm *const vm = env->vm;
+	_jc_object_array *array = NULL;
+	_jc_stack_crawl crawl;
+	jboolean top;
+	int num;
+
+	/* Lock VM */
+	_JC_MUTEX_LOCK(env, vm->mutex);
+
+again:
+	/* Crawl the Java stack and add Class objects for each method */
+	for (num = 0, top = JNI_TRUE, _jc_stack_crawl_first(env, &crawl);
+	    crawl.method != NULL; _jc_stack_crawl_next(vm, &crawl)) {
+
+		/* Ignore non-Java stack frames */
+		if (crawl.method->class == NULL)
+			continue;
+
+		/* Ignore internal methods on top of stack */
+		if (top && _jc_poppable_method(vm, crawl.method))
+			continue;
+		top = JNI_FALSE;
+
+		/* Add method's class */
+		if (array != NULL)
+			array->elems[~num] = crawl.method->class->instance;
+		num++;
+	}
+
+	/* Create array and loop back */
+	if (array == NULL) {
+		if ((array = (_jc_object_array *)_jc_new_array(env,
+		    vm->boot.types.Class_array, num)) == NULL) {
+			_JC_MUTEX_UNLOCK(env, vm->mutex);
+			_jc_throw_exception(env);
+		}
+		goto again;
+	}
+	_JC_ASSERT(num == array->length);
+
+	/* Unlock VM */
+	_JC_MUTEX_UNLOCK(env, vm->mutex);
+
+	/* Done */
+	return array;
+}
+

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/gnu_classpath_VMSystemProperties.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/gnu_classpath_VMSystemProperties.c?rev=294974&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/gnu_classpath_VMSystemProperties.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/gnu_classpath_VMSystemProperties.c Tue Oct  4 19:19:16 2005
@@ -0,0 +1,93 @@
+
+/*
+ * Copyright 2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ * 
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ * $Id: gnu_classpath_VMSystemProperties.c,v 1.2 2005/05/15 21:41:01 archiecobbs Exp $
+ */
+
+#include "libjc.h"
+#include "gnu_classpath_VMSystemProperties.h"
+
+/*
+ * static native void preInit(Properties)
+ */
+void _JC_JCNI_ATTR
+JCNI_gnu_classpath_VMSystemProperties_preInit(_jc_env *env, _jc_object *props)
+{
+	static const char *const method_name = "setProperty";
+	static const char *const method_sig
+	    = "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;";
+	_jc_jvm *const vm = env->vm;
+	jobject name_ref = NULL;
+	jobject value_ref = NULL;
+	jint status = JNI_ERR;
+	_jc_method *method;
+	_jc_type *type;
+	int i;
+
+	/* Find Properties class */
+	if ((type = _jc_load_type(env, vm->boot.loader,
+	    "java/util/Properties")) == NULL)
+		goto done;
+
+	/* Sanity check */
+	_JC_ASSERT(_jc_subclass_of(props, type));
+	_JC_ASSERT(_JC_FLG_TEST(type, INITIALIZED));
+
+	/* Find Properties.setProperty() method */
+	if ((method = _jc_get_declared_method(env, type,
+	    method_name, method_sig, _JC_ACC_STATIC, 0)) == NULL) {
+		_jc_post_exception_info(env);
+		_jc_throw_exception(env);
+	}
+
+	/* Insert properties */
+	for (i = 0; i < vm->system_properties.length; i++) {
+		_jc_property *const prop = &vm->system_properties.elems[i];
+
+		/* Create Strings from name and value */
+		if ((name_ref = _jc_new_local_native_ref(env,
+		    _jc_new_string(env, prop->name,
+		      strlen(prop->name)))) == NULL)
+			goto done;
+		if ((value_ref = _jc_new_local_native_ref(env,
+		    _jc_new_string(env, prop->value,
+		      strlen(prop->value)))) == NULL)
+			goto done;
+
+		/* Invoke method */
+		if (_jc_invoke_virtual(env, method,
+		    props, *name_ref, *value_ref) != JNI_OK)
+			goto done;
+
+		/* Free local references */
+		_jc_free_local_native_ref(&name_ref);
+		_jc_free_local_native_ref(&value_ref);
+	}
+
+	/* OK */
+	status = JNI_OK;
+
+done:
+	/* Clean up and return */
+	_jc_free_local_native_ref(&name_ref);
+	_jc_free_local_native_ref(&value_ref);
+
+	/* Bail out if exception */
+	if (status != JNI_OK)
+		_jc_throw_exception(env);
+}
+

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/java_io_VMObjectStreamClass.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/java_io_VMObjectStreamClass.c?rev=294974&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/java_io_VMObjectStreamClass.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/jchevm/jchevm/libjc/native/java_io_VMObjectStreamClass.c Tue Oct  4 19:19:16 2005
@@ -0,0 +1,45 @@
+
+/*
+ * Copyright 2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ * 
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ * $Id: java_io_VMObjectStreamClass.c,v 1.4 2005/05/15 21:41:01 archiecobbs Exp $
+ */
+
+#include "libjc.h"
+#include "java_io_VMObjectStreamClass.h"
+
+/*
+ * static final native boolean hasClassInitializer(Class)
+ */
+jboolean _JC_JCNI_ATTR
+JCNI_java_io_VMObjectStreamClass_hasClassInitializer(_jc_env *env,
+	_jc_object *class)
+{
+	_jc_jvm *const vm = env->vm;
+	_jc_type *type;
+
+	/* Check for null and get type */
+	if (class == NULL) {
+		_jc_post_exception(env, _JC_NullPointerException);
+		_jc_throw_exception(env);
+	}
+	type = _jc_get_vm_pointer(class, vm->boot.fields.Class.vmdata);
+
+	/* Look for class initializer */
+	return _jc_get_declared_method(env, type, "<clinit>",
+	    "()V", _JC_ACC_STATIC, _JC_ACC_STATIC) != NULL;
+}
+



Mime
View raw message