comparison lispref/internals.texi @ 70576:97c00016e50b

(Writing Emacs Primitives): Clarify GCPRO rules.
author Richard M. Stallman <rms@gnu.org>
date Thu, 11 May 2006 00:59:35 +0000
parents f7aff7b6d4af
children e610fb70748a a5812696f7bf
comparison
equal deleted inserted replaced
70575:aaf48e9df47b 70576:97c00016e50b
613 receives exactly two arguments: the first is the number of Lisp 613 receives exactly two arguments: the first is the number of Lisp
614 arguments, and the second is the address of a block containing their 614 arguments, and the second is the address of a block containing their
615 values. They have types @code{int} and @w{@code{Lisp_Object *}}. 615 values. They have types @code{int} and @w{@code{Lisp_Object *}}.
616 616
617 Within the function @code{For} itself, note the use of the macros 617 Within the function @code{For} itself, note the use of the macros
618 @code{GCPRO1} and @code{UNGCPRO}. @code{GCPRO1} is used to ``protect'' 618 @code{GCPRO1} and @code{UNGCPRO}. @code{GCPRO1} is used to
619 a variable from garbage collection---to inform the garbage collector that 619 ``protect'' a variable from garbage collection---to inform the garbage
620 it must look in that variable and regard its contents as an accessible 620 collector that it must look in that variable and regard its contents
621 object. This is necessary whenever you call @code{Feval} or anything 621 as an accessible object. GC protection is necessary whenever you call
622 that can directly or indirectly call @code{Feval}. At such a time, any 622 @code{Feval} or anything that can directly or indirectly call
623 Lisp object that you intend to refer to again must be protected somehow. 623 @code{Feval}. At such a time, any Lisp object that this function may
624 @code{UNGCPRO} cancels the protection of the variables that are 624 refer to again must be protected somehow.
625 protected in the current function. It is necessary to do this explicitly.
626 625
627 It suffices to ensure that at least one pointer to each object is 626 It suffices to ensure that at least one pointer to each object is
628 GC-protected; as long as the object is not recycled, all pointers to 627 GC-protected; that way, the object cannot be recycled, so all pointers
629 it remain valid. So if you are sure that a local variable points to 628 to it remain valid. Thus, a particular local variable can do without
630 an object that will be preserved by some other pointer, that local 629 protection if it is certain that the object it points to will be
631 variable does not need a @code{GCPRO}. (Formerly, strings were an 630 preserved by some other pointer (such as another local variable which
632 exception to this rule; in older Emacs versions, every pointer to a 631 has a @code{GCPRO})@footnote{Formerly, strings were a special
633 string needed to be marked by GC.) 632 exception; in older Emacs versions, every local variable that might
633 point to a string needed a @code{GCPRO}.}. Otherwise, the local
634 variable needs a @code{GCPRO}.
634 635
635 The macro @code{GCPRO1} protects just one local variable. If you 636 The macro @code{GCPRO1} protects just one local variable. If you
636 want to protect two, use @code{GCPRO2} instead; repeating 637 want to protect two variables, use @code{GCPRO2} instead; repeating
637 @code{GCPRO1} will not work. Macros, @code{GCPRO3}, @code{GCPRO4}, 638 @code{GCPRO1} will not work. Macros @code{GCPRO3}, @code{GCPRO4},
638 @code{GCPRO5}, and @code{GCPRO6} also exist. These macros implicitly 639 @code{GCPRO5}, and @code{GCPRO6} also exist. All these macros
639 use local variables such as @code{gcpro1}; you must declare these 640 implicitly use local variables such as @code{gcpro1}; you must declare
640 explicitly, with type @code{struct gcpro}. Thus, if you use 641 these explicitly, with type @code{struct gcpro}. Thus, if you use
641 @code{GCPRO2}, you must declare @code{gcpro1} and @code{gcpro2}. 642 @code{GCPRO2}, you must declare @code{gcpro1} and @code{gcpro2}.
642 Alas, we can't explain all the tricky details here. 643 Alas, we can't explain all the tricky details here.
644
645 @code{UNGCPRO} cancels the protection of the variables that are
646 protected in the current function. It is necessary to do this
647 explicitly.
643 648
644 Built-in functions that take a variable number of arguments actually 649 Built-in functions that take a variable number of arguments actually
645 accept two arguments at the C level: the number of Lisp arguments, and 650 accept two arguments at the C level: the number of Lisp arguments, and
646 a @code{Lisp_Object *} pointer to a C vector containing those Lisp 651 a @code{Lisp_Object *} pointer to a C vector containing those Lisp
647 arguments. This C vector may be part of a Lisp vector, but it need 652 arguments. This C vector may be part of a Lisp vector, but it need