Previous: Object Internals, Up: GNU Emacs Internals [Contents][Index]
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.
int len = strlen
(s);
unless the length of s
is required for other reasons to
fit in int
range.
size_t
instead of ptrdiff_t
, or uintptr_t
instead
of intptr_t
).
int
for Emacs character codes, in the range 0 .. 0x3FFFFF.
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 ptrdiff_t
limits objects to
PTRDIFF_MAX
bytes, but larger objects would cause trouble
anyway since they would break pointer subtraction, so this does not
impose an arbitrary limit.
intptr_t
for internal representations of pointers, or
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.
Currently Emacs sometimes uses other types when intptr_t
would
be better; fixing this is lower priority, as the code works as-is on
Emacs’s current porting targets.
EMACS_INT
for representing values
converted to or from Emacs Lisp fixnums, as fixnum arithmetic is based
on EMACS_INT
.
off_t
, time_t
). Do not assume that a system type is
signed, unless this assumption is known to be safe. For example,
although off_t
is always signed, time_t
need not be.
printmax_t
for representing
values that might be any signed integer that can be printed,
using a printf
-family function.
intmax_t
for representing values that might be any
signed integer value.
bool
, false
and true
for booleans.
Using bool
can make programs easier to read and a bit faster than
using int
. Although it is also OK to use int
, 0
and 1
, this older style is gradually being phased out. When
using bool
, respect the limitations of the replacement
implementation of bool
, as documented in the source file
lib/stdbool.in.h, so that Emacs remains portable to pre-C99
platforms. In particular, boolean bitfields should be of type
bool_bf
, not bool
, so that they work correctly even when
compiling Objective C with standard GCC.
unsigned int
or signed int
to
int
, as int
is less portable: it might be signed, and
might not be. Single-bit bit fields should be unsigned int
or
bool_bf
so that their values are 0 or 1.
Previous: Object Internals, Up: GNU Emacs Internals [Contents][Index]