-
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new libjnf, which is the JavaNativeFoundation code from Apple.
- Loading branch information
Showing
39 changed files
with
3,832 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright (c) 2008-2020 Apple Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* -- | ||
* | ||
* Assertions used by the JNF_COCOA_ENTER()/JNF_COCOA_EXIT() and class | ||
* caching macros. When building debug builds, improper use of the caching | ||
* macros will trigger warnings output to the console. | ||
*/ | ||
|
||
#import <JavaNativeFoundation/JNFJNI.h> | ||
|
||
#ifdef DEBUG | ||
#define JAVA_ASSERTIONS_ON | ||
#endif /* DEBUG */ | ||
|
||
// Use the WARN macro to send a message to stderr in the | ||
// debug build. It gets removed from the optimized build | ||
// during preprocessing. | ||
#ifdef DEBUG | ||
#define JNF_WARN JNFDebugWarning | ||
#else | ||
#define JNF_WARN if (0) JNFDebugWarning | ||
#endif /* DEBUG */ | ||
|
||
__BEGIN_DECLS | ||
|
||
JNF_EXPORT extern void JNFDebugWarning(const char *fmt, ...); | ||
|
||
JNF_EXPORT extern void JNFAssertionFailure(const char *file, int line, const char *condition, const char *msg); | ||
|
||
#ifdef JAVA_ASSERTIONS_ON | ||
|
||
#define JNF_ASSERT_FAILURE(condition, msg) \ | ||
JNFAssertionFailure(__FILE__, __LINE__, condition, msg) \ | ||
|
||
|
||
#define JNF_ASSERT_MSG(condition, msg) \ | ||
do { \ | ||
if (!(condition)) { \ | ||
JNF_ASSERT_FAILURE(#condition, msg); \ | ||
} \ | ||
} while(0) \ | ||
|
||
|
||
#define JNF_ASSERT_COND(condition) \ | ||
JNF_ASSERT_MSG(condition, NULL) \ | ||
|
||
|
||
#define JNF_EXCEPTION_WARN(env, msg) \ | ||
do { \ | ||
(*(env))->ExceptionDescribe(env); \ | ||
JNF_ASSERT_FAILURE("Java exception thrown", msg); \ | ||
} while (0) \ | ||
|
||
|
||
#define JNF_ASSERT_NO_EXCEPTION_MSG(env, msg) \ | ||
if ((*(env))->ExceptionOccurred(env)) { \ | ||
JNF_EXCEPTION_WARN(env, msg); \ | ||
} \ | ||
|
||
|
||
#define JNF_ASSERT_NO_EXCEPTION(env) \ | ||
JNF_ASSERT_NO_EXCEPTION_MSG(env, NULL) \ | ||
|
||
#else | ||
|
||
#define JNF_ASSERT_COND(condition) | ||
#define JNF_ASSERT_MSG(condition, msg) | ||
#define JNF_EXCEPTION_WARN(env, msg) | ||
#define JNF_ASSERT_NO_EXCEPTION(env) | ||
#define JNF_ASSERT_NO_EXCEPTION_MSG(env, msg) | ||
|
||
#endif /* JAVA_ASSERTIONS_ON */ | ||
|
||
JNF_EXPORT extern void JNFDumpJavaStack(JNIEnv *env); | ||
|
||
__END_DECLS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (c) 2008-2020 Apple Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#import "JNFJNI.h" | ||
#import "JNFAssert.h" | ||
|
||
#import "debug.h" | ||
|
||
static void JNFDebugMessageV(const char *fmt, va_list args) { | ||
// Prints a message and breaks into debugger. | ||
fprintf(stderr, "JavaNativeFoundation: "); | ||
vfprintf(stderr, fmt, args); | ||
fprintf(stderr, "\n"); | ||
} | ||
|
||
static void JNFDebugMessage(const char *fmt, ...) { | ||
// Takes printf args and then calls DebugBreak | ||
va_list args; | ||
va_start(args, fmt); | ||
JNFDebugMessageV(fmt, args); | ||
va_end(args); | ||
} | ||
|
||
void JNFDebugWarning(const char *fmt, ...) { | ||
// Takes printf args and then calls DebugBreak | ||
va_list args; | ||
va_start(args, fmt); | ||
JNFDebugMessageV(fmt, args); | ||
va_end(args); | ||
} | ||
|
||
void JNFAssertionFailure(const char *file, int line, const char *condition, const char *msg) { | ||
JNFDebugMessage("Assertion failure: %s", condition); | ||
if (msg) JNFDebugMessage(msg); | ||
JNFDebugMessage("File %s; Line %d", file, line); | ||
} | ||
|
||
void JNFDumpJavaStack(JNIEnv *env) { | ||
static JNF_CLASS_CACHE(jc_Thread, "java/lang/Thread"); | ||
static JNF_STATIC_MEMBER_CACHE(jsm_Thread_dumpStack, jc_Thread, "dumpStack", "()V"); | ||
JNFCallVoidMethod(env, jc_Thread.cls, jsm_Thread_dumpStack); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2008-2020 Apple Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* -- | ||
* | ||
* Utility class used by the JNF_COCOA_ENTER()/JNF_COCOA_EXIT() macros | ||
* from JNFJNI.h. Do not use this class or releated functions directly. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import <JavaNativeFoundation/JNFJNI.h> | ||
|
||
__BEGIN_DECLS | ||
|
||
typedef void JNFAutoreleasePoolToken; | ||
|
||
// JNFNativeMethodEnter - called on entry to each native method by the | ||
// JNF_COCOA_ENTER(env) macro in JNFJNI.h | ||
JNF_EXPORT extern JNFAutoreleasePoolToken *JNFNativeMethodEnter(void); | ||
|
||
// JNFNativeMethodExit - called on exit from each native method by the | ||
// JNF_COCOA_EXIT(env) macro in JNFJNI.h | ||
JNF_EXPORT extern void JNFNativeMethodExit(JNFAutoreleasePoolToken *token); | ||
|
||
__END_DECLS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (c) 2008-2020 Apple Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* -- | ||
* | ||
* The JNFAutoreleasePool manages setting up and tearing down autorelease | ||
* pools for Java calls into the Cocoa frameworks. | ||
* | ||
* The external entry point into this machinery is JNFMethodEnter() and JNFMethodExit(). | ||
*/ | ||
|
||
#import "JNFAutoreleasePool.h" | ||
|
||
#import <mach/mach_time.h> | ||
|
||
// These are vended by the Objective-C runtime, but they are unfortunately | ||
// not available as API in the macOS SDK. We are following suit with swift | ||
// and clang in declaring them inline here. They canot be removed or changed | ||
// in the OS without major bincompat ramifications. | ||
// | ||
// These were added in macOS 10.7. | ||
void * _Nonnull objc_autoreleasePoolPush(void); | ||
void objc_autoreleasePoolPop(void * _Nonnull context); | ||
|
||
#if TIMED | ||
static int64_t elapsedTime = 0; | ||
#endif | ||
|
||
#pragma mark - | ||
#pragma mark External API | ||
|
||
// JNFNativeMethodEnter - called on entry to each native method | ||
// | ||
// It sets up an autorelease pool, and will return a token if | ||
// JNFNativeMethodExit should be called. It attempts to consider | ||
// how much time has elapsed since the last autorelease pop. | ||
|
||
JNFAutoreleasePoolToken *JNFNativeMethodEnter() { | ||
#if TIMED | ||
int64_t start = mach_absolute_time(); | ||
#endif | ||
|
||
JNFAutoreleasePoolToken * const tokenToReturn = objc_autoreleasePoolPush(); | ||
|
||
#if TIMED | ||
elapsedTime += (mach_absolute_time() - start); | ||
#endif | ||
|
||
return tokenToReturn; | ||
} | ||
|
||
|
||
// JNFNativeMethodExit - called on exit from native methods | ||
// | ||
// This method is only called on exit from the first | ||
// native method to appear in the execution stack. | ||
// This function does not need to be called on exit | ||
// from the inner native methods (as an optimization). | ||
// JNFNativeMethodEnter sets the token to non-nil if | ||
// JNFNativeMethodExit needs to be called on exit. | ||
|
||
void JNFNativeMethodExit(JNFAutoreleasePoolToken *token) { | ||
|
||
#if TIMED | ||
int64_t start = mach_absolute_time(); | ||
#endif | ||
|
||
objc_autoreleasePoolPop(token); | ||
|
||
#if TIMED | ||
elapsedTime += (mach_absolute_time() - start); | ||
|
||
NSLog(@"elapsedTime: %llu", elapsedTime); | ||
// elapsedTime = 0; | ||
#endif | ||
} |
Oops, something went wrong.