[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Luajit and curstom data structures
- From: KR <krunal.rao78@...>
- Date: Wed, 13 Apr 2011 20:55:41 +0000 (UTC)
Mike Pall <mikelu-1104 <at> mike.de> writes:
> Well, I thought the error message was pretty unambiguous.
> You have to declare C functions before you use them:
>
> ffi.cdef[[
> void *malloc(size_t size);
> void free(void *ptr);
> ]]
You are right! I don't know how I became convinced that everything in the C
namespace was pre-declared, sorry about the stupid mistake! :P
> The array itself is a garbage-collected object and nothing can
> change that. The point was to show how to free the 10 pointers
> _inside_ the array, too (the GC doesn't know anything about them).
Understood, thanks for pointing this out.
> In general, you need to attach the GC finalizer to the garbage
> collected cdata object that's returned to the user. In your use
> case this is the vector struct and not the pointer inside:
>
> local vec_mt = {
> __len = function(x) return x.m_size end,
> __index = function(x, i) return x.m_ptr[i] end,
> __newindex = function(x, i, v) x.m_ptr[i] = v end,
>
> -- Free array before struct is deallocated by the GC.
> __gc = function(x) ffi.C.free(x.m_ptr) end,
> }
> local vec_ctor = ffi.metatype("vec_t", vec_mt)
>
> function Vec(size)
> -- Allocate array + struct.
> return vec_ctor(ffi.C.malloc(size*8), size)
> end
>
> --Mike
That's very clear again, thank you very much for your help and your time! I
should (hopefully!) be able to proceed without help from now on to complete the
vector and matrix classes.
KR