blob: 9ef7debdbd492bbb03394db5101b75fd7158cde7 [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:
Brian Carlstrom8a436592011-08-15 21:27:23 -070024
25 typedef std::vector<std::pair<StringPiece, const void*> > Options;
26
27 class ParsedOptions {
28 public:
29 ParsedOptions(const Options& options, bool ignore_unrecognized);
30
31 std::vector<DexFile*> boot_class_path_;
32 const char* boot_image_;
33 size_t heap_initial_size_;
34 size_t heap_maximum_size_;
35 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070036
Carl Shapiro61e019d2011-07-14 16:53:09 -070037 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070038 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070039 static Runtime* Create(const std::vector<const DexFile*>& boot_class_path);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070040
41 static Runtime* Current() {
42 return instance_;
43 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070044
Carl Shapiro61e019d2011-07-14 16:53:09 -070045 // Compiles a dex file.
46 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070047
Elliott Hughesffe67362011-07-17 12:09:27 -070048 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
49 // callers should prefer.
50 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
51 // in a single function together. This reduces code size slightly, but means
52 // that the native stack trace we get may point at the wrong call site.
53 static void Abort(const char* file, int line);
54
Carl Shapiro61e019d2011-07-14 16:53:09 -070055 // Attaches the current native thread to the runtime.
Elliott Hughes40ef99e2011-08-11 17:44:34 -070056 bool AttachCurrentThread(const char* name, JNIEnv** jni_env);
57 bool AttachCurrentThreadAsDaemon(const char* name, JNIEnv** jni_env);
Carl Shapiro61e019d2011-07-14 16:53:09 -070058
59 // Detaches the current native thread from the runtime.
60 bool DetachCurrentThread();
61
62 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -070063
Carl Shapiro7a909592011-07-24 19:21:59 -070064 ClassLinker* GetClassLinker() {
65 return class_linker_;
66 }
67
Elliott Hughesf2682d52011-08-15 16:37:04 -070068 JavaVM* GetJavaVM() {
69 return java_vm_.get();
70 }
71
Carl Shapiro2ed144c2011-07-26 16:52:08 -070072 void SetVfprintfHook(void* hook);
73
74 void SetExitHook(void* hook);
75
76 void SetAbortHook(void* hook);
77
Carl Shapirob5573532011-07-12 18:22:59 -070078 private:
Elliott Hughesffe67362011-07-17 12:09:27 -070079 static void PlatformAbort(const char*, int);
80
Brian Carlstromb0460ea2011-07-29 10:08:05 -070081 Runtime() : thread_list_(NULL), class_linker_(NULL) {}
Carl Shapiro61e019d2011-07-14 16:53:09 -070082
83 // Initializes a new uninitialized runtime.
Brian Carlstrom8a436592011-08-15 21:27:23 -070084 bool Init(const Options& options, bool ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -070085
Carl Shapirob5573532011-07-12 18:22:59 -070086 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -070087
Brian Carlstromb0460ea2011-07-29 10:08:05 -070088 ClassLinker* class_linker_;
89
Elliott Hughesf2682d52011-08-15 16:37:04 -070090 scoped_ptr<JavaVM> java_vm_;
91
Carl Shapiro2ed144c2011-07-26 16:52:08 -070092 // A pointer to the active runtime or NULL.
93 static Runtime* instance_;
94
Carl Shapiro61e019d2011-07-14 16:53:09 -070095 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -070096};
97
98} // namespace art
99
Carl Shapiro1fb86202011-06-27 17:43:13 -0700100#endif // ART_SRC_RUNTIME_H_