LuaJIT FFI to Fortran iso_c_binding example
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

particle.lua 522B

1234567891011121314151617181920212223242526
  1. local ffi = require("ffi")
  2. ffi.cdef[[
  3. struct particle {
  4. const int n;
  5. struct { double x, y, z; } *const pos, *const vel;
  6. };
  7. void *calloc(size_t, size_t);
  8. void free(void *);
  9. ]]
  10. local double = ffi.typeof("double")
  11. return ffi.metatype("struct particle", {
  12. __new = function(ct, n)
  13. local pos = ffi.C.calloc(n, 3*ffi.sizeof(double))
  14. local vel = ffi.C.calloc(n, 3*ffi.sizeof(double))
  15. return ffi.new(ct, n, pos, vel)
  16. end,
  17. __gc = function(self)
  18. ffi.C.free(self.pos)
  19. ffi.C.free(self.vel)
  20. end,
  21. })