技术标签: # Lua
准备工作:
1.在编写mm库前必须对lua c api要熟悉,否则会无从下手,这是一个痛苦的过程,还好风云大大对这个api进行的翻译,相对来说写lua c 库就轻松很多了,api翻译地址为:https://www.codingnow.com/2000/download/lua_manual.html#lua_getstack。
2.在扩展snapshot库时会用到uthash这个第三方工具,主要是使用哈希表的方式来做快照存储以及数组来存储对象引用路径的,地址为:https://troydhanson.github.io/uthash/userguide.html
mm库具有的功能:
1.快照Registry或者_G表。
2.计算Lua对象内存大小以及引用关系。
3.储存快照。
快照Registry或者_G表:lua里面的回收对象类型如下图所示:
所以我们只需提供MarkObject,MarkTable,MarkUserData,MarkFunction,MarkThread来递归统计对象即可。核心代码如下所示:
void MarkObject(lua_State *L, const char *desc, const char *parent)
{
luaL_checkstack(L, LUA_MINSTACK, NULL);
int type = lua_type(L, -1);
switch (type) {
case LUA_TTABLE:
MarkTable(L, desc, parent);
break;
case LUA_TUSERDATA:
MarkUserData(L, desc, parent);
break;
case LUA_TFUNCTION:
MarkFunction(L, desc, parent);
break;
case LUA_TTHREAD:
MarkThread(L, desc, parent);
break;
default:
lua_pop(L,1);
break;
}
}
void MarkTable(lua_State *L, const char *desc, const char *parent)
{
const void *addr = lua_topointer(L, -1);
if (addr == NULL)
{
return;
}
struct GCObject *obj = GetGCObject(addr);
if (obj != NULL)
{
AddRefPath(obj, desc, parent);
lua_pop(L, 1);
}
else
{
obj = AddGCObject(L, addr, desc, parent);
bool weakk = false;
bool weakv = false;
if (lua_getmetatable(L, -1))
{
lua_pushliteral(L, "__mode");
lua_rawget(L, -2);
if (lua_isstring(L, -1))
{
const char *mode = lua_tostring(L, -1);
if (strchr(mode, 'k'))
{
weakk = true;
}
if (strchr(mode, 'v'))
{
weakv = true;
}
}
lua_pop(L, 1);
luaL_checkstack(L, LUA_MINSTACK, NULL);
char **path = (char**)utarray_front(obj->paths);
MarkTable(L, "[metatable]", *path);
}
lua_pushnil(L);
while (lua_next(L, -2) != 0)
{
if (weakv)
{
lua_pop(L, 1);
}
else
{
char temp[128];
const char *name = KeyString(L, -2, temp);
char **path = (char**)utarray_front(obj->paths);
MarkObject(L, name, *path);
}
if (!weakk)
{
lua_pushvalue(L, -1);
char **path = (char**)utarray_front(obj->paths);
MarkObject(L, "[key]", *path);
}
}
lua_pop(L, 1);
}
}
void MarkUserData(lua_State *L, const char *desc, const char *parent)
{
const void *addr = lua_topointer(L, -1);
if (addr == NULL)
{
return;
}
struct GCObject *obj = GetGCObject(addr);
if(obj != NULL)
{
AddRefPath(obj, desc, parent);
lua_pop(L, 1);
}
else
{
obj = AddGCObject(L, addr, desc, parent);
if (lua_getmetatable(L, -1))
{
char **path = (char**)utarray_front(obj->paths);
MarkTable(L, "[metatable]", *path);
}
GetUserValue(L, -1);
if (lua_isnil(L, -1))
{
lua_pop(L, 2);
}
else
{
char **path = (char**)utarray_front(obj->paths);
MarkTable(L, "[uservalue]", *path);
lua_pop(L,1);
}
}
}
void MarkFunction(lua_State *L, const char *desc, const char *parent)
{
const void *addr = lua_topointer(L, -1);
if (addr == NULL)
{
return;
}
struct GCObject *obj = GetGCObject(addr);
if(obj != NULL)
{
AddRefPath(obj, desc, parent);
lua_pop(L, 1);
}
else
{
obj = AddGCObject(L, addr, desc, parent);
char **path = (char**)utarray_front(obj->paths);
MarkFunctionEnv(L, *path);
int i;
for (i=1;;i++)
{
const char *name = lua_getupvalue(L, -1, i);
if (name == NULL)
{
break;
}
path = (char**)utarray_front(obj->paths);
MarkObject(L, name[0] ? name : "[upvalue]", *path);
}
if (lua_iscfunction(L, -1) == 0)
{
lua_Debug ar;
lua_getinfo(L, ">S", &ar);
char temp[128];
sprintf(temp, "[%s:%d]", ar.short_src, ar.linedefined);
path = (char**)utarray_front(obj->paths);
AppendRefPath(obj, path, temp);
}
else
{
lua_pop(L, 1);
}
}
}
void MarkThread(lua_State *L, const char *desc, const char *parent)
{
const void *addr = lua_topointer(L, -1);
if (addr == NULL)
{
return;
}
struct GCObject *obj = GetGCObject(addr);
if (obj != NULL)
{
AddRefPath(obj, desc, parent);
lua_pop(L, 1);
}
else
{
obj = AddGCObject(L, addr, desc, parent);
int level = 0;
lua_State *cL = lua_tothread(L, -1);
if (cL == L)
{
level = 1;
}
else
{
// mark stack
int top = lua_gettop(cL);
luaL_checkstack(cL, 1, NULL);
int i;
char temp[16];
for (i = 0; i < top; i++)
{
lua_pushvalue(cL, i + 1);
sprintf(temp, "[thread %d]", i + 1);
MarkObject(cL, temp, parent);
}
}
lua_Debug ar;
while (lua_getstack(cL, level, &ar))
{
lua_getinfo(cL, "Sl", &ar);
char temp[128];
if (ar.currentline >= 0)
{
sprintf(temp, "[%s:%d]", ar.short_src, ar.currentline);
}
else
{
sprintf(temp, "[%s]", ar.short_src);
}
char **path = (char**)utarray_front(obj->paths);
AppendRefPath(obj, path, temp);
int i,j;
for (j = 1; j > -1; j-=2)
{
for (i = j;;i+=j)
{
const char *name = lua_getlocal(cL, &ar, i);
if (name == NULL)
{
break;
}
snprintf(temp, sizeof(temp), "%s : %s:%d", name, ar.short_src, ar.currentline);
path = (char**)utarray_front(obj->paths);
MarkObject(cL, temp, *path);
}
}
++level;
}
lua_pop(L,1);
}
}
计算Lua对象内存大小以及引用关系:由于lua并没有提供对象内存的计算方式,只能对整体内存进行统计,所以这里就做引用关系的处理。内存对象计算往后想到好的办法再补上。核心代码如下:
void AddRefPath(struct GCObject *obj, const char *desc, const char *parent)
{
UT_string *temp;
utstring_new(temp);
if (parent != NULL)
{
utstring_printf(temp, "%s.%s", parent, desc);
}
else
{
utstring_printf(temp, "%s", desc);
}
char *path = utstring_body(temp);
utarray_push_back(obj->paths, &path);
utstring_free(temp);
}
储存快照:快照存储在hash表中,并通过json串形式写入到本地文件中。核心代码如下:
void GenJsonResult(lua_State *L, const char *fileName)
{
FILE *fp = fopen(fileName, "w+");
if (fp == NULL)
{
printf("无法打开文件\n");
FreeGCHash();
return;
}
int count = lua_gc(L, LUA_GCCOUNT, 0);
UT_string *jsonStr;
utstring_new(jsonStr);
utstring_printf(jsonStr, "{count:%d, items:[", count);
unsigned int hashLen = HASH_COUNT(gcHash);
unsigned int hashIndex = 0;
// 遍历统计对象列表,填充json字符串串
struct GCObject *obj;
for (obj = gcHash; obj != NULL; obj = obj->hh.next)
{
utstring_printf(jsonStr, "{addr:\"%p\", type:\"%s\", memory:%d, refPath:\"",
obj->addr,
obj->type,
obj->memory);
unsigned int arrayLen = utarray_len(obj->paths);
unsigned int arrayIndex = 0;
char **path = NULL;
while ((path = (char**)utarray_next(obj->paths, path))) {
utstring_printf(jsonStr, "%s", *path);
if (arrayIndex != (arrayLen - 1))
{
utstring_printf(jsonStr, ",");
}
arrayIndex++;
}
utstring_printf(jsonStr, "\"}");
if (hashIndex != (hashLen - 1))
{
utstring_printf(jsonStr, ",\n");
}
hashIndex++;
}
utstring_printf(jsonStr, "]}");
fputs(utstring_body(jsonStr), fp);
fclose(fp);
utstring_free(jsonStr);
FreeGCHash();
}
mm库的测试结果:经验证mm运行正确。
测试用例如下:
测试输出如下:
文章浏览阅读1k次。通过使用ajax方法跨域请求是浏览器所不允许的,浏览器出于安全考虑是禁止的。警告信息如下:不过jQuery对跨域问题也有解决方案,使用jsonp的方式解决,方法如下:$.ajax({ async:false, url: 'http://www.mysite.com/demo.do', // 跨域URL ty..._nginx不停的xhr
文章浏览阅读2k次。关于在 Oracle 中配置 extproc 以访问 ST_Geometry,也就是我们所说的 使用空间SQL 的方法,官方文档链接如下。http://desktop.arcgis.com/zh-cn/arcmap/latest/manage-data/gdbs-in-oracle/configure-oracle-extproc.htm其实简单总结一下,主要就分为以下几个步骤。..._extproc
文章浏览阅读1.5w次。linux下没有上面的两个函数,需要使用函数 mbstowcs和wcstombsmbstowcs将多字节编码转换为宽字节编码wcstombs将宽字节编码转换为多字节编码这两个函数,转换过程中受到系统编码类型的影响,需要通过设置来设定转换前和转换后的编码类型。通过函数setlocale进行系统编码的设置。linux下输入命名locale -a查看系统支持的编码_linux c++ gbk->utf8
文章浏览阅读750次。今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到“ IMP-00009:导出文件异常结束” 错误,google一下,发现可能有如下原因导致imp的数据太大,没有写buffer和commit两个数据库字符集不同从低版本exp的dmp文件,向高版本imp导出的dmp文件出错传输dmp文件时,文件损坏解决办法:imp时指定..._imp-00009导出文件异常结束
文章浏览阅读143次。当下是一个大数据的时代,各个行业都离不开数据的支持。因此,网络爬虫就应运而生。网络爬虫当下最为火热的是Python,Python开发爬虫相对简单,而且功能库相当完善,力压众多开发语言。本次教程我们爬取前程无忧的招聘信息来分析Python程序员需要掌握那些编程技术。首先在谷歌浏览器打开前程无忧的首页,按F12打开浏览器的开发者工具。浏览器开发者工具是用于捕捉网站的请求信息,通过分析请求信息可以了解请..._初级python程序员能力要求
文章浏览阅读7.6k次,点赞2次,收藏6次。@Service标注的bean,类名:ABDemoService查看源码后发现,原来是经过一个特殊处理:当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致public class AnnotationBeanNameGenerator implements BeanNameGenerator { private static final String C..._@service beanname
文章浏览阅读6.9w次,点赞73次,收藏463次。1.前序创建#include<stdio.h>#include<string.h>#include<stdlib.h>#include<malloc.h>#include<iostream>#include<stack>#include<queue>using namespace std;typed_二叉树的建立
文章浏览阅读7.1k次。在Asp.net上使用Excel导出功能,如果文件名出现中文,便会以乱码视之。 解决方法: fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);_asp.net utf8 导出中文字符乱码
文章浏览阅读2.1k次,点赞4次,收藏23次。第一次实验 词法分析实验报告设计思想词法分析的主要任务是根据文法的词汇表以及对应约定的编码进行一定的识别,找出文件中所有的合法的单词,并给出一定的信息作为最后的结果,用于后续语法分析程序的使用;本实验针对 PL/0 语言 的文法、词汇表编写一个词法分析程序,对于每个单词根据词汇表输出: (单词种类, 单词的值) 二元对。词汇表:种别编码单词符号助记符0beginb..._对pl/0作以下修改扩充。增加单词
文章浏览阅读773次。我在使用adb.exe时遇到了麻烦.我想使用与bash相同的adb.exe shell提示符,所以我决定更改默认的bash二进制文件(当然二进制文件是交叉编译的,一切都很完美)更改bash二进制文件遵循以下顺序> adb remount> adb push bash / system / bin /> adb shell> cd / system / bin> chm..._adb shell mv 权限
文章浏览阅读6.8k次,点赞12次,收藏125次。1. 单目相机标定引言相机标定已经研究多年,标定的算法可以分为基于摄影测量的标定和自标定。其中,应用最为广泛的还是张正友标定法。这是一种简单灵活、高鲁棒性、低成本的相机标定算法。仅需要一台相机和一块平面标定板构建相机标定系统,在标定过程中,相机拍摄多个角度下(至少两个角度,推荐10~20个角度)的标定板图像(相机和标定板都可以移动),即可对相机的内外参数进行标定。下面介绍张氏标定法(以下也这么称呼)的原理。原理相机模型和单应矩阵相机标定,就是对相机的内外参数进行计算的过程,从而得到物体到图像的投影_相机-投影仪标定
文章浏览阅读2.2k次。文章目录Wayland 架构Wayland 渲染Wayland的 硬件支持简 述: 翻译一篇关于和 wayland 有关的技术文章, 其英文标题为Wayland Architecture .Wayland 架构若是想要更好的理解 Wayland 架构及其与 X (X11 or X Window System) 结构;一种很好的方法是将事件从输入设备就开始跟踪, 查看期间所有的屏幕上出现的变化。这就是我们现在对 X 的理解。 内核是从一个输入设备中获取一个事件,并通过 evdev 输入_wayland