Light User Data
来源:互联网 发布:node js ide 编辑:程序博客网 时间:2024/05/21 01:06
转自:http://lua-users.org/wiki/LightUserData
Light userdata, like heavy userdata, are a form of userdata, which is one of the basic data types in Lua [1]. Light userdata are characterized by the following properties:
- Light userdata and heavy userdata have deceptively similar names, and both have the property
type(x) == 'userdata'
, but these two forms of userdata are otherwise quite different in behavior [1], as outlined below. - A light userdatum represents a single pointer to a physical memory address (void *), which is typically a 32- or 64-bit value depending on platform. Light userdata are intended to store C pointers in Lua (note: Lua numbers may or may not be suitable for this purpose depending on the data types on the platform).
- A heavy userdatum represents a region of mutable bytes allocated in Lua's memory and managed by Lua (garbage collected). This is the only memory in Lua that you are permitted to read/write directly from C (via the userdata's pointer) without the C API.
- Light userdata have the semantics of values, while heavy userdata have the semantics of objects. Objects have a unique identity [2]: Two heavy userdata constructed with the same data will always be distinguishable (e.g.
rawequal
will differentiate them by memory address); two light userdata so constructed will never be distinguishable because they are compared by value not by address. - Light userdata (unlike heavy userdata) are not garbage collected. LuaImplementations typically fit each light userdatum in a single register and copy them around by value, while heavy userdata is allocated on the heap and passed around by reference (pointer).
- Equality: If
x
is a lightuserdatum, thenx == y
if-and-only-ify
is a light userdatum representing the same pointer. Light userdata can be used as table keys, in which casex == y
impliest[x]
andt[y]
refer to the same table value (though not necessarily thatt[x] == t[y]
since NaN ~= NaN). The__eq
metamethod has no effect on light userdata (note: the manual isn't clear on this [1]). - Light userdata (unlike heavy userdata) have no per-value metatables. All light userdata share the same metatable, which by default is not set (
nil
). tostring(x)
typically displays the pointer in hex notation, although this is specific to LuaImplementations.
Some interesting points:
- A common technique for mapping C pointers to Lua values is to store a light userdata of that pointer as a key in the registry table. [Reference Manual, 3.5]. Bear in mind that these mapping are not automatically garbage collected [3], and you might want to use a weak table rather than the registry (which by default is not weak, but a weak table can be stored in it).
- Some people represent handles rather just pointers as lightuserdata [4]. It's possible to represent other data in them as well [5]. Take care not to mix pointer and non-pointer userdata in a way that might conflict (e.g. storing both in the registry table).
- Light userdata are created from the C API via [lua_pushlightuserdata]. Out-of-the-box, Lua doesn't provide a way to create lightuserdata from Lua.
- A one-page summary of light userdata is provided in [1].
--DavidManura
See Also
- [1] ProgrammingInLua?, Section 28.5 "Light Userdata".
0 0
- Light User Data
- 10-user-based-data
- Marshmallow and User Data
- CloudInit & User-Data
- FLGUI -- a Fast & Light Graphic User Interface
- 保存user data镜像
- Light
- User client windows logon basic data
- Light OJ 1303——Basic Data Structure
- Alternative-data firms are shedding new light on corporate performance
- Seeing Data : Designing User Interfaces for Database Systems Using .NET
- Enabling User-Controlled Collection of Application Crash Data With DTrace
- the user data image is used by another emulator. aborting
- Oracle Data Integrator Driver for XML - User's Manual
- OpenStack虚拟机的用户客制化方法(User Data)
- different user control type: reuse layout vs reuse data
- openstack中 file/meta/user-data的区别
- OpenStack Nova:虚拟机初始化user-data & Cloud-init
- 特征提取
- uva 1152 和为0的4个值
- Hibernate criterion 方法总结
- python3使用reload函数
- 经典多线程面试题
- Light User Data
- 矩阵快速幂 经典模板
- LintCode 18 带重复元素的子集
- 线性代数笔记(1):向量空间与子空间
- jq中的map函数
- HDU - 2090 - 算菜价
- Linux自学笔记(七)常用命令之用户管理命令,压缩解压命令
- Linux [root@bogon]怎么改成[root@yourname]
- 数据库连接池