Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_RUNTIME_H_ |
| 4 | #define ART_SRC_RUNTIME_H_ |
| 5 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame^] | 6 | #include <set> |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 7 | #include <string> |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 8 | #include <utility> |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 9 | #include <vector> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | |
| 11 | #include "globals.h" |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame^] | 12 | #include "jni_internal.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 13 | #include "macros.h" |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 14 | #include "scoped_ptr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 15 | #include "stringpiece.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 16 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 17 | namespace art { |
| 18 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 19 | class ClassLinker; |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 20 | class DexFile; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 21 | class Heap; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 22 | class String; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 23 | class ThreadList; |
| 24 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 25 | class Runtime { |
| 26 | public: |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 27 | |
| 28 | typedef std::vector<std::pair<StringPiece, const void*> > Options; |
| 29 | |
| 30 | class ParsedOptions { |
| 31 | public: |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 32 | // returns null if problem parsing and ignore_unrecognized is false |
| 33 | static ParsedOptions* Create(const Options& options, bool ignore_unrecognized); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 34 | |
| 35 | std::vector<DexFile*> boot_class_path_; |
| 36 | const char* boot_image_; |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 37 | bool check_jni_; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 38 | size_t heap_initial_size_; |
| 39 | size_t heap_maximum_size_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 40 | jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap); |
| 41 | void (*hook_exit_)(jint status); |
| 42 | void (*hook_abort_)(); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame^] | 43 | std::set<std::string> verbose_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 44 | std::vector<std::string> properties_; |
| 45 | |
| 46 | private: |
| 47 | ParsedOptions() {}; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 48 | }; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 49 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 50 | // Creates and initializes a new runtime. |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 51 | static Runtime* Create(const Options& options, bool ignore_unrecognized); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 52 | static Runtime* Create(const std::vector<const DexFile*>& boot_class_path); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 53 | |
| 54 | static Runtime* Current() { |
| 55 | return instance_; |
| 56 | } |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 57 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 58 | // Compiles a dex file. |
| 59 | static void Compile(const StringPiece& filename); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 60 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 61 | // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most |
| 62 | // callers should prefer. |
| 63 | // This isn't marked ((noreturn)) because then gcc will merge multiple calls |
| 64 | // in a single function together. This reduces code size slightly, but means |
| 65 | // that the native stack trace we get may point at the wrong call site. |
| 66 | static void Abort(const char* file, int line); |
| 67 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 68 | // Attaches the current native thread to the runtime. |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 69 | bool AttachCurrentThread(const char* name, JNIEnv** jni_env); |
| 70 | bool AttachCurrentThreadAsDaemon(const char* name, JNIEnv** jni_env); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 71 | |
| 72 | // Detaches the current native thread from the runtime. |
| 73 | bool DetachCurrentThread(); |
| 74 | |
| 75 | ~Runtime(); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 76 | |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 77 | ClassLinker* GetClassLinker() { |
| 78 | return class_linker_; |
| 79 | } |
| 80 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame^] | 81 | JavaVMExt* GetJavaVM() const { |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 82 | return java_vm_.get(); |
| 83 | } |
| 84 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 85 | private: |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 86 | static void PlatformAbort(const char*, int); |
| 87 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 88 | Runtime() : thread_list_(NULL), class_linker_(NULL) {} |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 89 | |
| 90 | // Initializes a new uninitialized runtime. |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 91 | bool Init(const Options& options, bool ignore_unrecognized); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 92 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 93 | ThreadList* thread_list_; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 94 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 95 | ClassLinker* class_linker_; |
| 96 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame^] | 97 | scoped_ptr<JavaVMExt> java_vm_; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 98 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 99 | // Hooks supported by JNI_CreateJavaVM |
| 100 | jint (*vfprintf_)(FILE* stream, const char* format, va_list ap); |
| 101 | void (*exit_)(jint status); |
| 102 | void (*abort_)(); |
| 103 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 104 | // A pointer to the active runtime or NULL. |
| 105 | static Runtime* instance_; |
| 106 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 107 | DISALLOW_COPY_AND_ASSIGN(Runtime); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace art |
| 111 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 112 | #endif // ART_SRC_RUNTIME_H_ |