9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: David Leimbach <leimy2k@gmail.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] "Blocks" in C
Date: Thu,  3 Sep 2009 22:35:35 -0700	[thread overview]
Message-ID: <3e1162e60909032235s6e5a67dau6109f30246f255d@mail.gmail.com> (raw)
In-Reply-To: <3e1162e60909032231h5a2cc329x89744a497052e551@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 4532 bytes --]

On Thu, Sep 3, 2009 at 10:31 PM, David Leimbach <leimy2k@gmail.com> wrote:

>
>
> On Thu, Sep 3, 2009 at 9:44 PM, erik quanstrom <quanstro@quanstro.net>wrote:
>
>> > > that sucker is on the stack.  by-by no-execute stack.
>> > > how does it get to the stack?  is it just copied from
>> > > the text segment or is it compiled at run time?
>> > >
>> >
>> > I don't think I posted the whole code, so that's my bad.  The X was on
>> the
>> > stack to begin with as the first X was an automatic variable in a
>> function.
>> >  I'd be a little surprised to find an automatic variable in the text
>> > segment, but perhaps that's just my not remembering things properly.
>> >  (didn't mean that tongue in cheek, I don't think about that stuff much
>> > these days, as I've spent the last year or so doing Erlang and Haskell.)
>>
>> it is the block itself that apple claims is on the stacp
>> (your grand centeral reference, p. 38).  and i wonder
>> how it gets there.  is it just copied from the text segment?
>> that seems kind of pointless.  why not just execute it
>> from the text segment?  or is it modified (compiled?)
>> at run time?
>>
>
> Right, my understanding is that blocks live on the stack, however it
> appears that they get copied to a particular queue before being run, either
> explicitly or implicitly depending on how it gets submitted.  If you use
> dispatch_after, it gets copied and released automatically after a certain
> amount of time.
>
> The whole thing is very "eventy".
>
> Some of the documentation is not terribly explicit about how things get
> copied if they do at all.  Example is the dispatch_async call, which behaves
> based on the queue to which it is submitted, but whether or not a copy
> occurs is not mentioned.
>
> This document goes into more detail:
> http://clang.llvm.org/docs/BlockImplementation.txt
>
> "Block literals may occur within functions where the structure is created
> in stack local memory. They may also appear as initialization expressions
> for Block variables of global or static local variables.
>
> When a Block literal expression is evaluated the stack based structure is initialized as follows:
>
> 1) static descriptor structure is declared and initialized as follows:
> 1a) the invoke function pointer is set to a function that takes the Block structure as its first argument and the rest of the arguments (if any) to the Block and executes the Block compound statement.
> 1b) the size field is set to the size of the following Block literal structure.
> 1c) the copy_helper and dispose_helper function pointers are set to respective helper functions if they are required by the Block literal
> 2) a stack (or global) Block literal data structure is created and initialized as follows:
> 2a) the isa field is set to the address of the external _NSConcreteStackBlock, which is a block of uninitialized memory supplied in libSystem, or _NSConcreteGlobalBlock if this is a static or file level block literal.
> 2) The flags field is set to zero unless there are variables imported into the block that need helper functions for program level Block_copy() and Block_release() operations, in which case the (1<<25) flags bit is set."
>
> I'm still left feeling somewhat not understanding how it all works :-)
>
>
>

Actually, reading on a bit more they deal with the "variable capture"
talking about const copies.

Automatic storage variables not marked with __block are imported as
const copies.

The simplest example is that of importing a variable of type int.

   int x = 10;
   void (^vv)(void) = ^{ printf("x is %d\n", x); }
   x = 11;
   vv();

would be compiled

struct __block_literal_2 {
    void *isa;
    int flags;
    int reserved;
    void (*invoke)(struct __block_literal_2 *);
    struct __block_descriptor_2 *descriptor;
    const int x;
};

void __block_invoke_2(struct __block_literal_2 *_block) {
    printf("x is %d\n", _block->x);
}

static struct __block_descriptor_2 {
    unsigned long int reserved;
    unsigned long int Block_size;
} __block_descriptor_2 = { 0, sizeof(struct __block_literal_2) };

and

  struct __block_literal_2 __block_literal_2 = {
	&_NSConcreteStackBlock,
	(1<<29), <uninitialized>,
	__block_invoke_2,
	&__block_descriptor_2,
        x
   };

In summary, scalars, structures, unions, and function pointers are
generally imported as const copies with no need for helper functions.




>
>> - erik
>>
>>
>

[-- Attachment #2: Type: text/html, Size: 6457 bytes --]

  reply	other threads:[~2009-09-04  5:35 UTC|newest]

Thread overview: 117+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-02  8:04 Anant Narayanan
2009-09-02  9:43 ` Francisco J Ballesteros
2009-09-02 11:00   ` Philippe Anel
2009-09-02 11:13   ` Charles Forsyth
2009-09-03  9:52   ` Greg Comeau
2009-09-02 11:40 ` Eris Discordia
2009-09-02 12:32 ` Uriel
2009-09-02 14:20   ` Devon H. O'Dell
2009-09-03  9:52     ` Greg Comeau
2009-09-03 10:48       ` Akshat Kumar
2009-09-03 11:25         ` Charles Forsyth
2009-09-03 13:59         ` Greg Comeau
2009-09-03 15:15     ` Uriel
2009-09-03 15:44       ` David Leimbach
2009-09-03 16:01         ` Roman V Shaposhnik
2009-09-04  9:15           ` Greg Comeau
2009-09-04 16:49             ` Roman Shaposhnik
2009-09-03 16:02         ` erik quanstrom
2009-09-03 18:56           ` David Leimbach
2009-09-03 18:58             ` erik quanstrom
2009-09-03 19:13               ` David Leimbach
2009-09-03 19:36                 ` erik quanstrom
2009-09-03 19:50                   ` David Leimbach
2009-09-03 20:30                     ` Iruata Souza
2009-09-06 22:35                     ` Uriel
2009-09-07  0:41                       ` David Leimbach
2009-09-03 21:08                   ` Roman V Shaposhnik
2009-09-03 21:35                     ` erik quanstrom
2009-09-03 21:45                       ` David Leimbach
2009-09-03 21:49                         ` David Leimbach
2009-09-03 21:51                           ` David Leimbach
2009-09-03 22:36                       ` Roman V Shaposhnik
2009-09-04  9:16               ` Greg Comeau
2009-09-03 22:10           ` Daniel Lyons
2009-09-03 22:07         ` Daniel Lyons
2009-09-04  9:15         ` Greg Comeau
2009-09-04  3:21       ` Anant Narayanan
2009-09-04  3:52         ` erik quanstrom
2009-09-04  4:18           ` David Leimbach
2009-09-04  4:44             ` erik quanstrom
2009-09-04  5:31               ` David Leimbach
2009-09-04  5:35                 ` David Leimbach [this message]
2009-09-04  7:11                   ` Bakul Shah
2009-09-04  7:47                     ` David Leimbach
2009-09-04 14:41                       ` Bakul Shah
2009-09-04 15:04                         ` David Leimbach
2009-09-04 15:58                           ` Bakul Shah
2009-09-04 12:14                     ` erik quanstrom
2009-09-04 13:52                       ` David Leimbach
2009-09-04 13:59                         ` matt
2009-09-04 14:20                         ` erik quanstrom
2009-09-04 14:56                           ` David Leimbach
2009-09-06 23:03                             ` Uriel
2009-09-07  0:45                               ` David Leimbach
2009-09-04 16:50                       ` Roman Shaposhnik
2009-09-04  6:45               ` Bakul Shah
2009-09-04 16:44           ` Roman Shaposhnik
2009-09-04 16:58             ` Iruata Souza
2009-09-04 17:09               ` Roman Shaposhnik
2009-09-04 17:42             ` erik quanstrom
2009-09-04 18:34               ` erik quanstrom
2009-09-04 21:54               ` Roman V Shaposhnik
2009-09-07  9:06             ` Greg Comeau
2009-09-07  9:05         ` Greg Comeau
2009-09-07  9:40           ` Uriel
2009-09-08 15:31             ` David Leimbach
2009-09-08 15:44               ` Uriel
2009-09-08 16:40               ` Bakul Shah
2009-09-11 18:15                 ` Iruata Souza
2009-09-11 18:46                   ` David Leimbach
2009-09-11 20:36                   ` Roman V Shaposhnik
2009-09-11 21:28                     ` David Leimbach
2009-09-14 16:07                       ` Lawrence E. Bakst
2009-09-12 11:08                     ` Anant Narayanan
2009-09-13 19:03                       ` David Leimbach
2009-09-17  1:54                         ` Lawrence E. Bakst
2009-09-17  8:43                           ` Charles Forsyth
2009-09-17  9:12                             ` Daniel Lyons
2009-09-17 15:56                             ` David Leimbach
2009-09-17 17:38                               ` Jack Norton
2009-09-17 20:23                                 ` Anant Narayanan
2009-09-17 20:26                                   ` erik quanstrom
2009-09-17 20:31                                     ` Anant Narayanan
2009-09-17 20:47                                       ` Akshat Kumar
2009-09-17 21:02                                         ` erik quanstrom
2009-09-17 22:26                                           ` Steve Simon
2009-09-17 22:32                                           ` Roman V Shaposhnik
2009-09-18  2:05                                             ` Daniel Lyons
2009-09-18  9:30                                             ` Charles Forsyth
2009-09-18 11:41                                               ` erik quanstrom
2009-09-17 21:03                                       ` Uriel
2009-09-03  9:52   ` Greg Comeau
2009-09-02 14:08 ` Rodolfo (kix)
2009-09-03  9:52   ` Greg Comeau
     [not found] ` <C56117D7BD9A097270529AAE@192.168.1.2>
2009-09-02 15:07   ` David Leimbach
2009-09-02 15:26     ` Roman V Shaposhnik
2009-09-02 15:45       ` David Leimbach
2009-09-03 15:32     ` Uriel
2009-09-03 15:49       ` Roman V Shaposhnik
2009-09-03 15:54         ` erik quanstrom
2009-09-03 16:20           ` Roman V Shaposhnik
2009-09-03 16:44             ` erik quanstrom
2009-09-04  1:05               ` Roman V Shaposhnik
2009-09-04  1:30               ` Russ Cox
2009-09-04  1:32                 ` erik quanstrom
2009-09-03 21:54             ` James Tomaschke
2009-09-06 22:26           ` Uriel
2009-09-04  9:04       ` Greg Comeau
2009-09-02 15:20 ` Roman V Shaposhnik
2009-09-02 17:51   ` Bakul Shah
2009-09-04  1:35 drivers
2009-09-04  1:52 ` erik quanstrom
2009-09-17  9:19 Andrew Simmons
2009-09-17 14:45 ` LiteStar numnums
2009-09-17 17:24 ` Daniel Lyons
2009-09-18 15:50 drivers
2009-09-18 15:56 ` erik quanstrom

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3e1162e60909032235s6e5a67dau6109f30246f255d@mail.gmail.com \
    --to=leimy2k@gmail.com \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).