[Vm-dev] VM handle management

David T. Lewis lewis at mail.msen.com
Wed Mar 24 12:40:51 UTC 2010


On Tue, Mar 23, 2010 at 08:10:18PM -0700, Andreas Raab wrote:
> 
> Folks -
> 
> [Warning: Long post. Please try to stay on topic and don't get into any 
> SmartSyntaxInterpreterPlugin / FFI syntax digressions that always come 
> up with these issues. This is a practical proposal that I'd like to 
> implement].
> 
> For the third time this month I ran into the situation that I was 
> writing a plugin that would create an internal data structure 
> (containing pointers etc) and where I wanted to return a handle to this 
> structure and pass that back to the VM.
> 
> Since I'm a lazy bastard, what I usually do is something like this in 
> the support code:
> 
> int sqNewFooBar(void) {
>   /* creates a new foobar and returns a handle */
>   FooBar *newFoo = malloc(1, sizeof(FooBar));
> 
>   /* return a handle, sort of */
>   return (int) newFoo;
> }

This seems like solving the wrong problem. If the problem is
that pointers cannot be treated as sqInt or OOP values, then
the solution is simple: Don't Do That. Declaring the types
properly (in slang where necessary) results in simpler slang
and simpler support code overall. That is my actual experience
after working through quite a few 64/32 bit issues, not just
a theoretical statement.

In practice, I don't think I have ever actually encounted a problem
due to accidentally passing a bogus handle to a plugin. It is definitely
possible, but it does not seem to be a practical concern. So to me,
adding a protocol for registering handles feels like adding complexity
with not much real benefit.

That said, the approach should work fine and if there is a use
case where it is solving real problems, then go for it.

If you want to test performance, some of the unit tests for
CommandShell will generate thousands of file handles, so that
might be a good smoke test. Web servers will presumably also
create and destroy handles as a high rate over long periods
of time.

Dave

p.s. I would certainly like to have a way to have OSProcess work
with the win32 HANDLE registry, so any solution that supports this
is very welcome :)



More information about the Vm-dev mailing list