Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- luajit test_gc.lua 20000000 cast
- -- luajit test_gc.lua 20000000 coat
- local n, m = ...
- n = n and tonumber(n) or 20000000
- m = m or 'cast'
- print(n, m)
- local ffi = require 'ffi'
- ffi.cdef"struct tst{ int64_t i, j, k, h; }"
- ffi.cdef"struct tsts{ struct tst*p; }"
- ffi.cdef"void *malloc(int i); void free(void *);"
- local tst=ffi.typeof("struct tst")
- local tstp=ffi.typeof("struct tst*")
- local tsts=ffi.typeof("struct tsts")
- local mtt = {__gc=function(t) ffi.C.free(t.p) end}
- ffi.metatype(tsts, mtt)
- local t = {}
- for i=1,n do
- local g
- if m == 'cast' then
- local p = ffi.C.malloc(ffi.sizeof(tst))
- local s = ffi.cast(tstp, p)
- g = ffi.gc(s, ffi.C.free)
- elseif m == 'coat' then
- local p = ffi.C.malloc(ffi.sizeof(tst))
- g = ffi.new(tsts)
- g.p = p
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement