blob: 828e677feb266a9e7b188f31a66e0d4f9bf08a08 [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_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070036 bool check_jni_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070037 size_t heap_initial_size_;
38 size_t heap_maximum_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070039 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
40 void (*hook_exit_)(jint status);
41 void (*hook_abort_)();
42 std::vector<std::string> verbose_;
43 std::vector<std::string> properties_;
44
45 private:
46 ParsedOptions() {};
Brian Carlstrom8a436592011-08-15 21:27:23 -070047 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070048
Carl Shapiro61e019d2011-07-14 16:53:09 -070049 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070050 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070051 static Runtime* Create(const std::vector<const DexFile*>& boot_class_path);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070052
53 static Runtime* Current() {
54 return instance_;
55 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070056
Carl Shapiro61e019d2011-07-14 16:53:09 -070057 // Compiles a dex file.
58 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070059
Elliott Hughesffe67362011-07-17 12:09:27 -070060 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
61 // callers should prefer.
62 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
63 // in a single function together. This reduces code size slightly, but means
64 // that the native stack trace we get may point at the wrong call site.
65 static void Abort(const char* file, int line);
66
Carl Shapiro61e019d2011-07-14 16:53:09 -070067 // Attaches the current native thread to the runtime.
Elliott Hughes40ef99e2011-08-11 17:44:34 -070068 bool AttachCurrentThread(const char* name, JNIEnv** jni_env);
69 bool AttachCurrentThreadAsDaemon(const char* name, JNIEnv** jni_env);
Carl Shapiro61e019d2011-07-14 16:53:09 -070070
71 // Detaches the current native thread from the runtime.
72 bool DetachCurrentThread();
73
74 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -070075
Carl Shapiro7a909592011-07-24 19:21:59 -070076 ClassLinker* GetClassLinker() {
77 return class_linker_;
78 }
79
Elliott Hughes515a5bc2011-08-17 11:08:34 -070080 JavaVM* GetJavaVM() const {
Elliott Hughesf2682d52011-08-15 16:37:04 -070081 return java_vm_.get();
82 }
83
Carl Shapirob5573532011-07-12 18:22:59 -070084 private:
Elliott Hughesffe67362011-07-17 12:09:27 -070085 static void PlatformAbort(const char*, int);
86
Brian Carlstromb0460ea2011-07-29 10:08:05 -070087 Runtime() : thread_list_(NULL), class_linker_(NULL) {}
Carl Shapiro61e019d2011-07-14 16:53:09 -070088
89 // Initializes a new uninitialized runtime.
Brian Carlstrom8a436592011-08-15 21:27:23 -070090 bool Init(const Options& options, bool ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -070091
Carl Shapirob5573532011-07-12 18:22:59 -070092 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -070093
Brian Carlstromb0460ea2011-07-29 10:08:05 -070094 ClassLinker* class_linker_;
95
Elliott Hughesf2682d52011-08-15 16:37:04 -070096 scoped_ptr<JavaVM> java_vm_;
97
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070098 // Hooks supported by JNI_CreateJavaVM
99 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
100 void (*exit_)(jint status);
101 void (*abort_)();
102
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700103 // A pointer to the active runtime or NULL.
104 static Runtime* instance_;
105
Carl Shapiro61e019d2011-07-14 16:53:09 -0700106 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700107};
108
109} // namespace art
110
Carl Shapiro1fb86202011-06-27 17:43:13 -0700111#endif // ART_SRC_RUNTIME_H_