164 lines
13 KiB
HTML
164 lines
13 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Declaration DYNAMIC-EXTENT</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="d_ignore.htm">
|
|
<LINK REL=UP HREF="c_evalua.htm">
|
|
<LINK REL=NEXT HREF="d_type.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="d_ignore.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_evalua.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="d_type.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="dynamic-extent"><I>Declaration</I> <B>DYNAMIC-EXTENT</B></A> <P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
<TT>(dynamic-extent </TT>[[<TT></TT><I>var</I><TT></TT><B>*</B><TT> </TT>|<TT> </TT>(<TT><A REL=DEFINITION HREF="a_fn.htm#function"></TT><B>function</B><TT></A> </TT><I>fn</I><TT></TT>)<TT></TT>*<TT></TT>]]<TT>)</TT> <P>
|
|
<P><B>Arguments:</B><P>
|
|
<P>
|
|
<I>var</I>---a <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variable</I></A> <A REL=DEFINITION HREF="26_glo_n.htm#name"><I>name</I></A>. <P>
|
|
<I>fn</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> <A REL=DEFINITION HREF="26_glo_n.htm#name"><I>name</I></A>. <P>
|
|
<P><B>Valid Context:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="26_glo_d.htm#declaration"><I>declaration</I></A> <P>
|
|
<P><B>Binding Types Affected:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variable</I></A>, <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
<P>
|
|
In some containing <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>, <I>F</I>, this declaration asserts for each <I>vari</I> (which need not be bound by <I>F</I>), and for each <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>value</I></A> <I>vij</I> that <I>vari</I> takes on, and for each <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> <I>xijk</I> that is an <A REL=DEFINITION HREF="26_glo_o.htm#otherwise_inaccessible_part"><I>otherwise inaccessible part</I></A> of <I>vij</I> at any time when <I>vij</I> becomes the value of <I>vari</I>, that just after the execution of <I>F</I> terminates, <I>xijk</I> is either <A REL=DEFINITION HREF="26_glo_i.htm#inaccessible"><I>inaccessible</I></A> (if <I>F</I> established a <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> for <I>vari</I>) or still an <A REL=DEFINITION HREF="26_glo_o.htm#otherwise_inaccessible_part"><I>otherwise inaccessible part</I></A> of the current value of <I>vari</I> (if <I>F</I> did not establish a <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> for <I>vari</I>). The same relation holds for each <I>fni</I>, except that the <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> are in the <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> <A REL=DEFINITION HREF="26_glo_n.htm#namespace"><I>namespace</I></A>. <P>
|
|
The compiler is permitted to use this information in any way that is appropriate to the <A REL=DEFINITION HREF="26_glo_i.htm#implementation"><I>implementation</I></A> and that does not conflict with the semantics of Common Lisp. <P>
|
|
<A REL=DEFINITION HREF="#dynamic-extent"><B>dynamic-extent</B></A> declarations can be <A REL=DEFINITION HREF="26_glo_f.htm#free_declaration"><I>free declarations</I></A> or <A REL=DEFINITION HREF="26_glo_b.htm#bound_declaration"><I>bound declarations</I></A>. <P>
|
|
The <I>vars</I> and <I>fns</I> named in a <A REL=DEFINITION HREF="#dynamic-extent"><B>dynamic-extent</B></A> declaration must not refer to <A REL=DEFINITION HREF="26_glo_s.htm#symbol_macro"><I>symbol macro</I></A> or <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macro</I></A> bindings. <P>
|
|
<P><B>Examples:</B><P>
|
|
<P>
|
|
Since stack allocation of the initial value entails knowing at the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>'s creation time that the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> can be <A REL=DEFINITION HREF="26_glo_s.htm#stack-allocated"><I>stack-allocated</I></A>, it is not generally useful to make a <A REL=DEFINITION HREF="#dynamic-extent"><B>dynamic-extent</B></A> <A REL=DEFINITION HREF="26_glo_d.htm#declaration"><I>declaration</I></A> for <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variables</I></A> which have no lexically apparent initial value. For example, it is probably useful to write: <P>
|
|
<PRE>
|
|
(defun f ()
|
|
(let ((x (list 1 2 3)))
|
|
(declare (dynamic-extent x))
|
|
...))
|
|
</PRE>
|
|
</TT> <P>
|
|
This would permit those compilers that wish to do so to <A REL=DEFINITION HREF="26_glo_s.htm#stack_allocate"><I>stack allocate</I></A> the list held by the local variable <TT>x</TT>. It is permissible, but in practice probably not as useful, to write: <P>
|
|
<PRE>
|
|
(defun g (x) (declare (dynamic-extent x)) ...)
|
|
(defun f () (g (list 1 2 3)))
|
|
</PRE>
|
|
</TT> <P>
|
|
Most compilers would probably not <A REL=DEFINITION HREF="26_glo_s.htm#stack_allocate"><I>stack allocate</I></A> the <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>argument</I></A> to <TT>g</TT> in <TT>f</TT> because it would be a modularity violation for the compiler to assume facts about <TT>g</TT> from within <TT>f</TT>. Only an implementation that was willing to be responsible for recompiling <TT>f</TT> if the definition of <TT>g</TT> changed incompatibly could legitimately <A REL=DEFINITION HREF="26_glo_s.htm#stack_allocate"><I>stack allocate</I></A> the <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> argument to <TT>g</TT> in <TT>f</TT>. <P>
|
|
Here is another example: <P>
|
|
<PRE>
|
|
(declaim (inline g))
|
|
(defun g (x) (declare (dynamic-extent x)) ...)
|
|
(defun f () (g (list 1 2 3)))
|
|
|
|
(defun f ()
|
|
(flet ((g (x) (declare (dynamic-extent x)) ...))
|
|
(g (list 1 2 3))))
|
|
|
|
</PRE>
|
|
</TT> In the previous example, some compilers might determine that optimization was possible and others might not. <P>
|
|
A variant of this is the so-called ``stack allocated rest list'' that can be achieved (in implementations supporting the optimization) by: <P>
|
|
<PRE>
|
|
(defun f (&rest x)
|
|
(declare (dynamic-extent x))
|
|
...)
|
|
</PRE>
|
|
</TT> <P>
|
|
Note that although the initial value of <TT>x</TT> is not explicit, the <TT>f</TT> function is responsible for assembling the list <TT>x</TT> from the passed arguments, so the <TT>f</TT> function can be optimized by the compiler to construct a <A REL=DEFINITION HREF="26_glo_s.htm#stack-allocated"><I>stack-allocated</I></A> list instead of a heap-allocated list in implementations that support such. <P>
|
|
In the following example, <P>
|
|
<PRE>
|
|
(let ((x (list 'a1 'b1 'c1))
|
|
(y (cons 'a2 (cons 'b2 (cons 'c2 nil)))))
|
|
(declare (dynamic-extent x y))
|
|
...)
|
|
</PRE>
|
|
</TT> The <A REL=DEFINITION HREF="26_glo_o.htm#otherwise_inaccessible_part"><I>otherwise inaccessible parts</I></A> of <TT>x</TT> are three <A REL=DEFINITION HREF="26_glo_c.htm#cons"><I>conses</I></A>, and the <A REL=DEFINITION HREF="26_glo_o.htm#otherwise_inaccessible_part"><I>otherwise inaccessible parts</I></A> of <TT>y</TT> are three other <A REL=DEFINITION HREF="26_glo_c.htm#cons"><I>conses</I></A>. None of the symbols <TT>a1</TT>, <TT>b1</TT>, <TT>c1</TT>, <TT>a2</TT>, <TT>b2</TT>, <TT>c2</TT>, or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> is an <A REL=DEFINITION HREF="26_glo_o.htm#otherwise_inaccessible_part"><I>otherwise inaccessible part</I></A> of <TT>x</TT> or <TT>y</TT> because each is <A REL=DEFINITION HREF="26_glo_i.htm#interned"><I>interned</I></A> and hence <A REL=DEFINITION HREF="26_glo_a.htm#accessible"><I>accessible</I></A> by the <A REL=DEFINITION HREF="26_glo_p.htm#package"><I>package</I></A> (or <A REL=DEFINITION HREF="26_glo_p.htm#package"><I>packages</I></A>) in which it is <A REL=DEFINITION HREF="26_glo_i.htm#interned"><I>interned</I></A>. However, if a freshly allocated <A REL=DEFINITION HREF="26_glo_u.htm#uninterned"><I>uninterned</I></A> <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A> had been used, it would have been an <A REL=DEFINITION HREF="26_glo_o.htm#otherwise_inaccessible_part"><I>otherwise inaccessible part</I></A> of the <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> which contained it. <P>
|
|
<PRE>
|
|
;; In this example, the implementation is permitted to <A REL=DEFINITION HREF="26_glo_s.htm#stack_allocate">stack allocate</A>
|
|
;; the list that is bound to X.
|
|
(let ((x (list 1 2 3)))
|
|
(declare (dynamic-extent x))
|
|
(print x)
|
|
:done)
|
|
>> (1 2 3)
|
|
=> :DONE
|
|
|
|
;; In this example, the list to be bound to L can be <A REL=DEFINITION HREF="26_glo_s.htm#stack-allocated">stack-allocated</A>.
|
|
(defun zap (x y z)
|
|
(do ((l (list x y z) (cdr l)))
|
|
((null l))
|
|
(declare (dynamic-extent l))
|
|
(prin1 (car l)))) => ZAP
|
|
(zap 1 2 3)
|
|
>> 123
|
|
=> NIL
|
|
|
|
;; Some implementations might open-code LIST-ALL-PACKAGES in a way
|
|
;; that permits using stack allocation of the list to be bound to L.
|
|
(do ((l (list-all-packages) (cdr l)))
|
|
((null l))
|
|
(declare (dynamic-extent l))
|
|
(let ((name (package-name (car l))))
|
|
(when (string-search "COMMON-LISP" name) (print name))))
|
|
>> "COMMON-LISP"
|
|
>> "COMMON-LISP-USER"
|
|
=> NIL
|
|
|
|
;; Some implementations might have the ability to <A REL=DEFINITION HREF="26_glo_s.htm#stack_allocate">stack allocate</A>
|
|
;; rest lists. A declaration such as the following should be a cue
|
|
;; to such implementations that stack-allocation of the rest list
|
|
;; would be desirable.
|
|
(defun add (&rest x)
|
|
(declare (dynamic-extent x))
|
|
(apply #'+ x)) => ADD
|
|
(add 1 2 3) => 6
|
|
|
|
(defun zap (n m)
|
|
;; Computes (RANDOM (+ M 1)) at relative speed of roughly O(N).
|
|
;; It may be slow, but with a good compiler at least it
|
|
;; doesn't waste much heap storage. :-}
|
|
(let ((a (make-array n)))
|
|
(declare (dynamic-extent a))
|
|
(dotimes (i n)
|
|
(declare (dynamic-extent i))
|
|
(setf (aref a i) (random (+ i 1))))
|
|
(aref a m))) => ZAP
|
|
(< (zap 5 3) 3) => <A REL=DEFINITION HREF="26_glo_t.htm#true">true</A>
|
|
</PRE>
|
|
</TT> <P>
|
|
The following are in error, since the value of <TT>x</TT> is used outside of its <A REL=DEFINITION HREF="26_glo_e.htm#extent"><I>extent</I></A>: <P>
|
|
<PRE>
|
|
(length (list (let ((x (list 1 2 3))) ; Invalid
|
|
(declare (dynamic-extent x))
|
|
x)))
|
|
|
|
(progn (let ((x (list 1 2 3))) ; Invalid
|
|
(declare (dynamic-extent x))
|
|
x)
|
|
nil)
|
|
</PRE>
|
|
</TT> <P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="s_declar.htm#declare"><B>declare</B></A> <P>
|
|
<P><B>Notes:</B><P>
|
|
<P>
|
|
The most common optimization is to <A REL=DEFINITION HREF="26_glo_s.htm#stack_allocate"><I>stack allocate</I></A> the initial value of the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>objects</I></A> named by the <I>vars</I>. <P>
|
|
It is permissible for an implementation to simply ignore this declaration. <P>
|
|
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss228.htm">MACRO-DECLARATIONS:MAKE-EXPLICIT</A><LI> <A REL=CHILD HREF="../Issues/iss141.htm">DYNAMIC-EXTENT-FUNCTION:EXTEND</A><LI> <A REL=CHILD HREF="../Issues/iss142.htm">DYNAMIC-EXTENT:NEW-DECLARATION</A><P></UL><HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|