码迷,mamicode.com
首页 > 其他好文 > 详细

JniHelper

时间:2014-08-08 15:49:16      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   java   io   for   ar   

using System;
using System.Runtime.CompilerServices;
using UnityEngine;

internal static class JniHelper
{
    public static void CallStaticSafe(this AndroidJavaClass clazz, string methodName, params object[] args)
    {
        PushLocalFrame();
        try
        {
            clazz.CallStatic(methodName, args);
        }
        finally
        {
            PopLocalFrame();
        }
    }

    public static void CallStaticSafe(this AndroidJavaObject jo, string methodName, params object[] args)
    {
        PushLocalFrame();
        try
        {
            jo.CallStatic(methodName, args);
        }
        finally
        {
            PopLocalFrame();
        }
    }

    private static bool DoesHaveLocalReferenceCount(int count)
    {
        if (AndroidJNI.PushLocalFrame(count) == 0)
        {
            AndroidJNI.PopLocalFrame(IntPtr.Zero);
            return true;
        }
        AndroidJNI.ExceptionClear();
        return false;
    }

    public static int GetFreeLocalReferenceCount()
    {
        int num2 = 0;
        int count = 1;
        while (DoesHaveLocalReferenceCount(count) && (count < 0x40000000))
        {
            count *= 2;
        }
        while (count > 0)
        {
            int num4 = num2 + count;
            if (DoesHaveLocalReferenceCount(num4))
            {
                num2 = num4;
            }
            count /= 2;
        }
        return num2;
    }

    public static void PopLocalFrame()
    {
        AndroidJNI.PopLocalFrame(IntPtr.Zero);
    }

    public static IntPtr PopLocalFrame(IntPtr result)
    {
        return AndroidJNI.PopLocalFrame(result);
    }

    public static void PushLocalFrame()
    {
        PushLocalFrame(0x80);
    }

    public static void PushLocalFrame(int capacity)
    {
        if (AndroidJNI.PushLocalFrame(capacity) != 0)
        {
            AndroidJNI.ExceptionClear();
            if (AndroidJNI.PushLocalFrame(0) != 0)
            {
                throw new InsufficientMemoryException(string.Format("Failed to allocate memory for {0} local JNI references", capacity));
            }
        }
    }
}

 

JniHelper,布布扣,bubuko.com

JniHelper

标签:android   style   blog   color   java   io   for   ar   

原文地址:http://www.cnblogs.com/123ing/p/3899317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!