Re: Message stack limitations
Re: Message stack limitations
- Subject: Re: Message stack limitations
- From: "Michael Ash" <email@hidden>
- Date: Wed, 3 Sep 2008 12:34:52 -0400
On Wed, Sep 3, 2008 at 5:23 AM, RGA <email@hidden> wrote:
> Putting aside for now whether this is the right way to implement, my
> question is how are messages processed?
Messages are processed using a family of functions called
objc_msgSend. You can find declarations and a bit of discussion of
these in /usr/include/objc/message.h.
At the low level, a message send works exactly like a C function call,
except with an extra level of indirection. A C function call sets up
parameters according to the platform's calling conventions, then jumps
to the function being called. An Objective-C message sets up
parameters in exactly the same way, but then jumps to objc_msgSend (or
one of the variants). That function then examines the target and
selector of the message being sent, looks up the implementation of the
corresponding method, and in turn jumps to that implementation. The
net result is that objc_msgSend functions as a trampoline. The sender
of a message calls it, and it in turn calls the proper implementation
for that method. When you look at the call stack, you just see method
implementations all the way down (unless there are C functions, of
course).
But the important thing here is that ObjC messages aren't nearly as
fancy as they sound. They aren't Erlang-style asynchronous calls, they
aren't descriptive objects, they're really just a slightly more
indirect function call.
> What is the maximum stack depth for
> number of recursive messages? How is this affected by the introduction of
> 64bit frameworks? Is there a way to see the current stack depth
> auto-magically?
The maximum stack depth depends on your thread's stack size. On 10.5
this can be inspected and set using NSThread methods, although you can
only set it for newly created threads. The default stack size is
system dependent, and how that size translates into a function call
count will depend on exactly how much stack each function call takes
(which depends on your local variables and other such things). But
generally I've found that the stack limit for normal functions is
around 100,000 calls. I don't know how 64-bit influences this, but
since the stack limit is partly in place to catch runaway processes, I
would expect it to remain the same.
> What happens in the event of the message handling stack
> becoming full and is there a way to deal with this event?
If you overflow the stack then you segfault, crash, and terminate.
It's not recommended.
Generally, with the limits in place, programs either stay far under
the limit or they are experiencing infinite recursion and need to be
stopped anyway. It's rare to have a program which legitimately needs
to recurse 100,000 times, and if you have such a program then it's
probably advisable to rewrite it to be non-recursive anyway.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden