Advertisement
funny_falcon

test_gc.lua

Oct 14th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.82 KB | None | 0 0
  1.  
  2. -- luajit test_gc.lua 20000000 cast
  3. -- luajit test_gc.lua 20000000 coat
  4. local n, m = ...
  5. n = n and tonumber(n) or 20000000
  6. m = m or 'cast'
  7.  
  8. print(n, m)
  9.  
  10. local ffi = require 'ffi'
  11. ffi.cdef"struct tst{ int64_t i, j, k, h; }"
  12. ffi.cdef"struct tsts{ struct tst*p; }"
  13. ffi.cdef"void *malloc(int i); void free(void *);"
  14. local tst=ffi.typeof("struct tst")
  15. local tstp=ffi.typeof("struct tst*")
  16. local tsts=ffi.typeof("struct tsts")
  17. local mtt = {__gc=function(t) ffi.C.free(t.p) end}
  18. ffi.metatype(tsts, mtt)
  19.  
  20. local t = {}
  21. for i=1,n do
  22.     local g
  23.     if m == 'cast' then
  24.         local p = ffi.C.malloc(ffi.sizeof(tst))
  25.         local s = ffi.cast(tstp, p)
  26.         g = ffi.gc(s, ffi.C.free)
  27.     elseif m == 'coat' then
  28.         local p = ffi.C.malloc(ffi.sizeof(tst))
  29.         g = ffi.new(tsts)
  30.         g.p = p
  31.     end
  32. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement