正确lua简单的扩展,可以加速相关C++数据。

正确lua简单的扩展,可以加速相关C++数据。

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

很早的时候,我有一件事纠结。如果,我在这里C++打开界面脚本。使用C++其中一个目标,和。我的程序有很多不同的lua虚拟机。每个虚拟机与一个关联C++对象,它是多线程,那么如何快速应利用这个好时机lua_State针来定位到对象指针呢?

曾经我没有能力读懂lua的源代码,也能够说不知道关键部分怎样操作,我当时的做法。是利用临界区和std::map来解决这个问题的。非常明显这个方式的效率非常低非常低。

如今有能力读lua源代码了。当然有更有效的解决的方法了。由于在我们利用lua的过程中。lua_State这个结构指针是要贯穿全部用到lua的地方的,那么我就行对这个结构进行扩展,让它可以保存我的数据,仅仅须要保存一个指针就可以。

lua_State这个结构,定义在 lstate.h中   (lua.h中仅仅是作者为了不让用户可以主动訪问结构成员而定义的空结构指针。各种开源脚本引擎都是这样,为了安全性。大家懂的)

以lua5.2.3为例,该结构原始定义例如以下:

struct lua_State {
  CommonHeader;
  lu_byte status;
  StkId top;  /* first free slot in the stack */
  global_State *l_G;
  CallInfo *ci;  /* call info for current function */
  const Instruction *oldpc;  /* last pc traced */
  StkId stack_last;  /* last free slot in the stack */
  StkId stack;  /* stack base */
  int stacksize;
  unsigned short nny;  /* number of non-yieldable calls in stack */
  unsigned short nCcalls;  /* number of nested C calls */
  lu_byte hookmask;
  lu_byte allowhook;
  int basehookcount;
  int hookcount;
  lua_Hook hook;
  GCObject *openupval;  /* list of open upvalues in this stack */
  GCObject *gclist;
  struct lua_longjmp *errorJmp;  /* current error recover point */
  ptrdiff_t errfunc;  /* current error handling function (stack index) */
  CallInfo base_ci;  /* CallInfo for first level (C calling Lua) */
};

那么对这个结构扩展之后例如以下:

struct lua_State {
  CommonHeader;
  lu_byte status;
  StkId top;  /* first free slot in the stack */
  global_State *l_G;
  CallInfo *ci;  /* call info for current function */
  const Instruction *oldpc;  /* last pc traced */
  StkId stack_last;  /* last free slot in the stack */
  StkId stack;  /* stack base */
  int stacksize;
  unsigned short nny;  /* number of non-yieldable calls in stack */
  unsigned short nCcalls;  /* number of nested C calls */
  lu_byte hookmask;
  lu_byte allowhook;
  int basehookcount;
  int hookcount;
  lua_Hook hook;
  GCObject *openupval;  /* list of open upvalues in this stack */
  GCObject *gclist;
  struct lua_longjmp *errorJmp;  /* current error recover point */
  ptrdiff_t errfunc;  /* current error handling function (stack index) */
  CallInfo base_ci;  /* CallInfo for first level (C calling Lua) */
  int __mydata;//这里
};

//同一时候添加两个lua接口,能够将函数接口放到lapi.c中,声明放到lua.h中就可以,或者你是发烧追求极限效率不在乎很多其它的扩展和更新的朋友,那么你能够用硬编码定位,__mydata的偏移是0x70。

LUA_API void lua_setmydata(lua_State *L, int data){
 L->__mydata = data;
}

LUA_API int lua_getmydata(lua_State *L){
 return L->__mydata;
}

这样就万事具备了,又一次编译lua,试试结果怎样:

正确lua简单的扩展,可以加速相关C++数据。

更抽象一点的做法:

正确lua简单的扩展,可以加速相关C++数据。





使用硬编码进行定位:
正确lua简单的扩展,可以加速相关C++数据。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117401.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Could not find artifact com.sun:tools:jar:1.5.0

    Could not find artifact com.sun:tools:jar:1.5.0【mavenpackage】,则依然报错,但报的是另外一个错误:[INFO]Scanningforprojects…[INFO][INFO]BuildingStruts2BlankWebapp1.0-SNAPSHOT[INFO][INFO]BUILDFAILURE[INFO][IN…

    2022年9月29日
    0
  • linux如何安装node_node 环境变量

    linux如何安装node_node 环境变量前言:linux中安装node环境步骤:第一步:官网下载node安装包,点我进入第二步:解压到个人的根目录下,也就是/home/haoxing(这是你自己的名字)/nodejs把文件夹名字改成nodejs方便使用第三步:配置环境变量1,打开终端入口2,输入命令:注意,带sudo是可编辑,不带的是只读sudovim/etc/profile3,输入你的密码4,shift+i打开编辑模式,加上以下代码,注意/home/…

    2022年9月13日
    0
  • C3P0jar包下载方法

    C3P0jar包下载方法百度C3P0下载官网进行下载或者进入下载链接:https://sourceforge.net/projects/c3p0/点击download下载即可进入官网:https://sourceforge.net/在右边搜索框搜索c3p0也能找到…

    2022年5月14日
    135
  • Python 100道基础入门练习题(附答案)

    Python 100道基础入门练习题(附答案)实例001:数字组合题目有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?程序分析遍历全部可能,把有重复的剃掉。1num=02forainrange(1,5):3forbinrange(1,5):4forcinrange(1,5):5if((a!=b)and(a!=c)and(b!=c)):6print(a,b,c)7nu

    2022年10月2日
    0
  • jquery弹窗插件dialog_jquery进度条插件

    jquery弹窗插件dialog_jquery进度条插件143行js顶部进度条最小插件-nanobar.js源码解析

    2022年4月20日
    67
  • 【python】python获取时间戳「建议收藏」

    【python】python获取时间戳「建议收藏」在阿里云物联网发送属性信息的时候,报文体为:{“id”:1630031333953,”params”:{},”version”:”1.0″,”method”:”thing.event.property.post”}其中里面的id为毫秒时间戳的信息在python里面获取时间戳可以用以下代码:importtimeprint(time.time())print(int(time.time()))#获取秒时间戳print(int(time.time()*1000))#获取毫秒时间戳pri

    2022年10月2日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号