blob: 323c6c8e7c902bfa2959147156d0f7d918fa0f34 [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 Hughesc5f7c912011-08-18 14:00:42 -07006#include <stdio.h>
7
Brian Carlstrom6ea095a2011-08-16 15:26:54 -07008#include <string>
Carl Shapirofc322c72011-07-27 00:20:01 -07009#include <utility>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070010#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011
Elliott Hughesc5f7c912011-08-18 14:00:42 -070012#include <jni.h>
13
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "globals.h"
15#include "macros.h"
Elliott Hughesf2682d52011-08-15 16:37:04 -070016#include "scoped_ptr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "stringpiece.h"
Brian Carlstrom44753c32011-08-17 22:22:11 -070018#include "unordered_set.h"
Carl Shapirob5573532011-07-12 18:22:59 -070019
Carl Shapiro1fb86202011-06-27 17:43:13 -070020namespace art {
21
Carl Shapiro61e019d2011-07-14 16:53:09 -070022class ClassLinker;
Carl Shapirofc322c72011-07-27 00:20:01 -070023class DexFile;
Carl Shapiro61e019d2011-07-14 16:53:09 -070024class Heap;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070025class JavaVMExt;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070026class String;
Carl Shapiro61e019d2011-07-14 16:53:09 -070027class ThreadList;
28
Carl Shapiro1fb86202011-06-27 17:43:13 -070029class Runtime {
30 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070031
32 typedef std::vector<std::pair<StringPiece, const void*> > Options;
33
34 class ParsedOptions {
35 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070036 // returns null if problem parsing and ignore_unrecognized is false
37 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070038
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070039 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070040 const char* boot_image_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070041 bool check_jni_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070042 size_t heap_initial_size_;
43 size_t heap_maximum_size_;
Brian Carlstromf734cf52011-08-17 16:28:14 -070044 size_t stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070045 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
46 void (*hook_exit_)(jint status);
47 void (*hook_abort_)();
Brian Carlstrom44753c32011-08-17 22:22:11 -070048 std::tr1::unordered_set<std::string> verbose_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070049 std::vector<std::string> properties_;
50
51 private:
52 ParsedOptions() {};
Brian Carlstrom8a436592011-08-15 21:27:23 -070053 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070054
Carl Shapiro61e019d2011-07-14 16:53:09 -070055 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070056 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070057 static Runtime* Create(const std::vector<const DexFile*>& boot_class_path);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070058
59 static Runtime* Current() {
60 return instance_;
61 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070062
Carl Shapiro61e019d2011-07-14 16:53:09 -070063 // Compiles a dex file.
64 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070065
Elliott Hughesffe67362011-07-17 12:09:27 -070066 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
67 // callers should prefer.
68 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
69 // in a single function together. This reduces code size slightly, but means
70 // that the native stack trace we get may point at the wrong call site.
71 static void Abort(const char* file, int line);
72
Carl Shapiro61e019d2011-07-14 16:53:09 -070073 // Attaches the current native thread to the runtime.
Elliott Hughes75770752011-08-24 17:52:38 -070074 bool AttachCurrentThread(const char* name, JNIEnv** jni_env, bool as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -070075
76 // Detaches the current native thread from the runtime.
77 bool DetachCurrentThread();
78
79 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -070080
Brian Carlstromb765be02011-08-17 23:54:10 -070081 size_t GetStackSize() const {
82 return stack_size_;
83 }
84
85 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -070086 return class_linker_;
87 }
88
Elliott Hughes0af55432011-08-17 18:37:28 -070089 JavaVMExt* GetJavaVM() const {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070090 return java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -070091 }
92
Carl Shapirob5573532011-07-12 18:22:59 -070093 private:
Elliott Hughesffe67362011-07-17 12:09:27 -070094 static void PlatformAbort(const char*, int);
95
Brian Carlstromb765be02011-08-17 23:54:10 -070096 Runtime() : stack_size_(0), thread_list_(NULL), class_linker_(NULL) {}
Carl Shapiro61e019d2011-07-14 16:53:09 -070097
98 // Initializes a new uninitialized runtime.
Brian Carlstrom8a436592011-08-15 21:27:23 -070099 bool Init(const Options& options, bool ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700100
Brian Carlstromb765be02011-08-17 23:54:10 -0700101 // The default stack size for managed threads created by the runtime.
102 size_t stack_size_;
103
Carl Shapirob5573532011-07-12 18:22:59 -0700104 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700105
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700106 ClassLinker* class_linker_;
107
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700108 JavaVMExt* java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700109
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700110 // Hooks supported by JNI_CreateJavaVM
111 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
112 void (*exit_)(jint status);
113 void (*abort_)();
114
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700115 // A pointer to the active runtime or NULL.
116 static Runtime* instance_;
117
Carl Shapiro61e019d2011-07-14 16:53:09 -0700118 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700119};
120
121} // namespace art
122
Carl Shapiro1fb86202011-06-27 17:43:13 -0700123#endif // ART_SRC_RUNTIME_H_