396 |
the @code{SCM} value. This may be significant when traversing lists, a |
the @code{SCM} value. This may be significant when traversing lists, a |
397 |
common activity in a Scheme system. |
common activity in a Scheme system. |
398 |
|
|
399 |
Again, most real Scheme systems use a slighty different implementation; |
Again, most real Scheme systems use a slightly different implementation; |
400 |
for example, if GET_PAIR subtracts off the low bits of @code{x}, instead |
for example, if GET_PAIR subtracts off the low bits of @code{x}, instead |
401 |
of masking them off, the optimizer will often be able to combine that |
of masking them off, the optimizer will often be able to combine that |
402 |
subtraction with the addition of the offset of the structure member we |
subtraction with the addition of the offset of the structure member we |
564 |
includes small integers, characters, boolean values, the empty list, the |
includes small integers, characters, boolean values, the empty list, the |
565 |
mysterious end-of-file object, and some others. |
mysterious end-of-file object, and some others. |
566 |
|
|
567 |
The remaining types are called, not suprisingly, @dfn{non-immediates}. |
The remaining types are called, not surprisingly, @dfn{non-immediates}. |
568 |
They include pairs, procedures, strings, vectors, and all other data |
They include pairs, procedures, strings, vectors, and all other data |
569 |
types in Guile. |
types in Guile. |
570 |
|
|
797 |
contents. |
contents. |
798 |
@end deftypefun |
@end deftypefun |
799 |
|
|
800 |
The macros below perform no typechecking. The results are undefined if |
The macros below perform no type checking. The results are undefined if |
801 |
@var{cell} is an immediate. However, since all non-immediate Guile |
@var{cell} is an immediate. However, since all non-immediate Guile |
802 |
objects are constructed from cells, and these macros simply return the |
objects are constructed from cells, and these macros simply return the |
803 |
first element of a cell, they actually can be useful on datatypes other |
first element of a cell, they actually can be useful on datatypes other |
949 |
A subr is a pointer to a C function, packaged up as a Scheme object to |
A subr is a pointer to a C function, packaged up as a Scheme object to |
950 |
make it callable by Scheme code. In addition to the function pointer, |
make it callable by Scheme code. In addition to the function pointer, |
951 |
the subr also contains a pointer to the name of the function, and |
the subr also contains a pointer to the name of the function, and |
952 |
information about the number of arguments accepted by the C fuction, for |
information about the number of arguments accepted by the C function, for |
953 |
the sake of error checking. |
the sake of error checking. |
954 |
|
|
955 |
There is no single type predicate macro that recognizes subrs, as |
There is no single type predicate macro that recognizes subrs, as |
1143 |
@node Non-immediate objects |
@node Non-immediate objects |
1144 |
@subsubsection Non-immediate objects |
@subsubsection Non-immediate objects |
1145 |
|
|
1146 |
A Scheme object of type @code{SCM} that does not fullfill the |
A Scheme object of type @code{SCM} that does not fulfill the |
1147 |
@code{SCM_IMP} predicate holds an encoded reference to a heap cell. |
@code{SCM_IMP} predicate holds an encoded reference to a heap cell. |
1148 |
This reference can be decoded to a C pointer to a heap cell using the |
This reference can be decoded to a C pointer to a heap cell using the |
1149 |
@code{SCM2PTR} macro. The encoding of a pointer to a heap cell into a |
@code{SCM2PTR} macro. The encoding of a pointer to a heap cell into a |
1388 |
@menu |
@menu |
1389 |
* Describing a New Type:: |
* Describing a New Type:: |
1390 |
* Creating Instances:: |
* Creating Instances:: |
1391 |
* Typechecking:: |
* Type checking:: |
1392 |
* Garbage Collecting Smobs:: |
* Garbage Collecting Smobs:: |
1393 |
* A Common Mistake In Allocating Smobs:: |
* A Common Mistake In Allocating Smobs:: |
1394 |
* Garbage Collecting Simple Smobs:: |
* Garbage Collecting Simple Smobs:: |
1413 |
Guile will apply this function to each instance of the new type it could |
Guile will apply this function to each instance of the new type it could |
1414 |
not find any live pointers to. The function should release all |
not find any live pointers to. The function should release all |
1415 |
resources held by the object and return the number of bytes released. |
resources held by the object and return the number of bytes released. |
1416 |
This is analagous to the Java finalization method-- it is invoked at an |
This is analogous to the Java finalization method-- it is invoked at an |
1417 |
unspecified time (when garbage collection occurs) after the object is |
unspecified time (when garbage collection occurs) after the object is |
1418 |
dead. The default free function frees the smob data (if the size of the |
dead. The default free function frees the smob data (if the size of the |
1419 |
struct passed to @code{scm_make_smob_type} is non-zero) using |
struct passed to @code{scm_make_smob_type} is non-zero) using |
1499 |
to add a new smob type named @var{name}, with instance size @var{size} to the system. |
to add a new smob type named @var{name}, with instance size @var{size} to the system. |
1500 |
It also registers the @var{mark}, @var{free}, @var{print}, @var{equalp} smob |
It also registers the @var{mark}, @var{free}, @var{print}, @var{equalp} smob |
1501 |
special functions for that new type. Any of these parameters can be @code{NULL} |
special functions for that new type. Any of these parameters can be @code{NULL} |
1502 |
to have that special function use the default behaviour for guile. |
to have that special function use the default behavior for guile. |
1503 |
The return value is a tag that is used in creating instances of the type. If @var{size} |
The return value is a tag that is used in creating instances of the type. If @var{size} |
1504 |
is 0, then no memory will be allocated when instances of the smob are created, and |
is 0, then no memory will be allocated when instances of the smob are created, and |
1505 |
nothing will be freed by the default free function. |
nothing will be freed by the default free function. |
1506 |
@end deftypefun |
@end deftypefun |
1507 |
|
|
1508 |
For example, here is how one might declare and register a new type |
For example, here is how one might declare and register a new type |
1509 |
representing eight-bit grayscale images: |
representing eight-bit gray-scale images: |
1510 |
|
|
1511 |
@example |
@example |
1512 |
#include <libguile.h> |
#include <libguile.h> |
1646 |
@end example |
@end example |
1647 |
|
|
1648 |
|
|
1649 |
@node Typechecking |
@node Type checking |
1650 |
@subsection Typechecking |
@subsection Type checking |
1651 |
|
|
1652 |
Functions that operate on smobs should aggressively check the types of |
Functions that operate on smobs should aggressively check the types of |
1653 |
their arguments, to avoid misinterpreting some other datatype as a smob, |
their arguments, to avoid misinterpreting some other datatype as a smob, |