local mem = love.data.newByteData(2 ^ 31) -- 2 GB
local ptr = ffi.cast('uint8_t*', mem:getFFIPointer()) -- PVOID
local testkey = '0123456789ABCDE'
local length = #testkey
ffi.copy(ptr,testkey,length)
ffi.copy(ptr + length,testkey,length)
local whole = ffi.string(ptr,length*2)
print(whole)
local index = 3
local out = ffi.string(ptr+index,1)
print(out)
Motivated by three things: 1) avoiding string interning of millions of temporary strings, 2) avoiding the garbage collector, 3) working with very large, contiguous arrays of (32-bit unsigned) integers.
You have a pointer to the data, of type uint8_t* in this case, which means it's a pointer to byte values. You can access that memory like a 0-based sequential Lua table that contains byte values.
local byte100 = ptr[99] -- get the 100th byte out of the allocated data
You can cast the pointer to any ctype you want and access the memory accordingly. If you want to access the memory in units of uint32_t, then just cast it to uint32_t*