blob: 11c6f78fcf0088945f62cc9e3a978c26d61943f1 [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 Carlstrom6ea095a2011-08-16 15:26:54 -070040 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
41 void (*hook_exit_)(jint status);
42 void (*hook_abort_)();
Elliott Hughes0af55432011-08-17 18:37:28 -070043 std::set<std::string> verbose_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070044 std::vector<std::string> properties_;
45
46 private:
47 ParsedOptions() {};
Brian Carlstrom8a436592011-08-15 21:27:23 -070048 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070049
Carl Shapiro61e019d2011-07-14 16:53:09 -070050 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070051 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070052 static Runtime* Create(const std::vector<const DexFile*>& boot_class_path);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070053
54 static Runtime* Current() {
55 return instance_;
56 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070057
Carl Shapiro61e019d2011-07-14 16:53:09 -070058 // Compiles a dex file.
59 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070060
Elliott Hughesffe67362011-07-17 12:09:27 -070061 // 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 Shapiro61e019d2011-07-14 16:53:09 -070068 // Attaches the current native thread to the runtime.
Elliott Hughes40ef99e2011-08-11 17:44:34 -070069 bool AttachCurrentThread(const char* name, JNIEnv** jni_env);
70 bool AttachCurrentThreadAsDaemon(const char* name, JNIEnv** jni_env);
Carl Shapiro61e019d2011-07-14 16:53:09 -070071
72 // Detaches the current native thread from the runtime.
73 bool DetachCurrentThread();
74
75 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -070076
Carl Shapiro7a909592011-07-24 19:21:59 -070077 ClassLinker* GetClassLinker() {
78 return class_linker_;
79 }
80
Elliott Hughes0af55432011-08-17 18:37:28 -070081 JavaVMExt* GetJavaVM() const {
Elliott Hughesf2682d52011-08-15 16:37:04 -070082 return java_vm_.get();
83 }
84
Carl Shapirob5573532011-07-12 18:22:59 -070085 private:
Elliott Hughesffe67362011-07-17 12:09:27 -070086 static void PlatformAbort(const char*, int);
87
Brian Carlstromb0460ea2011-07-29 10:08:05 -070088 Runtime() : thread_list_(NULL), class_linker_(NULL) {}
Carl Shapiro61e019d2011-07-14 16:53:09 -070089
90 // Initializes a new uninitialized runtime.
Brian Carlstrom8a436592011-08-15 21:27:23 -070091 bool Init(const Options& options, bool ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -070092
Carl Shapirob5573532011-07-12 18:22:59 -070093 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -070094
Brian Carlstromb0460ea2011-07-29 10:08:05 -070095 ClassLinker* class_linker_;
96
Elliott Hughes0af55432011-08-17 18:37:28 -070097 scoped_ptr<JavaVMExt> java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -070098
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070099 // 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 Shapiro2ed144c2011-07-26 16:52:08 -0700104 // A pointer to the active runtime or NULL.
105 static Runtime* instance_;
106
Carl Shapiro61e019d2011-07-14 16:53:09 -0700107 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700108};
109
110} // namespace art
111
Carl Shapiro1fb86202011-06-27 17:43:13 -0700112#endif // ART_SRC_RUNTIME_H_