luajit struct

2023-11-18

This page is intended to give you an overview of the features of the FFI library by presenting a few use cases and guidelines.

This page makes no attempt to explain all of the FFI library, though. You'll want to have a look at the ffi.* API function reference and the FFI semantics to learn more.

Loading the FFI Library

The FFI library is built into LuaJIT by default, but it's not loaded and initialized by default. The suggested way to use the FFI library is to add the following to the start of every Lua file that needs one of its functions:

local ffi = require("ffi")

Please note this doesn't define an ffi variable in the table of globals — you really need to use the local variable. The require function ensures the library is only loaded once.

Note: If you want to experiment with the FFI from the interactive prompt of the command line executable, omit the local, as it doesn't preserve local variables across lines.

Accessing Standard System Functions

The following code explains how to access standard system functions. We slowly print two lines of dots by sleeping for 10 milliseconds after each dot:

 
①





②
③
④



⑤





⑥local ffi = require("ffi")
ffi.cdef[[
void Sleep(int ms);
int poll(struct pollfd *fds, unsigned long nfds, int timeout);
]]

local sleep
if ffi.os == "Windows" then
  function sleep(s)
    ffi.C.Sleep(s*1000)
  end
else
  function sleep(s)
    ffi.C.poll(nil, 0, s*1000)
  end
end

for i=1,160 do
  io.write("."); io.flush()
  sleep(0.01)
end
io.write("\n")

Here's the step-by-step explanation:

 This defines the C library functions we're going to use. The part inside the double-brackets (in green) is just standard C syntax. You can usually get this info from the C header files or the documentation provided by each C library or C compiler.

 The difficulty we're facing here, is that there are different standards to choose from. Windows has a simple Sleep() function. On other systems there are a variety of functions available to achieve sub-second sleeps, but with no clear consensus. Thankfully poll() can be used for this task, too, and it's present on most non-Windows systems. The check for ffi.os makes sure we use the Windows-specific function only on Windows systems.

 Here we're wrapping the call to the C function in a Lua function. This isn't strictly necessary, but it's helpful to deal with system-specific issues only in one part of the code. The way we're wrapping it ensures the check for the OS is only done during initialization and not for every call.

 A more subtle point is that we defined our sleep() function (for the sake of this example) as taking the number of seconds, but accepting fractional seconds. Multiplying this by 1000 gets us milliseconds, but that still leaves it a Lua number, which is a floating-point value. Alas, the Sleep() function only accepts an integer value. Luckily for us, the FFI library automatically performs the conversion when calling the function (truncating the FP value towards zero, like in C).

Some readers will notice that Sleep() is part of KERNEL32.DLL and is also a stdcall function. So how can this possibly work? The FFI library provides the ffi.C default C library namespace, which allows calling functions from the default set of libraries, like a C compiler would. Also, the FFI library automatically detects stdcall functions, so you don't need to declare them as such.

 The poll() function takes a couple more arguments we're not going to use. You can simply use nil to pass a NULL pointer and 0 for the nfds parameter. Please note that the number 0 does not convert to a pointer value, unlike in C++. You really have to pass pointers to pointer arguments and numbers to number arguments.

The page on FFI semantics has all of the gory details about conversions between Lua objects and C types. For the most part you don't have to deal with this, as it's performed automatically and it's carefully designed to bridge the semantic differences between Lua and C.

 Now that we have defined our own sleep() function, we can just call it from plain Lua code. That wasn't so bad, huh? Turning these boring animated dots into a fascinating best-selling game is left as an exercise for the reader. :-)

Accessing the zlib Compression Library

The following code shows how to access the zlib compression library from Lua code. We'll define two convenience wrapper functions that take a string and compress or uncompress it to another string:

 
①






②


③

④


⑤


⑥







⑦local ffi = require("ffi")
ffi.cdef[[
unsigned long compressBound(unsigned long sourceLen);
int compress2(uint8_t *dest, unsigned long *destLen,
	      const uint8_t *source, unsigned long sourceLen, int level);
int uncompress(uint8_t *dest, unsigned long *destLen,
	       const uint8_t *source, unsigned long sourceLen);
]]
local zlib = ffi.load(ffi.os == "Windows" and "zlib1" or "z")

local function compress(txt)
  local n = zlib.compressBound(#txt)
  local buf = ffi.new("uint8_t[?]", n)
  local buflen = ffi.new("unsigned long[1]", n)
  local res = zlib.compress2(buf, buflen, txt, #txt, 9)
  assert(res == 0)
  return ffi.string(buf, buflen[0])
end

local function uncompress(comp, n)
  local buf = ffi.new("uint8_t[?]", n)
  local buflen = ffi.new("unsigned long[1]", n)
  local res = zlib.uncompress(buf, buflen, comp, #comp)
  assert(res == 0)
  return ffi.string(buf, buflen[0])
end

-- Simple test code.
local txt = string.rep("abcd", 1000)
print("Uncompressed size: ", #txt)
local c = compress(txt)
print("Compressed size: ", #c)
local txt2 = uncompress(c, #txt)
assert(txt2 == txt)

Here's the step-by-step explanation:

 This defines some of the C functions provided by zlib. For the sake of this example, some type indirections have been reduced and it uses the pre-defined fixed-size integer types, while still adhering to the zlib API/ABI.

 This loads the zlib shared library. On POSIX systems it's named libz.so and usually comes pre-installed. Since ffi.load() automatically adds any missing standard prefixes/suffixes, we can simply load the "z" library. On Windows it's named zlib1.dll and you'll have to download it first from the zlib site. The check for ffi.osmakes sure we pass the right name to ffi.load().

 First, the maximum size of the compression buffer is obtained by calling thezlib.compressBound function with the length of the uncompressed string. The next line allocates a byte buffer of this size. The [?] in the type specification indicates a variable-length array (VLA). The actual number of elements of this array is given as the 2nd argument to ffi.new().

 This may look strange at first, but have a look at the declaration of the compress2function from zlib: the destination length is defined as a pointer! This is because you pass in the maximum buffer size and get back the actual length that was used.

In C you'd pass in the address of a local variable (&buflen). But since there's no address-of operator in Lua, we'll just pass in a one-element array. Conveniently it can be initialized with the maximum buffer size in one step. Calling the actual zlib.compress2 function is then straightforward.

 We want to return the compressed data as a Lua string, so we'll use ffi.string(). It needs a pointer to the start of the data and the actual length. The length has been returned in the buflen array, so we'll just get it from there.

Note that since the function returns now, the buf and buflen variables will eventually be garbage collected. This is fine, because ffi.string() has copied the contents to a newly created (interned) Lua string. If you plan to call this function lots of times, consider reusing the buffers and/or handing back the results in buffers instead of strings. This will reduce the overhead for garbage collection and string interning.

 The uncompress functions does the exact opposite of the compress function. The compressed data doesn't include the size of the original string, so this needs to be passed in. Otherwise no surprises here.

 The code, that makes use of the functions we just defined, is just plain Lua code. It doesn't need to know anything about the LuaJIT FFI — the convenience wrapper functions completely hide it.

One major advantage of the LuaJIT FFI is that you are now able to write those wrappers in Lua. And at a fraction of the time it would cost you to create an extra C module using the Lua/C API. Many of the simpler C functions can probably be used directly from your Lua code, without any wrappers.

Side note: the zlib API uses the long type for passing lengths and sizes around. But all those zlib functions actually only deal with 32 bit values. This is an unfortunate choice for a public API, but may be explained by zlib's history — we'll just have to deal with it.

First, you should know that a long is a 64 bit type e.g. on POSIX/x64 systems, but a 32 bit type on Windows/x64 and on 32 bit systems. Thus a long result can be either a plain Lua number or a boxed 64 bit integer cdata object, depending on the target system.

Ok, so the ffi.* functions generally accept cdata objects wherever you'd want to use a number. That's why we get a away with passing n to ffi.string() above. But other Lua library functions or modules don't know how to deal with this. So for maximum portability one needs to use tonumber()on returned long results before passing them on. Otherwise the application might work on some systems, but would fail in a POSIX/x64 environment.

Defining Metamethods for a C Type

The following code explains how to define metamethods for a C type. We define a simple point type and add some operations to it:

 
①



②

③

④



⑤

⑥local ffi = require("ffi")
ffi.cdef[[
typedef struct { double x, y; } point_t;
]]

local point
local mt = {
  __add = function(a, b) return point(a.x+b.x, a.y+b.y) end,
  __len = function(a) return math.sqrt(a.x*a.x + a.y*a.y) end,
  __index = {
    area = function(a) return a.x*a.x + a.y*a.y end,
  },
}
point = ffi.metatype("point_t", mt)

local a = point(3, 4)
print(a.x, a.y)  --> 3  4
print(#a)        --> 5
print(a:area())  --> 25
local b = a + point(0.5, 8)
print(#b)        --> 12.5

Here's the step-by-step explanation:

 This defines the C type for a two-dimensional point object.

 We have to declare the variable holding the point constructor first, because it's used inside of a metamethod.

 Let's define an __add metamethod which adds the coordinates of two points and creates a new point object. For simplicity, this function assumes that both arguments are points. But it could be any mix of objects, if at least one operand is of the required type (e.g. adding a point plus a number or vice versa). Our __len metamethod returns the distance of a point to the origin.

 If we run out of operators, we can define named methods, too. Here the __indextable defines an area function. For custom indexing needs, one might want to define __index and __newindex functions instead.

 This associates the metamethods with our C type. This only needs to be done once. For convenience, a constructor is returned by ffi.metatype(). We're not required to use it, though. The original C type can still be used e.g. to create an array of points. The metamethods automatically apply to any and all uses of this type.

Please note that the association with a metatable is permanent and the metatable must not be modified afterwards! Ditto for the __index table.

 Here are some simple usage examples for the point type and their expected results. The pre-defined operations (such as a.x) can be freely mixed with the newly defined metamethods. Note that area is a method and must be called with the Lua syntax for methods: a:area(), not a.area().

The C type metamethod mechanism is most useful when used in conjunction with C libraries that are written in an object-oriented style. Creators return a pointer to a new instance and methods take an instance pointer as the first argument. Sometimes you can just point __index to the library namespace and __gc to the destructor and you're done. But often enough you'll want to add convenience wrappers, e.g. to return actual Lua strings or when returning multiple values.

Some C libraries only declare instance pointers as an opaque void * type. In this case you can use a fake type for all declarations, e.g. a pointer to a named (incomplete) struct will do: typedef struct foo_type *foo_handle. The C side doesn't know what you declare with the LuaJIT FFI, but as long as the underlying types are compatible, everything still works.

Translating C Idioms

Here's a list of common C idioms and their translation to the LuaJIT FFI:

Idiom C code Lua code
Pointer dereference
int *p;
x = *p;
*p = y;
x = p[0]
p[0] = y
Pointer indexing
int i, *p;
x = p[i];
p[i+1] = y;
x = p[i]
p[i+1] = y
Array indexing
int i, a[];
x = a[i];
a[i+1] = y;
x = a[i]
a[i+1] = y
struct/union dereference
struct foo s;
x = s.field;
s.field = y;
x = s.field
s.field = y
struct/union pointer deref.
struct foo *sp;
x = sp->field;
sp->field = y;
x = s.field
s.field = y
Pointer arithmetic
int i, *p;
x = p + i;
y = p - i;
x = p + i
y = p - i
Pointer difference
int *p1, *p2;
x = p1 - p2; x = p1 - p2
Array element pointer
int i, a[];
x = &a[i]; x = a+i
Cast pointer to address
int *p;
x = (intptr_t)p; x = tonumber(
 ffi.cast("intptr_t",
          p))
Functions with outargs
void foo(int *inoutlen);
int len = x;
foo(&len);
y = len;
local len =
  ffi.new("int[1]", x)
foo(len)
y = len[0]
Vararg conversions
int printf(char *fmt, ...);
printf("%g", 1.0);
printf("%d", 1);
 
printf("%g", 1);
printf("%d",
  ffi.new("int", 1))

To Cache or Not to Cache

It's a common Lua idiom to cache library functions in local variables or upvalues, e.g.:

local byte, char = string.byte, string.char
local function foo(x)
  return char(byte(x)+1)
end

This replaces several hash-table lookups with a (faster) direct use of a local or an upvalue. This is less important with LuaJIT, since the JIT compiler optimizes hash-table lookups a lot and is even able to hoist most of them out of the inner loops. It can't eliminate all of them, though, and it saves some typing for often-used functions. So there's still a place for this, even with LuaJIT.

The situation is a bit different with C function calls via the FFI library. The JIT compiler has special logic to eliminate all of the lookup overhead for functions resolved from aC library namespace! Thus it's not helpful and actually counter-productive to cache individual C functions like this:

local funca, funcb = ffi.C.funca, ffi.C.funcb -- Not helpful!
local function foo(x, n)
  for i=1,n do funcb(funca(x, i), 1) end
end

This turns them into indirect calls and generates bigger and slower machine code. Instead you'll want to cache the namespace itself and rely on the JIT compiler to eliminate the lookups:

local C = ffi.C          -- Instead use this!
local function foo(x, n)
  for i=1,n do C.funcb(C.funca(x, i), 1) end
end

This generates both shorter and faster code. So don't cache C functions, but do cache namespaces! Most often the namespace is already in a local variable at an outer scope, e.g. from local lib = ffi.load(...). Note that copying it to a local variable in the function scope is unnecessary.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

luajit struct 的相关文章

  • 信号和槽

    信号和槽用于对象间的通讯 信号 槽机制是Qt的一个中心特征并且也许是Qt与 其它工具包的最不相同的部分 在图形用户界面编程中 我们经常希望一个窗口部件的一个变化被通知给另一个 窗口部件 更一般地 我们希望任何一类的对象可以和其它对象进行通讯
  • 网页按钮点击动画

    要求 一个按钮 每点击一次在大小可随时变化的按钮表面生成一个实心圆形 对每个圆形配置的时间 T T T 单位 毫秒 内有如下过程 第 i i i次点击生成一个圆形
  • 如何快速构建一个SpringBoot项目

    我们主要介绍如何快速构建一个SpringBoot项目 以此来提升日常开发效率 SpringBoot是搭建应用的手脚架 由Spring公司的核心团队在2013年开始研发 2014年4月发布第一个版本的全新开源的轻量级框架 它基于Spring4
  • Windows10系统自动登录

    1 打开注册表 在搜索框内 输入regedit 或者 注册表 2 找到 HKEY LOCAL MACHINE SOFTWARE Microsoft Windows NT CurrentVersion Winlogon 3 添加新键 类型是字

随机推荐

  • openGL之API学习(二零三)GL_TEXTURE_WRAP_S GL_TEXTURE_WRAP_T

    设置纹理坐标超出0 1范围时的处理方式 使用函数glTexParameteri 设置纹理参数 设置纹理参数 GL TEXTURE WRAP S 为 GL REPEAT 表示纹理X方向循环使用纹理 glTexParameteri GL TEX
  • 实际工作中的高级技术(训练加速、推理加速、深度学习自适应、对抗神经网络)

    目录 一 训练加速 1 基于数据的并行 Model Average 模型平均 SSGD 同步随机梯度下降
  • 大学生选课抢课如何提高选中概率

    作者位于哈尔滨某高校 选课总是激动人心的一件大事 但是明明与同学一起进的系统 他就能顺利选课 而我却被强退出来 无数辛酸让我知道了一些道理 写下这篇文章给学弟学妹们作为参考 原理 问 为什么大多数学校教务系统选课时都会卡 答 学校教务系统平
  • 热敏电阻测温

    热敏电阻器主要分为 PTC 和 NTC 正温度系数热敏电阻器 PTC 在温度越高时电阻值越大 负温度系数热敏电阻器 NTC 在温度越高时电阻值越低 它们同属于半导体器件 测温的热敏电阻一般为NTC 其主要参数有以下几个 标称阻值 标称阻值是
  • 期货有哪些(正规期货公司排名)

    期货有哪些 期货暂时重要分为两大版块 辨别是商品期货和金融期货 与此同声这两大版块又不妨辨别细化出各别的品种 商品期货又可细分为非金属商品 动力商品 农产物等 金融期货重要指保守的金融商品或东西 如一手一足 内债 税率 汇率等 商品期货农产
  • 58同城面经

    文章目录 58一面 58二面 58同城通过了技术面试 但迟迟没有hr面 可能表现的不是很好 58一面 自我介绍 数据结构大概有哪些分类 关于项目 为什么会考虑做商城项目 商城首页的优化 操作系统为什么会有线程这个操作吗 Java创建线程的方
  • Golang基础 流程控制 循环控制

    循环控制 01 基础循环 for 02 键值循环 for range 参考资料 循环控制通常用于程序中需要重复执行的逻辑模块 循环结构通常由循环变量 循环终止条件和循环体三个部分构成 01 基础循环 for Golang 中所有的循环控制都
  • PCL 最小点数约束的改进半径滤波(C++详细过程版)

    目录 一 概述 1 不足 2 改进 二 代码实现 三 结果展示 一 概述 1 不足 传统半径滤波算法在点云数据量巨大的情况下 算法效率会大幅度降低 而对于稠密点云数据 一个影响效率的重要因素就是搜索半径的大小 当搜索半径较大时 需要计算邻域
  • @vue/cli 创建项目报Cannot find module ‘inquirer‘错

    解决 这可能是因为cli版本问题 1 第一步 2 第二步 npm uninstall g vue cli 3 第三步 npm install g vue cli
  • 由PyRetri浅谈基于深度学习的图像检索

    前言 最近发现face 开源了一个图像检索和行人重识别的基于深度学习的软件包 最近一段时间也一直在接触图像检索相关的东西 故借此机会 对里面涉及的一些常用的方法模块进行一个简单的介绍总结 便于日后回顾 PyRetri是什么 PyRetri是
  • 如何查看linux服务器字符集,Linux字符集查看与设置

    查看字符集 Linux 中字符集在系统中的体现是一个环境变量 以 CentOS 6 5 为例 查看当前终端使用的字符集的方式有 1 root jerry echo LANG zh CN GB18030 2 root jerry env gr
  • 对 React Hook的闭包陷阱的理解,有哪些解决方案?

    hooks中 奇怪 其实符合逻辑 的 闭包陷阱 的场景 同时 在许多 react hooks 的文章里 也能看到 useRef 的身影 那么为什么使用 useRef 又能摆脱 这个 闭包陷阱 搞清楚这些问题 将能较大的提升对 react h
  • vue 全局组件注册_如何注册vue3全局组件

    vue 全局组件注册 With the new versions of Vue3 out now it s useful to start learning how the new updates will change the way w
  • unity playerprefs android,Unity持久化存储之PlayerPrefs的使用

    一 PlayerPrefs类支持3中数据类型的保存和读取 浮点型 整形 和字符串型 分别对应的函数为 php SetInt 保存整型数据 GetInt 读取整形数据 SetFloat 保存浮点型数据 GetFlost 读取浮点型数据 Set
  • pygame之五子棋的实现

    先上代码 调用pygame库 import pygame import sys 调用常用关键字常量 from pygame locals import QUIT KEYDOWN import numpy as np 初始化pygame py
  • laravel-vue后端返回数据的字符串中(<br/> \n)换行无效

    laravel 做后端 vue做前端 后端返回数据的字符串中含有 br 或 n r n 等换行符 在前端页面无法正常渲染出换行效果 尝试用str replace方法无效 最终找到解决办法 解决办法 给包含换行符的字符串元素增加css whi
  • 【STM32学习】——串口通信协议&STM32-USART外设&数据帧/输入数据策略/波特率发生器&串口发送/接受实操

    文章目录 前言 一 串口通信 1 通信接口 2 串口通信 1 串口简介 2 串口硬件电路 3 串口软件部分 二 STM32的USART外设 1 USART简介 2 图示详解 三 细节问题 1 数据帧 2 输入数据策略 1 起始位侦测 2 数
  • iOS开发,tableView中cell的重用详解

    注意 原创版权 转载必须标明出处作者 翻版必究 iOS中tableView是一个大的模块组件 它的重要性每个iOSCoder都是了解的 但是tableView中却有个重大的坑 就是cell的重用 每个刚接触iOS开发的人都深受其海 那么经过
  • AD18出现Unknown Pin报错解决

    问题描述 检查错误 检查原理图对应元件的封装是否存在 检查原理图与封装PCB引脚数量是否对应 检查原理图与封装的管脚是否统一 找到原因 原理图的管脚命名与PCB封装管脚命名不一致 问题解决 修改原理图管脚名称 修改PCB Library的管
  • luajit struct

    This page is intended to give you an overview of the features of the FFI library by presenting a few use cases and guide