我想将不透明的数据缓冲区从C传递给Lua,而不实际复制所有数据(因为它可能是非常大的100 MB的MB或GB)。将只读缓冲区从C传递给Lua
更具体地说,我有一个C段,看起来像这样:
uint8_t *buffer = // points to some memory
size_t size = // size of the buffer
我的C程序不知道在缓冲区中的数据结构。它完全无视它。除了调用负责使用这些数据的Lua函数之外,它绝对不会触及这些数据。
我的目标是将此缓冲区传递给Lua,然后Lua将解释数据的内容并基于此执行各种操作(Lua代码知道如何根据其内容确定数据的结构或尺寸)。此外,Lua代码不会修改缓冲区(只能从中读取)。
下面是我理想中的功能的一些示例伪代码:
bool perform_action(lua_State *L, uint8_t *buffer, size_t size) {
// Pass buffer to lua
// call "process_buffer" function (written in Lua)
// get return from "process_buffer" function (a boolean)
// free(buffer);
// return the result above
}
这个问题,我想,是关系到:lua newbie : C-Lua How to pass a struct/buffer to lua from C?。但是,该线程中的解决方案是:“使用LuaJIT”,这对我没有任何意义。
一个潜在的解决方案是使用:
lua_pushlstring (L, buffer, size);
,但不会这使得数据的副本?有没有办法做到零拷贝(因为数据非常大)?
任何帮助,将不胜感激。谢谢!
为什么不简单地写这个函数到Lua read_part_of_buffer(start_offset,length,return_this_part_as_number_instead_of_string)? –