jni_internal.h revision b465ab0e103d7760df903c1fddf4fa6b89d5d1f5
1df20fe0c097073f75f22d16e72fd3636a31d3ca1Ian Rogers// Copyright 2011 Google Inc. All Rights Reserved.
2df20fe0c097073f75f22d16e72fd3636a31d3ca1Ian Rogers
3df20fe0c097073f75f22d16e72fd3636a31d3ca1Ian Rogers#ifndef ART_SRC_JNI_INTERNAL_H_
4df20fe0c097073f75f22d16e72fd3636a31d3ca1Ian Rogers#define ART_SRC_JNI_INTERNAL_H_
5df20fe0c097073f75f22d16e72fd3636a31d3ca1Ian Rogers
6df20fe0c097073f75f22d16e72fd3636a31d3ca1Ian Rogers#include "jni.h"
7578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom
86c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes#include "indirect_reference_table.h"
9578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "macros.h"
10bbd76717c14822b68ae6d122d88610b46286272fElliott Hughes#include "reference_table.h"
11df20fe0c097073f75f22d16e72fd3636a31d3ca1Ian Rogers
120af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes#include <map>
130af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes#include <string>
140af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes
15df20fe0c097073f75f22d16e72fd3636a31d3ca1Ian Rogersnamespace art {
16df20fe0c097073f75f22d16e72fd3636a31d3ca1Ian Rogers
17814e40397fe6c8a2c645bae99f356dbddd6dbe18Elliott Hughesclass ClassLoader;
1818c0753c1659629021b4becdaa3f6ea81aecce35Elliott Hughesclass Mutex;
19f2682d5a6ce0f7de58da8fd4ec8aec200c43b92eElliott Hughesclass Runtime;
200af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughesclass SharedLibrary;
2118c0753c1659629021b4becdaa3f6ea81aecce35Elliott Hughesclass Thread;
22ea4dca856f8c19299a1858d2cc1f35b03ca0f694Carl Shapiro
2369f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughesstruct JavaVMExt : public JavaVM {
240af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes  JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni);
25de69d7f8c32be83c405bf5a9c5f15fc655f578faElliott Hughes  ~JavaVMExt();
260af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes
270af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes  /*
280af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   * Load native code from the specified absolute pathname.  Per the spec,
290af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   * if we've already loaded a library with the specified pathname, we
300af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   * return without doing anything.
310af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   *
320af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   * TODO: for better results we should canonicalize the pathname.  For fully
330af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   * correct results we should stat to get the inode and compare that.  The
340af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   * existing implementation is fine so long as everybody is using
350af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   * System.loadLibrary.
360af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   *
370af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   * The library will be associated with the specified class loader.  The JNI
380af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   * spec says we can't load the same library into more than one class loader.
390af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   *
400af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   * Returns true on success. On failure, returns false and sets *detail to a
410af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   * human-readable description of the error or NULL if no detail is
420af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   * available; ownership of the string is transferred to the caller.
430af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes   */
44814e40397fe6c8a2c645bae99f356dbddd6dbe18Elliott Hughes  bool LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, char** detail);
45bbd76717c14822b68ae6d122d88610b46286272fElliott Hughes
46f2682d5a6ce0f7de58da8fd4ec8aec200c43b92eElliott Hughes  Runtime* runtime;
47bbd76717c14822b68ae6d122d88610b46286272fElliott Hughes
48515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes  bool check_jni;
490af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes  bool verbose_jni;
50515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes
51bbd76717c14822b68ae6d122d88610b46286272fElliott Hughes  // Used to hold references to pinned primitive arrays.
52bbd76717c14822b68ae6d122d88610b46286272fElliott Hughes  ReferenceTable pin_table;
536c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
546c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  // JNI global references.
5518c0753c1659629021b4becdaa3f6ea81aecce35Elliott Hughes  Mutex* globals_lock;
566c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  IndirectReferenceTable globals;
576c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
586c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  // JNI weak global references.
5918c0753c1659629021b4becdaa3f6ea81aecce35Elliott Hughes  Mutex* weak_globals_lock;
606c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  IndirectReferenceTable weak_globals;
610af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes
620af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes  std::map<std::string, SharedLibrary*> libraries;
63f2682d5a6ce0f7de58da8fd4ec8aec200c43b92eElliott Hughes};
64f2682d5a6ce0f7de58da8fd4ec8aec200c43b92eElliott Hughes
6569f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughesstruct JNIEnvExt : public JNIEnv {
66515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes  JNIEnvExt(Thread* self, bool check_jni);
67bbd76717c14822b68ae6d122d88610b46286272fElliott Hughes
6840ef99eb9dd91c2fa549f40973964529c927bb3cElliott Hughes  Thread* self;
69ea4dca856f8c19299a1858d2cc1f35b03ca0f694Carl Shapiro
70515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes  bool check_jni;
71515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes
7240ef99eb9dd91c2fa549f40973964529c927bb3cElliott Hughes  // Are we in a "critical" JNI call?
7340ef99eb9dd91c2fa549f40973964529c927bb3cElliott Hughes  bool critical;
74ea4dca856f8c19299a1858d2cc1f35b03ca0f694Carl Shapiro
75bbd76717c14822b68ae6d122d88610b46286272fElliott Hughes  // Entered JNI monitors, for bulk exit on thread detach.
766c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  ReferenceTable  monitors;
776c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
786c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  // JNI local references.
796c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  IndirectReferenceTable locals;
80ea4dca856f8c19299a1858d2cc1f35b03ca0f694Carl Shapiro};
81ea4dca856f8c19299a1858d2cc1f35b03ca0f694Carl Shapiro
82df20fe0c097073f75f22d16e72fd3636a31d3ca1Ian Rogers}  // namespace art
83ea4dca856f8c19299a1858d2cc1f35b03ca0f694Carl Shapiro
84b465ab0e103d7760df903c1fddf4fa6b89d5d1f5Elliott Hughesstd::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs);
85b465ab0e103d7760df903c1fddf4fa6b89d5d1f5Elliott Hughes
86df20fe0c097073f75f22d16e72fd3636a31d3ca1Ian Rogers#endif  // ART_SRC_JNI_INTERNAL_H_
87