~ chicken-core (master) /manual/C interface


   1[[tags: manual]]
   2[[toc:]]
   3
   4
   5== C interface
   6
   7The following functions and macros are available for C code that invokes
   8Scheme or foreign procedures that are called by Scheme:
   9
  10=== Temporary stack
  11
  12==== C_save
  13
  14 [C macro] void C_save (C_word x) :
  15
  16Saves the Scheme data object {{x}} on the temporary stack.
  17
  18==== C_restore
  19
  20 [C macro] C_word C_restore
  21
  22Pops and returns the topmost value from the temporary stack.
  23
  24
  25=== Type/value predicates
  26
  27When writing C code that accepts Scheme objects you often need to do
  28checking what type of object is passed.  These can help you determine
  29the type of an object.
  30
  31==== C predicates
  32
  33These return regular C integer values (ie, zero is false, nonzero true).
  34
  35===== C_truep
  36
  37 [C macro] int C_truep(C_word x)
  38
  39Is {{x}} a truthy value, i.e. anything except {{C_SCHEME_FALSE}}?
  40
  41===== C_immediatep
  42
  43 [C macro] int C_immediatep(C_word x)
  44
  45Is {{x}} an immediate object?
  46(see below for a definition)
  47
  48===== C_fitsinfixnump
  49
  50 [C macro] int C_fitsinfixnump(int number)
  51
  52Will {{number}} fit in a fixnum?  It will fit when there is room for
  53one additional type bit to tag it as a fixnum (assuming one bit is
  54already used for the sign). In practice this means that the number's
  55top two bits must be identical.
  56
  57===== C_ufitsinfixnump
  58
  59 [C macro] int C_ufitsinfixnump(unsigned int number)
  60
  61Like {{C_fitsinfixnump}} but for unsigned integers.  This checks the
  62top ''two'' bits are zero, since fixnums '''always''' carry a sign.
  63
  64
  65==== Scheme predicates
  66
  67These return Scheme booleans (ie, C_SCHEME_TRUE or C_SCHEME_FALSE).
  68This means they can be used directly from Scheme using {{##core#inline}}.
  69
  70===== C_zero_length_p
  71
  72 [C macro] C_word C_zero_length_p(C_word x)
  73
  74Is {{x}} a Scheme object of zero length?  Only accepts non-immediate objects.
  75
  76===== C_unboundvaluep
  77
  78 [C macro] C_word C_unboundvaluep(C_word x)
  79
  80Is {{x}} the special unbound variable placeholder {{C_SCHEME_UNBOUND}}?
  81
  82===== C_boundp
  83
  84 [C macro] C_word C_boundp(C_word x)
  85
  86Is {{x}} a bound value?  Only accepts non-immediate objects.
  87
  88===== C_blockp
  89
  90 [C macro] C_word C_blockp(C_word x)
  91
  92Is {{x}} a "block" value?
  93
  94A "block" value is a value that contains a memory block, i.e. is not
  95an immediate value.
  96
  97===== C_immp
  98
  99 [C macro] C_word C_immp(C_word x)
 100
 101Is {{x}} an immediate value?
 102
 103===== C_forwardedp
 104
 105 [C macro] C_word C_forwardedp(C_word x)
 106
 107Is {{x}} a GC-forwarded object?
 108
 109===== C_flonump
 110
 111 [C macro] C_word C_flonump(C_word x)
 112
 113Is {{x}} a Scheme flonum object?  Accepts only non-immediate objects.
 114
 115===== C_stringp
 116
 117 [C macro] C_word C_stringp(C_word x)
 118
 119Is {{x}} a Scheme string object?  Accepts only non-immediate objects.
 120
 121===== C_symbolp
 122
 123 [C macro] C_word C_symbolp(C_word x)
 124
 125Is {{x}} a symbol?  Accepts only non-immediate objects.
 126
 127===== C_pairp
 128
 129 [C macro] C_word C_pairp(C_word x)
 130
 131Is {{x}} a pair?  Accepts only non-immediate objects.
 132
 133===== C_closurep
 134
 135 [C macro] C_word C_closurep(C_word x)
 136
 137Is {{x}} a closure?  Accepts only non-immediate objects.
 138
 139===== C_vectorp
 140
 141 [C macro] C_word C_vectorp(C_word x)
 142
 143Is {{x}} any kind of vector?  Accepts only non-immediate objects.
 144
 145This returns true for both regular heterogenous R7RS vectors and
 146bytevectors.  However, it does ''not'' return true for
 147SRFI-4 vectors, as those are actually bytevectors wrapped in a
 148structure with a type tag.
 149
 150===== C_bytevectorp
 151
 152 [C macro] C_word C_bytevectorp(C_word x)
 153
 154Is {{x}} a bytevector? Accepts only non-immediate objects.
 155
 156===== C_portp
 157
 158 [C macro] C_word C_portp(C_word x)
 159
 160Is {{x}} a port object?  Accepts only non-immediate objects.
 161
 162===== C_structurep
 163
 164 [C macro] C_word C_structurep(C_word x)
 165
 166Is {{x}} a structure (record) object?  Accepts only non-immediate objects.
 167
 168===== C_locativep
 169
 170 [C macro] C_word C_locativep(C_word x)
 171
 172Is {{x}} a locative object?  Accepts only non-immediate objects.
 173
 174===== C_charp
 175
 176 [C macro] C_word C_charp(C_word x)
 177
 178Is {{x}} a character object?
 179
 180===== C_booleanp
 181
 182 [C macro] C_word C_booleanp(C_word x)
 183
 184Is {{x}} a boolean object?
 185
 186===== C_eofp
 187
 188 [C macro] C_word C_eofp(C_word x)
 189
 190Is {{x}} the {{#!eof}} object?
 191
 192===== C_undefinedp
 193
 194 [C macro] C_word C_undefinedp(C_word x)
 195
 196Is {{x}} the undefined value?
 197
 198===== C_fixnump
 199
 200 [C macro] C_word C_fixnump(C_word x)
 201
 202Is {{x}} a fixnum object?
 203
 204===== C_nfixnump
 205
 206 [C macro] C_word C_nfixnump(C_word x)
 207
 208Is {{x}} ''not'' a fixnum object?
 209
 210===== C_bignump
 211
 212 [C macro] C_word C_bignump(C_word x)
 213
 214Is {{x}} a Scheme bignum object?  Accepts only non-immediate objects.
 215
 216===== C_i_numberp
 217
 218 [C function] C_word C_i_numberp(C_word x)
 219
 220Is {{x}} a number object (fixnum, bignum, flonum, ratnum, cplxnum)?
 221
 222===== C_i_bignump
 223
 224 [C function] C_word C_i_bignump(C_word x)
 225
 226Is {{x}} a Scheme bignum object?
 227
 228===== C_i_cplxnump
 229
 230 [C function] C_word C_i_cplxnump(C_word x)
 231
 232Is {{x}} a Scheme cplxnum object?
 233
 234===== C_i_ratnump
 235
 236 [C function] C_word C_i_ratnump(C_word x)
 237
 238Is {{x}} a Scheme ratnum object?
 239
 240===== C_i_flonump
 241
 242 [C function] C_word C_i_flonump(C_word x)
 243
 244Is {{x}} a flonum object?
 245
 246===== C_i_exact_integerp
 247
 248 [C macro] C_word C_i_exact_integerp(C_word x)
 249
 250Is {{x}} an exact integer (i.e., a fixnum or a bignum)?
 251
 252===== C_pointerp
 253
 254 [C macro] C_word C_pointerp(C_word x)
 255
 256Is {{x}} a C pointer object?  Only accepts non-immediate objects.
 257
 258===== C_taggedpointerp
 259
 260 [C macro] C_word C_taggedpointerp(C_word x)
 261
 262Is {{x}} a tagged pointer object?  Only accepts non-immediate objects.
 263
 264===== C_anypointerp
 265
 266 [C macro] C_word C_anypointerp(C_word x)
 267
 268Is {{x}} any type of pointer object?  Only accepts non-immediate objects.
 269
 270===== C_lambdainfop
 271
 272 [C macro] C_word C_lambdainfop(C_word x)
 273
 274Is {{x}} a lambda-info object?  Only accepts non-immediate objects.
 275
 276===== C_byteblockp
 277
 278 [C macro] C_word C_byteblockp(C_word x)
 279
 280Is {{x}} a "byteblock" object?  Only accepts non-immediate objects.
 281
 282Strings, flonums, bytevectors and lambda-info objects are considered
 283"byteblock" objects, as they are not containers for Scheme objects but
 284simply point to contiguous memory ranges of bytes.
 285
 286===== C_specialp
 287
 288 [C macro] C_word C_specialp(C_word x)
 289
 290Is {{x}} a "special" object?  Only accepts non-immediate objects.
 291
 292Closures, ports, pointers and locatives are considered "special"
 293objects, as they are not containers for Scheme objects (and they are
 294not byte blocks either), so they have to be treated specially by the GC.
 295
 296===== C_nullp
 297
 298 [C macro] C_word C_nullp(C_word x)
 299
 300Is {{x}} the empty list, i.e. is it {{C_SCHEME_END_OF_LIST}}?
 301
 302===== C_anyp
 303
 304 [C macro] C_word C_anyp(C_word x)
 305
 306Always returns {{C_SCHEME_TRUE}}.
 307
 308
 309=== Constructors
 310
 311==== Constructors for immediate Scheme objects
 312
 313"immediate" Scheme objects are objects that are represented directly
 314by a {{C_word}}.  There's no additional memory used by them.
 315
 316===== C_fix
 317
 318 [C macro] C_word C_fix (int integer)
 319
 320===== C_make_character
 321
 322 [C macro] C_word C_make_character (int char_code)
 323
 324===== C_mk_bool
 325
 326 [C macro] C_word C_mk_bool(int truth_value)
 327
 328===== C_mk_nbool
 329
 330 [C macro] C_word C_mk_nbool(int truth_value_to_negate)
 331
 332===== C_SCHEME_END_OF_LIST
 333
 334 [C macro] C_SCHEME_END_OF_LIST
 335
 336===== C_SCHEME_END_OF_FILE
 337
 338 [C macro] C_SCHEME_END_OF_FILE
 339
 340===== C_SCHEME_FALSE
 341
 342 [C macro] C_SCHEME_FALSE
 343
 344===== C_SCHEME_TRUE
 345
 346 [C macro] C_SCHEME_TRUE
 347
 348
 349==== Constructors for non-immediate Scheme objects
 350
 351Non-immediate Scheme objects are still represented and passed around
 352by a single {{C_word}}, but this is basically just a pointer to the
 353start of the object (which should never be treated as such, use the
 354accessor macros instead).
 355
 356===== C_string
 357
 358 [C function] C_word C_string (C_word **ptr, int length, char *string)
 359
 360===== C_string2
 361
 362 [C function] C_word C_string2 (C_word **ptr, char *zero_terminated_string)
 363
 364===== C_intern
 365
 366 [C function] C_word C_intern (C_word **ptr, int length, char *string)
 367
 368===== C_intern2
 369
 370 [C function] C_word C_intern2 (C_word **ptr, char *zero_terminated_string)
 371
 372===== C_intern3
 373
 374 [C function] C_word C_intern3 (C_word **ptr, char *zero_terminated_string, C_word initial_value)
 375
 376===== C_a_pair
 377
 378 [C function] C_word C_a_pair (C_word **ptr, C_word car, C_word cdr)
 379
 380===== C_flonum
 381
 382 [C function] C_word C_flonum (C_word **ptr, double number)
 383
 384===== C_int_to_num
 385
 386 [C function] C_word C_int_to_num (C_word **ptr, int integer)
 387
 388===== C_mpointer
 389
 390 [C function] C_word C_mpointer (C_word **ptr, void *pointer)
 391
 392===== C_vector
 393
 394 [C function] C_word C_vector (C_word **ptr, int length, ...)
 395
 396===== C_bytevector
 397
 398 [C function] C_word C_bytevector (C_word **ptr, int length, C_char *data)
 399
 400===== C_structure
 401
 402 [C function] C_word C_structure (C_word **ptr, int length, ...)
 403
 404===== C_list
 405
 406 [C function] C_word C_list (C_word **ptr, int length, ...)
 407
 408===== C_closure
 409
 410 [C function] C_word C_closure (C_word **ptr, int length, C_word procedure, ...)
 411
 412These functions allocate memory from {{ptr}} and initialize a fresh
 413data object. The new data object is returned. {{ptr}} should be the
 414'''address''' of an allocation pointer created with {{C_alloc}}.
 415
 416To find out how big the memory block should be, use the {{C_SIZEOF_*}}
 417macros described below.
 418
 419Here's an example how to create a closure that accepts a vector,
 420stores the Scheme numbers 1, 2 and 3 and a given string in it and
 421returns that vector to its continuation:
 422
 423<enscript highlight=scheme>
 424#>
 425
 426#include <assert.h>
 427
 428void fill_vector(C_word c, C_word *av)
 429{
 430  C_word closure = av[0];
 431  C_word kontinuation = C_block_item(closure, 1);
 432  C_word vec = av[1];
 433
 434  C_block_item(vec, 0) = C_fix(1);
 435  C_block_item(vec, 1) = C_fix(2);
 436  C_block_item(vec, 2) = C_fix(3);
 437  C_block_item(vec, 3) = C_block_item(closure, 2);
 438
 439  C_kontinue(kontinuation, vec);
 440}
 441
 442void one_two_three(C_word continuation, C_word str)
 443{
 444  /*
 445   * Allocate room on the stack to hold the closure:  1 word for
 446   * the type tag, 1 word for the procedure and 2 words for the
 447   * values "closed over"; this procedure's continuation "k" and
 448   * the argument "str".  We could also use C_alloc(4).
 449   */
 450  C_word closure[4], *cp = closure;
 451
 452  /* Allocate room for the argvector for C_allocate_vector */
 453  C_word av[6];
 454
 455  /* Create the closure.  It holds 3 values, not counting the tag */
 456  C_word closure_object = C_closure(&cp, 3, (C_word)fill_vector, continuation, str);
 457
 458  /*
 459   * After this, cp points just beyond the last word of the allocated
 460   * data and closure_object is an opaque representation of the newly
 461   * created closure as a whole, i.e. the following relations hold:
 462   */
 463  assert(  (closure + 4) == cp  );
 464  assert(  C_block_header(closure_object) == (*closure)  );
 465  assert(  C_data_pointer(closure_object) == (closure + 1)  );
 466  assert(  C_block_item(closure_object, 0) == (*(closure + 1))  );
 467
 468  /* Set up the arguments for C_allocate_vector */
 469  av[0] = (C_word)NULL;   /* Closure of allocate_vector - unused, so pass NULL */
 470  av[1] = closure_object; /* Continuation to call after allocating Scheme vector */
 471  av[2] = C_fix(4);       /* Size of Scheme vector to allocate */
 472  av[3] = C_SCHEME_FALSE; /* We want a regular vector, not a bytevector */
 473  av[4] = C_SCHEME_FALSE; /* Initialization value for slots.  Don't care */
 474  av[5] = C_SCHEME_FALSE; /* Do not align at 8 byte (64-bit word) boundary */
 475  /* Make a vector of 4 objects and use closure_object as continuation */
 476  C_allocate_vector(6, av);
 477  /* .. C_allocate_vector does not return ... */
 478}
 479<#
 480
 481
 482(define one-two-three
 483   (foreign-primitive ((scheme-object str)) "one_two_three(C_k, str);"))
 484
 485(print (one-two-three "hi"))
 486</enscript>
 487
 488This is equivalent to the following in Scheme:
 489
 490<enscript highlight=scheme>
 491(define (one-two-three str)
 492  (let ((fill-vector (lambda (vec)
 493                       (vector-set! vec 0 1)
 494                       (vector-set! vec 1 2)
 495                       (vector-set! vec 2 3)
 496                       (vector-set! vec 3 str)
 497                       vec)))
 498    (fill-vector (make-vector 4 #f))))
 499
 500(print (one-two-three "hi"))
 501</enscript>
 502
 503
 504==== Memory allocation
 505
 506These can be used to allocate memory for non-immediate objects.
 507
 508===== C_alloc
 509
 510 [C macro] C_word* C_alloc (int words)
 511
 512Allocates memory from the C stack ({{C_alloc}}) and returns a pointer to
 513it. {{words}} should be the number of words needed for all data
 514objects that are to be created in this function.  Note that stack-allocated
 515data objects have to be passed to Scheme callback functions, or they will
 516not be seen by the garbage collector. This is really only usable for
 517callback procedure invocations, make sure not to use it in normal code,
 518because the allocated memory will be re-used after the foreign procedure
 519returns. When invoking Scheme callback procedures a minor garbage
 520collection is performed, so data allocated with {{C_alloc}}
 521will already have moved to a safe place.
 522
 523Note that {{C_alloc}} is really just a wrapper around {{alloca}},
 524and can also be simulated by declaring a stack-allocated array of
 525{{C_word}}s:
 526
 527
 528===== C_SIZEOF_LIST
 529
 530 [C macro] int C_SIZEOF_LIST (int length)
 531
 532Returns the size in words needed for allocation of a list with ''length'' elements.
 533
 534===== C_SIZEOF_STRING
 535
 536 [C macro] int C_SIZEOF_STRING (int length)
 537
 538Returns the size in words needed for allocation of a string with ''length'' characters.
 539
 540===== C_SIZEOF_BYTEVECTOR
 541
 542 [C macro] int C_SIZEOF_BYTEVECTOR (int length)
 543
 544Returns the size in words needed for allocation of a bytevector with ''length'' bytes of data.
 545
 546===== C_SIZEOF_VECTOR
 547
 548 [C macro] int C_SIZEOF_VECTOR (int length)
 549
 550Returns the size in words needed for allocation of vector with ''length'' elements.
 551
 552===== C_SIZEOF_CLOSURE
 553
 554 [C macro] int C_SIZEOF_CLOSURE (int length)
 555
 556Returns the size in words needed for allocation of a closure with {{length}} slots.  The C function pointer also counts as a slot, so always remember to include it when calculating {{length}}.
 557
 558===== C_SIZEOF_STRUCT
 559
 560 [C macro] int C_SIZEOF_STRUCT (int length)
 561
 562Returns the size in words needed for allocation of a structure (record type) object with {{length}} slots.  The structure's type tag also counts as a slot, so always remember to include it when calculating {{length}}.
 563
 564===== C_SIZEOF_BIGNUM
 565
 566 [C macro] int C_SIZEOF_BIGNUM (int length)
 567
 568Returns the size in words needed for allocation of a bignum object with {{length}} word-sized digits (limbs).
 569
 570===== C_SIZEOF_FIX_BIGNUM
 571
 572 [C macro] int C_SIZEOF_FIX_BIGNUM
 573
 574The size in words needed for allocation of a bignum object which is large enough to store any fixnum (ie, if it were converted to a denormalized bignum, because if a number ''can'' be represented as a fixnum, it ''will'' be).
 575
 576===== C_SIZEOF_INTERNED_SYMBOL
 577
 578 [C macro] int C_SIZEOF_INTERNED_SYMBOL (int length)
 579
 580===== C_SIZEOF_PAIR
 581
 582 [C macro] int C_SIZEOF_PAIR
 583
 584===== C_SIZEOF_FLONUM
 585
 586 [C macro] int C_SIZEOF_FLONUM
 587
 588===== C_SIZEOF_POINTER
 589
 590 [C macro] int C_SIZEOF_POINTER
 591
 592===== C_SIZEOF_LOCATIVE
 593
 594 [C macro] int C_SIZEOF_LOCATIVE
 595
 596===== C_SIZEOF_TAGGED_POINTER
 597
 598 [C macro] int C_SIZEOF_TAGGED_POINTER
 599
 600These are macros that return the size in words needed for a data object
 601of a given type.
 602
 603=== Accessors
 604
 605==== C_character_code
 606
 607 [C macro] int C_character_code (C_word character)
 608
 609==== C_unfix
 610
 611 [C macro] int C_unfix (C_word fixnum)
 612
 613==== C_flonum_magnitude
 614
 615 [C macro] double C_flonum_magnitude (C_word flonum)
 616
 617==== C_c_string
 618
 619 [C function] char* C_c_string (C_word string)
 620
 621==== C_num_to_int
 622
 623 [C function] int C_num_to_int (C_word fixnum_or_bignum)
 624
 625==== C_pointer_address
 626
 627 [C function] void* C_pointer_address (C_word pointer)
 628
 629These macros and functions can be used to convert Scheme data objects
 630back to C data. Note that {{C_c_string()}} takes a bytevector and returns
 631a pointer to the contents as a character pointer. If the bytevector is the
 632character sequence obtained from a string, then it is implicitly
 633zero-terminated.
 634
 635==== C_header_size
 636
 637 [C macro] int C_header_size (C_word x)
 638
 639==== C_header_bits
 640
 641 [C macro] int C_header_bits (C_word x)
 642
 643Return the number of elements and the type-bits of the non-immediate
 644Scheme data object {{x}}.
 645
 646
 647==== C_block_item
 648
 649 [C macro] C_word C_block_item (C_word x, int index)
 650
 651This macro can be used to access slots of the non-immediate Scheme data
 652object {{x}}.  {{index}} specifies the index of the slot to
 653be fetched, starting at 0. Pairs have 2 slots, one for the '''car'''
 654and one for the '''cdr'''. Vectors have one slot for each element.
 655
 656
 657==== C_u_i_car
 658
 659 [C macro] C_word C_u_i_car (C_word x)
 660
 661==== C_u_i_cdr
 662
 663 [C macro] C_word C_u_i_cdr (C_word x)
 664
 665Aliases for {{C_block_item(x, 0)}} and {{C_block_item(x, 1)}}, respectively.
 666
 667==== C_port_file
 668
 669 [C macro] C_word C_port_file (C_word x)
 670
 671Alias for {{(FILE *)C_block_item(x, 0)}}.  To be used with port
 672objects representing files (but will not work on sockets, for example).
 673
 674
 675==== C_data_pointer
 676
 677 [C macro] void* C_data_pointer (C_word x)
 678
 679Returns a pointer to the data-section of a non-immediate Scheme object.
 680
 681
 682=== C_make_header
 683
 684 [C macro] C_word C_make_header (C_word bits, C_word size)
 685
 686A macro to build a Scheme object header from its bits and size parts.
 687
 688
 689=== C_mutate
 690
 691 [C function] C_word C_mutate (C_word *slot, C_word val)
 692
 693Assign the Scheme value {{val}} to the location specified by
 694{{slot}}.  If the value points to data inside the nursery (the first
 695heap-generation), then the garbage collector will remember to handle the
 696data appropriately. Assigning nursery-pointers directly will otherwise
 697result in lost data. Note that no copying takes place at the moment
 698when {{C_mutate}} is called, but later - at the next (minor) garbage
 699collection.
 700
 701
 702=== C_symbol_value
 703
 704 [C macro] C_word C_symbol_value (C_word symbol)
 705
 706Returns the global value of the variable with the name {{symbol}}. If the
 707variable is unbound {{C_SCHEME_UNBOUND}} is returned. You can set a variable's
 708value with {{C_mutate(&C_symbol_value(SYMBOL), VALUE)}}.
 709
 710
 711=== GC interface
 712
 713==== C_gc_protect
 714
 715 [C function] void C_gc_protect (C_word *ptrs[], int n)
 716
 717Registers {{n}} variables at address {{ptrs}} to be garbage collection roots.
 718The locations should not contain pointers to data allocated in the nursery, only
 719immediate values or pointers to heap-data are valid. Any
 720assignment of potential nursery data into a root-array should be done
 721via {{C_mutate()}}. The variables have to be initialized to sensible values
 722before the next garbage collection starts (when in doubt, set all locations
 723in {{ptrs}} to {{C_SCHEME_UNDEFINED}})
 724{{C_gc_protect}} may not called before the runtime system has been
 725initialized (either by {{CHICKEN_initialize}}, {{CHICKEN_run}} or
 726{{CHICKEN_invoke}}.
 727
 728For a slightly simpler interface to creating and using GC roots see
 729{{CHICKEN_new_gc_root}}.
 730
 731
 732==== C_gc_unprotect
 733
 734 [C function] void C_gc_unprotect (int n)
 735
 736Removes the last {{n}} registered variables from the set of
 737root variables.
 738
 739
 740==== C_pre_gc_hook
 741
 742 [C Variable] void (*C_pre_gc_hook)(int mode)
 743
 744If not {{NULL}}, the function pointed to by this variable will be
 745called before each garbage collection with a flag indicating what kind
 746of collection was performed (either {{0}} for a minor or major
 747collection or {{2}} for a resizing collection). A "resizing"
 748collection means a secondary collection that moves all live data into
 749a enlarged (or shrinked) heap-space. Minor collections happen very
 750frequently, so the hook function should not consume too much time. The
 751hook function may not invoke Scheme callbacks.
 752
 753Note that resizing collections may be nested in normal major collections.
 754
 755==== C_post_gc_hook
 756
 757 [C Variable] void (*C_post_gc_hook)(int mode, long ms)
 758
 759If not {{NULL}}, the function pointed to by this variable will be
 760called after each garbage collection with a flag indicating what kind
 761of collection was performed (either {{0}} for a minor collection,
 762{{1}} for a major collection or {{2}} for a resizing
 763collection). Minor collections happen very frequently, so the hook
 764function should not consume too much time. The hook function may not
 765invoke Scheme callbacks. The {{ms}} argument records the number of
 766milliseconds required for the garbage collection, if the collection
 767was a major one. For minor collections the value of the {{ms}} argument
 768is undefined.
 769
 770=== Type-specific macros and functions
 771
 772The following are macros and functions to ask information or perform
 773operations on objects once their types are already known.  If you call
 774it on any object of another type, it is not defined what will happen
 775and likely your program will crash, especially if you pass immediates
 776to procedures expecting non-immediates.
 777
 778==== Vectors
 779
 780===== C_vemptyp
 781
 782 [C macro] C_word C_vemptyp(C_word v)
 783
 784Is the (byte- or heterogenous) vector {{v}} empty?
 785
 786===== C_notvemptyp
 787
 788 [C macro] C_word C_notvemptyp(C_word v)
 789
 790Is the (byte- or heterogenous) vector {{v}} nonempty?
 791
 792==== Numbers
 793
 794These procedures accept any type of number, so you can pass in a
 795fixnum, a flonum, a bignum, a ratnum or a cplxnum.  You shouldn't pass
 796in another type though, since that could crash your program.
 797
 798===== C_u_i_exactp
 799
 800 [C macro] C_word C_u_i_exactp(C_word x)
 801
 802Is {{x}} an exact number (i.e., a fixnum, bignum, ratnum or exact cplxnum)?
 803
 804===== C_u_i_inexactp
 805
 806 [C macro] C_word C_u_i_inexactp(C_word x)
 807
 808Is {{x}} an inexact number (i.e., a flonum or an inexact cplxnum)?
 809
 810===== C_i_finitep
 811
 812 [C function] C_word C_i_finitep(C_word x)
 813
 814Is {{x}} a finite number?  This returns false only when {{x}} is a
 815flonum representing {{-inf}} or {{+inf}}.
 816
 817==== Bignums
 818
 819===== C_bignum_negativep
 820
 821 [C macro] int C_bignum_negativep(C_word b)
 822
 823Returns nonzero if the bignum {{b}} is negative, zero if it is not.
 824
 825===== C_bignum_digits
 826
 827 [C macro] C_uword *C_bignum_digits(C_word b)
 828
 829Returns a pointer to the first digit (the least significant one) of the bignum {{b}}.
 830
 831===== C_bignum_size
 832
 833 [C macro] C_word C_bignum_size(b)
 834
 835Returns the number of digits in the bignum {{b}}, as an unboxed C number.  If you want a fixnum, use {{C_u_i_bignum_size}}.
 836
 837===== C_u_i_bignum_size
 838
 839 [C macro] C_word C_u_i_bignum_size(b)
 840
 841Returns the number of digits in the bignum {{b}}, as a Scheme fixnum.  If you want an unboxed integer, use {{C_bignum_size}}.
 842
 843===== C_i_bignum_cmp
 844
 845 [C macro] C_word C_i_bignum_cmp(x, y)
 846
 847Compares the bignums {{x}} and {{y}} and returns the fixnums {{-1}}, {{0}} or {{1}} if {{x}} is less than, equal to or greater than {{y}}, respectively.
 848
 849==== Fixnums
 850
 851Note: Fixnums are immediates, so there is no {{C_fixnum_equalp}}
 852macro.  You can just compare them without hassle (or use
 853[[#c-eqp|C_eqp]] if you prefer).
 854
 855===== C_i_fixnumevenp
 856
 857 [C macro] C_word C_i_fixnumevenp(C_word x)
 858
 859Is {{x}} an even fixnum?
 860
 861===== C_i_fixnumoddp
 862
 863 [C macro] C_word C_i_fixnumoddp(C_word x)
 864
 865Is {{x}} an odd fixnum?
 866
 867===== C_fixnum_times
 868
 869 [C macro] C_word C_fixnum_times(C_word n1, C_word n2)
 870
 871Multiply fixnum n1 by fixnum n2.  Will not overflow into a bignum, but
 872will handle overflows safely in the sense that it always produces a
 873fixnum.
 874
 875===== C_a_i_fixnum_times
 876
 877 [C macro] C_word C_a_i_fixnum_times(C_word **ptr, C_word n, C_word x, C_word y)
 878
 879Calculate {{x}} * {{y}}, safely overflowing into a bignum, using the
 880storage in {{ptr}} (which should be at least {{C_SIZEOF_BIGNUM(2)}}).
 881
 882
 883===== C_fixnum_plus
 884
 885 [C macro] C_word C_fixnum_plus(C_word n1, C_word n2)
 886
 887Add fixnum {{n1}} to fixnum {{n2}}.  Will not overflow into a bignum,
 888but will handle overflows safely in the sense that it always produces
 889a fixnum.
 890
 891===== C_u_fixnum_plus
 892
 893 [C macro] C_word C_u_fixnum_plus(C_word n1, C_word n2)
 894
 895Like {{C_fixnum_plus}}, but unsafe (assumes no overflow/underflow).
 896
 897===== C_a_i_fixnum_plus
 898
 899 [C macro] C_word C_a_i_fixnum_plus(C_word **ptr, C_word n, C_word x, C_word y)
 900
 901Calculate {{x}} + {{y}}, safely overflowing into a bignum, using the
 902storage in {{ptr}} (which should be at least {{C_SIZEOF_FIX_BIGNUM}}).
 903
 904===== C_fixnum_difference
 905
 906 [C macro] C_word C_fixnum_difference(C_word n1, C_word n2)
 907
 908Calculate {{n1}} - {{n2}}.  Will not overflow into a bignum, but will
 909handle overflows safely in the sense that it always produces a fixnum.
 910
 911===== C_u_fixnum_difference
 912
 913 [C macro] C_word C_u_fixnum_difference(C_word n1, C_word n2)
 914
 915Like {{C_fixnum_difference}}, but unsafe (assumes no overflow/underflow).
 916
 917===== C_a_i_fixnum_difference
 918
 919 [C macro] C_word C_a_i_fixnum_difference(C_word **ptr, C_word n, C_word x, C_word y)
 920
 921Calculate {{x}} - {{y}}, safely overflowing into a bignum, using the
 922storage in {{ptr}} (which should be at least {{C_SIZEOF_FIX_BIGNUM}}).
 923
 924
 925===== C_fixnum_divide
 926===== C_u_fixnum_divide
 927
 928 [C macro] C_word C_fixnum_divide(C_word n1, C_word n2)
 929 [C macro] C_word C_u_fixnum_divide(C_word n1, C_word n2)
 930
 931Divide {{n1}} by {{n2}}, returning the quotient (i.e., integer
 932division). {{C_fixnum_divide}} signals an error if {{n2}} is zero.
 933
 934===== C_fixnum_modulo
 935===== C_u_fixnum_modulo
 936
 937 [C macro] C_word C_fixnum_modulo(C_word n1, C_word n2)
 938 [C macro] C_word C_u_fixnum_modulo(C_word n1, C_word n2)
 939
 940Calculate {{n1}} modulo {{n2}}. {{C_fixnum_modulo}} signals an error if {{n2}} is zero.  Neither handles overflow into bignums.
 941
 942===== C_a_i_fixnum_quotient_checked
 943
 944 [C macro] C_word C_a_i_fixnum_quotient_checked(C_word **ptr, C_word n, C_word x, C_word y)
 945
 946Calculate integer division of {{x}} / {{y}}, safely overflowing into a
 947bignum (which can happen when dividing by {{C_MOST_NEGATIVE_FIXNUM}}),
 948using the storage in {{ptr}} (which should be at least
 949{{C_SIZEOF_FIX_BIGNUM}}).  If {{y}} is zero, this will signal an
 950error.
 951
 952===== C_i_fixnum_remainder_checked
 953
 954 [C macro] C_word C_i_fixnum_remainder_checked(C_word x, C_word y)
 955
 956Calculate the remainder of integer division {{x}} / {{y}}.  If {{y}}
 957is zero, this will signal an error.
 958
 959
 960===== C_fixnum_and
 961
 962 [C macro] C_word C_fixnum_and(C_word n1, C_word n2)
 963
 964Calculate the bitwise {{AND}} of the integral values of {{n1}} and {{n2}}.
 965
 966===== C_u_fixnum_and
 967
 968 [C macro] C_word C_u_fixnum_and(C_word n1, C_word n2)
 969
 970Like {{C_fixnum_and}}, but unsafe.
 971
 972===== C_fixnum_or
 973
 974 [C macro] C_word C_fixnum_or(C_word n1, C_word n2)
 975
 976Calculate the bitwise {{OR}} of the integral values of {{n1}} and {{n2}}.
 977
 978===== C_u_fixnum_or
 979
 980 [C macro] C_word C_u_fixnum_or(C_word n1, C_word n2)
 981
 982Like {{C_fixnum_or}}, but unsafe.
 983
 984===== C_fixnum_xor
 985
 986 [C macro] C_word C_fixnum_xor(C_word n1, C_word n2)
 987
 988Calculate the bitwise {{XOR}} of the integral values of {{n1}} and {{n2}}.
 989
 990===== C_fixnum_not
 991
 992 [C macro] C_word C_fixnum_not(C_word n)
 993
 994Calculate the bitwise {{NOT}} (inversion of bits) of the integral
 995value of {{n}}.
 996
 997===== C_fixnum_shift_left
 998
 999 [C macro] C_word C_fixnum_shift_left(C_word n1, C_word n2)
 1000
1001Shift the integral value of {{n1}} left by {{n2}} positions.
1002
1003===== C_fixnum_shift_right
1004
1005 [C macro] C_word C_fixnum_shift_right(C_word n1, C_word n2)
1006
1007Shift the integral value of {{n1}} right by {{n2}}
1008
1009===== C_fixnum_negate
1010
1011 [C macro] C_word C_fixnum_negate(C_word n)
1012
1013Negate {{n}}, i.e. return {{-n}}.  This will ''not'' overflow into a
1014bignum.
1015
1016===== C_a_i_fixnum_negate
1017
1018 [C macro] C_word C_a_i_fixnum_negate(C_word **ptr, C_word n, C_word x)
1019
1020Negate {{n}}, i.e. return {{-n}}.  This will not overflow into a
1021bignum, using the storage pointed to by {{ptr}}, which should at least
1022be {{C_SIZEOF_FIX_BIGNUM}}.
1023
1024
1025===== C_fixnum_greaterp
1026
1027 [C macro] C_word C_fixnum_greaterp(C_word n1, C_word n2)
1028
1029Returns {{C_SCHEME_TRUE}} when {{n1}} is greater than {{n2}},
1030{{C_SCHEME_FALSE}} if not.
1031
1032===== C_fixnum_greater_or_equal_p
1033
1034 [C macro] C_word C_fixnum_greater_or_equalp(C_word n1, C_word n2)
1035
1036Returns {{C_SCHEME_TRUE}} when {{n1}} is greater than or equal to
1037{{n2}}, {{C_SCHEME_FALSE}} if not.
1038
1039===== C_fixnum_lessp
1040
1041 [C macro] C_word C_fixnum_lessp(C_word n1, C_word n2)
1042
1043Returns {{C_SCHEME_TRUE}} when {{n1}} is less than {{n2}},
1044{{C_SCHEME_FALSE}} if not.
1045
1046===== C_fixnum_less_or_equal_p
1047
1048 [C macro] C_word C_fixnum_less_or_equalp(C_word n1, C_word n2)
1049
1050Returns {{C_SCHEME_TRUE}} when {{n1}} is less than or equal to
1051{{n2}}, {{C_SCHEME_FALSE}} if not.
1052
1053===== C_i_fixnum_positivep
1054
1055 [C macro] C_word C_i_fixnum_positivep(C_word n)
1056
1057Returns {{C_SCHEME_TRUE}} when {{n}} is a positive fixnum,
1058{{C_SCHEME_FALSE}} if it is zero or negative.
1059
1060===== C_i_fixnum_negativep
1061
1062 [C macro] C_word C_i_fixnum_negativep(C_word n)
1063
1064Returns {{C_SCHEME_TRUE}} when {{n}} is a negative fixnum,
1065{{C_SCHEME_FALSE}} if it is zero or positive.
1066
1067===== C_fixnum_increase
1068
1069 [C macro] C_word C_fixnum_increase(C_word n)
1070
1071Adds 1 to {{n}}
1072
1073===== C_u_fixnum_increase
1074
1075 [C macro] C_word C_u_fixnum_increase(C_word n)
1076
1077As {{C_fixnum_increase}}, but unsafe (assumes the result will not overflow).
1078
1079===== C_fixnum_decrease
1080
1081 [C macro] C_word C_fixnum_decrease(C_word n)
1082
1083Subtracts 1 from {{n}}
1084
1085===== C_u_fixnum_decrease
1086
1087 [C macro] C_word C_u_fixnum_decrease(C_word n)
1088
1089As {{C_fixnum_increase}}, but unsafe (assumes the result will not underflow).
1090
1091===== C_fixnum_abs
1092
1093 [C macro] C_word C_fixnum_abs(C_word n)
1094
1095Returns the absolute value of {{n}}.
1096
1097===== C_i_fixnum_min
1098
1099 [C function] C_word C_i_fixnum_min(C_word n1, C_word n2)
1100
1101Returns the smallest of the two fixnums {{n1}} and {{n2}}.
1102
1103===== C_i_fixnum_max
1104
1105 [C function] C_word C_i_fixnum_max(C_word n1, C_word n2)
1106
1107Returns the largest of the two fixnums {{n1}} and {{n2}}.
1108
1109===== C_i_fixnum_gcd
1110
1111 [C function] C_word C_i_fixnum_gcd(C_word n1, C_word n2)
1112
1113Returns the greatest common divisor of the two fixnums {{n1}} and {{n2}}.
1114
1115===== C_i_fixnum_length
1116
1117 [C function] C_word C_i_fixnum_length(C_word x)
1118
1119Returns the integer length in bits of the fixnum {{x}} (as a fixnum).
1120
1121
1122==== Flonums
1123
1124===== C_flonum_equalp
1125
1126 [C macro] C_word C_flonum_equalp(C_word n1, C_word n2)
1127
1128Returns {{C_SCHEME_TRUE}} when {{n1}} and {{n2}} are equal flonums,
1129{{C_SCHEME_FALSE}} otherwise.
1130
1131===== C_flonum_greaterp
1132
1133 [C macro] C_word C_flonum_greaterp(C_word n1, C_word n2)
1134
1135Returns {{C_SCHEME_TRUE}} when {{n1}} is greater than {{n2}},
1136{{C_SCHEME_FALSE}} if not.
1137
1138===== C_flonum_greater_or_equal_p
1139
1140 [C macro] C_word C_flonum_greater_or_equal_p(C_word n1, C_word n2)
1141
1142Returns {{C_SCHEME_TRUE}} when {{n1}} is greater than or equal to {{n2}},
1143{{C_SCHEME_FALSE}} if not.
1144
1145===== C_flonum_lessp
1146
1147 [C macro] C_word C_flonum_lessp(C_word n1, C_word n2)
1148
1149Returns {{C_SCHEME_TRUE}} when {{n1}} is less than {{n2}},
1150{{C_SCHEME_FALSE}} if not.
1151
1152===== C_flonum_less_or_equal_p
1153
1154 [C macro] C_word C_flonum_less_or_equal_p(C_word n1, C_word n2)
1155
1156Returns {{C_SCHEME_TRUE}} when {{n1}} is less than or equal to {{n2}},
1157{{C_SCHEME_FALSE}} if not.
1158
1159===== C_a_i_flonum_plus
1160
1161 [C macro] C_word C_a_i_flonum_plus(C_word **ptr, int c, C_word n1, C_word n2)
1162
1163Adds the flonum {{n1}} to the flonum {{n2}}, using the storage at
1164{{ptr}}.  {{c}} should always be 2.
1165
1166Example:
1167
1168<enscript highlight=scheme>
1169#include <chicken.h>
1170#include <stdio.h>
1171
1172int main(void)
1173{
1174  C_word *mema, *memb, *memresult;
1175  C_word a, b, result;
1176
1177  mema = C_alloc(C_SIZEOF_FLONUM);
1178  memb = C_alloc(C_SIZEOF_FLONUM);
1179  memresult = C_alloc(C_SIZEOF_FLONUM);
1180  a = C_flonum(&mema, 1.2);
1181  b = C_flonum(&memb, 4.7);
1182  result = C_a_i_flonum_plus(&memresult, 2, a, b);
1183  printf("%lf\n", C_flonum_magnitude(result));
1184  return 0;
1185}
1186</enscript>
1187
1188This will print {{5.9}}
1189
1190===== C_a_i_flonum_difference
1191
1192 [C macro] C_word C_a_i_flonum_difference(C_word **ptr, int c, C_word n1, C_word n2)
1193
1194Subtracts the flonum {{n2}} from the flonum {{n1}}, using the storage at
1195{{ptr}}.  {{c}} should always be 2.
1196
1197===== C_a_i_flonum_times
1198
1199 [C macro] C_word C_a_i_flonum_times(C_word **ptr, int c, C_word n1, C_word n2)
1200
1201Multiplies the flonum {{n1}} by the flonum {{n2}}, using the storage at
1202{{ptr}}.  {{c}} should always be 2.
1203
1204===== C_a_i_flonum_quotient
1205
1206 [C macro] C_word C_a_i_flonum_quotient(C_word **ptr, int c, C_word n1, C_word n2)
1207 [C macro] C_word C_a_i_flonum_quotient_checked(C_word **ptr, int c, C_word n1, C_word n2)
1208
1209These are misnamed because they don't calculate the Scheme "quotient",
1210but the simple result of flonum {{n1}} divided by the flonum {{n2}},
1211using the storage at {{ptr}}.  {{c}} should always be 2.
1212
1213{{C_a_i_flonum_quotient_checked}} will signal an error if {{n2}} is zero.
1214
1215===== C_a_i_flonum_actual_quotient_checked
1216
1217 [C macro] C_word C_a_i_flonum_actual_quotient_checked(C_word **ptr, int c, C_word n1, C_word n2)
1218
1219Due to the misnaming of {{C_a_i_flonum_quotient[_checked]}}, this
1220function has a peculiar name.  It calculates the Scheme integer
1221quotient of {{n1}} divided by {{n2}}, using the storage at {{ptr}}.
1222{{c}} should always be 2.
1223
1224If {{n2}} is zero or either of the numbers is not an integral flonum,
1225an error will be signaled.
1226
1227===== C_a_i_flonum_gcd
1228
1229 [C macro] C_word C_a_i_flonum_gcd(C_word **ptr, int c, C_word n1, C_word n2)
1230
1231Calculates the greatest common divisor of the flonums {{n1}} and
1232{{n2}}, using the storage at {{ptr}}.  {{c}} should always be 2.
1233
1234===== C_a_i_flonum_negate
1235
1236 [C macro] C_word C_a_i_flonum_negate(C_word **ptr, int c, C_word n)
1237
1238Negates the flonum {{n}}, using the storage at {{ptr}}. {{c}} should
1239always be 1.
1240
1241===== C_a_i_flonum_truncate
1242
1243 [C macro] C_word C_a_i_flonum_truncate(C_word **ptr, int c, C_word n)
1244
1245Truncate the flonum {{n}}, using the storage at {{ptr}}. {{c}} should
1246always be 1.
1247
1248===== C_a_i_flonum_ceiling
1249
1250 [C macro] C_word C_a_i_flonum_ceiling(C_word **ptr, int c, C_word n)
1251
1252Round the flonum {{n}}, rounding upwards, using the storage at
1253{{ptr}}. {{c}} should always be 1.
1254
1255===== C_a_i_flonum_floor
1256
1257 [C macro] C_word C_a_i_flonum_floor(C_word **ptr, int c, C_word n)
1258
1259Round the flonum {{n}}, rounding downwards, using the storage at
1260{{ptr}}. {{c}} should always be 1.
1261
1262===== C_a_i_flonum_round
1263
1264 [C macro] C_word C_a_i_flonum_round(C_word **ptr, int c, C_word n)
1265
1266Round the flonum {{n}}, rounding towards the nearest integer, using
1267the storage at {{ptr}}. {{c}} should always be 1.
1268
1269This macro returns the value like returned by C's {{round()}}
1270function.  That means it rounds to the larger value (away from 0) when
1271rounding numbers halfway between two integers.
1272
1273===== C_a_i_flonum_round_proper
1274
1275 [C macro] C_word C_a_i_flonum_round_proper(C_word **ptr, int c, C_word n)
1276
1277Round the flonum {{n}}, rounding towards the nearest integer, using
1278the storage at {{ptr}}. {{c}} should always be 1.
1279
1280This macro returns the value like returned by Scheme's {{round}}
1281procedure.  That means it rounds to even numbers when rounding
1282numbers halfway between two integers.
1283
1284===== C_a_i_flonum_sin
1285
1286 [C macro] C_word C_a_i_flonum_sin(C_word **ptr, int c, C_word n)
1287
1288Calculates the sine of {{n}} (in radians).
1289
1290===== C_a_i_flonum_cos
1291
1292 [C macro] C_word C_a_i_flonum_cos(C_word **ptr, int c, C_word n)
1293
1294Calculates the cosine of {{n}} (in radians).
1295
1296===== C_a_i_flonum_tan
1297
1298 [C macro] C_word C_a_i_flonum_tan(C_word **ptr, int c, C_word n)
1299
1300Calculates the tangent of {{n}} (in radians).
1301
1302===== C_a_i_flonum_asin
1303
1304 [C macro] C_word C_a_i_flonum_asin(C_word **ptr, int c, C_word n)
1305
1306Calculates the arc sine of {{n}} (in radians, in the range -pi/2 through +pi/2).
1307
1308===== C_a_i_flonum_acos
1309
1310 [C macro] C_word C_a_i_flonum_acos(C_word **ptr, int c, C_word n)
1311
1312Calculates the arc cosine of {{n}} (in radians, in the range 0 through pi).
1313
1314===== C_a_i_flonum_atan
1315
1316 [C macro] C_word C_a_i_flonum_atan(C_word **ptr, int c, C_word n)
1317
1318Calculates the arc tangent of {{n}} (in radians, in the range -pi/2 through +pi/2).
1319
1320Like C's {{atan()}} or Scheme's unary {{atan}}.
1321
1322===== C_a_i_flonum_atan2
1323
1324 [C macro] C_word C_a_i_flonum_atan2(C_word **ptr, int c, C_word n1, C_word n2)
1325
1326Calculates the arc tangent of {{n1/n2}} (in radians), using the sign of both
1327to determine the quadrant of the result.
1328
1329Like C's {{atan2()}} or Scheme's binary {{atan}}.
1330
1331===== C_a_i_flonum_log
1332
1333 [C macro] C_word C_a_i_flonum_log(C_word **ptr, int c, C_word n)
1334
1335Calculate the natural (base {{e}}) logarithm of {{n}}.
1336
1337===== C_a_i_flonum_exp
1338
1339 [C macro] C_word C_a_i_flonum_exp(C_word **ptr, int c, C_word n)
1340
1341Calculates the base {{e}} exponent of {{n}} (i.e., the inverse
1342operation of {{C_a_i_flonum_log}}).
1343
1344===== C_a_i_flonum_expt
1345
1346 [C macro] C_word C_a_i_flonum_expt(C_word **ptr, int c, C_word n1, C_word n2)
1347
1348Calculates {{n1}} raised to the power {{n2}}.
1349
1350===== C_a_i_flonum_sqrt
1351
1352 [C macro] C_word C_a_i_flonum_sqrt(C_word **ptr, int c, C_word n)
1353
1354Calculates the square root of {{n}}.
1355
1356===== C_a_i_flonum_abs
1357
1358 [C macro] C_word C_a_i_flonum_abs(C_word **ptr, int c, C_word n)
1359
1360Calculates the absolute value of {{n}}.
1361
1362===== C_u_i_flonum_nanp
1363
1364 [C macro] C_word C_u_i_flonum_nanp(C_word n)
1365
1366Is {{n}} a flonum NaN value?
1367
1368===== C_u_i_flonum_finitep
1369
1370 [C macro] C_word C_u_i_flonum_finitep(C_word n)
1371
1372Is {{n}} a finite flonum (i.e., not NaN or one of the infinities)?
1373
1374===== C_u_i_flonum_infinitep
1375
1376 [C macro] C_word C_u_i_flonum_infinitep(C_word n)
1377
1378Is {{n}} an infinite flonum?
1379
1380==== Exact integers
1381
1382Often you know a value is an integer, but you don't know whether it's
1383a fixnum or a bignum.  In those cases, there are some optimized C
1384functions and macros to perform operations on them.
1385
1386===== C_i_integer_evenp
1387
1388 [C macro] C_word C_i_integer_evenp(C_word n)
1389
1390Returns {{C_SCHEME_TRUE}} when {{n}} is an even fixnum or bignum,
1391{{C_SCHEME_FALSE}} if it is odd.
1392
1393===== C_i_integer_oddp
1394
1395 [C macro] C_word C_i_integer_oddp(C_word n)
1396
1397Returns {{C_SCHEME_TRUE}} when {{n}} is an odd fixnum or bignum,
1398{{C_SCHEME_FALSE}} if it is even.
1399
1400===== C_i_integer_positivep
1401
1402 [C macro] C_word C_i_integer_positivep(C_word n)
1403
1404Returns {{C_SCHEME_TRUE}} when {{n}} is a positive fixnum or bignum,
1405{{C_SCHEME_FALSE}} if it is zero or negative.
1406
1407===== C_i_integer_negativep
1408
1409 [C macro] C_word C_i_integer_negativep(C_word n)
1410
1411Returns {{C_SCHEME_TRUE}} when {{n}} is a negative fixnum or bignum,
1412{{C_SCHEME_FALSE}} if it is zero or positive.
1413
1414===== C_i_integer_equalp
1415
1416 [C macro] C_word C_i_integer_equalp(x, y)
1417
1418Returns {{C_SCHEME_TRUE}} when {{x}} and {{y}} are numerically equal,
1419{{C_SCHEME_FALSE}} if they differ.
1420
1421===== C_i_integer_greaterp
1422
1423 [C macro] C_word C_i_integer_greaterp(x, y)
1424
1425Returns {{C_SCHEME_TRUE}} when {{x}} is greater than {{y}},
1426{{C_SCHEME_FALSE}} if it is equal or less.
1427
1428===== C_i_integer_greater_or_equalp
1429
1430 [C macro] C_word C_i_integer_greaterp(x, y)
1431
1432Returns {{C_SCHEME_TRUE}} when {{x}} is greater than or equal to {{y}},
1433{{C_SCHEME_FALSE}} if it is less.
1434
1435===== C_i_integer_lessp
1436
1437 [C macro] C_word C_i_integer_lessp(x, y)
1438
1439Returns {{C_SCHEME_TRUE}} when {{x}} is less than {{y}},
1440{{C_SCHEME_FALSE}} if it is equal or greater.
1441
1442===== C_i_integer_less_or_equalp
1443
1444 [C macro] C_word C_i_integer_less_or_equalp(x, y)
1445
1446Returns {{C_SCHEME_TRUE}} when {{x}} is less than or equal to {{y}},
1447{{C_SCHEME_FALSE}} if it is greater.
1448
1449
1450==== Pointers
1451
1452===== C_null_pointerp
1453
1454 [C macro] C_word C_null_pointerp(C_word x)
1455
1456Is {{x}} a NULL pointer?
1457
1458===== C_a_i_address_to_pointer
1459
1460 [C macro] C_word C_a_i_address_to_pointer(C_word **ptr, int c, C_word addr)
1461
1462Convert {{addr}} to a pointer object using the storage at {{ptr}}.
1463{{addr}} is can be either a flonum or a fixnum representing a memory
1464address.
1465
1466===== C_a_i_pointer_to_address
1467
1468 [C macro] C_word C_a_i_pointer_to_address(C_word **ptr, int c, C_word pptr)
1469
1470Convert back the pointer {{pptr}} to an address number, possibly using
1471the storage at {{ptr}}.  The number returned can be either a fixnum or
1472a flonum, so you will have to pass a memory storage that can hold a
1473flonum at {{ptr}}.  Whether it is actually used depends on the size of
1474the address.
1475
1476
1477==== Ports
1478
1479===== C_tty_portp
1480
1481 [C macro] C_word C_tty_portp(C_word x)
1482
1483Is {{x}} a TTY port object?
1484
1485==== Structures
1486
1487===== C_i_structurep
1488
1489 [C macro] C_word C_i_structurep(C_word x, C_word s)
1490
1491Is {{x}} a structure (record) object with type tag {{s}}?  This is
1492completely safe to use, because it checks whether x is an immediate or
1493not.
1494
1495==== Characters
1496
1497These understand only ASCII characters.
1498
1499===== C_u_i_char_alphabeticp
1500
1501 [C macro] C_word C_u_i_char_alphabeticp(C_word c)
1502
1503Is {{c}} an alphabetic character?
1504
1505===== C_u_i_char_numericp
1506
1507 [C macro] C_word C_u_i_char_numericp(C_word c)
1508
1509Is {{c}} a numeric character?
1510
1511===== C_u_i_char_whitespacep
1512
1513 [C macro] C_word C_u_i_char_whitespacep(C_word c)
1514
1515Is {{c}} a whitespace character?
1516
1517===== C_u_i_char_upper_casep
1518
1519 [C macro] C_word C_u_i_char_upper_casep(C_word c)
1520
1521Is {{c}} an uppercase character?
1522
1523===== C_u_i_char_lower_casep
1524
1525 [C macro] C_word C_u_i_char_lower_casep(C_word c)
1526
1527Is {{c}} a lowercase character?
1528
1529
1530=== Other Scheme procedures from C
1531
1532There are a number of Scheme procedures that have a direct C
1533implementation, so you can call them from C too.
1534
1535==== C_eqp
1536
1537 [C macro] C_word C_eqp(C_word a, C_word b)
1538
1539The C version of {{(eq? a b)}}.
1540
1541==== C_equalp
1542
1543 [C macro] C_word C_equalp(C_word a, C_word b)
1544
1545The C version of {{(equal? a b)}}.
1546
1547==== C_i_pairp
1548
1549 [C function] C_word C_i_pairp(C_word x)
1550
1551The C version of {{(pair? x)}}.
1552
1553==== C_i_not_pair_p
1554
1555 [C macro] C_word C_i_not_pair_p(C_word x)
1556
1557The C version of {{(not (pair? x))}}.
1558
1559
1560=== An example for simple calls to foreign code involving callbacks
1561
1562 % cat foo.scm
1563 #>
1564 extern int callout(int, int, int);
1565 <#
1566 
1567 (define callout (foreign-safe-lambda int "callout" int int int))
1568 
1569 (define-external (callin (scheme-object xyz)) int
1570   (print "This is 'callin': " xyz)
1571   123)
1572 
1573 (print (callout 1 2 3))
1574
1575 % cat bar.c
1576 #include <stdio.h>
1577 #include "chicken.h"
1578 
1579 extern int callout(int, int, int);
1580 extern int callin(C_word x);
1581 
1582 int callout(int x, int y, int z)
1583 {
1584   C_word *ptr = C_alloc(C_SIZEOF_LIST(3));
1585   C_word lst;
1586 
1587   printf("This is 'callout': %d, %d, %d\n", x, y, z);
1588   lst = C_list(&ptr, 3, C_fix(x), C_fix(y), C_fix(z));
1589   return callin(lst);  /* Note: `callin' will have GC'd the data in `ptr' */
1590 }
1591
1592 % csc foo.scm bar.c -o foo
1593 % foo
1594 This is 'callout': 1, 2, 3
1595 This is 'callin': (1 2 3)
1596 123
1597
1598
1599=== Notes:
1600
1601* Scheme procedures can call C functions, and C functions can call
1602  Scheme procedures, but for every pending C stack frame, the available
1603  size of the first heap generation (the ''nursery'') will be decreased,
1604  because the C stack is identical to the nursery. On systems with a small
1605  nursery this might result in thrashing, since the C code between the
1606  invocation of C from Scheme and the actual calling back to Scheme might
1607  build up several stack-frames or allocates large amounts of stack data.
1608  To prevent this it is advisable to increase the default nursery size,
1609  either when compiling the file (using the {{-nursery}} option)
1610  or when running the executable (using the {{-:s}} runtime option).
1611* Calls to Scheme/C may be nested arbitrarily, and Scheme
1612  continuations can be invoked as usual, but keep in mind that C stack
1613  frames will not be recovered, when a Scheme procedure call from C does
1614  not return normally.
1615* When multiple threads are running concurrently, and control switches
1616  from one thread to another, then the continuation of the current thread
1617  is captured and saved. Any pending C stack frame still active from a
1618  callback will remain on the stack until the threads is re-activated
1619  again. This means that in a multithreading situation, when C callbacks
1620  are involved, the available nursery space can be smaller than expected.
1621  So doing many nested Scheme->C->Scheme calls can reduce the available
1622  memory up to the point of thrashing. It is advisable to have only a
1623  single thread with pending C stack-frames at any given time.
1624* Pointers to Scheme data objects should not be stored in local or
1625  global variables while calling back to Scheme.  Any Scheme object not
1626  passed back to Scheme will be reclaimed or moved by the garbage collector.
1627* Calls from C to Scheme are never tail-recursive.
1628* Continuations captured via {{call-with-current-continuation}}
1629  and passed to C code can be invoked like any other Scheme procedure.
1630
1631
1632---
1633Previous: [[Embedding]]
1634
1635Next: [[Data representation]]
Trap