blob: 4a5063cf83dbb8c2c9d851cf5244b5be2db785f5 [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 Hughes0af55432011-08-17 18:37:28 -07006#include <set>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -07007#include <string>
Carl Shapirofc322c72011-07-27 00:20:01 -07008#include <utility>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -07009#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010
11#include "globals.h"
Elliott Hughes0af55432011-08-17 18:37:28 -070012#include "jni_internal.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "macros.h"
Elliott Hughesf2682d52011-08-15 16:37:04 -070014#include "scoped_ptr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070015#include "stringpiece.h"
Carl Shapirob5573532011-07-12 18:22:59 -070016
Carl Shapiro1fb86202011-06-27 17:43:13 -070017namespace art {
18
Carl Shapiro61e019d2011-07-14 16:53:09 -070019class ClassLinker;
Carl Shapirofc322c72011-07-27 00:20:01 -070020class DexFile;
Carl Shapiro61e019d2011-07-14 16:53:09 -070021class Heap;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070022class String;
Carl Shapiro61e019d2011-07-14 16:53:09 -070023class ThreadList;
24
Carl Shapiro1fb86202011-06-27 17:43:13 -070025class Runtime {
26 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070027
28 typedef std::vector<std::pair<StringPiece, const void*> > Options;
29
30 class ParsedOptions {
31 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070032 // returns null if problem parsing and ignore_unrecognized is false
33 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070034
35 std::vector<DexFile*> boot_class_path_;
36 const char* boot_image_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070037 bool check_jni_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070038 size_t heap_initial_size_;
39 size_t heap_maximum_size_;
Brian Carlstromf734cf52011-08-17 16:28:14 -070040 size_t stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070041 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
42 void (*hook_exit_)(jint status);
43 void (*hook_abort_)();
Elliott Hughes0af55432011-08-17 18:37:28 -070044 std::set<std::string> verbose_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070045 std::vector<std::string> properties_;
46
47 private:
48 ParsedOptions() {};
Brian Carlstrom8a436592011-08-15 21:27:23 -070049 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070050
Carl Shapiro61e019d2011-07-14 16:53:09 -070051 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070052 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070053 static Runtime* Create(const std::vector<const DexFile*>& boot_class_path);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070054
55 static Runtime* Current() {
56 return instance_;
57 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070058
Carl Shapiro61e019d2011-07-14 16:53:09 -070059 // Compiles a dex file.
60 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070061
Elliott Hughesffe67362011-07-17 12:09:27 -070062 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
63 // callers should prefer.
64 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
65 // in a single function together. This reduces code size slightly, but means
66 // that the native stack trace we get may point at the wrong call site.
67 static void Abort(const char* file, int line);
68
Carl Shapiro61e019d2011-07-14 16:53:09 -070069 // Attaches the current native thread to the runtime.
Elliott Hughes40ef99e2011-08-11 17:44:34 -070070 bool AttachCurrentThread(const char* name, JNIEnv** jni_env);
71 bool AttachCurrentThreadAsDaemon(const char* name, JNIEnv** jni_env);
Carl Shapiro61e019d2011-07-14 16:53:09 -070072
73 // Detaches the current native thread from the runtime.
74 bool DetachCurrentThread();
75
76 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -070077
Brian Carlstromb765be02011-08-17 23:54:10 -070078 size_t GetStackSize() const {
79 return stack_size_;
80 }
81
82 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -070083 return class_linker_;
84 }
85
Elliott Hughes0af55432011-08-17 18:37:28 -070086 JavaVMExt* GetJavaVM() const {
Elliott Hughesf2682d52011-08-15 16:37:04 -070087 return java_vm_.get();
88 }
89
Carl Shapirob5573532011-07-12 18:22:59 -070090 private:
Elliott Hughesffe67362011-07-17 12:09:27 -070091 static void PlatformAbort(const char*, int);
92
Brian Carlstromb765be02011-08-17 23:54:10 -070093 Runtime() : stack_size_(0), thread_list_(NULL), class_linker_(NULL) {}
Carl Shapiro61e019d2011-07-14 16:53:09 -070094
95 // Initializes a new uninitialized runtime.
Brian Carlstrom8a436592011-08-15 21:27:23 -070096 bool Init(const Options& options, bool ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -070097
Brian Carlstromb765be02011-08-17 23:54:10 -070098 // The default stack size for managed threads created by the runtime.
99 size_t stack_size_;
100
Carl Shapirob5573532011-07-12 18:22:59 -0700101 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700102
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700103 ClassLinker* class_linker_;
104
Elliott Hughes0af55432011-08-17 18:37:28 -0700105 scoped_ptr<JavaVMExt> java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700106
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700107 // Hooks supported by JNI_CreateJavaVM
108 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
109 void (*exit_)(jint status);
110 void (*abort_)();
111
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700112 // A pointer to the active runtime or NULL.
113 static Runtime* instance_;
114
Carl Shapiro61e019d2011-07-14 16:53:09 -0700115 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700116};
117
118} // namespace art
119
Carl Shapiro1fb86202011-06-27 17:43:13 -0700120#endif // ART_SRC_RUNTIME_H_