Difference between revisions of "Data:getFFIPointer (日本語)"

(Created page with "{{newin (日本語)|11.3|113|type=関数}} Data に関する [http://luajit.org/ext_ffi.html 外部関数インタフェース (FFI)] のポインタを...")
 
m (preparation work.)
 
Line 5: Line 5:
 
[[light userdata (日本語)|軽量 userdata]] を使うため、この関数は
 
[[light userdata (日本語)|軽量 userdata]] を使うため、この関数は
 
[[Data:getPointer (日本語)|Data:getPointer]] よりも望ましいです。
 
[[Data:getPointer (日本語)|Data:getPointer]] よりも望ましいです。
 +
{{notice|自己責任で使用してください。Data が所有する生のメモリを直接読み書きする行為は Data が通常行っている安全性のための各種検査、およびスレッド・セーフを経由しません。}}
 
== 関数 ==
 
== 関数 ==
 
=== 概要 ===
 
=== 概要 ===
Line 14: Line 15:
 
=== 返値 ===
 
=== 返値 ===
 
{{param|cdata|pointer|Data の生 <code>void*</code> ポインタ、または <code>nil</code> ならば FFI は利用できません。}}
 
{{param|cdata|pointer|Data の生 <code>void*</code> ポインタ、または <code>nil</code> ならば FFI は利用できません。}}
 +
=== 用例 ===
 +
 +
<source lang="lua">
 +
local mem = love.data.newByteData(2 ^ 30)                      -- 1 GB
 +
local ptr = ffi.cast('uint8_t*', mem:getFFIPointer())          -- grab byte pointer
 +
local byte100 = ptr[99]                                        -- get the 100th byte out of the allocated data mem
 +
local uint32array = ffi.cast('uint32_t*', mem:getFFIPointer()) -- grab 4-byte integer pointer
 +
uint32array[0] = 0xdeadbeef                                    -- treat mem as integer array, assign to 1st position
 +
</source>
 +
 +
As an example usage, allocate memory and grab an FFI pointer.  The pointer can be cast to any FFI ctype one wishes in order to access the memory accordingly.  A pointer to the data could be cast to type <code>uint8_t*</code>, allowing one to access that memory like a 0-based sequential Lua table that contains byte values.  Or the same memory could be treated as an array of 32-bit integers by recasting the pointer to <code>uint32_t*</code> .  Note that these pointers will be invalid if the underlying ByteData goes out of scope.
 +
 
== 関連 ==
 
== 関連 ==
 
* [[parent::Data (日本語)]]
 
* [[parent::Data (日本語)]]

Latest revision as of 00:11, 19 July 2023

LÖVE 11.3 から使用可能
この関数は以前のバージョンでは非対応です。

Data に関する 外部関数インタフェース (FFI) のポインタを取得します。

LuaJIT 使用時、一部の新しい ARM64 アーキテクチャはメモリアドレス全体の範囲を格納できない 軽量 userdata を使うため、この関数は Data:getPointer よりも望ましいです。

O.png 自己責任で使用してください。Data が所有する生のメモリを直接読み書きする行為は Data が通常行っている安全性のための各種検査、およびスレッド・セーフを経由しません。  


関数

概要

pointer = Data:getFFIPointer( )

引数

なし。

返値

cdata pointer
Data の生 void* ポインタ、または nil ならば FFI は利用できません。

用例

local mem = love.data.newByteData(2 ^ 30)                      -- 1 GB
local ptr = ffi.cast('uint8_t*', mem:getFFIPointer())          -- grab byte pointer
local byte100 = ptr[99]                                        -- get the 100th byte out of the allocated data mem
local uint32array = ffi.cast('uint32_t*', mem:getFFIPointer()) -- grab 4-byte integer pointer
uint32array[0] = 0xdeadbeef                                    -- treat mem as integer array, assign to 1st position

As an example usage, allocate memory and grab an FFI pointer. The pointer can be cast to any FFI ctype one wishes in order to access the memory accordingly. A pointer to the data could be cast to type uint8_t*, allowing one to access that memory like a 0-based sequential Lua table that contains byte values. Or the same memory could be treated as an array of 32-bit integers by recasting the pointer to uint32_t* . Note that these pointers will be invalid if the underlying ByteData goes out of scope.

関連


そのほかの言語