lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



There is no memory management involved in David's code clip. There shouldn't.

'static' variable is not memory managed, and all the light user data are, are pointers to that memory location.

About your real question, I am still listening (don't know an answer myself). Maybe it's a twisted design, from your side. What exactly do you need the shared upvalues for? :)

-asko


slonik.az@gmail.com kirjoitti 14.8.2006 kello 3.03:

Hi David,
thanks a lot for your help and for your suggestion!!
Unfortunately, your solution does not quite solve the problem because
functions myfunc1 and myfunc2 do not really share upvalues. Each one
has its own upvalue that happens to contain a pointer that points to
some common memory block (or Lua table).
Firstly, it introduces one extra level of redirection. Secondly, how
the memory will be cleaned? Lightuserdata is not being garbage
collected and no one else is pointing to that memory block?

Is there a way to share a given upvalue amongst several C-closures the
same way it happens with Lua closures??

--Leo--

On 8/13/06, D Burgess <dburgess@gmail.com> wrote:
Case 1

static int val;
lua_pushlightuserdata(L, &val);
lua_pushcclosurer(L,  myfunc2, 1);
lua_pushlightuserdata(L, &val);
lua_pushcclosurer(L,  myfunc1, 1);

Case 2
If its lua values you wish to share (rather than C) create the value
as a table entry and use the table for the upvalue.

David B

On 8/14/06, slonik.az@gmail.com <slonik.az@gmail.com> wrote:
> Hi Lua Experts,
> Lua closures can share their private state with each other by sharing
> their "outer local" variables. In the following code snippet "local
> cnt" is accessible by both cnt_step() and cnt_get() and no other
> function in the world could touch it.
>
> function cnt_init()
>    local cnt= 0
>    function cnt_step() cnt= cnt + 1 end
>    function cnt_get()  return cnt; end
> end
> cnt_init();
>
> I would like to achieve the same effect using C-closures. But I cannot
> figure out how?
> Roberto's book "Programming in Lua, 2-nd edition" that I recently
> bought shows how a C-closure stores its individual state in an upvalue > (lua_pushcclosure). But here I need two functions to have an access to
> the same upvalue.
> Any help is greatly appreciated.
>
> Thanks and Best Regards,
> --Leo--
>