2007-09-06 04:25:08 +00:00
|
|
|
@c -*-texinfo-*-
|
|
|
|
@c This is part of the GNU Emacs Lisp Reference Manual.
|
2025-01-01 07:39:17 +00:00
|
|
|
@c Copyright (C) 1990--1993, 1998--1999, 2001--2025 Free Software
|
2013-01-01 09:11:05 +00:00
|
|
|
@c Foundation, Inc.
|
2007-09-06 04:25:08 +00:00
|
|
|
@c See the file elisp.texi for copying conditions.
|
2012-05-26 18:34:14 -07:00
|
|
|
@node GNU Emacs Internals
|
2007-09-06 04:25:08 +00:00
|
|
|
@appendix GNU Emacs Internals
|
|
|
|
|
|
|
|
This chapter describes how the runnable Emacs executable is dumped with
|
|
|
|
the preloaded Lisp libraries in it, how storage is allocated, and some
|
|
|
|
internal aspects of GNU Emacs that may be of interest to C programmers.
|
|
|
|
|
|
|
|
@menu
|
|
|
|
* Building Emacs:: How the dumped Emacs is made.
|
|
|
|
* Garbage Collection:: Reclaiming space for Lisp objects no longer used.
|
2014-09-30 19:35:16 +04:00
|
|
|
* Stack-allocated Objects:: Temporary conses and strings on C stack.
|
2007-09-06 04:25:08 +00:00
|
|
|
* Memory Usage:: Info about total size of Lisp objects made so far.
|
2014-05-07 13:51:35 -07:00
|
|
|
* C Dialect:: What C variant Emacs is written in.
|
2007-09-06 04:25:08 +00:00
|
|
|
* Writing Emacs Primitives:: Writing C code for Emacs.
|
2018-10-11 20:53:05 +03:00
|
|
|
* Writing Dynamic Modules:: Writing loadable modules for Emacs.
|
2007-09-06 04:25:08 +00:00
|
|
|
* Object Internals:: Data formats of buffers, windows, processes.
|
2012-12-10 16:13:44 -08:00
|
|
|
* C Integer Types:: How C integer types are used inside Emacs.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Building Emacs
|
2012-03-02 21:45:21 -05:00
|
|
|
@section Building Emacs
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex building Emacs
|
|
|
|
@pindex temacs
|
|
|
|
|
|
|
|
This section explains the steps involved in building the Emacs
|
|
|
|
executable. You don't have to know this material to build and install
|
|
|
|
Emacs, since the makefiles do all these things automatically. This
|
2012-03-18 23:02:12 +08:00
|
|
|
information is pertinent to Emacs developers.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2015-11-14 18:12:25 +02:00
|
|
|
Building Emacs requires GNU Make version 3.81 or later.
|
|
|
|
|
|
|
|
Compilation of the C source files in the @file{src} directory
|
2007-09-06 04:25:08 +00:00
|
|
|
produces an executable file called @file{temacs}, also called a
|
2012-03-18 23:02:12 +08:00
|
|
|
@dfn{bare impure Emacs}. It contains the Emacs Lisp interpreter and
|
|
|
|
I/O routines, but not the editing commands.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@cindex @file{loadup.el}
|
2012-03-18 23:02:12 +08:00
|
|
|
The command @w{@command{temacs -l loadup}} would run @file{temacs}
|
|
|
|
and direct it to load @file{loadup.el}. The @code{loadup} library
|
|
|
|
loads additional Lisp libraries, which set up the normal Emacs editing
|
|
|
|
environment. After this step, the Emacs executable is no longer
|
|
|
|
@dfn{bare}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@cindex dumping Emacs
|
2019-01-18 17:58:47 +02:00
|
|
|
@cindex @option{--temacs} option, and dumping method
|
2012-03-18 23:02:12 +08:00
|
|
|
Because it takes some time to load the standard Lisp files, the
|
|
|
|
@file{temacs} executable usually isn't run directly by users.
|
2019-01-18 17:58:47 +02:00
|
|
|
Instead, one of the last steps of building Emacs runs the command
|
|
|
|
@w{@samp{temacs -batch -l loadup --temacs=@var{dump-method}}}. The
|
|
|
|
special option @option{--temacs} tells @command{temacs} how to record
|
|
|
|
all the standard preloaded Lisp functions and variables, so that when
|
|
|
|
you subsequently run Emacs, it will start much faster. The
|
|
|
|
@option{--temacs} option requires an argument @var{dump-method}, which
|
|
|
|
can be one of the following:
|
|
|
|
|
|
|
|
@table @samp
|
|
|
|
@item pdump
|
2019-07-23 09:19:09 -07:00
|
|
|
@cindex dump file
|
|
|
|
Record the preloaded Lisp data in a @dfn{dump file}. This
|
2019-01-18 17:58:47 +02:00
|
|
|
method produces an additional data file which Emacs will load at
|
2019-07-23 09:19:09 -07:00
|
|
|
startup. The produced dump file is usually called @file{emacs.pdmp},
|
2019-01-29 18:58:54 +02:00
|
|
|
and is installed in the Emacs @code{exec-directory} (@pxref{Help
|
2019-01-18 17:58:47 +02:00
|
|
|
Functions}). This method is the most preferred one, as it does not
|
|
|
|
require Emacs to employ any special techniques of memory allocation,
|
|
|
|
which might get in the way of various memory-layout techniques used by
|
|
|
|
modern systems to enhance security and privacy.
|
|
|
|
|
|
|
|
@item pbootstrap
|
|
|
|
@cindex bootstrapping Emacs
|
|
|
|
Like @samp{pdump}, but used while @dfn{bootstrapping} Emacs, when no
|
|
|
|
previous Emacs binary and no @file{*.elc} byte-compiled Lisp files are
|
2019-07-23 09:19:09 -07:00
|
|
|
available. The produced dump file is usually named
|
2019-01-18 17:58:47 +02:00
|
|
|
@file{bootstrap-emacs.pdmp} in this case.
|
|
|
|
@end table
|
2012-03-18 23:02:12 +08:00
|
|
|
|
|
|
|
@cindex preloaded Lisp files
|
2009-09-22 02:24:20 +00:00
|
|
|
@vindex preloaded-file-list
|
2012-03-18 23:02:12 +08:00
|
|
|
The dumped @file{emacs} executable (also called a @dfn{pure} Emacs)
|
2019-07-23 09:19:09 -07:00
|
|
|
is the one which is installed. If the portable dumper was used to
|
2019-01-18 17:58:47 +02:00
|
|
|
build Emacs, the @file{emacs} executable is actually an exact copy of
|
|
|
|
@file{temacs}, and the corresponding @file{emacs.pdmp} file is
|
|
|
|
installed as well. The variable @code{preloaded-file-list} stores a
|
2019-07-23 09:19:09 -07:00
|
|
|
list of the preloaded Lisp files recorded in the dump file or
|
2019-01-18 17:58:47 +02:00
|
|
|
in the dumped Emacs executable. If you port Emacs to a new operating
|
|
|
|
system, and are not able to implement dumping of any kind, then Emacs
|
|
|
|
must load @file{loadup.el} each time it starts.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2016-03-02 10:21:45 -08:00
|
|
|
@cindex build details
|
|
|
|
@cindex deterministic build
|
|
|
|
@cindex @option{--disable-build-details} option to @command{configure}
|
|
|
|
By default the dumped @file{emacs} executable records details such
|
|
|
|
as the build time and host name. Use the
|
|
|
|
@option{--disable-build-details} option of @command{configure} to
|
|
|
|
suppress these details, so that building and installing Emacs twice
|
|
|
|
from the same sources is more likely to result in identical copies of
|
|
|
|
Emacs.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex @file{site-load.el}
|
|
|
|
You can specify additional files to preload by writing a library named
|
2024-12-15 19:13:35 +01:00
|
|
|
@file{site-load.el} that loads them. However, the advantage of
|
|
|
|
preloading additional files decreases as machines get faster. On modern
|
|
|
|
machines, it is usually not advisable.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
After @file{loadup.el} reads @file{site-load.el}, it finds the
|
|
|
|
documentation strings for primitive and preloaded functions (and
|
|
|
|
variables) in the file @file{etc/DOC} where they are stored, by
|
|
|
|
calling @code{Snarf-documentation} (@pxref{Definition of
|
|
|
|
Snarf-documentation,, Accessing Documentation}).
|
|
|
|
|
|
|
|
@cindex @file{site-init.el}
|
|
|
|
@cindex preloading additional functions and variables
|
|
|
|
You can specify other Lisp expressions to execute just before dumping
|
|
|
|
by putting them in a library named @file{site-init.el}. This file is
|
|
|
|
executed after the documentation strings are found.
|
|
|
|
|
|
|
|
If you want to preload function or variable definitions, there are
|
|
|
|
three ways you can do this and make their documentation strings
|
|
|
|
accessible when you subsequently run Emacs:
|
|
|
|
|
|
|
|
@itemize @bullet
|
|
|
|
@item
|
|
|
|
Arrange to scan these files when producing the @file{etc/DOC} file,
|
|
|
|
and load them with @file{site-load.el}.
|
|
|
|
|
|
|
|
@item
|
|
|
|
Load the files with @file{site-init.el}, then copy the files into the
|
|
|
|
installation directory for Lisp files when you install Emacs.
|
|
|
|
|
|
|
|
@item
|
2012-03-02 19:33:41 -08:00
|
|
|
Specify a @code{nil} value for @code{byte-compile-dynamic-docstrings}
|
|
|
|
as a local variable in each of these files, and load them with either
|
|
|
|
@file{site-load.el} or @file{site-init.el}. (This method has the
|
|
|
|
drawback that the documentation strings take up space in Emacs all the
|
|
|
|
time.)
|
2007-09-06 04:25:08 +00:00
|
|
|
@end itemize
|
|
|
|
|
2014-04-21 17:50:19 +03:00
|
|
|
@cindex change @code{load-path} at configure time
|
|
|
|
@cindex @option{--enable-locallisppath} option to @command{configure}
|
2007-09-06 04:25:08 +00:00
|
|
|
It is not advisable to put anything in @file{site-load.el} or
|
|
|
|
@file{site-init.el} that would alter any of the features that users
|
|
|
|
expect in an ordinary unmodified Emacs. If you feel you must override
|
|
|
|
normal features for your site, do it with @file{default.el}, so that
|
|
|
|
users can override your changes if they wish. @xref{Startup Summary}.
|
2013-12-13 13:25:30 -05:00
|
|
|
Note that if either @file{site-load.el} or @file{site-init.el} changes
|
|
|
|
@code{load-path}, the changes will be lost after dumping.
|
|
|
|
@xref{Library Search}. To make a permanent change to
|
|
|
|
@code{load-path}, use the @option{--enable-locallisppath} option
|
|
|
|
of @command{configure}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-03-02 21:45:21 -05:00
|
|
|
In a package that can be preloaded, it is sometimes necessary (or
|
|
|
|
useful) to delay certain evaluations until Emacs subsequently starts
|
|
|
|
up. The vast majority of such cases relate to the values of
|
|
|
|
customizable variables. For example, @code{tutorial-directory} is a
|
|
|
|
variable defined in @file{startup.el}, which is preloaded. The default
|
|
|
|
value is set based on @code{data-directory}. The variable needs to
|
|
|
|
access the value of @code{data-directory} when Emacs starts, not when
|
|
|
|
it is dumped, because the Emacs executable has probably been installed
|
|
|
|
in a different location since it was dumped.
|
|
|
|
|
|
|
|
@defun custom-initialize-delay symbol value
|
|
|
|
This function delays the initialization of @var{symbol} to the next
|
|
|
|
Emacs start. You normally use this function by specifying it as the
|
|
|
|
@code{:initialize} property of a customizable variable. (The argument
|
2012-03-12 15:35:55 -07:00
|
|
|
@var{value} is unused, and is provided only for compatibility with the
|
2012-03-02 21:45:21 -05:00
|
|
|
form Custom expects.)
|
|
|
|
@end defun
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-03-02 21:45:21 -05:00
|
|
|
In the unlikely event that you need a more general functionality than
|
|
|
|
@code{custom-initialize-delay} provides, you can use
|
|
|
|
@code{before-init-hook} (@pxref{Startup Summary}).
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2019-01-18 17:58:47 +02:00
|
|
|
@defun dump-emacs-portable to-file &optional track-referrers
|
2019-07-23 09:19:09 -07:00
|
|
|
This function dumps the current state of Emacs into a dump
|
2019-01-18 17:58:47 +02:00
|
|
|
file @var{to-file}, using the @code{pdump} method. Normally, the
|
2019-07-23 09:19:09 -07:00
|
|
|
dump file is called @file{@var{emacs-name}.dmp}, where
|
2019-01-18 17:58:47 +02:00
|
|
|
@var{emacs-name} is the name of the Emacs executable file. The
|
|
|
|
optional argument @var{track-referrers}, if non-@code{nil}, causes the
|
2019-07-23 09:19:09 -07:00
|
|
|
portable dumper to keep additional information to help track
|
2019-01-18 17:58:47 +02:00
|
|
|
down the provenance of object types that are not yet supported by the
|
|
|
|
@code{pdump} method.
|
|
|
|
|
2019-07-23 09:19:09 -07:00
|
|
|
Although the portable dumper code can run on many platforms, the dump
|
|
|
|
files that it produces are not portable---they can be loaded only by
|
|
|
|
the Emacs executable that dumped them.
|
|
|
|
|
2019-01-18 17:58:47 +02:00
|
|
|
If you want to use this function in an Emacs that was already dumped,
|
|
|
|
you must run Emacs with the @samp{-batch} option.
|
2021-10-12 13:55:28 +02:00
|
|
|
|
|
|
|
@vindex after-pdump-load-hook
|
|
|
|
If you're including @samp{.el} files in the dumped Emacs and that
|
|
|
|
@samp{.el} file has code that is normally run at load time, that code
|
|
|
|
won't be run when Emacs starts after dumping. To help work around
|
|
|
|
that problem, you can put functions on the
|
|
|
|
@code{after-pdump-load-hook} hook. This hook is run when starting
|
|
|
|
Emacs.
|
2019-01-18 17:58:47 +02:00
|
|
|
@end defun
|
|
|
|
|
2019-01-19 20:09:38 +02:00
|
|
|
@defun pdumper-stats
|
2019-07-23 09:19:09 -07:00
|
|
|
If the current Emacs session restored its state from a dump
|
2019-01-19 20:09:38 +02:00
|
|
|
file, this function returns information about the dump file and the
|
|
|
|
time it took to restore the Emacs state. The value is an alist
|
|
|
|
@w{@code{((dumped-with-pdumper . t) (load-time . @var{time})
|
|
|
|
(dump-file-name . @var{file}))}},
|
|
|
|
where @var{file} is the name of the dump file, and @var{time} is the
|
2019-01-19 14:38:52 -08:00
|
|
|
time in seconds it took to restore the state from the dump file.
|
2019-07-23 09:19:09 -07:00
|
|
|
If the current session was not restored from a dump file, the
|
Fix some Texinfo markup in manuals
* doc/emacs/macos.texi (Mac / GNUstep Customization):
* doc/lispintro/emacs-lisp-intro.texi (condition-case):
* doc/lispref/control.texi (pcase Macro):
* doc/lispref/debugging.texi (Internals of Debugger):
* doc/lispref/internals.texi (Building Emacs):
* doc/lispref/modes.texi (Imenu):
(Parser-based Font Lock, Parser-based Indentation):
* doc/lispref/parsing.texi (Retrieving Nodes, Tree-sitter C API):
* doc/lispref/processes.texi (Network, Bindat Types):
* doc/lispref/searching.texi (Rx Functions):
* doc/lispref/text.texi (Replacing):
* doc/lispref/windows.texi (Textual Scrolling):
* doc/misc/calc.texi (Killing From Stack, Customizing Calc):
* doc/misc/cc-mode.texi (Misc Font Locking, List Line-Up):
* doc/misc/ede.texi (ede-cpp-root-project)
(ede-proj-target-makefile, ede-sourcecode):
* doc/misc/ert.texi (Running Tests in Batch Mode):
* doc/misc/eudc.texi (Emacs-only Configuration, The Server Hotlist):
* doc/misc/eww.texi (Advanced):
* doc/misc/flymake.texi (Starting Flymake)
(Proc customization variables):
* doc/misc/tramp.texi (File name completion):
* doc/misc/gnus.texi (Summary Buffer Lines, Gnus Registry Setup)
(Fancy splitting to parent, Customizing the IMAP Connection)
(Mail Source Specifiers, Agent as Cache): Consistently mark up nil
and t as @code. Also fix the markup and wording of some surrounding
text (bug#64016).
* doc/lispref/display.texi (SVG Images, Icons):
* doc/lispref/modes.texi (Customizing Keywords): Prefer ASCII
apostrophe over Unicode right single quotation mark.
2023-06-12 14:42:31 +01:00
|
|
|
value is @code{nil}.
|
2019-01-19 20:09:38 +02:00
|
|
|
@end defun
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Garbage Collection
|
2012-03-02 21:45:21 -05:00
|
|
|
@section Garbage Collection
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@cindex memory allocation
|
2012-03-18 23:02:12 +08:00
|
|
|
When a program creates a list or the user defines a new function
|
|
|
|
(such as by loading a library), that data is placed in normal storage.
|
|
|
|
If normal storage runs low, then Emacs asks the operating system to
|
|
|
|
allocate more memory. Different types of Lisp objects, such as
|
Block-based vector allocation of small vectors.
* src/lisp.h (struct vectorlike_header): New field `nbytes',
adjust comment accordingly.
* src/alloc.c (enum mem_type): New type `MEM_TYPE_VECTOR_BLOCK'
to denote vector blocks. Adjust users (live_vector_p,
mark_maybe_pointer, valid_lisp_object_p) accordingly.
(COMMON_MULTIPLE): Move outside #if USE_LSB_TAG.
(VECTOR_BLOCK_SIZE, vroundup, VECTOR_BLOCK_BYTES),
(VBLOCK_BYTES_MIN, VBLOCK_BYTES_MAX, VECTOR_MAX_FREE_LIST_INDEX),
(VECTOR_FREE_LIST_FLAG, ADVANCE, VINDEX, SETUP_ON_FREE_LIST),
(VECTOR_SIZE, VECTOR_IN_BLOCK): New macros.
(roundup_size): New constant.
(struct vector_block): New data type.
(vector_blocks, vector_free_lists, zero_vector): New variables.
(all_vectors): Renamed to `large_vectors'.
(allocate_vector_from_block, init_vectors, allocate_vector_from_block)
(sweep_vectors): New functions.
(allocate_vectorlike): Return `zero_vector' as the only vector of
0 items. Allocate new vector from block if vector size is less than
or equal to VBLOCK_BYTES_MAX.
(Fgarbage_collect): Move all vector sweeping code to sweep_vectors.
(init_alloc_once): Add call to init_vectors.
* doc/lispref/internals.text (Garbage Collection): Document new
vector management code and vectorlike_header structure.
2012-06-08 12:44:30 +04:00
|
|
|
symbols, cons cells, small vectors, markers, etc., are segregated in
|
|
|
|
distinct blocks in memory. (Large vectors, long strings, buffers and
|
|
|
|
certain other editing types, which are fairly large, are allocated in
|
|
|
|
individual blocks, one per object; small strings are packed into blocks
|
|
|
|
of 8k bytes, and small vectors are packed into blocks of 4k bytes).
|
|
|
|
|
|
|
|
@cindex vector-like objects, storage
|
|
|
|
@cindex storage of vector-like Lisp objects
|
2018-08-14 12:07:09 -07:00
|
|
|
Beyond the basic vector, a lot of objects like markers, overlays and
|
|
|
|
buffers are managed as if they were vectors. The corresponding C data
|
2017-11-13 08:51:41 -08:00
|
|
|
structures include the @code{union vectorlike_header} field whose
|
2012-11-15 09:25:05 +04:00
|
|
|
@code{size} member contains the subtype enumerated by @code{enum pvec_type}
|
|
|
|
and an information about how many @code{Lisp_Object} fields this structure
|
|
|
|
contains and what the size of the rest data is. This information is
|
|
|
|
needed to calculate the memory footprint of an object, and used
|
|
|
|
by the vector allocation code while iterating over the vector blocks.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-03-18 23:02:12 +08:00
|
|
|
@cindex garbage collection
|
|
|
|
It is quite common to use some storage for a while, then release it
|
|
|
|
by (for example) killing a buffer or deleting the last pointer to an
|
|
|
|
object. Emacs provides a @dfn{garbage collector} to reclaim this
|
2021-09-16 17:13:11 +02:00
|
|
|
abandoned storage. The garbage collector operates, in essence, by
|
|
|
|
finding and marking all Lisp objects that are still accessible to Lisp
|
|
|
|
programs. To begin with, it assumes all the symbols, their values and
|
|
|
|
associated function definitions, and any data presently on the stack,
|
|
|
|
are accessible. Any objects that can be reached indirectly through
|
|
|
|
other accessible objects are also accessible, but this calculation is
|
|
|
|
done ``conservatively'', so it may slightly overestimate how many
|
|
|
|
objects that are accessible.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
When marking is finished, all objects still unmarked are garbage. No
|
|
|
|
matter what the Lisp program or the user does, it is impossible to refer
|
|
|
|
to them, since there is no longer a way to reach them. Their space
|
|
|
|
might as well be reused, since no one will miss them. The second
|
2021-09-16 17:13:11 +02:00
|
|
|
(sweep) phase of the garbage collector arranges to reuse them. (But
|
|
|
|
since the marking was done ``conservatively'', not all unused objects
|
|
|
|
are guaranteed to be garbage-collected by any one sweep.)
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@c ??? Maybe add something describing weak hash tables here?
|
|
|
|
|
|
|
|
@cindex free list
|
|
|
|
The sweep phase puts unused cons cells onto a @dfn{free list}
|
|
|
|
for future allocation; likewise for symbols and markers. It compacts
|
|
|
|
the accessible strings so they occupy fewer 8k blocks; then it frees the
|
Block-based vector allocation of small vectors.
* src/lisp.h (struct vectorlike_header): New field `nbytes',
adjust comment accordingly.
* src/alloc.c (enum mem_type): New type `MEM_TYPE_VECTOR_BLOCK'
to denote vector blocks. Adjust users (live_vector_p,
mark_maybe_pointer, valid_lisp_object_p) accordingly.
(COMMON_MULTIPLE): Move outside #if USE_LSB_TAG.
(VECTOR_BLOCK_SIZE, vroundup, VECTOR_BLOCK_BYTES),
(VBLOCK_BYTES_MIN, VBLOCK_BYTES_MAX, VECTOR_MAX_FREE_LIST_INDEX),
(VECTOR_FREE_LIST_FLAG, ADVANCE, VINDEX, SETUP_ON_FREE_LIST),
(VECTOR_SIZE, VECTOR_IN_BLOCK): New macros.
(roundup_size): New constant.
(struct vector_block): New data type.
(vector_blocks, vector_free_lists, zero_vector): New variables.
(all_vectors): Renamed to `large_vectors'.
(allocate_vector_from_block, init_vectors, allocate_vector_from_block)
(sweep_vectors): New functions.
(allocate_vectorlike): Return `zero_vector' as the only vector of
0 items. Allocate new vector from block if vector size is less than
or equal to VBLOCK_BYTES_MAX.
(Fgarbage_collect): Move all vector sweeping code to sweep_vectors.
(init_alloc_once): Add call to init_vectors.
* doc/lispref/internals.text (Garbage Collection): Document new
vector management code and vectorlike_header structure.
2012-06-08 12:44:30 +04:00
|
|
|
other 8k blocks. Unreachable vectors from vector blocks are coalesced
|
|
|
|
to create largest possible free areas; if a free area spans a complete
|
|
|
|
4k block, that block is freed. Otherwise, the free area is recorded
|
|
|
|
in a free list array, where each entry corresponds to a free list
|
|
|
|
of areas of the same size. Large vectors, buffers, and other large
|
|
|
|
objects are allocated and freed individually.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@cindex CL note---allocate more storage
|
|
|
|
@quotation
|
|
|
|
@b{Common Lisp note:} Unlike other Lisps, GNU Emacs Lisp does not
|
|
|
|
call the garbage collector when the free list is empty. Instead, it
|
|
|
|
simply requests the operating system to allocate more storage, and
|
|
|
|
processing continues until @code{gc-cons-threshold} bytes have been
|
|
|
|
used.
|
|
|
|
|
|
|
|
This means that you can make sure that the garbage collector will not
|
|
|
|
run during a certain portion of a Lisp program by calling the garbage
|
|
|
|
collector explicitly just before it (provided that portion of the
|
|
|
|
program does not use so much space as to force a second garbage
|
|
|
|
collection).
|
|
|
|
@end quotation
|
|
|
|
|
|
|
|
@deffn Command garbage-collect
|
|
|
|
This command runs a garbage collection, and returns information on
|
|
|
|
the amount of space in use. (Garbage collection can also occur
|
|
|
|
spontaneously if you use more than @code{gc-cons-threshold} bytes of
|
|
|
|
Lisp data since the previous garbage collection.)
|
|
|
|
|
2012-11-15 09:25:05 +04:00
|
|
|
@code{garbage-collect} returns a list with information on amount of space in
|
|
|
|
use, where each entry has the form @samp{(@var{name} @var{size} @var{used})}
|
|
|
|
or @samp{(@var{name} @var{size} @var{used} @var{free})}. In the entry,
|
|
|
|
@var{name} is a symbol describing the kind of objects this entry represents,
|
|
|
|
@var{size} is the number of bytes used by each one, @var{used} is the number
|
|
|
|
of those objects that were found live in the heap, and optional @var{free} is
|
|
|
|
the number of those objects that are not live but that Emacs keeps around for
|
|
|
|
future allocations. So an overall result is:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
2012-12-06 18:37:20 -08:00
|
|
|
((@code{conses} @var{cons-size} @var{used-conses} @var{free-conses})
|
2012-11-15 09:25:05 +04:00
|
|
|
(@code{symbols} @var{symbol-size} @var{used-symbols} @var{free-symbols})
|
|
|
|
(@code{strings} @var{string-size} @var{used-strings} @var{free-strings})
|
|
|
|
(@code{string-bytes} @var{byte-size} @var{used-bytes})
|
|
|
|
(@code{vectors} @var{vector-size} @var{used-vectors})
|
|
|
|
(@code{vector-slots} @var{slot-size} @var{used-slots} @var{free-slots})
|
|
|
|
(@code{floats} @var{float-size} @var{used-floats} @var{free-floats})
|
|
|
|
(@code{intervals} @var{interval-size} @var{used-intervals} @var{free-intervals})
|
|
|
|
(@code{buffers} @var{buffer-size} @var{used-buffers})
|
|
|
|
(@code{heap} @var{unit-size} @var{total-size} @var{free-size}))
|
2007-09-06 04:25:08 +00:00
|
|
|
@end example
|
|
|
|
|
|
|
|
Here is an example:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(garbage-collect)
|
2012-11-15 09:25:05 +04:00
|
|
|
@result{} ((conses 16 49126 8058) (symbols 48 14607 0)
|
Turn misc objects into pseudovectors
Eliminate the category of miscellaneous objects, and turn all
such objects into pseudovectors. The immediate motivation
for this change is to free up an enum Lisp_Type tag value, a
scarce resource that can be better used elsewhere. However,
this change is worthwhile in its own right, as it improves
performance slightly on my platform, 0.3% faster for 'make
compile-always' on Fedora 28, and it simplifies the garbage
collector and interpreter (Bug#32405).
* doc/lispref/internals.texi (Garbage Collection):
* etc/NEWS:
Document change to garbage-collect return value.
* src/alloc.c (total_markers, total_free_markers):
(union aligned_Lisp_Misc, MARKER_BLOCK_SIZE)
(struct marker_block, marker_block, marker_block_index)
(misc_free_list, allocate_misc, live_misc_holding)
(live_misc_p, sweep_misc):
* src/lisp.h (lisp_h_MARKERP, lisp_h_MISCP, MARKERP, MISCP)
(Lisp_Misc, enum Lisp_Misc_Type, Lisp_Misc_Free)
(Lisp_Misc_Marker, Lisp_Misc_Overlay, Lisp_Misc_Finalizer)
(Lisp_Misc_Ptr, Lisp_Misc_User_Ptr, Lisp_Misc_Limit)
(Lisp_Misc_Bignum)
(XSETMISC, struct Lisp_Misc_Any, XMISCANY, XMISCTYPE)
(struct Lisp_Free, union Lisp_Misc, XMISC):
Remove. All uses removed.
(cleanup_vector): Clean up objects that were formerly misc
and are now pseudovectors.
(make_misc_ptr, build_overlay, Fmake_marker, build_marker)
(make_bignum_str, make_number, make_pure_bignum)
(make_user_ptr, Fmake_finalizer):
Build as pseudovectors, not as misc objects.
(mark_finalizer_list, queue_doomed_finalizers)
(compact_undo_list, mark_overlay, mark_object)
(unchain_dead_markers):
Mark as vector-like objects, not as misc objects.
(mark_maybe_object, mark_maybe_pointer, valid_lisp_object_p)
(total_bytes_of_live_objects, survives_gc_p):
* src/fns.c (sxhash):
No need to worry about misc objects.
(garbage_collect_1): Do not generate a 'misc' component.
(syms_of_alloc): No need for 'misc' symbol.
* src/buffer.c (overlays_at, overlays_in, overlay_touches_p)
(overlay_strings, recenter_overlay_lists)
(fix_start_end_in_overlays, fix_overlays_before)
(Foverlay_lists, report_overlay_modification)
(evaporate_overlays):
* src/editfns.c (overlays_around):
* src/data.c (Ftype_of):
* src/fns.c (internal_equal):
* src/lisp.h (mint_ptrp, xmint_pointer, FINALIZERP)
(XFINALIZER, MARKERP, XMARKER, OVERLAYP, XOVERLAY, USER_PTRP)
(XUSER_PTR, BIGNUMP, XBIGNUM):
* src/print.c (print_vectorlike, print_object):
* src/undo.c (record_marker_adjustments):
* src/xdisp.c (load_overlay_strings):
Formerly misc objects are now pseudovectors.
* src/lisp.h (PVEC_MARKER, PVEC_OVERLAY, PVEC_FINALIZER)
(PVEC_BIGNUM, PVEC_MISC_PTR, PVEC_USER_PTR):
New constants, replacing their misc versions. All uses changed.
(struct Lisp_Marker, struct Lisp_Overlay, struct Lisp_Misc_Ptr)
(struct Lisp_Bignum, struct Lisp_User_Ptr, struct Lisp_Finalizer):
Make usable as a pseudovector by using a pseudovector header,
replacing any DIY components, and putting Lisp_Object members
first. All uses changed.
2018-08-08 19:46:29 -07:00
|
|
|
(strings 32 2942 2607)
|
2012-11-15 09:25:05 +04:00
|
|
|
(string-bytes 1 78607) (vectors 16 7247)
|
|
|
|
(vector-slots 8 341609 29474) (floats 8 71 102)
|
|
|
|
(intervals 56 27 26) (buffers 944 8)
|
|
|
|
(heap 1024 11715 2678))
|
2007-09-06 04:25:08 +00:00
|
|
|
@end example
|
|
|
|
|
2012-11-15 09:25:05 +04:00
|
|
|
Below is a table explaining each element. Note that last @code{heap} entry
|
|
|
|
is optional and present only if an underlying @code{malloc} implementation
|
|
|
|
provides @code{mallinfo} function.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@table @var
|
2012-11-15 09:25:05 +04:00
|
|
|
@item cons-size
|
2012-12-06 17:47:14 -08:00
|
|
|
Internal size of a cons cell, i.e., @code{sizeof (struct Lisp_Cons)}.
|
2012-11-15 09:25:05 +04:00
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@item used-conses
|
|
|
|
The number of cons cells in use.
|
|
|
|
|
|
|
|
@item free-conses
|
2012-11-15 09:25:05 +04:00
|
|
|
The number of cons cells for which space has been obtained from
|
|
|
|
the operating system, but that are not currently being used.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-11-15 09:25:05 +04:00
|
|
|
@item symbol-size
|
2012-12-06 17:47:14 -08:00
|
|
|
Internal size of a symbol, i.e., @code{sizeof (struct Lisp_Symbol)}.
|
2012-11-15 09:25:05 +04:00
|
|
|
|
|
|
|
@item used-symbols
|
2007-09-06 04:25:08 +00:00
|
|
|
The number of symbols in use.
|
|
|
|
|
2012-11-15 09:25:05 +04:00
|
|
|
@item free-symbols
|
|
|
|
The number of symbols for which space has been obtained from
|
|
|
|
the operating system, but that are not currently being used.
|
|
|
|
|
|
|
|
@item string-size
|
2012-12-06 17:47:14 -08:00
|
|
|
Internal size of a string header, i.e., @code{sizeof (struct Lisp_String)}.
|
2012-11-15 09:25:05 +04:00
|
|
|
|
|
|
|
@item used-strings
|
|
|
|
The number of string headers in use.
|
|
|
|
|
|
|
|
@item free-strings
|
|
|
|
The number of string headers for which space has been obtained
|
|
|
|
from the operating system, but that are not currently being used.
|
|
|
|
|
|
|
|
@item byte-size
|
|
|
|
This is used for convenience and equals to @code{sizeof (char)}.
|
|
|
|
|
|
|
|
@item used-bytes
|
|
|
|
The total size of all string data in bytes.
|
|
|
|
|
|
|
|
@item vector-size
|
2018-09-07 09:17:25 -07:00
|
|
|
Size in bytes of a vector of length 1, including its header.
|
2012-11-15 09:25:05 +04:00
|
|
|
|
|
|
|
@item used-vectors
|
|
|
|
The number of vector headers allocated from the vector blocks.
|
|
|
|
|
|
|
|
@item slot-size
|
|
|
|
Internal size of a vector slot, always equal to @code{sizeof (Lisp_Object)}.
|
|
|
|
|
|
|
|
@item used-slots
|
|
|
|
The number of slots in all used vectors.
|
2018-09-07 09:17:25 -07:00
|
|
|
Slot counts might include some or all overhead from vector headers,
|
|
|
|
depending on the platform.
|
2012-11-15 09:25:05 +04:00
|
|
|
|
|
|
|
@item free-slots
|
|
|
|
The number of free slots in all vector blocks.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-11-15 09:25:05 +04:00
|
|
|
@item float-size
|
2012-12-06 17:47:14 -08:00
|
|
|
Internal size of a float object, i.e., @code{sizeof (struct Lisp_Float)}.
|
2012-11-15 09:25:05 +04:00
|
|
|
(Do not confuse it with the native platform @code{float} or @code{double}.)
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item used-floats
|
|
|
|
The number of floats in use.
|
|
|
|
|
|
|
|
@item free-floats
|
2012-11-15 09:25:05 +04:00
|
|
|
The number of floats for which space has been obtained from
|
|
|
|
the operating system, but that are not currently being used.
|
|
|
|
|
|
|
|
@item interval-size
|
2012-12-06 17:47:14 -08:00
|
|
|
Internal size of an interval object, i.e., @code{sizeof (struct interval)}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item used-intervals
|
2012-11-15 09:25:05 +04:00
|
|
|
The number of intervals in use.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item free-intervals
|
2012-11-15 09:25:05 +04:00
|
|
|
The number of intervals for which space has been obtained from
|
|
|
|
the operating system, but that are not currently being used.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-11-15 09:25:05 +04:00
|
|
|
@item buffer-size
|
2012-12-06 17:47:14 -08:00
|
|
|
Internal size of a buffer, i.e., @code{sizeof (struct buffer)}.
|
2012-11-15 09:25:05 +04:00
|
|
|
(Do not confuse with the value returned by @code{buffer-size} function.)
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-11-15 09:25:05 +04:00
|
|
|
@item used-buffers
|
|
|
|
The number of buffer objects in use. This includes killed buffers
|
2012-12-06 17:47:14 -08:00
|
|
|
invisible to users, i.e., all buffers in @code{all_buffers} list.
|
2012-11-15 09:25:05 +04:00
|
|
|
|
|
|
|
@item unit-size
|
|
|
|
The unit of heap space measurement, always equal to 1024 bytes.
|
|
|
|
|
|
|
|
@item total-size
|
|
|
|
Total heap size, in @var{unit-size} units.
|
|
|
|
|
|
|
|
@item free-size
|
|
|
|
Heap space which is not currently used, in @var{unit-size} units.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end table
|
|
|
|
@end deffn
|
|
|
|
|
|
|
|
@defopt garbage-collection-messages
|
|
|
|
If this variable is non-@code{nil}, Emacs displays a message at the
|
|
|
|
beginning and end of garbage collection. The default value is
|
2012-03-02 19:59:22 -08:00
|
|
|
@code{nil}.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defopt
|
|
|
|
|
|
|
|
@defvar post-gc-hook
|
|
|
|
This is a normal hook that is run at the end of garbage collection.
|
|
|
|
Garbage collection is inhibited while the hook functions run, so be
|
|
|
|
careful writing them.
|
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@defopt gc-cons-threshold
|
|
|
|
The value of this variable is the number of bytes of storage that must
|
|
|
|
be allocated for Lisp objects after one garbage collection in order to
|
2012-11-15 09:25:05 +04:00
|
|
|
trigger another garbage collection. You can use the result returned by
|
|
|
|
@code{garbage-collect} to get an information about size of the particular
|
|
|
|
object type; space allocated to the contents of buffers does not count.
|
|
|
|
|
|
|
|
The initial threshold value is @code{GC_DEFAULT_THRESHOLD}, defined in
|
2022-11-20 13:10:08 +02:00
|
|
|
@file{alloc.c}. Since it's defined in @code{word_size} units, the
|
|
|
|
value is 400,000 for the default 32-bit configuration and 800,000 for
|
|
|
|
the 64-bit one. If you specify a larger value, garbage collection
|
|
|
|
will happen less often. This reduces the amount of time spent garbage
|
2023-03-11 18:16:35 +02:00
|
|
|
collecting (so Lisp programs will run faster between cycles of garbage
|
|
|
|
collection that happen more rarely), but increases total memory use.
|
|
|
|
You may want to do this when running a program that creates lots of
|
|
|
|
Lisp data, especially if you need it to run faster. However, we
|
2022-11-20 13:10:08 +02:00
|
|
|
recommend against increasing the threshold for prolonged periods of
|
|
|
|
time, and advise that you never set it higher than needed for the
|
|
|
|
program to run in reasonable time. Using thresholds higher than
|
2023-03-11 18:16:35 +02:00
|
|
|
necessary could potentially cause higher system-wide memory pressure,
|
2023-03-11 19:30:06 +02:00
|
|
|
and also make each garbage-collection cycle take much more time, and
|
2022-11-20 13:10:08 +02:00
|
|
|
should therefore be avoided.
|
2012-11-15 09:25:05 +04:00
|
|
|
|
|
|
|
You can make collections more frequent by specifying a smaller value, down
|
|
|
|
to 1/10th of @code{GC_DEFAULT_THRESHOLD}. A value less than this minimum
|
|
|
|
will remain in effect only until the subsequent garbage collection, at which
|
|
|
|
time @code{garbage-collect} will set the threshold back to the minimum.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defopt
|
|
|
|
|
|
|
|
@defopt gc-cons-percentage
|
|
|
|
The value of this variable specifies the amount of consing before a
|
|
|
|
garbage collection occurs, as a fraction of the current heap size.
|
|
|
|
This criterion and @code{gc-cons-threshold} apply in parallel, and
|
|
|
|
garbage collection occurs only when both criteria are satisfied.
|
|
|
|
|
|
|
|
As the heap size increases, the time to perform a garbage collection
|
|
|
|
increases. Thus, it can be desirable to do them less frequently in
|
|
|
|
proportion.
|
2022-11-20 13:10:08 +02:00
|
|
|
|
|
|
|
As with @code{gc-cons-threshold}, do not enlarge this more than
|
|
|
|
necessary, and never for prolonged periods of time.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defopt
|
|
|
|
|
2019-09-14 10:53:24 -07:00
|
|
|
Control over the garbage collector via @code{gc-cons-threshold} and
|
|
|
|
@code{gc-cons-percentage} is only approximate. Although Emacs checks
|
|
|
|
for threshold exhaustion regularly, for efficiency reasons it does not
|
|
|
|
do so immediately after every change to the heap or to
|
|
|
|
@code{gc-cons-threshold} or @code{gc-cons-percentage}, so exhausting
|
|
|
|
the threshold does not immediately trigger garbage collection. Also,
|
2019-12-09 18:44:35 -08:00
|
|
|
for efficiency in threshold calculations Emacs approximates the heap
|
2019-09-14 10:53:24 -07:00
|
|
|
size, which counts the bytes used by currently-accessible objects in
|
|
|
|
the heap.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
The value returned by @code{garbage-collect} describes the amount of
|
|
|
|
memory used by Lisp data, broken down by data type. By contrast, the
|
|
|
|
function @code{memory-limit} provides information on the total amount of
|
|
|
|
memory Emacs is currently using.
|
|
|
|
|
|
|
|
@defun memory-limit
|
2018-06-16 08:11:37 -07:00
|
|
|
This function returns an estimate of the total amount of bytes of
|
|
|
|
virtual memory that Emacs is currently using, divided by 1024.
|
2007-09-06 04:25:08 +00:00
|
|
|
You can use this to get a general idea of how your actions affect the
|
|
|
|
memory usage.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defvar memory-full
|
2012-03-18 23:02:12 +08:00
|
|
|
This variable is @code{t} if Emacs is nearly out of memory for Lisp
|
2007-09-06 04:25:08 +00:00
|
|
|
objects, and @code{nil} otherwise.
|
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@defun memory-use-counts
|
|
|
|
This returns a list of numbers that count the number of objects
|
|
|
|
created in this Emacs session. Each of these counters increments for
|
|
|
|
a certain kind of object. See the documentation string for details.
|
|
|
|
@end defun
|
|
|
|
|
2014-07-11 16:19:58 +04:00
|
|
|
@defun memory-info
|
|
|
|
This functions returns an amount of total system memory and how much
|
|
|
|
of it is free. On an unsupported system, the value may be @code{nil}.
|
2022-11-27 16:57:03 +01:00
|
|
|
|
|
|
|
If @code{default-directory} points to a remote host, memory
|
|
|
|
information of that host is returned.
|
2014-07-11 15:49:49 +03:00
|
|
|
@end defun
|
2014-07-11 16:19:58 +04:00
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@defvar gcs-done
|
|
|
|
This variable contains the total number of garbage collections
|
|
|
|
done so far in this Emacs session.
|
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@defvar gc-elapsed
|
|
|
|
This variable contains the total number of seconds of elapsed time
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-17 18:19:03 -07:00
|
|
|
during garbage collection so far in this Emacs session, as a
|
|
|
|
floating-point number.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defvar
|
|
|
|
|
2020-12-11 14:49:53 +01:00
|
|
|
@defun memory-report
|
|
|
|
It can sometimes be useful to see where Emacs is using memory (in
|
|
|
|
various variables, buffers, and caches). This command will open a new
|
|
|
|
buffer (called @samp{"*Memory Report*"}) that will give an overview,
|
|
|
|
in addition to listing the ``largest'' buffers and variables.
|
|
|
|
|
|
|
|
All the data here is approximate, because there's really no consistent
|
|
|
|
way to compute the size of a variable. For instance, two variables
|
|
|
|
may share parts of a data structure, and this will be counted twice,
|
|
|
|
but this command may still give a useful high-level overview of which
|
2020-12-18 23:22:39 +00:00
|
|
|
parts of Emacs are using memory.
|
2020-12-11 14:49:53 +01:00
|
|
|
@end defun
|
|
|
|
|
2014-09-30 19:35:16 +04:00
|
|
|
@node Stack-allocated Objects
|
|
|
|
@section Stack-allocated Objects
|
|
|
|
|
2014-09-30 19:21:22 +03:00
|
|
|
@cindex stack allocated Lisp objects
|
|
|
|
@cindex Lisp objects, stack-allocated
|
2014-09-30 19:35:16 +04:00
|
|
|
The garbage collector described above is used to manage data visible
|
2014-09-30 19:21:22 +03:00
|
|
|
from Lisp programs, as well as most of the data internally used by the
|
2014-09-30 12:10:37 -07:00
|
|
|
Lisp interpreter. Sometimes it may be useful to allocate temporary
|
|
|
|
internal objects using the C stack of the interpreter. This can help
|
|
|
|
performance, as stack allocation is typically faster than using heap
|
|
|
|
memory to allocate and the garbage collector to free. The downside is
|
|
|
|
that using such objects after they are freed results in undefined
|
|
|
|
behavior, so uses should be well thought out and carefully debugged by
|
|
|
|
using the @code{GC_CHECK_MARKED_OBJECTS} feature (see
|
|
|
|
@file{src/alloc.c}). In particular, stack-allocated objects should
|
|
|
|
never be made visible to user Lisp code.
|
|
|
|
|
|
|
|
Currently, cons cells and strings can be allocated this way. This
|
2014-09-30 20:28:16 -07:00
|
|
|
is implemented by C macros like @code{AUTO_CONS} and
|
|
|
|
@code{AUTO_STRING} that define a named @code{Lisp_Object} with block
|
2014-09-30 12:10:37 -07:00
|
|
|
lifetime. These objects are not freed by the garbage collector;
|
|
|
|
instead, they have automatic storage duration, i.e., they are
|
|
|
|
allocated like local variables and are automatically freed at the end
|
2014-09-30 20:28:16 -07:00
|
|
|
of execution of the C block that defined the object.
|
2014-09-30 12:10:37 -07:00
|
|
|
|
|
|
|
For performance reasons, stack-allocated strings are limited to
|
|
|
|
@acronym{ASCII} characters, and many of these strings are immutable,
|
|
|
|
i.e., calling @code{ASET} on them produces undefined behavior.
|
2014-09-30 19:35:16 +04:00
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Memory Usage
|
|
|
|
@section Memory Usage
|
|
|
|
@cindex memory usage
|
|
|
|
|
|
|
|
These functions and variables give information about the total amount
|
|
|
|
of memory allocation that Emacs has done, broken down by data type.
|
|
|
|
Note the difference between these and the values returned by
|
2012-03-02 20:18:09 -08:00
|
|
|
@code{garbage-collect}; those count objects that currently exist, but
|
2007-09-06 04:25:08 +00:00
|
|
|
these count the number or size of all allocations, including those for
|
|
|
|
objects that have since been freed.
|
|
|
|
|
|
|
|
@defvar cons-cells-consed
|
|
|
|
The total number of cons cells that have been allocated so far
|
|
|
|
in this Emacs session.
|
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@defvar floats-consed
|
|
|
|
The total number of floats that have been allocated so far
|
|
|
|
in this Emacs session.
|
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@defvar vector-cells-consed
|
|
|
|
The total number of vector cells that have been allocated so far
|
|
|
|
in this Emacs session.
|
2018-08-14 12:07:09 -07:00
|
|
|
This includes vector-like objects such as markers and overlays, plus
|
|
|
|
certain objects not visible to users.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@defvar symbols-consed
|
|
|
|
The total number of symbols that have been allocated so far
|
|
|
|
in this Emacs session.
|
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@defvar string-chars-consed
|
|
|
|
The total number of string characters that have been allocated so far
|
2012-05-04 19:26:45 -07:00
|
|
|
in this session.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@defvar intervals-consed
|
|
|
|
The total number of intervals that have been allocated so far
|
|
|
|
in this Emacs session.
|
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@defvar strings-consed
|
|
|
|
The total number of strings that have been allocated so far in this
|
|
|
|
Emacs session.
|
|
|
|
@end defvar
|
|
|
|
|
2014-05-07 13:51:35 -07:00
|
|
|
@node C Dialect
|
|
|
|
@section C Dialect
|
|
|
|
@cindex C programming language
|
|
|
|
|
2023-02-04 15:53:40 -08:00
|
|
|
The C part of Emacs is portable to C99 or later: later C features such
|
|
|
|
as @samp{<stdckdint.h>} and @samp{[[noreturn]]} are not used without a check,
|
2014-05-07 13:51:35 -07:00
|
|
|
typically at configuration time, and the Emacs build procedure
|
2023-02-04 15:53:40 -08:00
|
|
|
provides a substitute implementation if necessary. Some later features,
|
2014-05-17 01:11:31 -07:00
|
|
|
such as anonymous structures and unions, are too difficult to emulate,
|
|
|
|
so they are avoided entirely.
|
2014-05-07 13:51:35 -07:00
|
|
|
|
2023-02-04 15:53:40 -08:00
|
|
|
At some point in the future the base C dialect will no doubt change to
|
|
|
|
something later than C99.
|
2014-05-07 13:51:35 -07:00
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Writing Emacs Primitives
|
2012-03-02 21:45:21 -05:00
|
|
|
@section Writing Emacs Primitives
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex primitive function internals
|
|
|
|
@cindex writing Emacs primitives
|
|
|
|
|
2012-12-05 14:27:56 -08:00
|
|
|
Lisp primitives are Lisp functions implemented in C@. The details of
|
2007-09-06 04:25:08 +00:00
|
|
|
interfacing the C function so that Lisp can call it are handled by a few
|
|
|
|
C macros. The only way to really understand how to write new C code is
|
|
|
|
to read the source, but we can explain some things here.
|
|
|
|
|
|
|
|
An example of a special form is the definition of @code{or}, from
|
|
|
|
@file{eval.c}. (An ordinary function would have the same general
|
|
|
|
appearance.)
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
|
|
|
DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
|
2019-06-26 22:13:34 +01:00
|
|
|
doc: /* Eval args until one of them yields non-nil,
|
|
|
|
then return that value.
|
2012-03-18 23:02:12 +08:00
|
|
|
The remaining args are not evalled at all.
|
2007-09-06 04:25:08 +00:00
|
|
|
If all args return nil, return nil.
|
|
|
|
@end group
|
|
|
|
@group
|
Assume GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS
This removes the need for GCPRO1 etc. Suggested by Stefan Monnier in:
http://lists.gnu.org/archive/html/emacs-devel/2015-08/msg00918.html
* doc/lispref/internals.texi (Writing Emacs Primitives):
* etc/NEWS:
Document the change.
* src/alloc.c (gcprolist, dump_zombies, MAX_ZOMBIES, zombies)
(nzombies, ngcs, avg_zombies, max_live, max_zombies, avg_live)
(Fgc_status, check_gcpros, relocatable_string_data_p, gc-precise):
* src/bytecode.c (mark_byte_stack) [BYTE_MARK_STACK]:
* src/eval.c (gcpro_level) [DEBUG_GCPRO]:
* src/lisp.h (struct handler.gcpro, struct gcpro, GC_MARK_STACK)
(GC_USE_GCPROS_AS_BEFORE, GC_MAKE_GCPROS_NOOPS)
(GC_MARK_STACK_CHECK_GCPROS, GC_USE_GCPROS_CHECK_ZOMBIES)
(BYTE_MARK_STACK, GCPRO1, GCPRO2, GCPRO3, GCPRO4, GCPRO5, GCPRO6)
(GCPRO7, UNGCPRO, RETURN_UNGCPRO):
Remove. All uses removed. The code now assumes
GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS.
* src/bytecode.c (relocate_byte_stack):
Rename from unmark_byte_stack, since it now only relocates.
All callers changed.
* src/frame.c (make_frame): Add an IF_LINT to pacify GCC 5.2
with GCPROs removed.
* src/systime.h: Use EMACS_LISP_H as the canary instead of GCPRO1.
* test/automated/finalizer-tests.el (finalizer-basic)
(finalizer-circular-reference, finalizer-cross-reference)
(finalizer-error):
* test/automated/generator-tests.el (cps-test-iter-close-finalizer):
Remove tests, as they depend on gc-precise.
2015-08-26 19:24:28 -07:00
|
|
|
usage: (or CONDITIONS...) */)
|
2010-07-09 11:38:50 +03:00
|
|
|
(Lisp_Object args)
|
2007-09-06 04:25:08 +00:00
|
|
|
@{
|
Assume GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS
This removes the need for GCPRO1 etc. Suggested by Stefan Monnier in:
http://lists.gnu.org/archive/html/emacs-devel/2015-08/msg00918.html
* doc/lispref/internals.texi (Writing Emacs Primitives):
* etc/NEWS:
Document the change.
* src/alloc.c (gcprolist, dump_zombies, MAX_ZOMBIES, zombies)
(nzombies, ngcs, avg_zombies, max_live, max_zombies, avg_live)
(Fgc_status, check_gcpros, relocatable_string_data_p, gc-precise):
* src/bytecode.c (mark_byte_stack) [BYTE_MARK_STACK]:
* src/eval.c (gcpro_level) [DEBUG_GCPRO]:
* src/lisp.h (struct handler.gcpro, struct gcpro, GC_MARK_STACK)
(GC_USE_GCPROS_AS_BEFORE, GC_MAKE_GCPROS_NOOPS)
(GC_MARK_STACK_CHECK_GCPROS, GC_USE_GCPROS_CHECK_ZOMBIES)
(BYTE_MARK_STACK, GCPRO1, GCPRO2, GCPRO3, GCPRO4, GCPRO5, GCPRO6)
(GCPRO7, UNGCPRO, RETURN_UNGCPRO):
Remove. All uses removed. The code now assumes
GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS.
* src/bytecode.c (relocate_byte_stack):
Rename from unmark_byte_stack, since it now only relocates.
All callers changed.
* src/frame.c (make_frame): Add an IF_LINT to pacify GCC 5.2
with GCPROs removed.
* src/systime.h: Use EMACS_LISP_H as the canary instead of GCPRO1.
* test/automated/finalizer-tests.el (finalizer-basic)
(finalizer-circular-reference, finalizer-cross-reference)
(finalizer-error):
* test/automated/generator-tests.el (cps-test-iter-close-finalizer):
Remove tests, as they depend on gc-precise.
2015-08-26 19:24:28 -07:00
|
|
|
Lisp_Object val = Qnil;
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
while (CONSP (args))
|
|
|
|
@{
|
2012-03-03 14:13:28 -08:00
|
|
|
val = eval_sub (XCAR (args));
|
2007-09-06 04:25:08 +00:00
|
|
|
if (!NILP (val))
|
|
|
|
break;
|
|
|
|
args = XCDR (args);
|
2017-01-25 21:13:19 -08:00
|
|
|
maybe_quit ();
|
2007-09-06 04:25:08 +00:00
|
|
|
@}
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
return val;
|
|
|
|
@}
|
|
|
|
@end group
|
|
|
|
@end smallexample
|
|
|
|
|
|
|
|
@cindex @code{DEFUN}, C macro to define Lisp primitives
|
|
|
|
Let's start with a precise explanation of the arguments to the
|
|
|
|
@code{DEFUN} macro. Here is a template for them:
|
|
|
|
|
|
|
|
@example
|
|
|
|
DEFUN (@var{lname}, @var{fname}, @var{sname}, @var{min}, @var{max}, @var{interactive}, @var{doc})
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@table @var
|
|
|
|
@item lname
|
|
|
|
This is the name of the Lisp symbol to define as the function name; in
|
|
|
|
the example above, it is @code{or}.
|
|
|
|
|
|
|
|
@item fname
|
2012-03-18 23:02:12 +08:00
|
|
|
This is the C function name for this function. This is the name that
|
|
|
|
is used in C code for calling the function. The name is, by
|
|
|
|
convention, @samp{F} prepended to the Lisp name, with all dashes
|
|
|
|
(@samp{-}) in the Lisp name changed to underscores. Thus, to call
|
|
|
|
this function from C code, call @code{For}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item sname
|
|
|
|
This is a C variable name to use for a structure that holds the data for
|
|
|
|
the subr object that represents the function in Lisp. This structure
|
|
|
|
conveys the Lisp symbol name to the initialization routine that will
|
|
|
|
create the symbol and store the subr object as its definition. By
|
|
|
|
convention, this name is always @var{fname} with @samp{F} replaced with
|
|
|
|
@samp{S}.
|
|
|
|
|
|
|
|
@item min
|
|
|
|
This is the minimum number of arguments that the function requires. The
|
|
|
|
function @code{or} allows a minimum of zero arguments.
|
|
|
|
|
|
|
|
@item max
|
|
|
|
This is the maximum number of arguments that the function accepts, if
|
|
|
|
there is a fixed maximum. Alternatively, it can be @code{UNEVALLED},
|
|
|
|
indicating a special form that receives unevaluated arguments, or
|
|
|
|
@code{MANY}, indicating an unlimited number of evaluated arguments (the
|
|
|
|
equivalent of @code{&rest}). Both @code{UNEVALLED} and @code{MANY} are
|
2012-05-04 19:26:45 -07:00
|
|
|
macros. If @var{max} is a number, it must be more than @var{min} but
|
|
|
|
less than 8.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2013-04-21 19:16:30 +08:00
|
|
|
@cindex interactive specification in primitives
|
2007-09-06 04:25:08 +00:00
|
|
|
@item interactive
|
2013-04-21 19:16:30 +08:00
|
|
|
This is an interactive specification, a string such as might be used
|
2018-06-25 12:23:23 -05:00
|
|
|
as the argument of @code{interactive} in a Lisp function
|
|
|
|
(@pxref{Using Interactive}). In the case
|
2019-06-26 22:13:34 +01:00
|
|
|
of @code{or}, it is @code{0} (a null pointer), indicating that @code{or}
|
2013-04-23 11:57:44 -07:00
|
|
|
cannot be called interactively. A value of @code{""} indicates a
|
|
|
|
function that should receive no arguments when called interactively.
|
2013-04-21 19:16:30 +08:00
|
|
|
If the value begins with a @samp{"(}, the string is evaluated as a
|
|
|
|
Lisp form. For example:
|
|
|
|
|
2013-04-23 11:57:44 -07:00
|
|
|
@example
|
2013-04-21 19:16:30 +08:00
|
|
|
@group
|
2019-06-26 22:13:34 +01:00
|
|
|
DEFUN ("foo", Ffoo, Sfoo, 0, 3,
|
2013-04-23 11:57:44 -07:00
|
|
|
"(list (read-char-by-name \"Insert character: \")\
|
2013-04-21 19:16:30 +08:00
|
|
|
(prefix-numeric-value current-prefix-arg)\
|
2019-06-26 22:13:34 +01:00
|
|
|
t)",
|
|
|
|
doc: /* @dots{} */)
|
2013-04-21 19:16:30 +08:00
|
|
|
@end group
|
2013-04-23 11:57:44 -07:00
|
|
|
@end example
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item doc
|
|
|
|
This is the documentation string. It uses C comment syntax rather
|
|
|
|
than C string syntax because comment syntax requires nothing special
|
|
|
|
to include multiple lines. The @samp{doc:} identifies the comment
|
|
|
|
that follows as the documentation string. The @samp{/*} and @samp{*/}
|
|
|
|
delimiters that begin and end the comment are not part of the
|
|
|
|
documentation string.
|
|
|
|
|
|
|
|
If the last line of the documentation string begins with the keyword
|
|
|
|
@samp{usage:}, the rest of the line is treated as the argument list
|
|
|
|
for documentation purposes. This way, you can use different argument
|
|
|
|
names in the documentation string from the ones used in the C code.
|
|
|
|
@samp{usage:} is required if the function has an unlimited number of
|
|
|
|
arguments.
|
|
|
|
|
Stop keeping multiple doc copies for items defined multiple times
It was always a nuisance to keep all the copies in sync.
* src/doc.c (Fsnarf_documentation): Don't skip entire files.
Instead, skip individual doc strings starting with "SKIP".
* doc/lispref/internals.texi (Writing Emacs Primitives):
Mention this skipping.
* lisp/term/ns-win.el, lisp/term/pc-win.el, src/dosfns.c:
* src/frame.c, src/nsfns.m, src/nsmenu.m, src/nsterm.m:
* src/w16select.c, src/w32fns.c, src/w32menu.c, src/w32select.c:
* src/w32term.c, src/xmenu.c: Remove duplicated doc strings.
* src/xfns.c: Merge in information from doc string duplicates.
2018-02-13 14:22:16 -08:00
|
|
|
Some primitives have multiple definitions, one per platform (e.g.,
|
|
|
|
@code{x-create-frame}). In such cases, rather than writing the
|
|
|
|
same documentation string in each definition, only one definition has
|
|
|
|
the actual documentation. The others have placeholders beginning with
|
|
|
|
@samp{SKIP}, which are ignored by the function that parses the
|
|
|
|
@file{DOC} file.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
All the usual rules for documentation strings in Lisp code
|
|
|
|
(@pxref{Documentation Tips}) apply to C code documentation strings
|
|
|
|
too.
|
2018-03-22 14:57:43 +02:00
|
|
|
|
|
|
|
The documentation string can be followed by a list of C function
|
|
|
|
attributes for the C function that implements the primitive, like
|
|
|
|
this:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
DEFUN ("bar", Fbar, Sbar, 0, UNEVALLED, 0
|
2019-06-26 22:13:34 +01:00
|
|
|
doc: /* @dots{} */
|
|
|
|
attributes: @var{attr1} @var{attr2} @dots{})
|
2018-03-22 14:57:43 +02:00
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
You can specify more than a single attribute, one after the other.
|
|
|
|
Currently, only the following attributes are recognized:
|
|
|
|
|
|
|
|
@table @code
|
|
|
|
@item noreturn
|
|
|
|
Declares the C function as one that never returns. This corresponds
|
2023-02-04 15:53:40 -08:00
|
|
|
to C23's @code{[[noreturn]]}, to C11's @code{_Noreturn}, and to GCC's
|
|
|
|
@w{@code{__attribute__ ((__noreturn__))}} (@pxref{Function
|
|
|
|
Attributes,,, gcc, Using the GNU Compiler Collection}). (Internally,
|
|
|
|
Emacs's own C code uses @code{_Noreturn} as it can be defined as a
|
|
|
|
macro on C platforms that do not support it.)
|
2018-03-22 14:57:43 +02:00
|
|
|
|
|
|
|
@item const
|
|
|
|
Declares that the function does not examine any values except its
|
|
|
|
arguments, and has no effects except the return value. This
|
2023-02-04 15:53:40 -08:00
|
|
|
corresponds to C23's @code{[[unsequenced]]} and to GCC's
|
|
|
|
@w{@code{__attribute__ ((__const__))}}.
|
2018-03-22 14:57:43 +02:00
|
|
|
|
|
|
|
@item noinline
|
|
|
|
This corresponds to @w{@code{__attribute__ ((__noinline__))}}
|
|
|
|
attribute of GCC, which prevents the function from being considered
|
|
|
|
for inlining. This might be needed, e.g., to countermand effects of
|
|
|
|
link-time optimizations on stack-based variables.
|
|
|
|
@end table
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@end table
|
|
|
|
|
2010-07-09 11:38:50 +03:00
|
|
|
After the call to the @code{DEFUN} macro, you must write the
|
2012-03-18 23:02:12 +08:00
|
|
|
argument list for the C function, including the types for the
|
|
|
|
arguments. If the primitive accepts a fixed maximum number of Lisp
|
|
|
|
arguments, there must be one C argument for each Lisp argument, and
|
|
|
|
each argument must be of type @code{Lisp_Object}. (Various macros and
|
|
|
|
functions for creating values of type @code{Lisp_Object} are declared
|
2019-06-26 22:13:34 +01:00
|
|
|
in the file @file{lisp.h}.) If the primitive is a special form, it
|
|
|
|
must accept a Lisp list containing its unevaluated Lisp arguments as a
|
|
|
|
single argument of type @code{Lisp_Object}. If the primitive has no
|
|
|
|
upper limit on the number of evaluated Lisp arguments, it must have
|
|
|
|
exactly two C arguments: the first is the number of Lisp arguments,
|
|
|
|
and the second is the address of a block containing their values.
|
|
|
|
These have types @code{ptrdiff_t} and @w{@code{Lisp_Object *}},
|
|
|
|
respectively. Since @code{Lisp_Object} can hold any Lisp object of
|
|
|
|
any data type, you can determine the actual data type only at run
|
|
|
|
time; so if you want a primitive to accept only a certain type of
|
|
|
|
argument, you must check the type explicitly using a suitable
|
|
|
|
predicate (@pxref{Type Predicates}).
|
2012-11-15 09:25:05 +04:00
|
|
|
@cindex type checking internals
|
2007-09-06 04:25:08 +00:00
|
|
|
|
Assume GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS
This removes the need for GCPRO1 etc. Suggested by Stefan Monnier in:
http://lists.gnu.org/archive/html/emacs-devel/2015-08/msg00918.html
* doc/lispref/internals.texi (Writing Emacs Primitives):
* etc/NEWS:
Document the change.
* src/alloc.c (gcprolist, dump_zombies, MAX_ZOMBIES, zombies)
(nzombies, ngcs, avg_zombies, max_live, max_zombies, avg_live)
(Fgc_status, check_gcpros, relocatable_string_data_p, gc-precise):
* src/bytecode.c (mark_byte_stack) [BYTE_MARK_STACK]:
* src/eval.c (gcpro_level) [DEBUG_GCPRO]:
* src/lisp.h (struct handler.gcpro, struct gcpro, GC_MARK_STACK)
(GC_USE_GCPROS_AS_BEFORE, GC_MAKE_GCPROS_NOOPS)
(GC_MARK_STACK_CHECK_GCPROS, GC_USE_GCPROS_CHECK_ZOMBIES)
(BYTE_MARK_STACK, GCPRO1, GCPRO2, GCPRO3, GCPRO4, GCPRO5, GCPRO6)
(GCPRO7, UNGCPRO, RETURN_UNGCPRO):
Remove. All uses removed. The code now assumes
GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS.
* src/bytecode.c (relocate_byte_stack):
Rename from unmark_byte_stack, since it now only relocates.
All callers changed.
* src/frame.c (make_frame): Add an IF_LINT to pacify GCC 5.2
with GCPROs removed.
* src/systime.h: Use EMACS_LISP_H as the canary instead of GCPRO1.
* test/automated/finalizer-tests.el (finalizer-basic)
(finalizer-circular-reference, finalizer-cross-reference)
(finalizer-error):
* test/automated/generator-tests.el (cps-test-iter-close-finalizer):
Remove tests, as they depend on gc-precise.
2015-08-26 19:24:28 -07:00
|
|
|
@cindex garbage collection protection
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex protect C variables from garbage collection
|
Assume GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS
This removes the need for GCPRO1 etc. Suggested by Stefan Monnier in:
http://lists.gnu.org/archive/html/emacs-devel/2015-08/msg00918.html
* doc/lispref/internals.texi (Writing Emacs Primitives):
* etc/NEWS:
Document the change.
* src/alloc.c (gcprolist, dump_zombies, MAX_ZOMBIES, zombies)
(nzombies, ngcs, avg_zombies, max_live, max_zombies, avg_live)
(Fgc_status, check_gcpros, relocatable_string_data_p, gc-precise):
* src/bytecode.c (mark_byte_stack) [BYTE_MARK_STACK]:
* src/eval.c (gcpro_level) [DEBUG_GCPRO]:
* src/lisp.h (struct handler.gcpro, struct gcpro, GC_MARK_STACK)
(GC_USE_GCPROS_AS_BEFORE, GC_MAKE_GCPROS_NOOPS)
(GC_MARK_STACK_CHECK_GCPROS, GC_USE_GCPROS_CHECK_ZOMBIES)
(BYTE_MARK_STACK, GCPRO1, GCPRO2, GCPRO3, GCPRO4, GCPRO5, GCPRO6)
(GCPRO7, UNGCPRO, RETURN_UNGCPRO):
Remove. All uses removed. The code now assumes
GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS.
* src/bytecode.c (relocate_byte_stack):
Rename from unmark_byte_stack, since it now only relocates.
All callers changed.
* src/frame.c (make_frame): Add an IF_LINT to pacify GCC 5.2
with GCPROs removed.
* src/systime.h: Use EMACS_LISP_H as the canary instead of GCPRO1.
* test/automated/finalizer-tests.el (finalizer-basic)
(finalizer-circular-reference, finalizer-cross-reference)
(finalizer-error):
* test/automated/generator-tests.el (cps-test-iter-close-finalizer):
Remove tests, as they depend on gc-precise.
2015-08-26 19:24:28 -07:00
|
|
|
Within the function @code{For} itself, the local variable
|
|
|
|
@code{args} refers to objects controlled by Emacs's stack-marking
|
|
|
|
garbage collector. Although the garbage collector does not reclaim
|
|
|
|
objects reachable from C @code{Lisp_Object} stack variables, it may
|
2019-06-26 18:02:26 +03:00
|
|
|
move some of the components of an object, such as the contents of a
|
|
|
|
string or the text of a buffer. Therefore, functions that access
|
|
|
|
these components must take care to refetch their addresses after
|
|
|
|
performing Lisp evaluation. This means that instead of keeping C
|
|
|
|
pointers to string contents or buffer text, the code should keep the
|
|
|
|
buffer or string position, and recompute the C pointer from the
|
|
|
|
position after performing Lisp evaluation. Lisp evaluation can occur
|
|
|
|
via calls to @code{eval_sub} or @code{Feval}, either directly or
|
Assume GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS
This removes the need for GCPRO1 etc. Suggested by Stefan Monnier in:
http://lists.gnu.org/archive/html/emacs-devel/2015-08/msg00918.html
* doc/lispref/internals.texi (Writing Emacs Primitives):
* etc/NEWS:
Document the change.
* src/alloc.c (gcprolist, dump_zombies, MAX_ZOMBIES, zombies)
(nzombies, ngcs, avg_zombies, max_live, max_zombies, avg_live)
(Fgc_status, check_gcpros, relocatable_string_data_p, gc-precise):
* src/bytecode.c (mark_byte_stack) [BYTE_MARK_STACK]:
* src/eval.c (gcpro_level) [DEBUG_GCPRO]:
* src/lisp.h (struct handler.gcpro, struct gcpro, GC_MARK_STACK)
(GC_USE_GCPROS_AS_BEFORE, GC_MAKE_GCPROS_NOOPS)
(GC_MARK_STACK_CHECK_GCPROS, GC_USE_GCPROS_CHECK_ZOMBIES)
(BYTE_MARK_STACK, GCPRO1, GCPRO2, GCPRO3, GCPRO4, GCPRO5, GCPRO6)
(GCPRO7, UNGCPRO, RETURN_UNGCPRO):
Remove. All uses removed. The code now assumes
GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS.
* src/bytecode.c (relocate_byte_stack):
Rename from unmark_byte_stack, since it now only relocates.
All callers changed.
* src/frame.c (make_frame): Add an IF_LINT to pacify GCC 5.2
with GCPROs removed.
* src/systime.h: Use EMACS_LISP_H as the canary instead of GCPRO1.
* test/automated/finalizer-tests.el (finalizer-basic)
(finalizer-circular-reference, finalizer-cross-reference)
(finalizer-error):
* test/automated/generator-tests.el (cps-test-iter-close-finalizer):
Remove tests, as they depend on gc-precise.
2015-08-26 19:24:28 -07:00
|
|
|
indirectly.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2017-01-25 21:13:19 -08:00
|
|
|
@cindex @code{maybe_quit}, use in Lisp primitives
|
|
|
|
Note the call to @code{maybe_quit} inside the loop: this function
|
2015-10-16 13:01:53 +03:00
|
|
|
checks whether the user pressed @kbd{C-g}, and if so, aborts the
|
|
|
|
processing. You should do that in any loop that can potentially
|
|
|
|
require a large number of iterations; in this case, the list of
|
|
|
|
arguments could be very long. This increases Emacs responsiveness and
|
|
|
|
improves user experience.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
You must not use C initializers for static or global variables unless
|
|
|
|
the variables are never written once Emacs is dumped. These variables
|
|
|
|
with initializers are allocated in an area of memory that becomes
|
|
|
|
read-only (on certain operating systems) as a result of dumping Emacs.
|
|
|
|
|
|
|
|
@cindex @code{defsubr}, Lisp symbol for a primitive
|
|
|
|
Defining the C function is not enough to make a Lisp primitive
|
|
|
|
available; you must also create the Lisp symbol for the primitive and
|
|
|
|
store a suitable subr object in its function cell. The code looks like
|
|
|
|
this:
|
|
|
|
|
|
|
|
@example
|
2012-03-03 14:13:28 -08:00
|
|
|
defsubr (&@var{sname});
|
2007-09-06 04:25:08 +00:00
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
2012-03-03 14:13:28 -08:00
|
|
|
Here @var{sname} is the name you used as the third argument to @code{DEFUN}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
If you add a new primitive to a file that already has Lisp primitives
|
|
|
|
defined in it, find the function (near the end of the file) named
|
|
|
|
@code{syms_of_@var{something}}, and add the call to @code{defsubr}
|
|
|
|
there. If the file doesn't have this function, or if you create a new
|
|
|
|
file, add to it a @code{syms_of_@var{filename}} (e.g.,
|
|
|
|
@code{syms_of_myfile}). Then find the spot in @file{emacs.c} where all
|
|
|
|
of these functions are called, and add a call to
|
|
|
|
@code{syms_of_@var{filename}} there.
|
|
|
|
|
|
|
|
@anchor{Defining Lisp variables in C}
|
|
|
|
@vindex byte-boolean-vars
|
|
|
|
@cindex defining Lisp variables in C
|
2019-10-09 15:12:18 +02:00
|
|
|
@cindex @code{DEFVAR_INT}, @code{DEFVAR_LISP}, @code{DEFVAR_BOOL}, @code{DEFSYM}
|
2007-09-06 04:25:08 +00:00
|
|
|
The function @code{syms_of_@var{filename}} is also the place to define
|
|
|
|
any C variables that are to be visible as Lisp variables.
|
|
|
|
@code{DEFVAR_LISP} makes a C variable of type @code{Lisp_Object} visible
|
|
|
|
in Lisp. @code{DEFVAR_INT} makes a C variable of type @code{int}
|
|
|
|
visible in Lisp with a value that is always an integer.
|
|
|
|
@code{DEFVAR_BOOL} makes a C variable of type @code{int} visible in Lisp
|
|
|
|
with a value that is either @code{t} or @code{nil}. Note that variables
|
|
|
|
defined with @code{DEFVAR_BOOL} are automatically added to the list
|
|
|
|
@code{byte-boolean-vars} used by the byte compiler.
|
|
|
|
|
2019-10-09 15:12:18 +02:00
|
|
|
These macros all expect three arguments:
|
|
|
|
|
|
|
|
@table @code
|
|
|
|
@item lname
|
|
|
|
The name of the variable to be used by Lisp programs.
|
|
|
|
@item vname
|
|
|
|
The name of the variable in the C sources.
|
|
|
|
@item doc
|
|
|
|
The documentation for the variable, as a C
|
2019-12-29 15:41:52 -08:00
|
|
|
comment. @xref{Documentation Basics}, for more details.
|
2019-10-09 15:12:18 +02:00
|
|
|
@end table
|
|
|
|
|
|
|
|
By convention, when defining variables of a ``native'' type
|
|
|
|
(@code{int} and @code{bool}), the name of the C variable is the name
|
|
|
|
of the Lisp variable with @code{-} replaced by @code{_}. When the
|
|
|
|
variable has type @code{Lisp_Object}, the convention is to also prefix
|
2025-01-19 14:28:30 +01:00
|
|
|
the C variable name with @code{V}. This is an example:
|
2019-10-09 15:12:18 +02:00
|
|
|
|
|
|
|
@smallexample
|
|
|
|
DEFVAR_INT ("my-int-variable", my_int_variable,
|
|
|
|
doc: /* An integer variable. */);
|
|
|
|
|
|
|
|
DEFVAR_LISP ("my-lisp-variable", Vmy_lisp_variable,
|
|
|
|
doc: /* A Lisp variable. */);
|
|
|
|
@end smallexample
|
|
|
|
|
|
|
|
There are situations in Lisp where you need to refer to the symbol
|
|
|
|
itself rather than the value of that symbol. One such case is when
|
|
|
|
temporarily overriding the value of a variable, which in Lisp is done
|
|
|
|
with @code{let}. In C sources, this is done by defining a
|
|
|
|
corresponding, constant symbol, and using @code{specbind}. By
|
|
|
|
convention, @code{Qmy_lisp_variable} corresponds to
|
|
|
|
@code{Vmy_lisp_variable}; to define it, use the @code{DEFSYM} macro.
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
DEFSYM (Qmy_lisp_variable, "my-lisp-variable");
|
|
|
|
@end smallexample
|
|
|
|
|
|
|
|
To perform the actual binding:
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
specbind (Qmy_lisp_variable, Qt);
|
|
|
|
@end smallexample
|
|
|
|
|
2025-01-19 14:28:30 +01:00
|
|
|
In Lisp, symbols sometimes need to be quoted. To achieve the same
|
|
|
|
effect in C, you again use the corresponding constant symbol
|
2019-10-09 15:12:18 +02:00
|
|
|
@code{Qmy_lisp_variable}. For example, when creating a buffer-local
|
2025-01-19 14:28:30 +01:00
|
|
|
variable (@pxref{Buffer-Local Variables}) in Lisp, you would write:
|
2019-10-09 15:12:18 +02:00
|
|
|
|
|
|
|
@smallexample
|
|
|
|
(make-variable-buffer-local 'my-lisp-variable)
|
|
|
|
@end smallexample
|
|
|
|
|
2025-01-19 14:28:30 +01:00
|
|
|
In C, the corresponding code uses @code{Fmake_variable_buffer_local} in
|
|
|
|
combination with @code{DEFSYM}:
|
2019-10-09 15:12:18 +02:00
|
|
|
|
|
|
|
@smallexample
|
|
|
|
DEFSYM (Qmy_lisp_variable, "my-lisp-variable");
|
|
|
|
Fmake_variable_buffer_local (Qmy_lisp_variable);
|
|
|
|
@end smallexample
|
|
|
|
|
2012-03-03 14:13:28 -08:00
|
|
|
@cindex defining customization variables in C
|
2018-03-21 20:52:35 +01:00
|
|
|
If you want to make a Lisp variable that is defined in C behave
|
2012-03-03 14:13:28 -08:00
|
|
|
like one declared with @code{defcustom}, add an appropriate entry to
|
2019-10-09 15:12:18 +02:00
|
|
|
@file{cus-start.el}. @xref{Variable Definitions}, for a description of
|
|
|
|
the format to use.
|
2012-03-03 14:13:28 -08:00
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex @code{staticpro}, protection from GC
|
2019-10-09 15:12:18 +02:00
|
|
|
If you directly define a file-scope C variable of type
|
2025-01-19 13:59:13 +01:00
|
|
|
@code{Lisp_Object}, you must protect it from garbage collection by
|
2019-10-09 15:12:18 +02:00
|
|
|
calling @code{staticpro} in @code{syms_of_@var{filename}}, like this:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
staticpro (&@var{variable});
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Here is another example function, with more complicated arguments.
|
|
|
|
This comes from the code in @file{window.c}, and it demonstrates the use
|
|
|
|
of macros and functions to manipulate Lisp objects.
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
|
|
|
DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
|
2019-06-26 22:13:34 +01:00
|
|
|
Scoordinates_in_window_p, 2, 2, 0,
|
|
|
|
doc: /* Return non-nil if COORDINATES are in WINDOW.
|
|
|
|
@dots{}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@group
|
2012-03-03 14:13:28 -08:00
|
|
|
or `right-margin' is returned. */)
|
|
|
|
(register Lisp_Object coordinates, Lisp_Object window)
|
2007-09-06 04:25:08 +00:00
|
|
|
@{
|
2012-03-03 14:13:28 -08:00
|
|
|
struct window *w;
|
|
|
|
struct frame *f;
|
2007-09-06 04:25:08 +00:00
|
|
|
int x, y;
|
2012-03-03 14:13:28 -08:00
|
|
|
Lisp_Object lx, ly;
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
2019-06-26 22:13:34 +01:00
|
|
|
w = decode_live_window (window);
|
2012-03-03 14:13:28 -08:00
|
|
|
f = XFRAME (w->frame);
|
|
|
|
CHECK_CONS (coordinates);
|
|
|
|
lx = Fcar (coordinates);
|
|
|
|
ly = Fcdr (coordinates);
|
2019-06-26 22:13:34 +01:00
|
|
|
CHECK_NUMBER (lx);
|
|
|
|
CHECK_NUMBER (ly);
|
|
|
|
x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
|
|
|
|
y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
2012-03-03 14:13:28 -08:00
|
|
|
switch (coordinates_in_window (w, x, y))
|
2007-09-06 04:25:08 +00:00
|
|
|
@{
|
Minor quoting etc. fixes to lispref manual
* doc/lispref/tips.texi (Documentation Tips):
Distinguish more clearly among grave accent, apostrophe,
and single quote.
* doc/lispref/README, doc/lispref/buffers.texi:
* doc/lispref/commands.texi, doc/lispref/control.texi:
* doc/lispref/customize.texi, doc/lispref/display.texi:
* doc/lispref/elisp.texi, doc/lispref/files.texi:
* doc/lispref/frames.texi, doc/lispref/hash.texi:
* doc/lispref/help.texi, doc/lispref/internals.texi:
* doc/lispref/loading.texi, doc/lispref/makefile.w32-in:
* doc/lispref/markers.texi, doc/lispref/modes.texi:
* doc/lispref/nonascii.texi, doc/lispref/objects.texi:
* doc/lispref/os.texi, doc/lispref/positions.texi:
* doc/lispref/strings.texi, doc/lispref/syntax.texi:
* doc/lispref/text.texi, doc/lispref/tips.texi:
* doc/lispref/two-volume-cross-refs.txt, doc/lispref/windows.texi:
Use American-style double quoting in ordinary text,
and quote 'like this' when single-quoting in ASCII text.
Also, fix some minor spacing issues.
2015-04-10 11:27:21 -07:00
|
|
|
case ON_NOTHING: /* NOT in window at all. */
|
2007-09-06 04:25:08 +00:00
|
|
|
return Qnil;
|
|
|
|
@end group
|
|
|
|
|
2019-06-26 22:13:34 +01:00
|
|
|
@dots{}
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@group
|
Minor quoting etc. fixes to lispref manual
* doc/lispref/tips.texi (Documentation Tips):
Distinguish more clearly among grave accent, apostrophe,
and single quote.
* doc/lispref/README, doc/lispref/buffers.texi:
* doc/lispref/commands.texi, doc/lispref/control.texi:
* doc/lispref/customize.texi, doc/lispref/display.texi:
* doc/lispref/elisp.texi, doc/lispref/files.texi:
* doc/lispref/frames.texi, doc/lispref/hash.texi:
* doc/lispref/help.texi, doc/lispref/internals.texi:
* doc/lispref/loading.texi, doc/lispref/makefile.w32-in:
* doc/lispref/markers.texi, doc/lispref/modes.texi:
* doc/lispref/nonascii.texi, doc/lispref/objects.texi:
* doc/lispref/os.texi, doc/lispref/positions.texi:
* doc/lispref/strings.texi, doc/lispref/syntax.texi:
* doc/lispref/text.texi, doc/lispref/tips.texi:
* doc/lispref/two-volume-cross-refs.txt, doc/lispref/windows.texi:
Use American-style double quoting in ordinary text,
and quote 'like this' when single-quoting in ASCII text.
Also, fix some minor spacing issues.
2015-04-10 11:27:21 -07:00
|
|
|
case ON_MODE_LINE: /* In mode line of window. */
|
2007-09-06 04:25:08 +00:00
|
|
|
return Qmode_line;
|
|
|
|
@end group
|
|
|
|
|
2019-06-26 22:13:34 +01:00
|
|
|
@dots{}
|
2012-03-03 14:13:28 -08:00
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@group
|
2012-03-03 14:13:28 -08:00
|
|
|
case ON_SCROLL_BAR: /* On scroll-bar of window. */
|
|
|
|
/* Historically we are supposed to return nil in this case. */
|
|
|
|
return Qnil;
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
default:
|
2019-06-26 22:13:34 +01:00
|
|
|
emacs_abort ();
|
2007-09-06 04:25:08 +00:00
|
|
|
@}
|
|
|
|
@}
|
|
|
|
@end group
|
|
|
|
@end smallexample
|
|
|
|
|
|
|
|
Note that C code cannot call functions by name unless they are defined
|
2012-12-05 14:27:56 -08:00
|
|
|
in C@. The way to call a function written in Lisp is to use
|
2007-09-06 04:25:08 +00:00
|
|
|
@code{Ffuncall}, which embodies the Lisp function @code{funcall}. Since
|
|
|
|
the Lisp function @code{funcall} accepts an unlimited number of
|
|
|
|
arguments, in C it takes two: the number of Lisp-level arguments, and a
|
|
|
|
one-dimensional array containing their values. The first Lisp-level
|
|
|
|
argument is the Lisp function to call, and the rest are the arguments to
|
Assume GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS
This removes the need for GCPRO1 etc. Suggested by Stefan Monnier in:
http://lists.gnu.org/archive/html/emacs-devel/2015-08/msg00918.html
* doc/lispref/internals.texi (Writing Emacs Primitives):
* etc/NEWS:
Document the change.
* src/alloc.c (gcprolist, dump_zombies, MAX_ZOMBIES, zombies)
(nzombies, ngcs, avg_zombies, max_live, max_zombies, avg_live)
(Fgc_status, check_gcpros, relocatable_string_data_p, gc-precise):
* src/bytecode.c (mark_byte_stack) [BYTE_MARK_STACK]:
* src/eval.c (gcpro_level) [DEBUG_GCPRO]:
* src/lisp.h (struct handler.gcpro, struct gcpro, GC_MARK_STACK)
(GC_USE_GCPROS_AS_BEFORE, GC_MAKE_GCPROS_NOOPS)
(GC_MARK_STACK_CHECK_GCPROS, GC_USE_GCPROS_CHECK_ZOMBIES)
(BYTE_MARK_STACK, GCPRO1, GCPRO2, GCPRO3, GCPRO4, GCPRO5, GCPRO6)
(GCPRO7, UNGCPRO, RETURN_UNGCPRO):
Remove. All uses removed. The code now assumes
GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS.
* src/bytecode.c (relocate_byte_stack):
Rename from unmark_byte_stack, since it now only relocates.
All callers changed.
* src/frame.c (make_frame): Add an IF_LINT to pacify GCC 5.2
with GCPROs removed.
* src/systime.h: Use EMACS_LISP_H as the canary instead of GCPRO1.
* test/automated/finalizer-tests.el (finalizer-basic)
(finalizer-circular-reference, finalizer-cross-reference)
(finalizer-error):
* test/automated/generator-tests.el (cps-test-iter-close-finalizer):
Remove tests, as they depend on gc-precise.
2015-08-26 19:24:28 -07:00
|
|
|
pass to it.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2025-01-19 12:44:05 +01:00
|
|
|
The C macro @code{calln} is a convenient way to call a Lisp function
|
|
|
|
without having to specify the number of arguments. It works by calling
|
|
|
|
@code{Ffuncall}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@file{eval.c} is a very good file to look through for examples;
|
|
|
|
@file{lisp.h} contains the definitions for some important macros and
|
|
|
|
functions.
|
|
|
|
|
2019-04-17 16:34:47 +01:00
|
|
|
If you define a function which is side-effect free or pure, give it
|
|
|
|
a non-@code{nil} @code{side-effect-free} or @code{pure} property,
|
2025-01-19 13:57:23 +01:00
|
|
|
respectively (@pxref{Standard Properties}). See the lists defined in
|
|
|
|
@samp{byte-opt.el}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-10-11 20:53:05 +03:00
|
|
|
@node Writing Dynamic Modules
|
|
|
|
@section Writing Dynamically-Loaded Modules
|
|
|
|
@cindex writing emacs modules
|
|
|
|
@cindex dynamic modules, writing
|
|
|
|
|
|
|
|
@cindex module @acronym{API}
|
|
|
|
This section describes the Emacs module @acronym{API} and how to use
|
|
|
|
it as part of writing extension modules for Emacs. The module
|
|
|
|
@acronym{API} is defined in the C programming language, therefore the
|
|
|
|
description and the examples in this section assume the module is
|
|
|
|
written in C@. For other programming languages, you will need to use
|
|
|
|
the appropriate bindings, interfaces and facilities for calling C code.
|
|
|
|
Emacs C code requires a C99 or later compiler (@pxref{C Dialect}), and
|
|
|
|
so the code examples in this section also follow that standard.
|
|
|
|
|
|
|
|
Writing a module and integrating it into Emacs comprises the following
|
|
|
|
tasks:
|
|
|
|
|
|
|
|
@itemize @bullet
|
|
|
|
@item
|
|
|
|
Writing initialization code for the module.
|
|
|
|
|
|
|
|
@item
|
|
|
|
Writing one or more module functions.
|
|
|
|
|
|
|
|
@item
|
|
|
|
Communicating values and objects between Emacs and your module
|
|
|
|
functions.
|
|
|
|
|
|
|
|
@item
|
|
|
|
Handling of error conditions and nonlocal exits.
|
|
|
|
@end itemize
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
The following subsections describe these tasks and the @acronym{API}
|
|
|
|
itself in more detail.
|
|
|
|
|
|
|
|
Once your module is written, compile it to produce a shared library,
|
|
|
|
according to the conventions of the underlying platform. Then place
|
|
|
|
the shared library in a directory mentioned in @code{load-path}
|
|
|
|
(@pxref{Library Search}), where Emacs will find it.
|
|
|
|
|
2018-10-11 21:48:10 +03:00
|
|
|
If you wish to verify the conformance of a module to the Emacs dynamic
|
2018-10-11 20:53:05 +03:00
|
|
|
module @acronym{API}, invoke Emacs with the @kbd{--module-assertions}
|
|
|
|
option. @xref{Initial Options,,,emacs, The GNU Emacs Manual}.
|
|
|
|
|
|
|
|
@menu
|
|
|
|
* Module Initialization::
|
|
|
|
* Module Functions::
|
|
|
|
* Module Values::
|
|
|
|
* Module Misc::
|
|
|
|
* Module Nonlocal::
|
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Module Initialization
|
|
|
|
@subsection Module Initialization Code
|
|
|
|
@cindex module initialization
|
|
|
|
|
|
|
|
Begin your module by including the header file @file{emacs-module.h}
|
|
|
|
and defining the GPL compatibility symbol:
|
|
|
|
|
|
|
|
@example
|
|
|
|
#include <emacs-module.h>
|
|
|
|
|
|
|
|
int plugin_is_GPL_compatible;
|
|
|
|
@end example
|
|
|
|
|
|
|
|
The @file{emacs-module.h} file is installed into your system's include
|
|
|
|
tree as part of the Emacs installation. Alternatively, you can find
|
|
|
|
it in the Emacs source tree.
|
|
|
|
|
|
|
|
@anchor{module initialization function}
|
|
|
|
Next, write an initialization function for the module.
|
|
|
|
|
|
|
|
@deftypefn Function int emacs_module_init (struct emacs_runtime *@var{runtime})
|
|
|
|
Emacs calls this function when it loads a module. If a module does
|
|
|
|
not export a function named @code{emacs_module_init}, trying to load
|
|
|
|
the module will signal an error. The initialization function should
|
|
|
|
return zero if the initialization succeeds, non-zero otherwise. In
|
|
|
|
the latter case, Emacs will signal an error, and the loading of the
|
|
|
|
module will fail. If the user presses @kbd{C-g} during the
|
|
|
|
initialization, Emacs ignores the return value of the initialization
|
|
|
|
function and quits (@pxref{Quitting}). (If needed, you can catch user
|
|
|
|
quitting inside the initialization function, @pxref{should_quit}.)
|
|
|
|
|
|
|
|
The argument @var{runtime} is a pointer to a C @code{struct} that
|
|
|
|
includes 2 public fields: @code{size}, which provides the size of the
|
|
|
|
structure in bytes; and @code{get_environment}, which provides a
|
|
|
|
pointer to a function that allows the module initialization function
|
|
|
|
access to the Emacs environment object and its interfaces.
|
|
|
|
|
|
|
|
The initialization function should perform whatever initialization is
|
|
|
|
required for the module. In addition, it can perform the following
|
|
|
|
tasks:
|
|
|
|
|
|
|
|
@table @asis
|
|
|
|
@cindex compatibility, between modules and Emacs
|
|
|
|
@item Compatibility verification
|
|
|
|
A module can verify that the Emacs executable which loads the module
|
|
|
|
is compatible with the module, by comparing the @code{size} member of
|
|
|
|
the @var{runtime} structure with the value compiled into the module:
|
|
|
|
|
|
|
|
@example
|
|
|
|
int
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
emacs_module_init (struct emacs_runtime *runtime)
|
2018-10-11 20:53:05 +03:00
|
|
|
@{
|
2019-12-23 18:30:09 +01:00
|
|
|
if (runtime->size < sizeof (*runtime))
|
2018-10-11 20:53:05 +03:00
|
|
|
return 1;
|
|
|
|
@}
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
If the size of the runtime object passed to the module is smaller than
|
|
|
|
what it expects, it means the module was compiled for an Emacs version
|
|
|
|
newer (later) than the one which attempts to load it, i.e.@: the
|
|
|
|
module might be incompatible with the Emacs binary.
|
|
|
|
|
|
|
|
In addition, a module can verify the compatibility of the module
|
|
|
|
@acronym{API} with what the module expects. The following sample code
|
|
|
|
assumes it is part of the @code{emacs_module_init} function shown
|
|
|
|
above:
|
|
|
|
|
|
|
|
@example
|
2019-12-23 18:30:09 +01:00
|
|
|
emacs_env *env = runtime->get_environment (runtime);
|
2018-10-11 20:53:05 +03:00
|
|
|
if (env->size < sizeof (*env))
|
|
|
|
return 2;
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
@cindex module runtime environment
|
|
|
|
This calls the @code{get_environment} function using the pointer
|
|
|
|
provided in the @code{runtime} structure to retrieve a pointer to the
|
|
|
|
@acronym{API}'s @dfn{environment}, a C @code{struct} which also has a
|
|
|
|
@code{size} field holding the size of the structure in bytes.
|
|
|
|
|
|
|
|
Finally, you can write a module that will work with older versions of
|
|
|
|
Emacs, by comparing the size of the environment passed by Emacs with
|
|
|
|
known sizes, like this:
|
|
|
|
|
|
|
|
@example
|
2019-12-23 18:30:09 +01:00
|
|
|
emacs_env *env = runtime->get_environment (runtime);
|
2018-10-11 20:53:05 +03:00
|
|
|
if (env->size >= sizeof (struct emacs_env_26))
|
|
|
|
emacs_version = 26; /* Emacs 26 or later. */
|
|
|
|
else if (env->size >= sizeof (struct emacs_env_25))
|
|
|
|
emacs_version = 25;
|
|
|
|
else
|
|
|
|
return 2; /* Unknown or unsupported version. */
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
This works because later Emacs versions always @emph{add} members to
|
|
|
|
the environment, never @emph{remove} any members, so the size can only
|
|
|
|
grow with new Emacs releases. Given the version of Emacs, the module
|
|
|
|
can use only the parts of the module @acronym{API} that existed in
|
|
|
|
that version, since those parts are identical in later versions.
|
|
|
|
|
2019-04-24 10:22:18 +02:00
|
|
|
@file{emacs-module.h} defines a preprocessor macro
|
|
|
|
@code{EMACS_MAJOR_VERSION}. It expands to an integer literal which is
|
|
|
|
the latest major version of Emacs supported by the header.
|
|
|
|
@xref{Version Info}. Note that the value of
|
|
|
|
@code{EMACS_MAJOR_VERSION} is a compile-time constant and does not
|
|
|
|
represent the version of Emacs that is currently running and has
|
|
|
|
loaded your module. If you want your module to be compatible with
|
|
|
|
various versions of @file{emacs-module.h} as well as various versions
|
|
|
|
of Emacs, you can use conditional compilation based on
|
|
|
|
@code{EMACS_MAJOR_VERSION}.
|
|
|
|
|
2018-10-11 20:53:05 +03:00
|
|
|
We recommend that modules always perform the compatibility
|
|
|
|
verification, unless they do their job entirely in the initialization
|
|
|
|
function, and don't access any Lisp objects or use any Emacs functions
|
|
|
|
accessible through the environment structure.
|
|
|
|
|
|
|
|
@item Binding module functions to Lisp symbols
|
|
|
|
This gives the module functions names so that Lisp code could call it
|
|
|
|
by that name. We describe how to do this in @ref{Module Functions}
|
|
|
|
below.
|
|
|
|
@end table
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
@node Module Functions
|
|
|
|
@subsection Writing Module Functions
|
|
|
|
@cindex writing module functions
|
|
|
|
@cindex module functions
|
|
|
|
|
|
|
|
The main reason for writing an Emacs module is to make additional
|
|
|
|
functions available to Lisp programs that load the module. This
|
|
|
|
subsection describes how to write such @dfn{module functions}.
|
|
|
|
|
|
|
|
A module function has the following general form and signature:
|
|
|
|
|
2019-12-26 10:29:21 +01:00
|
|
|
@deftypefn Function emacs_value emacs_function (emacs_env *@var{env}, ptrdiff_t @var{nargs}, emacs_value *@var{args}, void *@var{data})
|
|
|
|
@tindex emacs_function
|
2018-10-11 20:53:05 +03:00
|
|
|
The @var{env} argument provides a pointer to the @acronym{API}
|
|
|
|
environment, needed to access Emacs objects and functions. The
|
|
|
|
@var{nargs} argument is the required number of arguments, which can be
|
|
|
|
zero (see @code{make_function} below for more flexible specification
|
|
|
|
of the argument number), and @var{args} is a pointer to the array of
|
|
|
|
the function arguments. The argument @var{data} points to additional
|
|
|
|
data required by the function, which was arranged when
|
|
|
|
@code{make_function} (see below) was called to create an Emacs
|
2019-12-26 10:29:21 +01:00
|
|
|
function from @code{emacs_function}.
|
2018-10-11 20:53:05 +03:00
|
|
|
|
|
|
|
Module functions use the type @code{emacs_value} to communicate Lisp
|
|
|
|
objects between Emacs and the module (@pxref{Module Values}). The
|
|
|
|
@acronym{API}, described below and in the following subsections,
|
|
|
|
provides facilities for conversion between basic C data types and the
|
|
|
|
corresponding @code{emacs_value} objects.
|
|
|
|
|
2024-01-13 12:01:47 +02:00
|
|
|
In the module function's body, do @emph{not} attempt to access
|
|
|
|
elements of the @var{args} array beyond the index
|
|
|
|
@code{@var{nargs}-1}: memory for the @var{args} array is allocated
|
|
|
|
exactly to accommodate @var{nargs} values, and accessing beyond that
|
|
|
|
will most probably crash your module. In particular, if the value of
|
|
|
|
@var{nargs} passed to the function at run time is zero, it must not
|
|
|
|
access @var{args} at all, as no memory will have been allocated for it
|
|
|
|
in that case.
|
|
|
|
|
2018-10-11 20:53:05 +03:00
|
|
|
A module function always returns a value. If the function returns
|
|
|
|
normally, the Lisp code which called it will see the Lisp object
|
|
|
|
corresponding to the @code{emacs_value} value the function returned.
|
|
|
|
However, if the user typed @kbd{C-g}, or if the module function or its
|
|
|
|
callees signaled an error or exited nonlocally (@pxref{Module
|
|
|
|
Nonlocal}), Emacs will ignore the returned value and quit or throw as
|
|
|
|
it does when Lisp code encounters the same situations.
|
2019-12-26 10:29:21 +01:00
|
|
|
|
|
|
|
The header @file{emacs-module.h} provides the type
|
|
|
|
@code{emacs_function} as an alias type for a function pointer to a
|
|
|
|
module function.
|
2018-10-11 20:53:05 +03:00
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
After writing your C code for a module function, you should make a
|
2018-10-14 19:12:49 +03:00
|
|
|
Lisp function object from it using the @code{make_function} function,
|
|
|
|
whose pointer is provided in the environment (recall that the pointer
|
|
|
|
to the environment is returned by @code{get_environment}). This is
|
2018-10-11 20:53:05 +03:00
|
|
|
normally done in the module initialization function (@pxref{module
|
|
|
|
initialization function}), after verifying the @acronym{API}
|
2018-10-14 19:12:49 +03:00
|
|
|
compatibility.
|
2018-10-11 20:53:05 +03:00
|
|
|
|
2019-12-26 10:29:21 +01:00
|
|
|
@deftypefn Function emacs_value make_function (emacs_env *@var{env}, ptrdiff_t @var{min_arity}, ptrdiff_t @var{max_arity}, emacs_function @var{func}, const char *@var{docstring}, void *@var{data})
|
2018-10-11 20:53:05 +03:00
|
|
|
@vindex emacs_variadic_function
|
|
|
|
This returns an Emacs function created from the C function @var{func},
|
2019-12-26 10:29:21 +01:00
|
|
|
whose signature is as described for @code{emacs_function} above.
|
|
|
|
The arguments
|
2018-10-11 20:53:05 +03:00
|
|
|
@var{min_arity} and @var{max_arity} specify the minimum and maximum
|
|
|
|
number of arguments that @var{func} can accept. The @var{max_arity}
|
|
|
|
argument can have the special value @code{emacs_variadic_function},
|
|
|
|
which makes the function accept an unlimited number of arguments, like
|
|
|
|
the @code{&rest} keyword in Lisp (@pxref{Argument List}).
|
|
|
|
|
|
|
|
The argument @var{data} is a way to arrange for arbitrary additional
|
|
|
|
data to be passed to @var{func} when it is called. Whatever pointer
|
|
|
|
is passed to @code{make_function} will be passed unaltered to
|
|
|
|
@var{func}.
|
|
|
|
|
|
|
|
The argument @var{docstring} specifies the documentation string for
|
|
|
|
the function. It should be either an @acronym{ASCII} string, or a
|
|
|
|
UTF-8 encoded non-@acronym{ASCII} string, or a @code{NULL} pointer; in
|
|
|
|
the latter case the function will have no documentation. The
|
|
|
|
documentation string can end with a line that specifies the advertised
|
|
|
|
calling convention, see @ref{Function Documentation}.
|
|
|
|
|
|
|
|
Since every module function must accept the pointer to the environment
|
|
|
|
as its first argument, the call to @code{make_function} could be made
|
|
|
|
from any module function, but you will normally want to do that from
|
|
|
|
the module initialization function, so that all the module functions
|
|
|
|
are known to Emacs once the module is loaded.
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
Finally, you should bind the Lisp function to a symbol, so that Lisp
|
|
|
|
code could call your function by name. For that, use the module
|
|
|
|
@acronym{API} function @code{intern} (@pxref{intern}) whose pointer is
|
|
|
|
also provided in the environment that module functions can access.
|
|
|
|
|
|
|
|
Combining the above steps, code that arranges for a C function
|
|
|
|
@code{module_func} to be callable as @code{module-func} from Lisp will
|
|
|
|
look like this, as part of the module initialization function:
|
|
|
|
|
|
|
|
@example
|
2019-12-23 18:30:09 +01:00
|
|
|
emacs_env *env = runtime->get_environment (runtime);
|
2018-10-11 20:53:05 +03:00
|
|
|
emacs_value func = env->make_function (env, min_arity, max_arity,
|
|
|
|
module_func, docstring, data);
|
|
|
|
emacs_value symbol = env->intern (env, "module-func");
|
|
|
|
emacs_value args[] = @{symbol, func@};
|
|
|
|
env->funcall (env, env->intern (env, "defalias"), 2, args);
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
This makes the symbol @code{module-func} known to Emacs by calling
|
|
|
|
@code{env->intern}, then invokes @code{defalias} from Emacs to bind
|
|
|
|
the function to that symbol. Note that it is possible to use
|
|
|
|
@code{fset} instead of @code{defalias}; the differences are described
|
|
|
|
in @ref{Defining Functions, defalias}.
|
|
|
|
|
2019-12-26 11:31:51 +01:00
|
|
|
Module functions including the @code{emacs_module_init} function
|
|
|
|
(@pxref{module initialization function}) may only interact with Emacs
|
|
|
|
by calling environment functions from some live @code{emacs_env}
|
|
|
|
pointer while being called directly or indirectly from Emacs. In
|
|
|
|
other words, if a module function wants to call Lisp functions or
|
|
|
|
Emacs primitives, convert @code{emacs_value} objects to and from C
|
|
|
|
datatypes (@pxref{Module Values}), or interact with Emacs in any other
|
|
|
|
way, some call from Emacs to @code{emacs_module_init} or to a module
|
2021-06-06 07:52:01 +01:00
|
|
|
function must be in the call stack. Module functions may not interact
|
2019-12-26 11:31:51 +01:00
|
|
|
with Emacs while garbage collection is running; @pxref{Garbage
|
|
|
|
Collection}. They may only interact with Emacs from Lisp interpreter
|
|
|
|
threads (including the main thread) created by Emacs; @pxref{Threads}.
|
|
|
|
The @kbd{--module-assertions} command-line option can detect some
|
|
|
|
violations of the above requirements. @xref{Initial Options,,,emacs,
|
|
|
|
The GNU Emacs Manual}.
|
|
|
|
|
2018-10-11 20:53:05 +03:00
|
|
|
Using the module @acronym{API}, it is possible to define more complex
|
2020-09-13 20:21:41 +02:00
|
|
|
function and data types: inline functions, macros, etc. However, the
|
|
|
|
resulting C code will be cumbersome and hard to read. Therefore, we
|
|
|
|
recommend that you limit the module code which creates functions and
|
|
|
|
data structures to the absolute minimum, and leave the rest for a Lisp
|
|
|
|
package that will accompany your module, because doing these
|
|
|
|
additional tasks in Lisp is much easier, and will produce a much more
|
|
|
|
readable code. For example, given a module function
|
|
|
|
@code{module-func} defined as above, one way of making a macro
|
|
|
|
@code{module-macro} based on it is with the following simple Lisp
|
|
|
|
wrapper:
|
2018-10-11 20:53:05 +03:00
|
|
|
|
|
|
|
@lisp
|
2020-09-13 20:21:41 +02:00
|
|
|
(defmacro module-macro (&rest args)
|
|
|
|
"Documentation string for the macro."
|
|
|
|
(module-func args))
|
2018-10-11 20:53:05 +03:00
|
|
|
@end lisp
|
|
|
|
|
|
|
|
The Lisp package which goes with your module could then load the
|
2019-12-26 11:08:56 +01:00
|
|
|
module using the @code{load} primitive (@pxref{Dynamic Modules}) when
|
|
|
|
the package is loaded into Emacs.
|
2018-10-11 20:53:05 +03:00
|
|
|
|
2020-09-13 20:21:41 +02:00
|
|
|
By default, module functions created by @code{make_function} are not
|
|
|
|
interactive. To make them interactive, you can use the following
|
|
|
|
function.
|
|
|
|
|
|
|
|
@deftypefun void make_interactive (emacs_env *@var{env}, emacs_value @var{function}, emacs_value @var{spec})
|
|
|
|
This function, which is available since Emacs 28, makes the function
|
|
|
|
@var{function} interactive using the interactive specification
|
|
|
|
@var{spec}. Emacs interprets @var{spec} like the argument to the
|
|
|
|
@code{interactive} form. @ref{Using Interactive}, and
|
|
|
|
@pxref{Interactive Codes}. @var{function} must be an Emacs module
|
|
|
|
function returned by @code{make_function}.
|
|
|
|
@end deftypefun
|
|
|
|
|
|
|
|
Note that there is no native module support for retrieving the
|
|
|
|
interactive specification of a module function. Use the function
|
|
|
|
@code{interactive-form} for that. @ref{Using Interactive}. It is not
|
|
|
|
possible to make a module function non-interactive once you have made
|
|
|
|
it interactive using @code{make_interactive}.
|
|
|
|
|
2018-02-11 21:38:22 +01:00
|
|
|
@anchor{Module Function Finalizers}
|
|
|
|
If you want to run some code when a module function object (i.e., an
|
|
|
|
object returned by @code{make_function}) is garbage-collected, you can
|
|
|
|
install a @dfn{function finalizer}. Function finalizers are available
|
|
|
|
since Emacs 28. For example, if you have passed some heap-allocated
|
|
|
|
structure to the @var{data} argument of @code{make_function}, you can
|
|
|
|
use the finalizer to deallocate the structure. @xref{Basic
|
|
|
|
Allocation,,,libc}, and @pxref{Freeing after Malloc,,,libc}. The
|
|
|
|
finalizer function has the following signature:
|
|
|
|
|
|
|
|
@example
|
|
|
|
void finalizer (void *@var{data})
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Here, @var{data} receives the value passed to @var{data} when calling
|
|
|
|
@code{make_function}. Note that the finalizer can't interact with
|
|
|
|
Emacs in any way.
|
|
|
|
|
|
|
|
Directly after calling @code{make_function}, the newly-created
|
|
|
|
function doesn't have a finalizer. Use @code{set_function_finalizer}
|
|
|
|
to add one, if desired.
|
|
|
|
|
|
|
|
@deftypefun void emacs_finalizer (void *@var{ptr})
|
|
|
|
The header @file{emacs-module.h} provides the type
|
|
|
|
@code{emacs_finalizer} as a type alias for an Emacs finalizer
|
|
|
|
function.
|
|
|
|
@end deftypefun
|
|
|
|
|
|
|
|
@deftypefun emacs_finalizer get_function_finalizer (emacs_env *@var{env}, emacs_value @var{arg})
|
|
|
|
This function, which is available since Emacs 28, returns the function
|
|
|
|
finalizer associated with the module function represented by
|
|
|
|
@var{arg}. @var{arg} must refer to a module function, that is, an
|
|
|
|
object returned by @code{make_function}. If no finalizer is
|
|
|
|
associated with the function, @code{NULL} is returned.
|
|
|
|
@end deftypefun
|
|
|
|
|
|
|
|
@deftypefun void set_function_finalizer (emacs_env *@var{env}, emacs_value @var{arg}, emacs_finalizer @var{fin})
|
|
|
|
This function, which is available since Emacs 28, sets the function
|
|
|
|
finalizer associated with the module function represented by @var{arg}
|
|
|
|
to @var{fin}. @var{arg} must refer to a module function, that is, an
|
|
|
|
object returned by @code{make_function}. @var{fin} can either be
|
|
|
|
@code{NULL} to clear @var{arg}'s function finalizer, or a pointer to a
|
|
|
|
function to be called when the object represented by @var{arg} is
|
|
|
|
garbage-collected. At most one function finalizer can be set per
|
|
|
|
function; if @var{arg} already has a finalizer, it is replaced by
|
|
|
|
@var{fin}.
|
|
|
|
@end deftypefun
|
|
|
|
|
2018-10-11 20:53:05 +03:00
|
|
|
@node Module Values
|
|
|
|
@subsection Conversion Between Lisp and Module Values
|
|
|
|
@cindex module values, conversion
|
|
|
|
|
|
|
|
@cindex @code{emacs_value} data type
|
|
|
|
With very few exceptions, most modules need to exchange data with
|
|
|
|
Lisp programs that call them: accept arguments to module functions and
|
|
|
|
return values from module functions. For this purpose, the module
|
|
|
|
@acronym{API} provides the @code{emacs_value} type, which represents
|
|
|
|
Emacs Lisp objects communicated via the @acronym{API}; it is the
|
|
|
|
functional equivalent of the @code{Lisp_Object} type used in Emacs C
|
|
|
|
primitives (@pxref{Writing Emacs Primitives}). This section describes
|
2023-09-17 16:49:21 +02:00
|
|
|
the parts of the module @acronym{API} that allow creating
|
2018-10-11 20:53:05 +03:00
|
|
|
@code{emacs_value} objects corresponding to basic Lisp data types, and
|
|
|
|
how to access from C data in @code{emacs_value} objects that
|
|
|
|
correspond to Lisp objects.
|
|
|
|
|
|
|
|
All of the functions described below are actually @emph{function
|
|
|
|
pointers} provided via the pointer to the environment which every
|
|
|
|
module function accepts. Therefore, module code should call these
|
|
|
|
functions through the environment pointer, like this:
|
|
|
|
|
|
|
|
@example
|
|
|
|
emacs_env *env; /* the environment pointer */
|
|
|
|
env->some_function (arguments@dots{});
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
The @code{emacs_env} pointer will usually come from the first argument
|
|
|
|
to the module function, or from the call to @code{get_environment} if
|
|
|
|
you need the environment in the module initialization function.
|
|
|
|
|
|
|
|
Most of the functions described below became available in Emacs 25,
|
|
|
|
the first Emacs release that supported dynamic modules. For the few
|
|
|
|
functions that became available in later Emacs releases, we mention
|
|
|
|
the first Emacs version that supported them.
|
|
|
|
|
|
|
|
The following @acronym{API} functions extract values of various C data
|
|
|
|
types from @code{emacs_value} objects. They all raise the
|
|
|
|
@code{wrong-type-argument} error condition (@pxref{Type Predicates})
|
|
|
|
if the argument @code{emacs_value} object is not of the type expected
|
|
|
|
by the function. @xref{Module Nonlocal}, for details of how signaling
|
|
|
|
errors works in Emacs modules, and how to catch error conditions
|
|
|
|
inside the module before they are reported to Emacs. The
|
|
|
|
@acronym{API} function @code{type_of} (@pxref{Module Misc, type_of})
|
|
|
|
can be used to obtain the type of a @code{emacs_value} object.
|
|
|
|
|
|
|
|
@deftypefn Function intmax_t extract_integer (emacs_env *@var{env}, emacs_value @var{arg})
|
|
|
|
This function returns the value of a Lisp integer specified by
|
|
|
|
@var{arg}. The C data type of the return value, @code{intmax_t}, is
|
2019-12-09 15:39:28 -08:00
|
|
|
the widest integer data type supported by the C compiler, typically
|
2019-04-18 23:31:00 +02:00
|
|
|
@w{@code{long long}}. If the value of @var{arg} doesn't fit into an
|
|
|
|
@code{intmax_t}, the function signals an error using the error symbol
|
|
|
|
@code{overflow-error}.
|
2018-10-11 20:53:05 +03:00
|
|
|
@end deftypefn
|
|
|
|
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
@deftypefn Function bool extract_big_integer (emacs_env *@var{env}, emacs_value @var{arg}, int *@var{sign}, ptrdiff_t *@var{count}, emacs_limb_t *@var{magnitude})
|
|
|
|
This function, which is available since Emacs 27, extracts the
|
2019-12-09 15:39:28 -08:00
|
|
|
integer value of @var{arg}. The value of @var{arg} must be an
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
integer (fixnum or bignum). If @var{sign} is not @code{NULL}, it
|
|
|
|
stores the sign of @var{arg} (-1, 0, or +1) into @code{*sign}. The
|
|
|
|
magnitude is stored into @var{magnitude} as follows. If @var{count}
|
2019-12-09 15:39:28 -08:00
|
|
|
and @var{magnitude} are both non-@code{NULL}, then @var{magnitude} must
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
point to an array of at least @code{*count} @code{unsigned long}
|
|
|
|
elements. If @var{magnitude} is large enough to hold the magnitude of
|
|
|
|
@var{arg}, then this function writes the magnitude into the
|
|
|
|
@var{magnitude} array in little-endian form, stores the number of
|
|
|
|
array elements written into @code{*count}, and returns @code{true}.
|
|
|
|
If @var{magnitude} is not large enough, it stores the required array
|
|
|
|
size into @code{*count}, signals an error, and returns @code{false}.
|
|
|
|
If @var{count} is not @code{NULL} and @var{magnitude} is @code{NULL},
|
|
|
|
then the function stores the required array size into @code{*count}
|
|
|
|
and returns @code{true}.
|
|
|
|
|
|
|
|
Emacs guarantees that the maximum required value of @code{*count}
|
|
|
|
never exceeds @code{min (PTRDIFF_MAX, SIZE_MAX) / sizeof
|
2019-12-09 15:39:28 -08:00
|
|
|
(emacs_limb_t)}, so you can use @code{malloc (*count * sizeof *magnitude)}
|
|
|
|
to allocate the @code{magnitude} array without worrying about integer
|
|
|
|
overflow in the size calculation.
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
@deftp {Type alias} emacs_limb_t
|
2019-12-23 15:37:49 +01:00
|
|
|
This is an unsigned integer type, used as the element type for the
|
|
|
|
magnitude arrays for the big integer conversion functions. The type
|
|
|
|
is guaranteed to have unique object representations, i.e., no padding
|
|
|
|
bits.
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
@end deftp
|
|
|
|
|
|
|
|
@defvr Macro EMACS_LIMB_MAX
|
2019-12-09 15:39:28 -08:00
|
|
|
This macro expands to a constant expression specifying the maximum
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
possible value for an @code{emacs_limb_t} object.
|
2019-12-09 15:39:28 -08:00
|
|
|
The expression is suitable for use in @code{#if}.
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
@end defvr
|
|
|
|
|
2018-10-11 20:53:05 +03:00
|
|
|
@deftypefn Function double extract_float (emacs_env *@var{env}, emacs_value @var{arg})
|
|
|
|
This function returns the value of a Lisp float specified by
|
|
|
|
@var{arg}, as a C @code{double} value.
|
|
|
|
@end deftypefn
|
|
|
|
|
2022-02-17 14:02:28 +01:00
|
|
|
@deftypefn Function {struct timespec} extract_time (emacs_env *@var{env}, emacs_value @var{arg})
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
This function, which is available since Emacs 27, interprets @var{arg}
|
|
|
|
as an Emacs Lisp time value and returns the corresponding @code{struct
|
|
|
|
timespec}. @xref{Time of Day}. @code{struct timespec} represents a
|
|
|
|
timestamp with nanosecond precision. It has the following members:
|
2019-04-18 17:42:45 +02:00
|
|
|
|
|
|
|
@table @code
|
|
|
|
@item time_t tv_sec
|
|
|
|
Whole number of seconds.
|
|
|
|
@item long tv_nsec
|
2019-12-10 20:41:28 -08:00
|
|
|
Fractional seconds as a number of nanoseconds.
|
|
|
|
For timestamps returned by @code{extract_time},
|
|
|
|
this is always nonnegative and less than one billion.
|
|
|
|
(Although POSIX requires the type of @code{tv_nsec} to be @code{long},
|
|
|
|
the type is @code{long long} on some nonstandard platforms.)
|
2019-04-18 17:42:45 +02:00
|
|
|
@end table
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
@xref{Elapsed Time,,,libc}.
|
|
|
|
|
|
|
|
If @var{time} has higher precision than nanoseconds, then this
|
2019-04-24 13:41:05 +02:00
|
|
|
function truncates it to nanosecond precision towards negative
|
|
|
|
infinity. This function signals an error if @var{time} (truncated to
|
|
|
|
nanoseconds) cannot be represented by @code{struct timespec}. For
|
2019-12-09 15:39:28 -08:00
|
|
|
example, if @code{time_t} is a 32-bit integer type, then a @var{time}
|
2019-04-24 13:41:05 +02:00
|
|
|
value of ten billion seconds would signal an error, but a @var{time}
|
|
|
|
value of 600 picoseconds would get truncated to zero.
|
2019-04-18 17:42:45 +02:00
|
|
|
|
|
|
|
If you need to deal with time values that are not representable by
|
|
|
|
@code{struct timespec}, or if you want higher precision, call the Lisp
|
|
|
|
function @code{encode-time} and work with its return value.
|
|
|
|
@xref{Time Conversion}.
|
|
|
|
@end deftypefn
|
|
|
|
|
2018-10-11 20:53:05 +03:00
|
|
|
@deftypefn Function bool copy_string_contents (emacs_env *@var{env}, emacs_value @var{arg}, char *@var{buf}, ptrdiff_t *@var{len})
|
|
|
|
This function stores the UTF-8 encoded text of a Lisp string specified
|
|
|
|
by @var{arg} in the array of @code{char} pointed by @var{buf}, which
|
|
|
|
should have enough space to hold at least @code{*@var{len}} bytes,
|
|
|
|
including the terminating null byte. The argument @var{len} must not
|
|
|
|
be a @code{NULL} pointer, and, when the function is called, it should
|
|
|
|
point to a value that specifies the size of @var{buf} in bytes.
|
|
|
|
|
|
|
|
If the buffer size specified by @code{*@var{len}} is large enough to
|
|
|
|
hold the string's text, the function stores in @code{*@var{len}} the
|
|
|
|
actual number of bytes copied to @var{buf}, including the terminating
|
|
|
|
null byte, and returns @code{true}. If the buffer is too small, the
|
|
|
|
function raises the @code{args-out-of-range} error condition, stores
|
|
|
|
the required number of bytes in @code{*@var{len}}, and returns
|
|
|
|
@code{false}. @xref{Module Nonlocal}, for how to handle pending error
|
|
|
|
conditions.
|
|
|
|
|
|
|
|
The argument @var{buf} can be a @code{NULL} pointer, in which case the
|
|
|
|
function stores in @code{*@var{len}} the number of bytes required for
|
|
|
|
storing the contents of @var{arg}, and returns @code{true}. This is
|
|
|
|
how you can determine the size of @var{buf} needed to store a
|
|
|
|
particular string: first call @code{copy_string_contents} with
|
|
|
|
@code{NULL} as @var{buf}, then allocate enough memory to hold the
|
|
|
|
number of bytes stored by the function in @code{*@var{len}}, and call
|
|
|
|
the function again with non-@code{NULL} @var{buf} to actually perform
|
|
|
|
the text copying.
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
@deftypefn Function emacs_value vec_get (emacs_env *@var{env}, emacs_value @var{vector}, ptrdiff_t @var{index})
|
|
|
|
This function returns the element of @var{vector} at @var{index}. The
|
|
|
|
@var{index} of the first vector element is zero. The function raises
|
|
|
|
the @code{args-out-of-range} error condition if the value of
|
|
|
|
@var{index} is invalid. To extract C data from the value the function
|
|
|
|
returns, use the other extraction functions described here, as
|
|
|
|
appropriate for the Lisp data type stored in that element of the
|
|
|
|
vector.
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
@deftypefn Function ptrdiff_t vec_size (emacs_env *@var{env}, emacs_value @var{vector})
|
|
|
|
This function returns the number of elements in @var{vector}.
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
@deftypefn Function void vec_set (emacs_env *@var{env}, emacs_value @var{vector}, ptrdiff_t @var{index}, emacs_value @var{value})
|
|
|
|
This function stores @var{value} in the element of @var{vector} whose
|
|
|
|
index is @var{index}. It raises the @code{args-out-of-range} error
|
|
|
|
condition if the value of @var{index} is invalid.
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
The following @acronym{API} functions create @code{emacs_value}
|
|
|
|
objects from basic C data types. They all return the created
|
|
|
|
@code{emacs_value} object.
|
|
|
|
|
|
|
|
@deftypefn Function emacs_value make_integer (emacs_env *@var{env}, intmax_t @var{n})
|
|
|
|
This function takes an integer argument @var{n} and returns the
|
2019-04-18 23:31:00 +02:00
|
|
|
corresponding @code{emacs_value} object. It returns either a fixnum
|
|
|
|
or a bignum depending on whether the value of @var{n} is inside the
|
|
|
|
limits set by @code{most-negative-fixnum} and
|
|
|
|
@code{most-positive-fixnum} (@pxref{Integer Basics}).
|
2018-10-11 20:53:05 +03:00
|
|
|
@end deftypefn
|
|
|
|
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
@deftypefn Function emacs_value make_big_integer (emacs_env *@var{env}, int sign, ptrdiff_t count, const emacs_limb_t *magnitude)
|
|
|
|
This function, which is available since Emacs 27, takes an
|
|
|
|
arbitrary-sized integer argument and returns a corresponding
|
|
|
|
@code{emacs_value} object. The @var{sign} argument gives the sign of
|
|
|
|
the return value. If @var{sign} is nonzero, then @var{magnitude} must
|
|
|
|
point to an array of at least @var{count} elements specifying the
|
|
|
|
little-endian magnitude of the return value.
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
The following example uses the GNU Multiprecision Library (GMP) to
|
|
|
|
calculate the next probable prime after a given integer.
|
2019-12-29 15:41:52 -08:00
|
|
|
@xref{Top,,,gmp}, for a general overview of GMP, and @pxref{Integer
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
Import and Export,,,gmp} for how to convert the @code{magnitude} array
|
|
|
|
to and from GMP @code{mpz_t} values.
|
|
|
|
|
|
|
|
@example
|
2019-12-09 15:39:28 -08:00
|
|
|
#include <emacs-module.h>
|
|
|
|
int plugin_is_GPL_compatible;
|
|
|
|
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <gmp.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
memory_full (emacs_env *env)
|
|
|
|
@{
|
2019-12-09 15:39:28 -08:00
|
|
|
static const char message[] = "Memory exhausted";
|
|
|
|
emacs_value data = env->make_string (env, message,
|
|
|
|
strlen (message));
|
|
|
|
env->non_local_exit_signal
|
|
|
|
(env, env->intern (env, "error"),
|
|
|
|
env->funcall (env, env->intern (env, "list"), 1, &data));
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
@}
|
|
|
|
|
|
|
|
enum
|
|
|
|
@{
|
2019-12-09 15:39:28 -08:00
|
|
|
order = -1, endian = 0, nails = 0,
|
|
|
|
limb_size = sizeof (emacs_limb_t),
|
|
|
|
max_nlimbs = ((SIZE_MAX < PTRDIFF_MAX ? SIZE_MAX : PTRDIFF_MAX)
|
|
|
|
/ limb_size)
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
@};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
extract_big_integer (emacs_env *env, emacs_value arg, mpz_t result)
|
|
|
|
@{
|
2019-12-09 15:39:28 -08:00
|
|
|
ptrdiff_t nlimbs;
|
|
|
|
bool ok = env->extract_big_integer (env, arg, NULL, &nlimbs, NULL);
|
|
|
|
if (!ok)
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
return false;
|
2019-12-09 15:39:28 -08:00
|
|
|
assert (0 < nlimbs && nlimbs <= max_nlimbs);
|
|
|
|
emacs_limb_t *magnitude = malloc (nlimbs * limb_size);
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
if (magnitude == NULL)
|
|
|
|
@{
|
|
|
|
memory_full (env);
|
|
|
|
return false;
|
|
|
|
@}
|
2019-12-09 15:39:28 -08:00
|
|
|
int sign;
|
|
|
|
ok = env->extract_big_integer (env, arg, &sign, &nlimbs, magnitude);
|
|
|
|
assert (ok);
|
|
|
|
mpz_import (result, nlimbs, order, limb_size, endian, nails, magnitude);
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
free (magnitude);
|
|
|
|
if (sign < 0)
|
|
|
|
mpz_neg (result, result);
|
|
|
|
return true;
|
|
|
|
@}
|
|
|
|
|
|
|
|
static emacs_value
|
|
|
|
make_big_integer (emacs_env *env, const mpz_t value)
|
|
|
|
@{
|
2019-12-09 15:39:28 -08:00
|
|
|
size_t nbits = mpz_sizeinbase (value, 2);
|
|
|
|
int bitsperlimb = CHAR_BIT * limb_size - nails;
|
|
|
|
size_t nlimbs = nbits / bitsperlimb + (nbits % bitsperlimb != 0);
|
|
|
|
emacs_limb_t *magnitude
|
|
|
|
= nlimbs <= max_nlimbs ? malloc (nlimbs * limb_size) : NULL;
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
if (magnitude == NULL)
|
|
|
|
@{
|
|
|
|
memory_full (env);
|
|
|
|
return NULL;
|
|
|
|
@}
|
|
|
|
size_t written;
|
2019-12-09 15:39:28 -08:00
|
|
|
mpz_export (magnitude, &written, order, limb_size, endian, nails, value);
|
|
|
|
assert (written == nlimbs);
|
|
|
|
assert (nlimbs <= PTRDIFF_MAX);
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
emacs_value result = env->make_big_integer (env, mpz_sgn (value),
|
2019-12-09 15:39:28 -08:00
|
|
|
nlimbs, magnitude);
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
free (magnitude);
|
|
|
|
return result;
|
|
|
|
@}
|
|
|
|
|
|
|
|
static emacs_value
|
|
|
|
next_prime (emacs_env *env, ptrdiff_t nargs, emacs_value *args,
|
|
|
|
void *data)
|
|
|
|
@{
|
|
|
|
assert (nargs == 1);
|
2019-12-09 15:39:28 -08:00
|
|
|
mpz_t p;
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
mpz_init (p);
|
|
|
|
extract_big_integer (env, args[0], p);
|
|
|
|
mpz_nextprime (p, p);
|
|
|
|
emacs_value result = make_big_integer (env, p);
|
|
|
|
mpz_clear (p);
|
|
|
|
return result;
|
|
|
|
@}
|
2019-12-09 15:39:28 -08:00
|
|
|
|
|
|
|
int
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
emacs_module_init (struct emacs_runtime *runtime)
|
2019-12-09 15:39:28 -08:00
|
|
|
@{
|
2019-12-23 18:30:09 +01:00
|
|
|
emacs_env *env = runtime->get_environment (runtime);
|
2019-12-09 15:39:28 -08:00
|
|
|
emacs_value symbol = env->intern (env, "next-prime");
|
|
|
|
emacs_value func
|
|
|
|
= env->make_function (env, 1, 1, next_prime, NULL, NULL);
|
|
|
|
emacs_value args[] = @{symbol, func@};
|
|
|
|
env->funcall (env, env->intern (env, "defalias"), 2, args);
|
|
|
|
return 0;
|
|
|
|
@}
|
Change module interface to no longer use GMP objects directly.
As described in the new comment added to emacs-module.c, using GMP
directly in the module interface has significant downsides: it couples
the module interface directly to the implementation and requires
module authors to link their module against the same GMP library as
Emacs itself, which is often difficult and an unnecessary burden. By
picking a representation for the magnitude that often matches the one
used by GMP, we can avoid overhead when converting from and to GMP in
most cases.
Loading the test module in test/data/emacs-module and evaluating
(dotimes (_ 10000)
(mod-test-double (* 2 most-negative-fixnum)))
under Callgrind shows that on my (GNU/Linux) machine Emacs only spends
10% of the CPU time of mod-test-double in mpz_import and mpz_export
combined, even though that function does little else. (By contrast,
30% is spent in allocate_pseudovector.)
* src/emacs-module.h.in: Don't check EMACS_MODULE_GMP. Don't include
gmp.h. Remove emacs_mpz structure. Instead, define type alias
emacs_limb_t and macro EMACS_LIMB_MAX.
* src/module-env-27.h: Change interface of extract_big_integer and
make_big_integer to take a sign-magnitude representation instead of
mpz_t.
* src/emacs-module.c: Don't check EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T. Add a comment about the chosen
implementation.
(module_extract_big_integer, module_make_big_integer): Reimplement
without using mpz_t in the interface.
* doc/lispref/internals.texi (Module Values): Adapt function
documentation and example. Stop mentioning GMP and EMACS_MODULE_GMP.
* test/data/emacs-module/mod-test.c: Don't define EMACS_MODULE_GMP or
EMACS_MODULE_HAVE_MPZ_T.
(memory_full, extract_big_integer, make_big_integer): New helper
functions, identical to example in the Info documentation.
(Fmod_test_nanoseconds, Fmod_test_double): Adapt to new interface.
2019-11-02 10:54:57 +01:00
|
|
|
@end example
|
|
|
|
|
2018-10-11 20:53:05 +03:00
|
|
|
@deftypefn Function emacs_value make_float (emacs_env *@var{env}, double @var{d})
|
|
|
|
This function takes a @code{double} argument @var{d} and returns the
|
|
|
|
corresponding Emacs floating-point value.
|
|
|
|
@end deftypefn
|
|
|
|
|
2019-04-18 17:42:45 +02:00
|
|
|
@deftypefn Function emacs_value make_time (emacs_env *@var{env}, struct timespec @var{time})
|
|
|
|
This function, which is available since Emacs 27, takes a @code{struct
|
|
|
|
timespec} argument @var{time} and returns the corresponding Emacs
|
|
|
|
timestamp as a pair @code{(@var{ticks} . @var{hz})}. @xref{Time of
|
|
|
|
Day}. The return value represents exactly the same timestamp as
|
|
|
|
@var{time}: all input values are representable, and there is never a
|
|
|
|
loss of precision. @code{@var{time}.tv_sec} and
|
|
|
|
@code{@var{time}.tv_nsec} can be arbitrary values. In particular,
|
|
|
|
there's no requirement that @var{time} be normalized. This means that
|
|
|
|
@code{@var{time}.tv_nsec} can be negative or larger than 999,999,999.
|
|
|
|
@end deftypefn
|
|
|
|
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
@deftypefn Function emacs_value make_string (emacs_env *@var{env}, const char *@var{str}, ptrdiff_t @var{len})
|
2018-10-11 20:53:05 +03:00
|
|
|
This function creates an Emacs string from C text string pointed by
|
|
|
|
@var{str} whose length in bytes, not including the terminating null
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
byte, is @var{len}. The original string in @var{str} can be either an
|
|
|
|
@acronym{ASCII} string or a UTF-8 encoded non-@acronym{ASCII} string;
|
|
|
|
it can include embedded null bytes, and doesn't have to end in a
|
|
|
|
terminating null byte at @code{@var{str}[@var{len}]}. The function
|
|
|
|
raises the @code{overflow-error} error condition if @var{len} is
|
2020-12-12 23:21:18 +01:00
|
|
|
negative or exceeds the maximum length of an Emacs string. If
|
|
|
|
@var{len} is zero, then @var{str} can be @code{NULL}, otherwise it
|
|
|
|
must point to valid memory. For nonzero @var{len}, @code{make_string}
|
|
|
|
returns unique mutable string objects.
|
2018-10-11 20:53:05 +03:00
|
|
|
@end deftypefn
|
|
|
|
|
2020-10-13 06:51:06 +02:00
|
|
|
@deftypefn Function emacs_value make_unibyte_string (emacs_env *@var{env}, const char *@var{str}, ptrdiff_t @var{len})
|
|
|
|
This function is like @code{make_string}, but has no restrictions on
|
|
|
|
the values of the bytes in the C string, and can be used to pass
|
|
|
|
binary data to Emacs in the form of a unibyte string.
|
|
|
|
@end deftypefn
|
|
|
|
|
2018-10-11 20:53:05 +03:00
|
|
|
The @acronym{API} does not provide functions to manipulate Lisp data
|
|
|
|
structures, for example, create lists with @code{cons} and @code{list}
|
|
|
|
(@pxref{Building Lists}), extract list members with @code{car} and
|
|
|
|
@code{cdr} (@pxref{List Elements}), create vectors with @code{vector}
|
|
|
|
(@pxref{Vector Functions}), etc. For these, use @code{intern} and
|
|
|
|
@code{funcall}, described in the next subsection, to call the
|
|
|
|
corresponding Lisp functions.
|
|
|
|
|
|
|
|
Normally, @code{emacs_value} objects have a rather short lifetime: it
|
|
|
|
ends when the @code{emacs_env} pointer used for their creation goes
|
|
|
|
out of scope. Occasionally, you may need to create @dfn{global
|
|
|
|
references}: @code{emacs_value} objects that live as long as you
|
|
|
|
wish. Use the following two functions to manage such objects.
|
|
|
|
|
|
|
|
@deftypefn Function emacs_value make_global_ref (emacs_env *@var{env}, emacs_value @var{value})
|
|
|
|
This function returns a global reference for @var{value}.
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
@deftypefn Function void free_global_ref (emacs_env *@var{env}, emacs_value @var{global_value})
|
|
|
|
This function frees the @var{global_value} previously created by
|
|
|
|
@code{make_global_ref}. The @var{global_value} is no longer valid
|
|
|
|
after the call. Your module code should pair each call to
|
|
|
|
@code{make_global_ref} with the corresponding @code{free_global_ref}.
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
@cindex user pointer, using in module functions
|
|
|
|
An alternative to keeping around C data structures that need to be
|
|
|
|
passed to module functions later is to create @dfn{user pointer}
|
|
|
|
objects. A user pointer, or @code{user-ptr}, object is a Lisp object
|
|
|
|
that encapsulates a C pointer and can have an associated finalizer
|
|
|
|
function, which is called when the object is garbage-collected
|
|
|
|
(@pxref{Garbage Collection}). The module @acronym{API} provides
|
|
|
|
functions to create and access @code{user-ptr} objects. These
|
|
|
|
functions raise the @code{wrong-type-argument} error condition if they
|
|
|
|
are called on @code{emacs_value} that doesn't represent a
|
|
|
|
@code{user-ptr} object.
|
|
|
|
|
|
|
|
@deftypefn Function emacs_value make_user_ptr (emacs_env *@var{env}, emacs_finalizer @var{fin}, void *@var{ptr})
|
|
|
|
This function creates and returns a @code{user-ptr} object which wraps
|
|
|
|
the C pointer @var{ptr}. The finalizer function @var{fin} can be a
|
|
|
|
@code{NULL} pointer (meaning no finalizer), or it can be a function of
|
|
|
|
the following signature:
|
|
|
|
|
|
|
|
@example
|
|
|
|
typedef void (*emacs_finalizer) (void *@var{ptr});
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
If @var{fin} is not a @code{NULL} pointer, it will be called with the
|
|
|
|
@var{ptr} as the argument when the @code{user-ptr} object is
|
|
|
|
garbage-collected. Don't run any expensive code in a finalizer,
|
|
|
|
because GC must finish quickly to keep Emacs responsive.
|
|
|
|
@end deftypefn
|
|
|
|
|
2022-02-17 14:02:28 +01:00
|
|
|
@deftypefn Function {void *}get_user_ptr (emacs_env *@var{env}, emacs_value @var{arg})
|
2018-10-11 20:53:05 +03:00
|
|
|
This function extracts the C pointer from the Lisp object represented
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
by @var{arg}.
|
2018-10-11 20:53:05 +03:00
|
|
|
@end deftypefn
|
|
|
|
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
@deftypefn Function void set_user_ptr (emacs_env *@var{env}, emacs_value @var{arg}, void *@var{ptr})
|
2018-10-11 20:53:05 +03:00
|
|
|
This function sets the C pointer embedded in the @code{user-ptr}
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
object represented by @var{arg} to @var{ptr}.
|
2018-10-11 20:53:05 +03:00
|
|
|
@end deftypefn
|
|
|
|
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
@deftypefn Function emacs_finalizer get_user_finalizer (emacs_env *@var{env}, emacs_value @var{arg})
|
2018-10-11 20:53:05 +03:00
|
|
|
This function returns the finalizer of the @code{user-ptr} object
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
represented by @var{arg}, or @code{NULL} if it doesn't have a
|
|
|
|
finalizer.
|
2018-10-11 20:53:05 +03:00
|
|
|
@end deftypefn
|
|
|
|
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
@deftypefn Function void set_user_finalizer (emacs_env *@var{env}, emacs_value @var{arg}, emacs_finalizer @var{fin})
|
2018-10-11 20:53:05 +03:00
|
|
|
This function changes the finalizer of the @code{user-ptr} object
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
represented by @var{arg} to be @var{fin}. If @var{fin} is a
|
|
|
|
@code{NULL} pointer, the @code{user-ptr} object will have no
|
|
|
|
finalizer.
|
2018-10-11 20:53:05 +03:00
|
|
|
@end deftypefn
|
|
|
|
|
2018-02-11 21:38:22 +01:00
|
|
|
Note that the @code{emacs_finalizer} type works for both user pointer
|
|
|
|
an module function finalizers. @xref{Module Function Finalizers}.
|
2019-12-26 10:29:21 +01:00
|
|
|
|
2018-10-11 20:53:05 +03:00
|
|
|
@node Module Misc
|
|
|
|
@subsection Miscellaneous Convenience Functions for Modules
|
|
|
|
|
|
|
|
This subsection describes a few convenience functions provided by
|
|
|
|
the module @acronym{API}. Like the functions described in previous
|
|
|
|
subsections, all of them are actually function pointers, and need to
|
|
|
|
be called via the @code{emacs_env} pointer. Description of functions
|
|
|
|
that were introduced after Emacs 25 calls out the first version where
|
|
|
|
they became available.
|
|
|
|
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
@deftypefn Function bool eq (emacs_env *@var{env}, emacs_value @var{a}, emacs_value @var{b})
|
2018-10-11 20:53:05 +03:00
|
|
|
This function returns @code{true} if the Lisp objects represented by
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
@var{a} and @var{b} are identical, @code{false} otherwise. This is
|
|
|
|
the same as the Lisp function @code{eq} (@pxref{Equality Predicates}),
|
|
|
|
but avoids the need to intern the objects represented by the
|
|
|
|
arguments.
|
2018-10-11 20:53:05 +03:00
|
|
|
|
|
|
|
There are no @acronym{API} functions for other equality predicates, so
|
|
|
|
you will need to use @code{intern} and @code{funcall}, described
|
|
|
|
below, to perform more complex equality tests.
|
|
|
|
@end deftypefn
|
|
|
|
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
@deftypefn Function bool is_not_nil (emacs_env *@var{env}, emacs_value @var{arg})
|
|
|
|
This function tests whether the Lisp object represented by @var{arg}
|
2018-10-11 20:53:05 +03:00
|
|
|
is non-@code{nil}; it returns @code{true} or @code{false} accordingly.
|
|
|
|
|
|
|
|
Note that you could implement an equivalent test by using
|
|
|
|
@code{intern} to get an @code{emacs_value} representing @code{nil},
|
|
|
|
then use @code{eq}, described above, to test for equality. But using
|
|
|
|
this function is more convenient.
|
|
|
|
@end deftypefn
|
|
|
|
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
@deftypefn Function emacs_value type_of (emacs_env *@var{env}, emacs_value @code{arg})
|
|
|
|
This function returns the type of @var{arg} as a value that represents
|
|
|
|
a symbol: @code{string} for a string, @code{integer} for an integer,
|
|
|
|
@code{process} for a process, etc. @xref{Type Predicates}. You can
|
|
|
|
use @code{intern} and @code{eq} to compare against known type symbols,
|
|
|
|
if your code needs to depend on the object type.
|
2018-10-11 20:53:05 +03:00
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
@anchor{intern}
|
|
|
|
@deftypefn Function emacs_value intern (emacs_env *@var{env}, const char *name)
|
|
|
|
This function returns an interned Emacs symbol whose name is
|
|
|
|
@var{name}, which should be an @acronym{ASCII} null-terminated string.
|
|
|
|
It creates a new symbol if one does not already exist.
|
|
|
|
|
|
|
|
Together with @code{funcall}, described below, this function provides
|
|
|
|
a means for invoking any Lisp-callable Emacs function, provided that
|
|
|
|
its name is a pure @acronym{ASCII} string. For example, here's how to
|
|
|
|
intern a symbol whose name @code{name_str} is non-@acronym{ASCII}, by
|
|
|
|
calling the more powerful Emacs @code{intern} function
|
|
|
|
(@pxref{Creating Symbols}):
|
|
|
|
|
|
|
|
@example
|
|
|
|
emacs_value fintern = env->intern (env, "intern");
|
|
|
|
emacs_value sym_name =
|
|
|
|
env->make_string (env, name_str, strlen (name_str));
|
2019-12-26 10:58:55 +01:00
|
|
|
emacs_value symbol = env->funcall (env, fintern, 1, &sym_name);
|
2018-10-11 20:53:05 +03:00
|
|
|
@end example
|
|
|
|
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
@deftypefn Function emacs_value funcall (emacs_env *@var{env}, emacs_value @var{func}, ptrdiff_t @var{nargs}, emacs_value *@var{args})
|
|
|
|
This function calls the specified @var{func} passing it @var{nargs}
|
|
|
|
arguments from the array pointed to by @var{args}. The argument
|
|
|
|
@var{func} can be a function symbol (e.g., returned by @code{intern}
|
|
|
|
described above), a module function returned by @code{make_function}
|
|
|
|
(@pxref{Module Functions}), a subroutine written in C, etc. If
|
|
|
|
@var{nargs} is zero, @var{args} can be a @code{NULL} pointer.
|
|
|
|
|
|
|
|
The function returns the value that @var{func} returned.
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
If your module includes potentially long-running code, it is a good
|
|
|
|
idea to check from time to time in that code whether the user wants to
|
|
|
|
quit, e.g., by typing @kbd{C-g} (@pxref{Quitting}). The following
|
|
|
|
function, which is available since Emacs 26.1, is provided for that
|
|
|
|
purpose.
|
|
|
|
|
|
|
|
@anchor{should_quit}
|
|
|
|
@deftypefn Function bool should_quit (emacs_env *@var{env})
|
|
|
|
This function returns @code{true} if the user wants to quit. In that
|
|
|
|
case, we recommend that your module function aborts any on-going
|
2019-01-02 22:04:56 +01:00
|
|
|
processing and returns as soon as possible. In most cases, use
|
|
|
|
@code{process_input} instead.
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
To process input events in addition to checking whether the user wants
|
|
|
|
to quit, use the following function, which is available since Emacs
|
|
|
|
27.1.
|
|
|
|
|
|
|
|
@anchor{process_input}
|
2022-02-22 21:39:32 +01:00
|
|
|
@deftypefn Function {enum emacs_process_input_result} process_input (emacs_env *@var{env})
|
2019-01-02 22:04:56 +01:00
|
|
|
This function processes pending input events. It returns
|
|
|
|
@code{emacs_process_input_quit} if the user wants to quit or an error
|
|
|
|
occurred while processing signals. In that case, we recommend that
|
|
|
|
your module function aborts any on-going processing and returns as
|
|
|
|
soon as possible. If the module code may continue running,
|
|
|
|
@code{process_input} returns @code{emacs_process_input_continue}. The
|
|
|
|
return value is @code{emacs_process_input_continue} if and only if
|
|
|
|
there is no pending nonlocal exit in @code{env}. If the module
|
|
|
|
continues after calling @code{process_input}, global state such as
|
|
|
|
variable values and buffer content may have been modified in arbitrary
|
|
|
|
ways.
|
2018-10-11 20:53:05 +03:00
|
|
|
@end deftypefn
|
|
|
|
|
2020-03-26 17:22:25 +01:00
|
|
|
@anchor{open_channel}
|
|
|
|
@deftypefun int open_channel (emacs_env *@var{env}, emacs_value @var{pipe_process})
|
2020-03-29 10:22:35 +02:00
|
|
|
This function, which is available since Emacs 28, opens a channel to
|
2020-03-26 17:22:25 +01:00
|
|
|
an existing pipe process. @var{pipe_process} must refer to an
|
|
|
|
existing pipe process created by @code{make-pipe-process}. @ref{Pipe
|
|
|
|
Processes}. If successful, the return value will be a new file
|
|
|
|
descriptor that you can use to write to the pipe. Unlike all other
|
|
|
|
module functions, you can use the returned file descriptor from
|
|
|
|
arbitrary threads, even if no module environment is active. You can
|
|
|
|
use the @code{write} function to write to the file descriptor. Once
|
|
|
|
done, close the file descriptor using @code{close}. @ref{Low-Level
|
|
|
|
I/O,,,libc}.
|
|
|
|
@end deftypefun
|
|
|
|
|
2018-10-11 20:53:05 +03:00
|
|
|
@node Module Nonlocal
|
|
|
|
@subsection Nonlocal Exits in Modules
|
|
|
|
@cindex nonlocal exits, in modules
|
|
|
|
|
|
|
|
Emacs Lisp supports nonlocal exits, whereby program control is
|
2020-09-21 15:03:57 +02:00
|
|
|
transferred from one point in a program to another remote point.
|
2018-10-11 20:53:05 +03:00
|
|
|
@xref{Nonlocal Exits}. Thus, Lisp functions called by your module
|
|
|
|
might exit nonlocally by calling @code{signal} or @code{throw}, and
|
|
|
|
your module functions must handle such nonlocal exits properly. Such
|
|
|
|
handling is needed because C programs will not automatically release
|
|
|
|
resources and perform other cleanups in these cases; your module code
|
|
|
|
must itself do it. The module @acronym{API} provides facilities for
|
|
|
|
that, described in this subsection. They are generally available
|
|
|
|
since Emacs 25; those of them that became available in later releases
|
|
|
|
explicitly call out the first Emacs version where they became part of
|
|
|
|
the @acronym{API}.
|
|
|
|
|
|
|
|
When some Lisp code called by a module function signals an error or
|
|
|
|
throws, the nonlocal exit is trapped, and the pending exit and its
|
|
|
|
associated data are stored in the environment. Whenever a nonlocal
|
|
|
|
exit is pending in the environment, any module @acronym{API} function
|
|
|
|
called with a pointer to that environment will return immediately
|
|
|
|
without any processing (the functions @code{non_local_exit_check},
|
|
|
|
@code{non_local_exit_get}, and @code{non_local_exit_clear} are
|
|
|
|
exceptions from this rule). If your module function then does nothing
|
|
|
|
and returns to Emacs, a pending nonlocal exit will cause Emacs to act
|
|
|
|
on it: signal an error or throw to the corresponding @code{catch}.
|
|
|
|
|
|
|
|
So the simplest ``handling'' of nonlocal exits in module functions is
|
|
|
|
to do nothing special and let the rest of your code to run as if
|
|
|
|
nothing happened. However, this can cause two classes of problems:
|
|
|
|
|
|
|
|
@itemize @minus
|
|
|
|
@item
|
|
|
|
Your module function might use uninitialized or undefined values,
|
|
|
|
since @acronym{API} functions return immediately without producing the
|
|
|
|
expected results.
|
|
|
|
|
|
|
|
@item
|
|
|
|
Your module might leak resources, because it might not have the
|
|
|
|
opportunity to release them.
|
|
|
|
@end itemize
|
|
|
|
|
|
|
|
Therefore, we recommend that your module functions check for nonlocal
|
|
|
|
exit conditions and recover from them, using the functions described
|
|
|
|
below.
|
|
|
|
|
2022-02-22 21:39:32 +01:00
|
|
|
@deftypefn Function {enum emacs_funcall_exit} non_local_exit_check (emacs_env *@var{env})
|
2018-10-11 20:53:05 +03:00
|
|
|
This function returns the kind of nonlocal exit condition stored in
|
|
|
|
@var{env}. The possible values are:
|
|
|
|
|
|
|
|
@vindex emacs_funcall_exit@r{, enumeration}
|
|
|
|
@vtable @code
|
|
|
|
@item emacs_funcall_exit_return
|
|
|
|
The last @acronym{API} function exited normally.
|
|
|
|
@item emacs_funcall_exit_signal
|
|
|
|
The last @acronym{API} function signaled an error.
|
|
|
|
@item emacs_funcall_exit_throw
|
|
|
|
The last @acronym{API} function exited via @code{throw}.
|
|
|
|
@end vtable
|
|
|
|
@end deftypefn
|
|
|
|
|
2022-02-22 21:39:32 +01:00
|
|
|
@deftypefn Function {enum emacs_funcall_exit} non_local_exit_get (emacs_env *@var{env}, emacs_value *@var{symbol}, emacs_value *@var{data})
|
2018-10-11 20:53:05 +03:00
|
|
|
This function returns the kind of nonlocal exit condition stored in
|
|
|
|
@var{env}, like @code{non_local_exit_check} does, but it also returns
|
|
|
|
the full information about the nonlocal exit, if any. If the return
|
|
|
|
value is @code{emacs_funcall_exit_signal}, the function stores the
|
|
|
|
error symbol in @code{*@var{symbol}} and the error data in
|
|
|
|
@code{*@var{data}} (@pxref{Signaling Errors}). If the return value is
|
|
|
|
@code{emacs_funcall_exit_throw}, the function stores the @code{catch}
|
|
|
|
tag symbol in @code{*@var{symbol}} and the @code{throw} value in
|
|
|
|
@code{*@var{data}}. The function doesn't store anything in memory
|
|
|
|
pointed by these arguments when the return value is
|
2025-01-30 16:12:49 +01:00
|
|
|
@code{emacs_funcall_exit_return}. If the function fails to allocate
|
|
|
|
storage for @var{symbol} or @var{data}, it stores a value representing
|
|
|
|
the symbol @code{module-out-of-memory} in @code{*@var{symbol}}, stores a
|
|
|
|
value representing @code{nil} in @code{*@var{data}}, and returns
|
|
|
|
@code{emacs_funcall_exit_signal}.
|
2018-10-11 20:53:05 +03:00
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
You should check nonlocal exit conditions where it matters: before you
|
|
|
|
allocated some resource or after you allocated a resource that might
|
|
|
|
need freeing, or where a failure means further processing is
|
|
|
|
impossible or infeasible.
|
|
|
|
|
|
|
|
Once your module function detected that a nonlocal exit is pending, it
|
|
|
|
can either return to Emacs (after performing the necessary local
|
|
|
|
cleanup), or it can attempt to recover from the nonlocal exit. The
|
|
|
|
following @acronym{API} functions will help with these tasks.
|
|
|
|
|
|
|
|
@deftypefn Function void non_local_exit_clear (emacs_env *@var{env})
|
|
|
|
This function clears the pending nonlocal exit conditions and data
|
|
|
|
from @var{env}. After calling it, the module @acronym{API} functions
|
|
|
|
will work normally. Use this function if your module function can
|
|
|
|
recover from nonlocal exits of the Lisp functions it calls and
|
|
|
|
continue, and also before calling any of the following two functions
|
|
|
|
(or any other @acronym{API} functions, if you want them to perform
|
|
|
|
their intended processing when a nonlocal exit is pending).
|
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
@deftypefn Function void non_local_exit_throw (emacs_env *@var{env}, emacs_value @var{tag}, emacs_value @var{value})
|
|
|
|
This function throws to the Lisp @code{catch} symbol represented by
|
|
|
|
@var{tag}, passing it @var{value} as the value to return. Your module
|
|
|
|
function should in general return soon after calling this function.
|
|
|
|
One use of this function is when you want to re-throw a non-local exit
|
|
|
|
from one of the called @acronym{API} or Lisp functions.
|
|
|
|
@end deftypefn
|
|
|
|
|
Make argument names in module interface more consistent.
Previously, the names of arguments and other details were needlessly
inconsistent between the documentation, the declarations, and the
definitions, as well as between each other. This commit makes them
more consistent, in most cases by applying the names from the
documentation everywhere.
* src/module-env-27.h:
* src/module-env-25.h:
* src/emacs-module.h.in:
* src/emacs-module.c (module_get_environment)
(module_make_global_ref, module_free_global_ref)
(module_non_local_exit_get, module_non_local_exit_signal)
(module_make_function, module_funcall, module_type_of)
(module_is_not_nil, module_extract_integer)
(module_extract_float, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get)
(module_vec_size, module_extract_time)
(module_assert_runtime):
* doc/lispref/internals.texi (Module Initialization)
(Module Functions, Module Values): Make argument names and some other
details consistent. No functional changes.
2019-12-23 17:12:56 +01:00
|
|
|
@deftypefn Function void non_local_exit_signal (emacs_env *@var{env}, emacs_value @var{symbol}, emacs_value @var{data})
|
|
|
|
This function signals the error represented by the error symbol
|
|
|
|
@var{symbol} with the specified error data @var{data}. The module
|
|
|
|
function should return soon after calling this function. This
|
|
|
|
function could be useful, e.g., for signaling errors from module
|
|
|
|
functions to Emacs.
|
2018-10-11 20:53:05 +03:00
|
|
|
@end deftypefn
|
|
|
|
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Object Internals
|
2012-03-02 21:45:21 -05:00
|
|
|
@section Object Internals
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex object internals
|
|
|
|
|
2012-11-15 09:25:05 +04:00
|
|
|
Emacs Lisp provides a rich set of the data types. Some of them, like cons
|
2012-12-06 18:37:20 -08:00
|
|
|
cells, integers and strings, are common to nearly all Lisp dialects. Some
|
2012-11-15 09:25:05 +04:00
|
|
|
others, like markers and buffers, are quite special and needed to provide
|
|
|
|
the basic support to write editor commands in Lisp. To implement such
|
|
|
|
a variety of object types and provide an efficient way to pass objects between
|
|
|
|
the subsystems of an interpreter, there is a set of C data structures and
|
|
|
|
a special type to represent the pointers to all of them, which is known as
|
|
|
|
@dfn{tagged pointer}.
|
|
|
|
|
|
|
|
In C, the tagged pointer is an object of type @code{Lisp_Object}. Any
|
|
|
|
initialized variable of such a type always holds the value of one of the
|
|
|
|
following basic data types: integer, symbol, string, cons cell, float,
|
2018-08-14 12:07:09 -07:00
|
|
|
or vectorlike object. Each of these data types has the
|
2012-11-15 09:25:05 +04:00
|
|
|
corresponding tag value. All tags are enumerated by @code{enum Lisp_Type}
|
|
|
|
and placed into a 3-bit bitfield of the @code{Lisp_Object}. The rest of the
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-17 18:19:03 -07:00
|
|
|
bits is the value itself. Integers are immediate, i.e., directly
|
2012-11-15 09:25:05 +04:00
|
|
|
represented by those @dfn{value bits}, and all other objects are represented
|
|
|
|
by the C pointers to a corresponding object allocated from the heap. Width
|
|
|
|
of the @code{Lisp_Object} is platform- and configuration-dependent: usually
|
2012-12-06 17:47:14 -08:00
|
|
|
it's equal to the width of an underlying platform pointer (i.e., 32-bit on
|
2012-11-15 09:25:05 +04:00
|
|
|
a 32-bit machine and 64-bit on a 64-bit one), but also there is a special
|
|
|
|
configuration where @code{Lisp_Object} is 64-bit but all pointers are 32-bit.
|
|
|
|
The latter trick was designed to overcome the limited range of values for
|
|
|
|
Lisp integers on a 32-bit system by using 64-bit @code{long long} type for
|
|
|
|
@code{Lisp_Object}.
|
|
|
|
|
|
|
|
The following C data structures are defined in @file{lisp.h} to represent
|
|
|
|
the basic data types beyond integers:
|
|
|
|
|
|
|
|
@table @code
|
|
|
|
@item struct Lisp_Cons
|
|
|
|
Cons cell, an object used to construct lists.
|
|
|
|
|
|
|
|
@item struct Lisp_String
|
|
|
|
String, the basic object to represent a sequence of characters.
|
|
|
|
|
|
|
|
@item struct Lisp_Vector
|
|
|
|
Array, a fixed-size set of Lisp objects which may be accessed by an index.
|
|
|
|
|
|
|
|
@item struct Lisp_Symbol
|
|
|
|
Symbol, the unique-named entity commonly used as an identifier.
|
|
|
|
|
|
|
|
@item struct Lisp_Float
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-17 18:19:03 -07:00
|
|
|
Floating-point value.
|
2012-11-15 09:25:05 +04:00
|
|
|
@end table
|
|
|
|
|
|
|
|
These types are the first-class citizens of an internal type system.
|
2018-08-14 12:07:09 -07:00
|
|
|
Since the tag space is limited, all other types are the subtypes of
|
|
|
|
@code{Lisp_Vectorlike}. Vector subtypes are enumerated
|
2012-11-15 09:25:05 +04:00
|
|
|
by @code{enum pvec_type}, and nearly all complex objects like windows, buffers,
|
2018-08-14 12:07:09 -07:00
|
|
|
frames, and processes fall into this category.
|
2012-11-15 09:25:05 +04:00
|
|
|
|
|
|
|
Below there is a description of a few subtypes of @code{Lisp_Vectorlike}.
|
|
|
|
Buffer object represents the text to display and edit. Window is the part
|
2018-11-20 20:45:07 +02:00
|
|
|
of display structure which shows the buffer or is used as a container to
|
2012-11-15 09:25:05 +04:00
|
|
|
recursively place other windows on the same frame. (Do not confuse Emacs Lisp
|
|
|
|
window object with the window as an entity managed by the user interface
|
|
|
|
system like X; in Emacs terminology, the latter is called frame.) Finally,
|
|
|
|
process object is used to manage the subprocesses.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@menu
|
|
|
|
* Buffer Internals:: Components of a buffer structure.
|
|
|
|
* Window Internals:: Components of a window structure.
|
|
|
|
* Process Internals:: Components of a process structure.
|
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Buffer Internals
|
2012-03-02 21:45:21 -05:00
|
|
|
@subsection Buffer Internals
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex internals, of buffer
|
|
|
|
@cindex buffer internals
|
|
|
|
|
2012-03-03 14:13:28 -08:00
|
|
|
Two structures (see @file{buffer.h}) are used to represent buffers
|
2012-12-05 14:27:56 -08:00
|
|
|
in C@. The @code{buffer_text} structure contains fields describing the
|
2012-03-03 14:13:28 -08:00
|
|
|
text of a buffer; the @code{buffer} structure holds other fields. In
|
|
|
|
the case of indirect buffers, two or more @code{buffer} structures
|
|
|
|
reference the same @code{buffer_text} structure.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2009-05-04 04:07:28 +00:00
|
|
|
Here are some of the fields in @code{struct buffer_text}:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@table @code
|
|
|
|
@item beg
|
2018-11-20 20:45:07 +02:00
|
|
|
The address of the buffer contents. The buffer contents is a linear C
|
|
|
|
array of @code{char}, with the gap somewhere in its midst.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item gpt
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx gpt_byte
|
|
|
|
The character and byte positions of the buffer gap. @xref{Buffer
|
|
|
|
Gap}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item z
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx z_byte
|
|
|
|
The character and byte positions of the end of the buffer text.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item gap_size
|
2009-05-04 04:07:28 +00:00
|
|
|
The size of buffer's gap. @xref{Buffer Gap}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item modiff
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx save_modiff
|
|
|
|
@itemx chars_modiff
|
|
|
|
@itemx overlay_modiff
|
|
|
|
These fields count the number of buffer-modification events performed
|
|
|
|
in this buffer. @code{modiff} is incremented after each
|
|
|
|
buffer-modification event, and is never otherwise changed;
|
|
|
|
@code{save_modiff} contains the value of @code{modiff} the last time
|
|
|
|
the buffer was visited or saved; @code{chars_modiff} counts only
|
|
|
|
modifications to the characters in the buffer, ignoring all other
|
2018-11-20 20:45:07 +02:00
|
|
|
kinds of changes (such as text properties); and @code{overlay_modiff}
|
|
|
|
counts only modifications to the buffer's overlays.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item beg_unchanged
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx end_unchanged
|
|
|
|
The number of characters at the start and end of the text that are
|
|
|
|
known to be unchanged since the last complete redisplay.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item unchanged_modified
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx overlay_unchanged_modified
|
|
|
|
The values of @code{modiff} and @code{overlay_modiff}, respectively,
|
2011-11-14 23:55:13 -08:00
|
|
|
after the last complete redisplay. If their current values match
|
2009-05-04 04:07:28 +00:00
|
|
|
@code{modiff} or @code{overlay_modiff}, that means
|
2007-09-06 04:25:08 +00:00
|
|
|
@code{beg_unchanged} and @code{end_unchanged} contain no useful
|
|
|
|
information.
|
|
|
|
|
|
|
|
@item markers
|
|
|
|
The markers that refer to this buffer. This is actually a single
|
2019-10-09 12:29:21 +03:00
|
|
|
marker, and successive elements in its marker @dfn{chain} (a linked
|
|
|
|
list) are the other markers referring to this buffer text.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item intervals
|
2009-05-04 04:07:28 +00:00
|
|
|
The interval tree which records the text properties of this buffer.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end table
|
|
|
|
|
2009-05-04 04:07:28 +00:00
|
|
|
Some of the fields of @code{struct buffer} are:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@table @code
|
2012-03-03 14:13:28 -08:00
|
|
|
@item header
|
2017-11-13 08:51:41 -08:00
|
|
|
A header of type @code{union vectorlike_header} is common to all
|
2012-11-15 09:25:05 +04:00
|
|
|
vectorlike objects.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item own_text
|
2009-05-04 04:07:28 +00:00
|
|
|
A @code{struct buffer_text} structure that ordinarily holds the buffer
|
|
|
|
contents. In indirect buffers, this field is not used.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item text
|
2009-05-04 04:07:28 +00:00
|
|
|
A pointer to the @code{buffer_text} structure for this buffer. In an
|
|
|
|
ordinary buffer, this is the @code{own_text} field above. In an
|
|
|
|
indirect buffer, this is the @code{own_text} field of the base buffer.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-11-15 09:25:05 +04:00
|
|
|
@item next
|
|
|
|
A pointer to the next buffer, in the chain of all buffers, including
|
|
|
|
killed buffers. This chain is used only for allocation and garbage
|
|
|
|
collection, in order to collect killed buffers properly.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@item pt
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx pt_byte
|
|
|
|
The character and byte positions of point in a buffer.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item begv
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx begv_byte
|
|
|
|
The character and byte positions of the beginning of the accessible
|
|
|
|
range of text in the buffer.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item zv
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx zv_byte
|
|
|
|
The character and byte positions of the end of the accessible range of
|
|
|
|
text in the buffer.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item base_buffer
|
|
|
|
In an indirect buffer, this points to the base buffer. In an ordinary
|
|
|
|
buffer, it is null.
|
|
|
|
|
2009-05-04 04:07:28 +00:00
|
|
|
@item local_flags
|
|
|
|
This field contains flags indicating that certain variables are local
|
|
|
|
in this buffer. Such variables are declared in the C code using
|
|
|
|
@code{DEFVAR_PER_BUFFER}, and their buffer-local bindings are stored
|
|
|
|
in fields in the buffer structure itself. (Some of these fields are
|
2007-09-06 04:25:08 +00:00
|
|
|
described in this table.)
|
|
|
|
|
|
|
|
@item modtime
|
2009-05-04 04:07:28 +00:00
|
|
|
The modification time of the visited file. It is set when the file is
|
|
|
|
written or read. Before writing the buffer into a file, this field is
|
|
|
|
compared to the modification time of the file to see if the file has
|
|
|
|
changed on disk. @xref{Buffer Modification}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item auto_save_modified
|
2009-05-04 04:07:28 +00:00
|
|
|
The time when the buffer was last auto-saved.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item last_window_start
|
2009-05-04 04:07:28 +00:00
|
|
|
The @code{window-start} position in the buffer as of the last time the
|
|
|
|
buffer was displayed in a window.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item clip_changed
|
2009-05-04 04:07:28 +00:00
|
|
|
This flag indicates that narrowing has changed in the buffer.
|
|
|
|
@xref{Narrowing}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item prevent_redisplay_optimizations_p
|
2009-05-04 04:07:28 +00:00
|
|
|
This flag indicates that redisplay optimizations should not be used to
|
|
|
|
display this buffer.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2020-12-19 12:39:45 +00:00
|
|
|
@item inhibit_buffer_hooks
|
|
|
|
This flag indicates that the buffer should not run the hooks
|
|
|
|
@code{kill-buffer-hook}, @code{kill-buffer-query-functions}
|
|
|
|
(@pxref{Killing Buffers}), and @code{buffer-list-update-hook}
|
|
|
|
(@pxref{Buffer List}). It is set at buffer creation (@pxref{Creating
|
|
|
|
Buffers}), and avoids slowing down internal or temporary buffers, such
|
|
|
|
as those created by @code{with-temp-buffer} (@pxref{Definition of
|
|
|
|
with-temp-buffer,, Current Buffer}).
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@item name
|
2009-05-04 04:07:28 +00:00
|
|
|
A Lisp string that names the buffer. It is guaranteed to be unique.
|
2018-11-20 20:45:07 +02:00
|
|
|
@xref{Buffer Names}. This and the following fields have their names
|
|
|
|
in the C struct definition end in a @code{_} to indicate that they
|
|
|
|
should not be accessed directly, but via the @code{BVAR} macro, like
|
|
|
|
this:
|
|
|
|
|
|
|
|
@example
|
|
|
|
Lisp_Object buf_name = BVAR (buffer, name);
|
|
|
|
@end example
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2009-05-04 04:07:28 +00:00
|
|
|
@item save_length
|
|
|
|
The length of the file this buffer is visiting, when last read or
|
2018-11-20 20:45:07 +02:00
|
|
|
saved. It can have 2 special values: @minus{}1 means auto-saving was
|
|
|
|
turned off in this buffer, and @minus{}2 means don't turn off
|
|
|
|
auto-saving if buffer text shrinks a lot. This and other fields
|
|
|
|
concerned with saving are not kept in the @code{buffer_text} structure
|
|
|
|
because indirect buffers are never saved.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item directory
|
2009-05-04 04:07:28 +00:00
|
|
|
The directory for expanding relative file names. This is the value of
|
|
|
|
the buffer-local variable @code{default-directory} (@pxref{File Name Expansion}).
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2009-05-04 04:07:28 +00:00
|
|
|
@item filename
|
|
|
|
The name of the file visited in this buffer, or @code{nil}. This is
|
|
|
|
the value of the buffer-local variable @code{buffer-file-name}
|
|
|
|
(@pxref{Buffer File Name}).
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2009-05-04 04:07:28 +00:00
|
|
|
@item undo_list
|
|
|
|
@itemx backed_up
|
|
|
|
@itemx auto_save_file_name
|
2012-03-03 14:13:28 -08:00
|
|
|
@itemx auto_save_file_format
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx read_only
|
|
|
|
@itemx file_format
|
|
|
|
@itemx file_truename
|
|
|
|
@itemx invisibility_spec
|
|
|
|
@itemx display_count
|
|
|
|
@itemx display_time
|
|
|
|
These fields store the values of Lisp variables that are automatically
|
|
|
|
buffer-local (@pxref{Buffer-Local Variables}), whose corresponding
|
|
|
|
variable names have the additional prefix @code{buffer-} and have
|
|
|
|
underscores replaced with dashes. For instance, @code{undo_list}
|
2012-02-27 20:17:02 -08:00
|
|
|
stores the value of @code{buffer-undo-list}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item mark
|
2009-05-04 04:07:28 +00:00
|
|
|
The mark for the buffer. The mark is a marker, hence it is also
|
|
|
|
included on the list @code{markers}. @xref{The Mark}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item local_var_alist
|
2009-05-04 04:07:28 +00:00
|
|
|
The association list describing the buffer-local variable bindings of
|
|
|
|
this buffer, not including the built-in buffer-local bindings that
|
|
|
|
have special slots in the buffer object. (Those slots are omitted
|
|
|
|
from this table.) @xref{Buffer-Local Variables}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item major_mode
|
|
|
|
Symbol naming the major mode of this buffer, e.g., @code{lisp-mode}.
|
|
|
|
|
|
|
|
@item mode_name
|
2009-05-04 04:07:28 +00:00
|
|
|
Pretty name of the major mode, e.g., @code{"Lisp"}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item keymap
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx abbrev_table
|
|
|
|
@itemx syntax_table
|
|
|
|
@itemx category_table
|
|
|
|
@itemx display_table
|
|
|
|
These fields store the buffer's local keymap (@pxref{Keymaps}), abbrev
|
|
|
|
table (@pxref{Abbrev Tables}), syntax table (@pxref{Syntax Tables}),
|
|
|
|
category table (@pxref{Categories}), and display table (@pxref{Display
|
|
|
|
Tables}).
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item downcase_table
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx upcase_table
|
|
|
|
@itemx case_canon_table
|
|
|
|
These fields store the conversion tables for converting text to lower
|
|
|
|
case, upper case, and for canonicalizing text for case-fold search.
|
2007-09-06 04:25:08 +00:00
|
|
|
@xref{Case Tables}.
|
|
|
|
|
|
|
|
@item minor_modes
|
|
|
|
An alist of the minor modes of this buffer.
|
|
|
|
|
|
|
|
@item pt_marker
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx begv_marker
|
|
|
|
@itemx zv_marker
|
|
|
|
These fields are only used in an indirect buffer, or in a buffer that
|
|
|
|
is the base of an indirect buffer. Each holds a marker that records
|
|
|
|
@code{pt}, @code{begv}, and @code{zv} respectively, for this buffer
|
2007-09-06 04:25:08 +00:00
|
|
|
when the buffer is not current.
|
|
|
|
|
2009-05-04 04:07:28 +00:00
|
|
|
@item mode_line_format
|
|
|
|
@itemx header_line_format
|
|
|
|
@itemx tab_width
|
|
|
|
@itemx fill_column
|
|
|
|
@itemx left_margin
|
|
|
|
@itemx auto_fill_function
|
|
|
|
@itemx truncate_lines
|
|
|
|
@itemx word_wrap
|
|
|
|
@itemx ctl_arrow
|
2012-03-03 14:13:28 -08:00
|
|
|
@itemx bidi_display_reordering
|
|
|
|
@itemx bidi_paragraph_direction
|
2009-05-04 04:07:28 +00:00
|
|
|
@itemx selective_display
|
|
|
|
@itemx selective_display_ellipses
|
|
|
|
@itemx overwrite_mode
|
|
|
|
@itemx abbrev_mode
|
|
|
|
@itemx mark_active
|
|
|
|
@itemx enable_multibyte_characters
|
|
|
|
@itemx buffer_file_coding_system
|
|
|
|
@itemx cache_long_line_scans
|
|
|
|
@itemx point_before_scroll
|
|
|
|
@itemx left_fringe_width
|
|
|
|
@itemx right_fringe_width
|
|
|
|
@itemx fringes_outside_margins
|
|
|
|
@itemx scroll_bar_width
|
|
|
|
@itemx indicate_empty_lines
|
|
|
|
@itemx indicate_buffer_boundaries
|
|
|
|
@itemx fringe_indicator_alist
|
|
|
|
@itemx fringe_cursor_alist
|
|
|
|
@itemx scroll_up_aggressively
|
|
|
|
@itemx scroll_down_aggressively
|
|
|
|
@itemx cursor_type
|
|
|
|
@itemx cursor_in_non_selected_windows
|
|
|
|
These fields store the values of Lisp variables that are automatically
|
|
|
|
buffer-local (@pxref{Buffer-Local Variables}), whose corresponding
|
|
|
|
variable names have underscores replaced with dashes. For instance,
|
|
|
|
@code{mode_line_format} stores the value of @code{mode-line-format}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2022-12-14 20:13:47 +02:00
|
|
|
@item overlays
|
2023-02-12 07:47:09 +01:00
|
|
|
The interval tree containing this buffer's overlays.
|
2022-12-14 20:13:47 +02:00
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@item last_selected_window
|
|
|
|
This is the last window that was selected with this buffer in it, or @code{nil}
|
|
|
|
if that window no longer displays this buffer.
|
|
|
|
@end table
|
|
|
|
|
|
|
|
@node Window Internals
|
2012-03-02 21:45:21 -05:00
|
|
|
@subsection Window Internals
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex internals, of window
|
|
|
|
@cindex window internals
|
|
|
|
|
2012-03-03 14:13:28 -08:00
|
|
|
The fields of a window (for a complete list, see the definition of
|
|
|
|
@code{struct window} in @file{window.h}) include:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@table @code
|
|
|
|
@item frame
|
2018-11-20 20:45:07 +02:00
|
|
|
The frame that this window is on, as a Lisp object.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-11-20 20:45:07 +02:00
|
|
|
@item mini
|
2018-12-03 09:35:33 +01:00
|
|
|
Non-zero if this window is a minibuffer window, a window showing the
|
|
|
|
minibuffer or the echo area.
|
|
|
|
|
|
|
|
@item pseudo_window_p
|
|
|
|
@cindex pseudo window
|
|
|
|
Non-zero if this window is a @dfn{pseudo window}. A pseudo window is
|
|
|
|
either a window used to display the menu bar or the tool bar (when
|
|
|
|
Emacs uses toolkits that don't display their own menu bar and tool
|
2019-10-13 23:56:17 +03:00
|
|
|
bar) or the tab bar or a window showing a tooltip on a tooltip frame.
|
|
|
|
Pseudo windows are in general not accessible from Lisp code.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item parent
|
2018-12-03 09:35:33 +01:00
|
|
|
Internally, Emacs arranges windows in a tree; each group of siblings
|
|
|
|
has a parent window whose area includes all the siblings. This field
|
|
|
|
points to the window's parent in that tree, as a Lisp object. For the
|
|
|
|
root window of the tree and a minibuffer window this is always
|
|
|
|
@code{nil}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
Parent windows do not display buffers, and play little role in display
|
2018-12-03 09:35:33 +01:00
|
|
|
except to shape their child windows. Emacs Lisp programs cannot
|
|
|
|
directly manipulate parent windows; they operate on the windows at the
|
2007-09-06 04:25:08 +00:00
|
|
|
leaves of the tree, which actually display buffers.
|
|
|
|
|
2018-12-03 09:35:33 +01:00
|
|
|
@item contents
|
|
|
|
For a leaf window and windows showing a tooltip, this is the buffer,
|
|
|
|
as a Lisp object, that the window is displaying. For an internal
|
|
|
|
(``parent'') window, this is its first child window. For a pseudo
|
|
|
|
window showing a menu or tool bar this is @code{nil}. It is also
|
|
|
|
@code{nil} for a window that has been deleted.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@item next
|
2009-05-04 23:25:38 +00:00
|
|
|
@itemx prev
|
2018-12-03 09:35:33 +01:00
|
|
|
The next and previous sibling of this window as Lisp objects.
|
|
|
|
@code{next} is @code{nil} if the window is the right-most or
|
|
|
|
bottom-most in its group; @code{prev} is @code{nil} if it is the
|
|
|
|
left-most or top-most in its group. Whether the sibling is left/right
|
|
|
|
or up/down is determined by the @code{horizontal} field of the
|
|
|
|
sibling's parent: if it's non-zero, the siblings are arranged
|
|
|
|
horizontally.
|
|
|
|
|
|
|
|
As a special case, @code{next} of a frame's root window points to the
|
|
|
|
frame's minibuffer window, provided this is not a minibuffer-only or
|
|
|
|
minibuffer-less frame. On such frames @code{prev} of the minibuffer
|
|
|
|
window points to that frame's root window. In any other case, the
|
|
|
|
root window's @code{next} and the minibuffer window's (if present)
|
|
|
|
@code{prev} fields are @code{nil}.
|
2009-05-04 23:25:38 +00:00
|
|
|
|
|
|
|
@item left_col
|
|
|
|
The left-hand edge of the window, measured in columns, relative to the
|
2018-12-03 09:35:33 +01:00
|
|
|
leftmost column (column 0) of the window's native frame.
|
2009-05-04 23:25:38 +00:00
|
|
|
|
|
|
|
@item top_line
|
|
|
|
The top edge of the window, measured in lines, relative to the topmost
|
2018-12-03 09:35:33 +01:00
|
|
|
line (line 0) of the window's native frame.
|
|
|
|
|
|
|
|
@item pixel_left
|
|
|
|
@itemx pixel_top
|
|
|
|
The left-hand and top edges of this window, measured in pixels,
|
|
|
|
relative to the top-left corner (0, 0) of the window's native frame.
|
2009-05-04 23:25:38 +00:00
|
|
|
|
|
|
|
@item total_cols
|
|
|
|
@itemx total_lines
|
2018-12-03 09:35:33 +01:00
|
|
|
The total width and height of the window, measured in columns and
|
|
|
|
lines respectively. The values include scroll bars and fringes,
|
|
|
|
dividers and/or the separator line on the right of the window (if
|
|
|
|
any).
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-12-03 09:35:33 +01:00
|
|
|
@item pixel_width;
|
|
|
|
@itemx pixel_height;
|
|
|
|
The total width and height of the window measured in pixels.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item start
|
2009-05-04 23:25:38 +00:00
|
|
|
A marker pointing to the position in the buffer that is the first
|
2018-11-20 20:45:07 +02:00
|
|
|
character (in the logical order, @pxref{Bidirectional Display})
|
|
|
|
displayed in the window.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item pointm
|
|
|
|
@cindex window point internals
|
|
|
|
This is the value of point in the current buffer when this window is
|
|
|
|
selected; when it is not selected, it retains its previous value.
|
|
|
|
|
2018-11-20 20:45:07 +02:00
|
|
|
@item old_pointm
|
|
|
|
The value of @code{pointm} at the last redisplay time.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@item force_start
|
|
|
|
If this flag is non-@code{nil}, it says that the window has been
|
2019-10-15 14:41:43 +02:00
|
|
|
scrolled explicitly by the Lisp program, and the value of the
|
2018-11-20 20:45:07 +02:00
|
|
|
window's @code{start} was set for redisplay to honor. This affects
|
|
|
|
what the next redisplay does if point is off the screen: instead of
|
|
|
|
scrolling the window to show the text around point, it moves point to
|
|
|
|
a location that is on the screen.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-11-20 20:45:07 +02:00
|
|
|
@item optional_new_start
|
|
|
|
This is similar to @code{force_start}, but the next redisplay will
|
|
|
|
only obey it if point stays visible.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item start_at_line_beg
|
|
|
|
Non-@code{nil} means current value of @code{start} was the beginning of a line
|
|
|
|
when it was chosen.
|
|
|
|
|
|
|
|
@item use_time
|
|
|
|
This is the last time that the window was selected. The function
|
|
|
|
@code{get-lru-window} uses this field.
|
|
|
|
|
|
|
|
@item sequence_number
|
|
|
|
A unique number assigned to this window when it was created.
|
|
|
|
|
|
|
|
@item last_modified
|
|
|
|
The @code{modiff} field of the window's buffer, as of the last time
|
|
|
|
a redisplay completed in this window.
|
|
|
|
|
|
|
|
@item last_overlay_modified
|
|
|
|
The @code{overlay_modiff} field of the window's buffer, as of the last
|
|
|
|
time a redisplay completed in this window.
|
|
|
|
|
|
|
|
@item last_point
|
|
|
|
The buffer's value of point, as of the last time a redisplay completed
|
|
|
|
in this window.
|
|
|
|
|
|
|
|
@item last_had_star
|
2018-11-20 20:45:07 +02:00
|
|
|
A non-zero value means the window's buffer was modified when the
|
2007-09-06 04:25:08 +00:00
|
|
|
window was last updated.
|
|
|
|
|
2018-12-03 09:35:33 +01:00
|
|
|
@item vertical_scroll_bar_type
|
|
|
|
@itemx horizontal_scroll_bar_type
|
|
|
|
The types of this window's vertical and horizontal scroll bars.
|
|
|
|
|
|
|
|
@item scroll_bar_width
|
|
|
|
@itemx scroll_bar_height
|
|
|
|
The width of this window's vertical scroll bar and the height of this
|
|
|
|
window's horizontal scroll bar, in pixels.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-03-03 14:13:28 -08:00
|
|
|
@item left_margin_cols
|
|
|
|
@itemx right_margin_cols
|
2009-05-04 23:25:38 +00:00
|
|
|
The widths of the left and right margins in this window. A value of
|
2018-11-20 20:45:07 +02:00
|
|
|
zero means no margin.
|
2012-03-03 14:13:28 -08:00
|
|
|
|
|
|
|
@item left_fringe_width
|
|
|
|
@itemx right_fringe_width
|
2018-11-20 20:45:07 +02:00
|
|
|
The pixel widths of the left and right fringes in this window. A
|
|
|
|
value of @minus{}1 means use the values of the frame.
|
2012-03-03 14:13:28 -08:00
|
|
|
|
|
|
|
@item fringes_outside_margins
|
2018-11-20 20:45:07 +02:00
|
|
|
A non-zero value means the fringes outside the display margins;
|
2012-03-03 14:13:28 -08:00
|
|
|
othersize they are between the margin and the text.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item window_end_pos
|
|
|
|
This is computed as @code{z} minus the buffer position of the last glyph
|
|
|
|
in the current matrix of the window. The value is only valid if
|
2018-11-20 20:45:07 +02:00
|
|
|
@code{window_end_valid} is non-zero.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item window_end_bytepos
|
|
|
|
The byte position corresponding to @code{window_end_pos}.
|
|
|
|
|
|
|
|
@item window_end_vpos
|
|
|
|
The window-relative vertical position of the line containing
|
|
|
|
@code{window_end_pos}.
|
|
|
|
|
|
|
|
@item window_end_valid
|
2018-11-20 20:45:07 +02:00
|
|
|
This field is set to a non-zero value if @code{window_end_pos} and
|
|
|
|
@code{window_end_vpos} are truly valid. This is zero if nontrivial
|
2025-01-23 03:13:01 +01:00
|
|
|
redisplay is preempted, since in that case the display that
|
2018-11-20 20:45:07 +02:00
|
|
|
@code{window_end_pos} was computed for did not get onto the screen.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item cursor
|
|
|
|
A structure describing where the cursor is in this window.
|
|
|
|
|
2018-11-20 20:45:07 +02:00
|
|
|
@item last_cursor_vpos
|
|
|
|
The window-relative vertical position of the line showing the cursor
|
|
|
|
as of the last redisplay that finished.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item phys_cursor
|
|
|
|
A structure describing where the cursor of this window physically is.
|
|
|
|
|
|
|
|
@item phys_cursor_type
|
2012-03-03 14:13:28 -08:00
|
|
|
@c FIXME What is this?
|
|
|
|
@c itemx phys_cursor_ascent
|
|
|
|
@itemx phys_cursor_height
|
|
|
|
@itemx phys_cursor_width
|
|
|
|
The type, height, and width of the cursor that was last displayed on
|
|
|
|
this window.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item phys_cursor_on_p
|
|
|
|
This field is non-zero if the cursor is physically on.
|
|
|
|
|
|
|
|
@item cursor_off_p
|
2012-03-03 14:13:28 -08:00
|
|
|
Non-zero means the cursor in this window is logically off. This is
|
|
|
|
used for blinking the cursor.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item last_cursor_off_p
|
|
|
|
This field contains the value of @code{cursor_off_p} as of the time of
|
|
|
|
the last redisplay.
|
|
|
|
|
|
|
|
@item must_be_updated_p
|
|
|
|
This is set to 1 during redisplay when this window must be updated.
|
|
|
|
|
|
|
|
@item hscroll
|
2018-11-20 20:45:07 +02:00
|
|
|
This is the number of columns that the display in the window is
|
|
|
|
scrolled horizontally to the left. Normally, this is 0. When only
|
|
|
|
the current line is hscrolled, this describes how much the current
|
|
|
|
line is scrolled.
|
|
|
|
|
|
|
|
@item min_hscroll
|
|
|
|
Minimum value of @code{hscroll}, set by the user via
|
|
|
|
@code{set-window-hscroll} (@pxref{Horizontal Scrolling}). When only
|
|
|
|
the current line is hscrolled, this describes the horizontal scrolling
|
|
|
|
of lines other than the current one.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item vscroll
|
|
|
|
Vertical scroll amount, in pixels. Normally, this is 0.
|
|
|
|
|
|
|
|
@item dedicated
|
|
|
|
Non-@code{nil} if this window is dedicated to its buffer.
|
|
|
|
|
2018-12-03 09:35:33 +01:00
|
|
|
@item combination_limit
|
|
|
|
This window's combination limit, meaningful only for a parent window.
|
|
|
|
If this is @code{t}, then it is not allowed to delete this window and
|
|
|
|
recombine its child windows with other siblings of this window.
|
|
|
|
|
|
|
|
@item window_parameters
|
|
|
|
The alist of this window's parameters.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@item display_table
|
|
|
|
The window's display table, or @code{nil} if none is specified for it.
|
|
|
|
|
|
|
|
@item update_mode_line
|
2018-11-20 20:45:07 +02:00
|
|
|
Non-zero means this window's mode line needs to be updated.
|
|
|
|
|
|
|
|
@item mode_line_height
|
|
|
|
@itemx header_line_height
|
|
|
|
The height in pixels of the mode line and the header line, or
|
|
|
|
@minus{}1 if not known.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item base_line_number
|
2018-11-20 20:45:07 +02:00
|
|
|
The line number of a certain position in the buffer, or zero.
|
2007-09-06 04:25:08 +00:00
|
|
|
This is used for displaying the line number of point in the mode line.
|
|
|
|
|
|
|
|
@item base_line_pos
|
|
|
|
The position in the buffer for which the line number is known, or
|
2018-11-20 20:45:07 +02:00
|
|
|
zero meaning none is known. If it is @minus{}1, don't display
|
2012-03-03 14:13:28 -08:00
|
|
|
the line number as long as the window shows that buffer.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item column_number_displayed
|
2018-11-20 20:45:07 +02:00
|
|
|
The column number currently displayed in this window's mode line, or
|
|
|
|
@minus{}1 if column numbers are not being displayed.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@item current_matrix
|
2012-03-03 14:13:28 -08:00
|
|
|
@itemx desired_matrix
|
|
|
|
Glyph matrices describing the current and desired display of this window.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end table
|
|
|
|
|
|
|
|
@node Process Internals
|
2012-03-02 21:45:21 -05:00
|
|
|
@subsection Process Internals
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex internals, of process
|
|
|
|
@cindex process internals
|
|
|
|
|
2012-03-03 14:13:28 -08:00
|
|
|
The fields of a process (for a complete list, see the definition of
|
|
|
|
@code{struct Lisp_Process} in @file{process.h}) include:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@table @code
|
|
|
|
@item name
|
2018-11-20 20:45:07 +02:00
|
|
|
A Lisp string, the name of the process.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item command
|
|
|
|
A list containing the command arguments that were used to start this
|
2008-06-13 08:05:52 +00:00
|
|
|
process. For a network or serial process, it is @code{nil} if the
|
|
|
|
process is running or @code{t} if the process is stopped.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item filter
|
2018-11-20 20:45:07 +02:00
|
|
|
A Lisp function used to accept output from the process.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item sentinel
|
2018-11-20 20:45:07 +02:00
|
|
|
A Lisp function called whenever the state of the process changes.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item buffer
|
|
|
|
The associated buffer of the process.
|
|
|
|
|
|
|
|
@item pid
|
|
|
|
An integer, the operating system's process @acronym{ID}.
|
2012-03-03 14:13:28 -08:00
|
|
|
Pseudo-processes such as network or serial connections use a value of 0.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item childp
|
2012-03-03 14:13:28 -08:00
|
|
|
A flag, @code{t} if this is really a child process. For a network or
|
|
|
|
serial connection, it is a plist based on the arguments to
|
|
|
|
@code{make-network-process} or @code{make-serial-process}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item mark
|
|
|
|
A marker indicating the position of the end of the last output from this
|
|
|
|
process inserted into the buffer. This is often but not always the end
|
|
|
|
of the buffer.
|
|
|
|
|
|
|
|
@item kill_without_query
|
2009-05-04 23:25:38 +00:00
|
|
|
If this is non-zero, killing Emacs while this process is still running
|
|
|
|
does not ask for confirmation about killing the process.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-03-03 14:13:28 -08:00
|
|
|
@item raw_status
|
|
|
|
The raw process status, as returned by the @code{wait} system call.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item status
|
2018-11-20 20:45:07 +02:00
|
|
|
The process status, as @code{process-status} should return it. This
|
|
|
|
is a Lisp symbol, a cons cell, or a list.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item tick
|
|
|
|
@itemx update_tick
|
|
|
|
If these two fields are not equal, a change in the status of the process
|
|
|
|
needs to be reported, either by running the sentinel or by inserting a
|
|
|
|
message in the process buffer.
|
|
|
|
|
|
|
|
@item pty_flag
|
2018-11-20 20:45:07 +02:00
|
|
|
Non-zero if communication with the subprocess uses a pty; zero if it
|
|
|
|
uses a pipe.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item infd
|
|
|
|
The file descriptor for input from the process.
|
|
|
|
|
|
|
|
@item outfd
|
|
|
|
The file descriptor for output to the process.
|
|
|
|
|
|
|
|
@item tty_name
|
|
|
|
The name of the terminal that the subprocess is using,
|
|
|
|
or @code{nil} if it is using pipes.
|
|
|
|
|
|
|
|
@item decode_coding_system
|
|
|
|
Coding-system for decoding the input from this process.
|
|
|
|
|
|
|
|
@item decoding_buf
|
|
|
|
A working buffer for decoding.
|
|
|
|
|
|
|
|
@item decoding_carryover
|
|
|
|
Size of carryover in decoding.
|
|
|
|
|
|
|
|
@item encode_coding_system
|
|
|
|
Coding-system for encoding the output to this process.
|
|
|
|
|
|
|
|
@item encoding_buf
|
|
|
|
A working buffer for encoding.
|
|
|
|
|
|
|
|
@item inherit_coding_system_flag
|
|
|
|
Flag to set @code{coding-system} of the process buffer from the
|
|
|
|
coding system used to decode process output.
|
2008-06-13 08:05:52 +00:00
|
|
|
|
|
|
|
@item type
|
|
|
|
Symbol indicating the type of process: @code{real}, @code{network},
|
2012-03-03 14:13:28 -08:00
|
|
|
@code{serial}.
|
2008-06-13 08:05:52 +00:00
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@end table
|
2012-03-03 14:13:28 -08:00
|
|
|
|
2012-12-10 16:13:44 -08:00
|
|
|
@node C Integer Types
|
|
|
|
@section C Integer Types
|
|
|
|
@cindex integer types (C programming language)
|
|
|
|
|
|
|
|
Here are some guidelines for use of integer types in the Emacs C
|
|
|
|
source code. These guidelines sometimes give competing advice; common
|
|
|
|
sense is advised.
|
|
|
|
|
|
|
|
@itemize @bullet
|
|
|
|
@item
|
|
|
|
Avoid arbitrary limits. For example, avoid @code{int len = strlen
|
|
|
|
(s);} unless the length of @code{s} is required for other reasons to
|
|
|
|
fit in @code{int} range.
|
|
|
|
|
|
|
|
@item
|
|
|
|
Do not assume that signed integer arithmetic wraps around on overflow.
|
|
|
|
This is no longer true of Emacs porting targets: signed integer
|
|
|
|
overflow has undefined behavior in practice, and can dump core or
|
2015-09-15 08:46:48 -07:00
|
|
|
even cause earlier or later code to behave illogically. Unsigned
|
2012-12-10 16:13:44 -08:00
|
|
|
overflow does wrap around reliably, modulo a power of two.
|
|
|
|
|
|
|
|
@item
|
|
|
|
Prefer signed types to unsigned, as code gets confusing when signed
|
|
|
|
and unsigned types are combined. Many other guidelines assume that
|
|
|
|
types are signed; in the rarer cases where unsigned types are needed,
|
|
|
|
similar advice may apply to the unsigned counterparts (e.g.,
|
|
|
|
@code{size_t} instead of @code{ptrdiff_t}, or @code{uintptr_t} instead
|
|
|
|
of @code{intptr_t}).
|
|
|
|
|
|
|
|
@item
|
2015-03-17 16:55:02 -07:00
|
|
|
Prefer @code{int} for Emacs character codes, in the range 0 ..@: 0x3FFFFF@.
|
2014-10-01 13:34:55 -07:00
|
|
|
More generally, prefer @code{int} for integers known to be in
|
|
|
|
@code{int} range, e.g., screen column counts.
|
2012-12-10 16:13:44 -08:00
|
|
|
|
|
|
|
@item
|
|
|
|
Prefer @code{ptrdiff_t} for sizes, i.e., for integers bounded by the
|
|
|
|
maximum size of any individual C object or by the maximum number of
|
|
|
|
elements in any C array. This is part of Emacs's general preference
|
|
|
|
for signed types. Using @code{ptrdiff_t} limits objects to
|
|
|
|
@code{PTRDIFF_MAX} bytes, but larger objects would cause trouble
|
|
|
|
anyway since they would break pointer subtraction, so this does not
|
|
|
|
impose an arbitrary limit.
|
|
|
|
|
2014-10-01 13:34:55 -07:00
|
|
|
@item
|
|
|
|
Avoid @code{ssize_t} except when communicating to low-level APIs that
|
|
|
|
have @code{ssize_t}-related limitations. Although it's equivalent to
|
|
|
|
@code{ptrdiff_t} on typical platforms, @code{ssize_t} is occasionally
|
|
|
|
narrower, so using it for size-related calculations could overflow.
|
|
|
|
Also, @code{ptrdiff_t} is more ubiquitous and better-standardized, has
|
|
|
|
standard @code{printf} formats, and is the basis for Emacs's internal
|
|
|
|
size-overflow checking. When using @code{ssize_t}, please note that
|
|
|
|
POSIX requires support only for values in the range @minus{}1 ..@:
|
|
|
|
@code{SSIZE_MAX}.
|
|
|
|
|
2012-12-10 16:13:44 -08:00
|
|
|
@item
|
2019-12-14 14:22:03 -08:00
|
|
|
Normally, prefer @code{intptr_t} for internal representations of pointers, or
|
2012-12-10 16:13:44 -08:00
|
|
|
for integers bounded only by the number of objects that can exist at
|
|
|
|
any given time or by the total number of bytes that can be allocated.
|
2019-12-14 14:22:03 -08:00
|
|
|
However, prefer @code{uintptr_t} to represent pointer arithmetic that
|
|
|
|
could cross page boundaries. For example, on a machine with a 32-bit
|
|
|
|
address space an array could cross the 0x7fffffff/0x80000000 boundary,
|
|
|
|
which would cause an integer overflow when adding 1 to
|
|
|
|
@code{(intptr_t) 0x7fffffff}.
|
2012-12-10 16:13:44 -08:00
|
|
|
|
|
|
|
@item
|
|
|
|
Prefer the Emacs-defined type @code{EMACS_INT} for representing values
|
|
|
|
converted to or from Emacs Lisp fixnums, as fixnum arithmetic is based
|
|
|
|
on @code{EMACS_INT}.
|
|
|
|
|
|
|
|
@item
|
|
|
|
When representing a system value (such as a file size or a count of
|
|
|
|
seconds since the Epoch), prefer the corresponding system type (e.g.,
|
|
|
|
@code{off_t}, @code{time_t}). Do not assume that a system type is
|
|
|
|
signed, unless this assumption is known to be safe. For example,
|
|
|
|
although @code{off_t} is always signed, @code{time_t} need not be.
|
|
|
|
|
|
|
|
@item
|
|
|
|
Prefer @code{intmax_t} for representing values that might be any
|
|
|
|
signed integer value.
|
2019-07-07 12:29:27 -07:00
|
|
|
A @code{printf}-family function can print such a value
|
|
|
|
via a format like @code{"%"PRIdMAX}.
|
2012-12-10 16:13:44 -08:00
|
|
|
|
|
|
|
@item
|
2014-03-20 16:15:19 -07:00
|
|
|
Prefer @code{bool}, @code{false} and @code{true} for booleans.
|
|
|
|
Using @code{bool} can make programs easier to read and a bit faster than
|
|
|
|
using @code{int}. Although it is also OK to use @code{int}, @code{0}
|
|
|
|
and @code{1}, this older style is gradually being phased out. When
|
2012-12-10 16:13:44 -08:00
|
|
|
using @code{bool}, respect the limitations of the replacement
|
2022-09-28 01:06:10 -07:00
|
|
|
implementation of @code{bool}. In particular,
|
|
|
|
boolean bitfields should be of type
|
2014-05-07 13:51:35 -07:00
|
|
|
@code{bool_bf}, not @code{bool}, so that they work correctly even when
|
|
|
|
compiling Objective C with standard GCC.
|
|
|
|
|
|
|
|
@item
|
|
|
|
In bitfields, prefer @code{unsigned int} or @code{signed int} to
|
|
|
|
@code{int}, as @code{int} is less portable: it might be signed, and
|
|
|
|
might not be. Single-bit bit fields should be @code{unsigned int} or
|
|
|
|
@code{bool_bf} so that their values are 0 or 1.
|
2012-12-10 16:13:44 -08:00
|
|
|
@end itemize
|
|
|
|
|
2012-03-03 14:13:28 -08:00
|
|
|
@c FIXME Mention src/globals.h somewhere in this file?
|