blob: 56a89708f0ebb45c9b095521a665aa3efa8930a4 [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 Carlstrom578bbdc2011-07-21 14:07:47 -07006#include <vector>
Carl Shapirofc322c72011-07-27 00:20:01 -07007#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008
Elliott Hughes40ef99e2011-08-11 17:44:34 -07009#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "globals.h"
11#include "macros.h"
Elliott Hughesf2682d52011-08-15 16:37:04 -070012#include "scoped_ptr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "stringpiece.h"
Carl Shapirob5573532011-07-12 18:22:59 -070014
Carl Shapiro1fb86202011-06-27 17:43:13 -070015namespace art {
16
Carl Shapiro61e019d2011-07-14 16:53:09 -070017class ClassLinker;
Carl Shapirofc322c72011-07-27 00:20:01 -070018class DexFile;
Carl Shapiro61e019d2011-07-14 16:53:09 -070019class Heap;
20class ThreadList;
21
Carl Shapiro1fb86202011-06-27 17:43:13 -070022class Runtime {
23 public:
Carl Shapirofc322c72011-07-27 00:20:01 -070024 typedef std::vector<std::pair<StringPiece, void*> > Options;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025
Carl Shapiro61e019d2011-07-14 16:53:09 -070026 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070027 static Runtime* Create(const Options& options, bool ignore_unrecognized);
28 static Runtime* Create(const std::vector<DexFile*>& boot_class_path);
29
30 static Runtime* Current() {
31 return instance_;
32 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070033
Carl Shapiro61e019d2011-07-14 16:53:09 -070034 // Compiles a dex file.
35 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070036
Elliott Hughesffe67362011-07-17 12:09:27 -070037 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
38 // callers should prefer.
39 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
40 // in a single function together. This reduces code size slightly, but means
41 // that the native stack trace we get may point at the wrong call site.
42 static void Abort(const char* file, int line);
43
Carl Shapiro61e019d2011-07-14 16:53:09 -070044 // Attaches the current native thread to the runtime.
Elliott Hughes40ef99e2011-08-11 17:44:34 -070045 bool AttachCurrentThread(const char* name, JNIEnv** jni_env);
46 bool AttachCurrentThreadAsDaemon(const char* name, JNIEnv** jni_env);
Carl Shapiro61e019d2011-07-14 16:53:09 -070047
48 // Detaches the current native thread from the runtime.
49 bool DetachCurrentThread();
50
51 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -070052
Carl Shapiro7a909592011-07-24 19:21:59 -070053 ClassLinker* GetClassLinker() {
54 return class_linker_;
55 }
56
Elliott Hughesf2682d52011-08-15 16:37:04 -070057 JavaVM* GetJavaVM() {
58 return java_vm_.get();
59 }
60
Carl Shapiro2ed144c2011-07-26 16:52:08 -070061 void SetVfprintfHook(void* hook);
62
63 void SetExitHook(void* hook);
64
65 void SetAbortHook(void* hook);
66
Carl Shapirob5573532011-07-12 18:22:59 -070067 private:
Elliott Hughesffe67362011-07-17 12:09:27 -070068 static void PlatformAbort(const char*, int);
69
Brian Carlstromb0460ea2011-07-29 10:08:05 -070070 Runtime() : thread_list_(NULL), class_linker_(NULL) {}
Carl Shapiro61e019d2011-07-14 16:53:09 -070071
72 // Initializes a new uninitialized runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070073 bool Init(const std::vector<DexFile*>& boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -070074
Carl Shapirob5573532011-07-12 18:22:59 -070075 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -070076
Brian Carlstromb0460ea2011-07-29 10:08:05 -070077 ClassLinker* class_linker_;
78
Elliott Hughesf2682d52011-08-15 16:37:04 -070079 scoped_ptr<JavaVM> java_vm_;
80
Carl Shapiro2ed144c2011-07-26 16:52:08 -070081 // A pointer to the active runtime or NULL.
82 static Runtime* instance_;
83
Carl Shapiro61e019d2011-07-14 16:53:09 -070084 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -070085};
86
87} // namespace art
88
Carl Shapiro1fb86202011-06-27 17:43:13 -070089#endif // ART_SRC_RUNTIME_H_