plugin handle question

View: New views
4 Messages — Rating Filter:   Alert me  

plugin handle question

by Rick Hazey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all.

I'm new to writing 4D plugins as well as this list. (sorry in advance  
for the newbie question)

Can anyone shed some light on when it's required to lock a handle?  
I'm scanning a blob in a tight loop and wondering if I should just  
use the pointer instead for performance purposes. If I don't make any  
callbacks to 4D or Mac OS X during the loop, am I guaranteed the  
pointer won't become invalid during the scan? Or perhaps with modern  
hardware a dereferencing a handle isn't much more costly than  
dereferencing a pointer?

Any info would be greatly appreciated.


Rick Hazey

**********************************************************************
4D Plugins hosted by 4D, Inc.                      http://www.4D.com/     
   

    Did you know?  The 4D Partner Program now gives you
    access to Hotfix releases as soon as they are available!
    To learn more, go to http://www.4D.com/support/partner.html

To Unsubscribe:                      mailto:4D-Plugins-off@...
***********************************************************************


Re: plugin handle question

by David Dancy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Rick

There are others far more qualified to help on plugins than I am, but
here's what I do, FWIW. I am aware that a "handle" is a
pointer-to-pointer, which is a simple mechanism for allocating memory
when you are implicitly allowing the OS to shift the data under your
feet (as it were). I don't actually know if the OS does in fact do
this. The first I knew of the technique was in the old MacOS's where
the OS explicitly said it *would* move the memory unless you locked it
down. I'm not aware of whether the current batch of OS's do this, but
I just assume they do and program accordingly.

Anyhow, as you've discovered, to get at the data you have two options:

1. Double-dereference the handle every time you need access to the
data. This is simplest from one point of view, but I find it harder to
read the code that it requires.
2. Lock the handle using PA_LockHandle, and dereference the pointer
that it returns. When you're done, use PA_UnlockHandle to restore the
handle's state. A complication is that the handle may already have
been locked by the time your code receives it. In this case, you'll
need to remember the handle's state (use PA_GetHandleState) and
lock/unlock as appropriate. This is a little harder to do, but IMHO
makes the "inner-loop"-type code a *lot* easier to read. I think a
class would be able to deal with the state transitions quite nicely,
but I've never bothered writing one to do it.

I personally don't really care all that much about processor
efficiency, especially about modern processor efficiency. In my work
it's not really relevant, since the computer is usually a lot faster
than the human who's using it, and if you wait a couple of months
there's a new computer that's faster than the current batch anyway.

I'm much more interested in the efficiency of the processor between my
ears, and I find it's easier to think about handles once you've locked
them down and got a pointer to the real data than it is to do lots of
double-dereference coding, which given the data structures we're
dealing with in 4D variables and the like can get a bit hairy at
times. :)

That said, you'll probably find it is quicker (but you'll be into many
millions of iterations before you notice) to use the pointer rather
than the handle in your scanning loop.

Cheers

David
**********************************************************************
4D Plugins hosted by 4D, Inc.                      http://www.4D.com/     
   

    Did you know?  The 4D Partner Program now gives you
    access to Hotfix releases as soon as they are available!
    To learn more, go to http://www.4D.com/support/partner.html

To Unsubscribe:                      mailto:4D-Plugins-off@...
***********************************************************************


Re: plugin handle question

by Rick Hazey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On May 4, 2008, at 6:36 PM, David Dancy wrote:

> That said, you'll probably find it is quicker (but you'll be into many
> millions of iterations before you notice) to use the pointer rather
> than the handle in your scanning loop.

Thanks for the reply David.

Still, I'm curious if it's necessary to lock a handle if you make no  
callbacks to 4D or Mac OS X. I googled the topic and found this:

http://www.zathras.de/angelweb/blog-carbon-for-the-cocoa-guy-handles.htm

I don't know if this is a reliable source, however, the last  
paragraph indicates that handles are never changed and it's  
unnecessary to lock/unlock handles in Mac OS X. I haven't found  
anything definitive on Apple's web site but I'm going to keep looking.


Rick Hazey
**********************************************************************
4D Plugins hosted by 4D, Inc.                      http://www.4D.com/     
   

    Did you know?  The 4D Partner Program now gives you
    access to Hotfix releases as soon as they are available!
    To learn more, go to http://www.4D.com/support/partner.html

To Unsubscribe:                      mailto:4D-Plugins-off@...
***********************************************************************


Re: plugin handle question

by David Dancy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Rick

Interesting information.

I'd guess that whether handles need to be locked depends more on the
version of 4D you're using than the version of the OS. When code runs
in a plugin, it can allocate its own memory using malloc/free or
new/delete as required, but if it wants some of 4D's memory, it has to
call NewHandle/DisposeHandle. That memory is then managed by 4D, and
who knows what it does with it. So I'd recommend for safety's sake to
lock handles if you want to use a pointer to their memory block, but
double-dereference all the time if not.

I'd guess also that the practical difference in one technique over the
other could probably only be measured nowadays in nanoseconds. :) It
may not be all that important from the machine's point of view! I
usually think of myself designing and/or coming back to the code
before I ever consider the machine, even in interpreted 4D, never mind
in C/C++! :)

Cheers

David
**********************************************************************
4D Plugins hosted by 4D, Inc.                      http://www.4D.com/     
   

    Did you know?  The 4D Partner Program now gives you
    access to Hotfix releases as soon as they are available!
    To learn more, go to http://www.4D.com/support/partner.html

To Unsubscribe:                      mailto:4D-Plugins-off@...
***********************************************************************

LightInTheBox - Buy quality products at wholesale price