From owner-freebsd-arch@FreeBSD.ORG Tue Oct 6 19:50:25 2009 Return-Path: Delivered-To: arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 79249106566B; Tue, 6 Oct 2009 19:50:25 +0000 (UTC) (envelope-from cswiger@mac.com) Received: from asmtpout018.mac.com (asmtpout018.mac.com [17.148.16.93]) by mx1.freebsd.org (Postfix) with ESMTP id 6332D8FC28; Tue, 6 Oct 2009 19:50:25 +0000 (UTC) MIME-version: 1.0 Content-transfer-encoding: 7BIT Content-type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Received: from cswiger1.apple.com ([17.227.140.124]) by asmtp018.mac.com (Sun Java(tm) System Messaging Server 6.3-8.01 (built Dec 16 2008; 32bit)) with ESMTPSA id <0KR3004PRWBRGK20@asmtp018.mac.com>; Tue, 06 Oct 2009 11:50:20 -0700 (PDT) Message-id: <2097B9F8-B96F-4A37-B1D1-D094D65211F4@mac.com> From: Chuck Swiger To: Hans Petter Selasky In-reply-to: <200910021440.50021.hselasky@freebsd.org> Date: Tue, 06 Oct 2009 11:50:14 -0700 References: <200910021440.50021.hselasky@freebsd.org> X-Mailer: Apple Mail (2.936) Cc: arch@freebsd.org, freebsd-current@freebsd.org, Robert Watson Subject: Re: [libdispatch-dev] GCD libdispatch w/Blocks support working on Free (f X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 06 Oct 2009 19:50:25 -0000 Hi, Hans-- On Oct 2, 2009, at 5:40 AM, Hans Petter Selasky wrote: > Can the Apple's "Blocks" C language extension be used when > programming in the kernel? Or is this a user-space only feature? While the main benefit of blocks is in conjunction with libdispatch for userland apps, they can be used by themselves, in the kernel or elsewhere. A block is a closure and starts off living on the stack (although, a block can outlive the stack frame of the caller by being copied over to the heap if needed); the compiler wraps automatic variables which were around in the scope of the caller, turns their type into const (unless you explicitly declare that you need to change a variable by using __block storage keyword, in which case that variable is kept on the heap and accessed by reference) in order to preserve the state until the block runs. In effect, blocks are normal function invocations which also have an extra argument which is the context or pointer to the saved environment state. They can be used to implement callbacks and continuations in a clean way, although you do have some overhead with accessing mutable variables via pointer dereference to the struct holding your saved context. However, most uses of callbacks in C are implemented as functions which accept a void *, which is used to feed the callback function a struct * of some sort, so the end result is fairly similar. Regards, -- -Chuck