1 @node Funcallable Instances
2 @comment node-name, next, previous, up
3 @chapter Funcallable Instances
6 * Overview of Funcallable Instances::
7 * Implementation of Funcallable Instances::
10 @node Overview of Funcallable Instances
11 @section Overview of Funcallable Instances
13 Funcallable instances in SBCL are implemented as a subtype of
14 @code{function}, and as such must be directly funcallable using the same
15 calling sequence as ordinary functions and closure objects, which means
16 reading the first word of the object after the header, and then jumping
17 to it (with an offset on non-x86 platforms). It must be possible to set
18 the function of a funcallable instance, as CLOS (one user of funcallable
19 instances) computes and sets the discriminating function for generic
20 functions with @code{sb-mop:set-funcallable-instance-function}, and also
21 allows the user to do the same.
23 Additionally, although this functionality is not exported to the normal
24 user, they must support an arbitrary number of slots definable with
25 @code{!defstruct-with-alternate-metaclass}. If generic functions were
26 the only users of funcallable instances, then this might be less
27 critical, but (as of SBCL 0.9.17) other users of funcallable instances
28 are: the @code{ctor} make-instance optimization; the
29 @code{method-function} funcallable instance which does the bookkeeping
30 for fast method function optimization; and interpreted functions in the
33 @node Implementation of Funcallable Instances
34 @section Implementation of Funcallable Instances
36 The first word after the header of a funcallable instance points to a
37 dedicated trampoline function (known as
38 @code{funcallable_instance_tramp} in SBCL 0.9.17) which is responsible
39 for calling the funcallable instance function, kept in the second word
40 after the header. The remaining words of a funcallable instance are
41 firstly the @code{layout}, and then the slots.
43 The implementation of funcallable instances inherited from CMUCL
44 differed in that there were two slots for the function: one for the
45 underlying @code{simple-fun}, and one for the function itself (which is
46 distinct from the @code{simple-fun} in the case of a closure. This,
47 coupled with an instruction in the prologue of a closure's function to
48 fetch the function from the latter slot, allowed a trampolineless
49 calling sequence for funcallable instances; however, drawbacks included
50 the loss of object identity for the funcallable instance function (if a
51 funcallable instance was set as the function of another, updates to the
52 first would not be reflected in calls to the second) and, more
53 importantly, a race condition in calling funcallable instances from one
54 thread while setting its funcallable instance function in another. The
55 current implementation, described in the paragraph above, does not
56 suffer from these problems (the function of a funcallable instance can
57 be set atomically and retains its identity) at the cost of an additional