blob: 0aa0cb59b13597e23c7ceab0432495fd408e9e89 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_RUNTIME_H_
4#define ART_SRC_RUNTIME_H_
5
Elliott Hughes0af55432011-08-17 18:37:28 -07006#include <set>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -07007#include <string>
Carl Shapirofc322c72011-07-27 00:20:01 -07008#include <utility>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -07009#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010
11#include "globals.h"
Elliott Hughes0af55432011-08-17 18:37:28 -070012#include "jni_internal.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "macros.h"
Elliott Hughesf2682d52011-08-15 16:37:04 -070014#include "scoped_ptr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070015#include "stringpiece.h"
Carl Shapirob5573532011-07-12 18:22:59 -070016
Carl Shapiro1fb86202011-06-27 17:43:13 -070017namespace art {
18
Carl Shapiro61e019d2011-07-14 16:53:09 -070019class ClassLinker;
Carl Shapirofc322c72011-07-27 00:20:01 -070020class DexFile;
Carl Shapiro61e019d2011-07-14 16:53:09 -070021class Heap;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070022class String;
Carl Shapiro61e019d2011-07-14 16:53:09 -070023class ThreadList;
24
Carl Shapiro1fb86202011-06-27 17:43:13 -070025class Runtime {
26 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070027
28 typedef std::vector<std::pair<StringPiece, const void*> > Options;
29
30 class ParsedOptions {
31 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070032 // returns null if problem parsing and ignore_unrecognized is false
33 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070034
35 std::vector<DexFile*> boot_class_path_;
36 const char* boot_image_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070037 bool check_jni_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070038 size_t heap_initial_size_;
39 size_t heap_maximum_size_;
Brian Carlstromf734cf52011-08-17 16:28:14 -070040 size_t stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070041 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
42 void (*hook_exit_)(jint status);
43 void (*hook_abort_)();
Elliott Hughes0af55432011-08-17 18:37:28 -070044 std::set<std::string> verbose_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070045 std::vector<std::string> properties_;
46
47 private:
48 ParsedOptions() {};
Brian Carlstrom8a436592011-08-15 21:27:23 -070049 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070050
Carl Shapiro61e019d2011-07-14 16:53:09 -070051 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070052 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070053 static Runtime* Create(const std::vector<const DexFile*>& boot_class_path);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070054
55 static Runtime* Current() {
56 return instance_;
57 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070058
Carl Shapiro61e019d2011-07-14 16:53:09 -070059 // Compiles a dex file.
60 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070061
Elliott Hughesffe67362011-07-17 12:09:27 -070062 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
63 // callers should prefer.
64 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
65 // in a single function together. This reduces code size slightly, but means
66 // that the native stack trace we get may point at the wrong call site.
67 static void Abort(const char* file, int line);
68
Carl Shapiro61e019d2011-07-14 16:53:09 -070069 // Attaches the current native thread to the runtime.
Elliott Hughes40ef99e2011-08-11 17:44:34 -070070 bool AttachCurrentThread(const char* name, JNIEnv** jni_env);
71 bool AttachCurrentThreadAsDaemon(const char* name, JNIEnv** jni_env);
Carl Shapiro61e019d2011-07-14 16:53:09 -070072
73 // Detaches the current native thread from the runtime.
74 bool DetachCurrentThread();
75
76 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -070077
Carl Shapiro7a909592011-07-24 19:21:59 -070078 ClassLinker* GetClassLinker() {
79 return class_linker_;
80 }
81
Elliott Hughes0af55432011-08-17 18:37:28 -070082 JavaVMExt* GetJavaVM() const {
Elliott Hughesf2682d52011-08-15 16:37:04 -070083 return java_vm_.get();
84 }
85
Carl Shapirob5573532011-07-12 18:22:59 -070086 private:
Elliott Hughesffe67362011-07-17 12:09:27 -070087 static void PlatformAbort(const char*, int);
88
Brian Carlstromb0460ea2011-07-29 10:08:05 -070089 Runtime() : thread_list_(NULL), class_linker_(NULL) {}
Carl Shapiro61e019d2011-07-14 16:53:09 -070090
91 // Initializes a new uninitialized runtime.
Brian Carlstrom8a436592011-08-15 21:27:23 -070092 bool Init(const Options& options, bool ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -070093
Carl Shapirob5573532011-07-12 18:22:59 -070094 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -070095
Brian Carlstromb0460ea2011-07-29 10:08:05 -070096 ClassLinker* class_linker_;
97
Elliott Hughes0af55432011-08-17 18:37:28 -070098 scoped_ptr<JavaVMExt> java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -070099
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700100 // Hooks supported by JNI_CreateJavaVM
101 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
102 void (*exit_)(jint status);
103 void (*abort_)();
104
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700105 // A pointer to the active runtime or NULL.
106 static Runtime* instance_;
107
Carl Shapiro61e019d2011-07-14 16:53:09 -0700108 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700109};
110
111} // namespace art
112
Carl Shapiro1fb86202011-06-27 17:43:13 -0700113#endif // ART_SRC_RUNTIME_H_