blob: da46ee6b5b621ea7a5dc2e684ce4888cc1ef9cf0 [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
Brian Carlstrom6ea095a2011-08-16 15:26:54 -07006#include <string>
Carl Shapirofc322c72011-07-27 00:20:01 -07007#include <utility>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -07008#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009
Elliott Hughes40ef99e2011-08-11 17:44:34 -070010#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "globals.h"
12#include "macros.h"
Elliott Hughesf2682d52011-08-15 16:37:04 -070013#include "scoped_ptr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "stringpiece.h"
Carl Shapirob5573532011-07-12 18:22:59 -070015
Carl Shapiro1fb86202011-06-27 17:43:13 -070016namespace art {
17
Carl Shapiro61e019d2011-07-14 16:53:09 -070018class ClassLinker;
Carl Shapirofc322c72011-07-27 00:20:01 -070019class DexFile;
Carl Shapiro61e019d2011-07-14 16:53:09 -070020class Heap;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070021class String;
Carl Shapiro61e019d2011-07-14 16:53:09 -070022class ThreadList;
23
Carl Shapiro1fb86202011-06-27 17:43:13 -070024class Runtime {
25 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070026
27 typedef std::vector<std::pair<StringPiece, const void*> > Options;
28
29 class ParsedOptions {
30 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070031 // returns null if problem parsing and ignore_unrecognized is false
32 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070033
34 std::vector<DexFile*> boot_class_path_;
35 const char* boot_image_;
36 size_t heap_initial_size_;
37 size_t heap_maximum_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070038 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
39 void (*hook_exit_)(jint status);
40 void (*hook_abort_)();
41 std::vector<std::string> verbose_;
42 std::vector<std::string> properties_;
43
44 private:
45 ParsedOptions() {};
Brian Carlstrom8a436592011-08-15 21:27:23 -070046 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070047
Carl Shapiro61e019d2011-07-14 16:53:09 -070048 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070049 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070050 static Runtime* Create(const std::vector<const DexFile*>& boot_class_path);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070051
52 static Runtime* Current() {
53 return instance_;
54 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070055
Carl Shapiro61e019d2011-07-14 16:53:09 -070056 // Compiles a dex file.
57 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070058
Elliott Hughesffe67362011-07-17 12:09:27 -070059 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
60 // callers should prefer.
61 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
62 // in a single function together. This reduces code size slightly, but means
63 // that the native stack trace we get may point at the wrong call site.
64 static void Abort(const char* file, int line);
65
Carl Shapiro61e019d2011-07-14 16:53:09 -070066 // Attaches the current native thread to the runtime.
Elliott Hughes40ef99e2011-08-11 17:44:34 -070067 bool AttachCurrentThread(const char* name, JNIEnv** jni_env);
68 bool AttachCurrentThreadAsDaemon(const char* name, JNIEnv** jni_env);
Carl Shapiro61e019d2011-07-14 16:53:09 -070069
70 // Detaches the current native thread from the runtime.
71 bool DetachCurrentThread();
72
73 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -070074
Carl Shapiro7a909592011-07-24 19:21:59 -070075 ClassLinker* GetClassLinker() {
76 return class_linker_;
77 }
78
Elliott Hughesf2682d52011-08-15 16:37:04 -070079 JavaVM* GetJavaVM() {
80 return java_vm_.get();
81 }
82
Carl Shapirob5573532011-07-12 18:22:59 -070083 private:
Elliott Hughesffe67362011-07-17 12:09:27 -070084 static void PlatformAbort(const char*, int);
85
Brian Carlstromb0460ea2011-07-29 10:08:05 -070086 Runtime() : thread_list_(NULL), class_linker_(NULL) {}
Carl Shapiro61e019d2011-07-14 16:53:09 -070087
88 // Initializes a new uninitialized runtime.
Brian Carlstrom8a436592011-08-15 21:27:23 -070089 bool Init(const Options& options, bool ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -070090
Carl Shapirob5573532011-07-12 18:22:59 -070091 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -070092
Brian Carlstromb0460ea2011-07-29 10:08:05 -070093 ClassLinker* class_linker_;
94
Elliott Hughesf2682d52011-08-15 16:37:04 -070095 scoped_ptr<JavaVM> java_vm_;
96
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070097 // Hooks supported by JNI_CreateJavaVM
98 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
99 void (*exit_)(jint status);
100 void (*abort_)();
101
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700102 // A pointer to the active runtime or NULL.
103 static Runtime* instance_;
104
Carl Shapiro61e019d2011-07-14 16:53:09 -0700105 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700106};
107
108} // namespace art
109
Carl Shapiro1fb86202011-06-27 17:43:13 -0700110#endif // ART_SRC_RUNTIME_H_