Skip to content

Instantly share code, notes, and snippets.

@justincormack
Created July 5, 2013 13:33
Show Gist options
  • Save justincormack/5934552 to your computer and use it in GitHub Desktop.
Save justincormack/5934552 to your computer and use it in GitHub Desktop.
ffi array bounds checking 2
ffi = require "ffi"
local VHOST_VRING_SIZE = 32*1024
ffi.cdef [[
enum { VHOST_VRING_SIZE = 32*1024 };
struct vring_avail {
uint16_t flags;
uint16_t idx;
uint16_t ring[VHOST_VRING_SIZE];
};
]]
vring_avail = ffi.metatype("struct vring_avail",
{__index = function(v, i)
if i < 0 or i > VHOST_VRING_SIZE then error "out of bounds" end
return v.ring[i]
end,
__newindex = function(v, i, k)
if i < 0 or i > VHOST_VRING_SIZE then error "out of bounds" end
v.ring[i] = k
end,
})
v = vring_avail()
v[2] = 4
assert(v[2] == 4)
x = v[65000]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment