Debian Codegraph @

src/doc/manual/extensions/ffi_uffi.txi

raw
1 @node UFFI Reference
2 @subsection UFFI Reference
3 @cindex Universal foreign function interface
4
5 @menu
6 * Primitive Types::
7 * Aggregate Types::
8 * Foreign Objects::
9 * Foreign Strings::
10 * Functions and Libraries::
11 @end menu
12
13 @node Primitive Types
14 @subsubsection Primitive Types
15 @cindex Foreign primitive types
16
17 Primitive types have a single value, these include characters, numbers,
18 and pointers. They are all symbols in the keyword package.
19
20 @lspindex :char
21 @lspindex :unsigned-char
22 @lspindex :byte
23 @lspindex :unsigned-byte
24 @lspindex :short
25 @lspindex :unsigned-short
26 @lspindex :int
27 @lspindex :unsigned-int
28 @lspindex :long
29 @lspindex :unsigned-long
30 @lspindex :int16-t
31 @lspindex :uint16-t
32 @lspindex :int32-t
33 @lspindex :uint32-t
34 @lspindex :int64-t
35 @lspindex :uint64-t
36 @lspindex :float
37 @lspindex :double
38 @lspindex :long-double
39 @lspindex :csfloat
40 @lspindex :cdfloat
41 @lspindex :clfloat
42 @lspindex :cstring
43 @lspindex :void
44 @lspindex :pointer-void
45 @lspindex :*
46 @lspindex :object
47
48 @ftindex LONG-LONG
49 @ftindex UINT16-T
50 @ftindex UINT32-T
51 @ftindex UINT64-T
52 @ftindex LONG-FLOAT
53 @ftindex COMPLEX-FLOAT
54
55 @table @samp
56 @item :char
57 @itemx :unsigned-char
58 Signed/unsigned 8-bits. Dereferenced pointer returns a character.
59 @item :byte
60 @itemx :unsigned-byte
61 Signed/unsigned 8-bits. Dereferenced pointer returns an integer.
62 @item :short
63 @itemx :unsigned-short
64 @itemx :int
65 @itemx :unsigned-int
66 @itemx :long
67 @itemx :unsigned-long
68 Standard integer types (16-bit, 32-bit and 32/64-bit).
69 @item :int16-t
70 @itemx :uint16-t
71 @itemx :int32-t
72 @itemx :uint32-t
73 @itemx :int64-t
74 @itemx :uint64-t
75 Integer types with guaranteed bitness.
76
77 @item :float
78 @itemx :double
79 Floating point numerals (32-bit and 64-bit).
80 @item :long-double
81 Floating point numeral (usually 80-bit, at least 64-bit, exact
82 bitness is compiler/architecture/platform dependent).
83 @item :csfloat
84 @itemx :cdfloat
85 @itemx :clfloat
86 Complex floating point numerals. These types exist only when ECL is
87 built with c99complex support.
88 @item :cstring
89 A @code{NULL} terminated string used for passing and returning
90 characters strings with a C function.
91 @item :void
92 The absence of a value. Used to indicate that a function does not return
93 a value.
94 @item :pointer-void
95 Points to a generic object.
96 @item *
97 Used to declare a pointer to an object.
98 @item :object
99 A generic lisp object (i.e. a @code{cl_object} in C)
100 @end table
101
102 @subsubheading Reference
103
104 @lspdef ffi:def-constant
105 @defmac ffi:def-constant name value &key (export nil)
106
107 Binds a symbol to a constant.
108
109 @table @var
110 @item name
111 A symbol that will be bound to the value.
112 @item value
113 An evaluated form that is bound the the name.
114 @item export
115 When @code{t}, the name is exported from the current package. Defaults
116 to @code{nil}.
117 @item returns
118 Constant name.
119 @end table
120
121 @paragraph Description
122 This is a thin wrapper around @code{defconstant}. It evaluates at
123 compile-time and optionally exports the symbol from the package.
124
125 @paragraph Examples
126 @exindex @coderef{ffi:def-constant} defining constants
127 @lisp
128 (ffi:def-constant pi2 (* 2 pi))
129 (ffi:def-constant exported-pi2 (* 2 pi) :export t)
130 @end lisp
131
132 @paragraph Side Effects
133 Creates a new special variable.
134 @end defmac
135
136
137
138 @lspdef ffi:def-foreign-type
139 @defmac ffi:def-foreign-type name definition
140
141 Defines a new foreign type
142
143 @table @var
144 @item name
145 A symbol naming the new foreign type.
146 @item value
147 A form that is not evaluated that defines the new foreign type.
148 @item returns
149 Foreign type designator (@var{value}).
150 @end table
151
152 @paragraph Description
153 Defines a new foreign type
154
155 @paragraph Examples
156 @exindex @coderef{ffi:def-foreign-type} examples
157 @lisp
158 (ffi:def-foreign-type my-generic-pointer :pointer-void)
159 (ffi:def-foreign-type a-double-float :double-float)
160 (ffi:def-foreign-type char-ptr (* :char))
161 @end lisp
162
163 @paragraph Side effects
164 Defines a new foreign type.
165 @end defmac
166
167
168
169 @lspdef ffi:null-char-p
170 @defun ffi:null-char-p char
171
172 Tests a character for NULL value
173
174 @table @var
175 @item char
176 A character or integer.
177 @item returns
178 A boolean flag indicating if @var{char} is a NULL value.
179 @end table
180
181 @paragraph Description
182 A predicate testing if a character or integer is NULL. This abstracts
183 the difference in implementations where some return a character and some
184 return a integer whence dereferencing a C character pointer.
185
186 @paragraph Examples
187 @exindex @coderef{ffi:null-char-p} example
188 @lisp
189 (ffi:def-array-pointer ca :unsigned-char)
190 (let ((fs (ffi:convert-to-foreign-string "ab")))
191 (values (ffi:null-char-p (ffi:deref-array fs 'ca 0))
192 (ffi:null-char-p (ffi:deref-array fs 'ca 2))))
193 ;; => NIL T
194 @end lisp
195 @end defun
196
197
198
199 @node Aggregate Types
200 @subsubsection Aggregate Types
201 @cindex Foreign aggregate types
202
203 @subsubheading Overview
204 Aggregate types are comprised of one or more primitive types.
205
206 @subsubheading Reference
207
208 @lspdef ffi:def-enum
209 @defmac ffi:def-enum name fields &key separator-string
210
211 Defines a C enumeration
212
213 @table @var
214 @item name
215 A symbol that names the enumeration.
216 @item fields
217 A list of field definitions. Each definition can be a symbol or a list of
218 two elements. Symbols get assigned a value of the current counter which
219 starts at 0 and increments by 1 for each subsequent symbol. It the field
220 definition is a list, the first position is the symbol and the second
221 position is the value to assign the the symbol. The current counter gets
222 set to 1+ this value.
223 @item returns
224 A string that governs the creation of constants. The default is "#".
225 @end table
226
227 @paragraph Description
228 Declares a C enumeration. It generates constants with integer values for
229 the elements of the enumeration. The symbols for the these constant
230 values are created by the concatenation of the enumeration name,
231 separator-string, and field symbol. Also creates a foreign type with the
232 name name of type @code{:int}.
233
234 @paragraph Examples
235 @exindex @coderef{ffi:def-enum} sample enumerations
236 @lisp
237 (ffi:def-enum abc (:a :b :c))
238 ;; Creates constants abc#a (1), abc#b (2), abc#c (3) and defines
239 ;; the foreign type "abc" to be :int
240
241 (ffi:def-enum efoo (:e1 (:e2 10) :e3) :separator-string "-")
242 ;; Creates constants efoo-e1 (1), efoo-e2 (10), efoo-e3 (11) and defines
243 ;; the foreign type efoo to be :int
244 @end lisp
245
246 @paragraph Side effects
247 Creates a @code{:int} foreign type, defines constants.
248 @end defmac
249
250
251
252 @lspdef ffi:def-struct
253 @defmac ffi:def-struct name &rest fields
254
255 Defines a C structure
256
257 @table @var
258 @item name
259 A symbol that names the structure.
260 @item fields
261 A variable number of field definitions. Each definition is a list
262 consisting of a symbol naming the field followed by its foreign type.
263 @end table
264
265 @paragraph Description
266 Declares a structure. A special type is available as a slot in the
267 field. It is a pointer that points to an instance of the parent
268 structure. It's type is @code{:pointer-self}.
269
270 @paragraph Examples
271 @exindex @coderef{ffi:def-struct} defining C structure
272 @lisp
273 (ffi:def-struct foo (a :unsigned-int)
274 (b (* :char))
275 (c (:array :int 10))
276 (next :pointer-self))
277 @end lisp
278
279 @paragraph Side effects
280 Creates a foreign type.
281 @end defmac
282
283
284
285 @lspdef ffi:get-slot-value
286 @defun ffi:get-slot-value obj type field
287
288 Retrieves a value from a slot of a structure
289
290 @table @var
291 @item obj
292 A pointer to the foreign structure.
293 @item type
294 The name of the foreign structure.
295 @item field
296 The name of the desired field in the foreign structure.
297 @item returns
298 The value of the @var{field} in the structure @var{obj}.
299 @end table
300
301 @paragraph Description
302 Accesses a slot value from a structure. This is generalized and can be
303 used with @code{setf}.
304
305 @paragraph Examples
306 @exindex @coderef{ffi:get-slot-value} manipulating a struct field
307 @lisp
308 (ffi:get-slot-value foo-ptr 'foo-structure 'field-name)
309 (setf (ffi:get-slot-value foo-ptr 'foo-structure 'field-name) 10)
310 @end lisp
311 @end defun
312
313
314
315 @lspdef ffi:get-slot-pointer
316 @defun ffi:get-slot-pointer obj type field
317
318 Retrieves a pointer from a slot of a structure
319
320 @table @var
321 @item obj
322 A pointer to the foreign structure.
323 @item type
324 The name of the foreign structure.
325 @item field
326 The name of the desired field in the foreign structure.
327 @item returns
328 The value of the pointer @var{field} in the structure @var{obj}.
329 @end table
330
331 @paragraph Description
332 This is similar to @coderef{ffi:get-slot-value}. It is used when the
333 value of a slot is a pointer type.
334
335 @paragraph Examples
336 @exindex @coderef{ffi:get-slot-value} usage
337 @lisp
338 (ffi:get-slot-pointer foo-ptr 'foo-structure 'my-char-ptr)
339 @end lisp
340 @end defun
341
342
343
344 @lspdef ffi:def-array-pointer
345 @defmac ffi:def-array-pointer name type
346
347 Defines a pointer to an array of @var{type}
348
349 @table @var
350 @item name
351 A name of the new foreign type.
352 @item type
353 The foreign type of the array elements.
354 @end table
355
356 @paragraph Description
357 Defines a type that is a pointer to an array of @var{type}.
358
359 @paragraph Examples
360 @exindex @coderef{ffi:def-array-pointer} usage
361 @lisp
362 (ffi:def-array-pointer byte-array-pointer :unsigned-char)
363 @end lisp
364
365 @paragraph Side effects
366 Defines a new foreign type.
367 @end defmac
368
369
370
371 @lspdef ffi:deref-array
372 @defun ffi:deref-array array type position
373
374 Dereference an array
375
376 @table @var
377 @item array
378 A foreign array.
379 @item type
380 The foreign type of the @var{array}.
381 @item position
382 An integer specifying the position to retrieve from the @var{array}.
383 @item returns
384 The value stored in the @var{position} of the @var{array}.
385 @end table
386
387 @paragraph Description
388 Dereferences (retrieves) the value of the foreign array
389 element. @code{setf}-able.
390
391 @paragraph Examples
392 @exindex @coderef{ffi:deref-array} retrieving array element
393 @lisp
394 (ffi:def-array-pointer ca :char)
395 (let ((fs (ffi:convert-to-foreign-string "ab")))
396 (values (ffi:null-char-p (ffi:deref-array fs 'ca 0))
397 (ffi:null-char-p (ffi:deref-array fs 'ca 2))))
398 ;; => NIL T
399 @end lisp
400 @end defun
401
402
403
404 @lspdef ffi:def-union
405 @defmac ffi:def-union name &rest fields
406
407 Defines a foreign union type
408
409 @table @var
410 @item name
411 A name of the new union type.
412 @item fields
413 A list of fields of the union in form @code{(field-name field-type)}.
414 @end table
415
416 @paragraph Description
417 Defines a foreign union type.
418
419 @paragraph Examples
420 @exindex @coderef{ffi:def-union} union definition and usage
421 @lisp
422 (ffi:def-union test-union
423 (a-char :char)
424 (an-int :int))
425
426 (let ((u (ffi:allocate-foreign-object 'test-union)))
427 (setf (ffi:get-slot-value u 'test-union 'an-int) (+ 65 (* 66 256)))
428 (prog1
429 (ffi:ensure-char-character (ffi:get-slot-value u 'test-union 'a-char))
430 (ffi:free-foreign-object u)))
431 ;; => #\A
432 @end lisp
433
434 @paragraph Side effects
435 Defines a new foreign type.
436 @end defmac
437
438 @node Foreign Objects
439 @subsubsection Foreign Objects
440 @cindex Foreign objects
441
442 @subsubheading Overview
443 Objects are entities that can allocated, referred to by pointers, and
444 can be freed.
445
446 @subsubheading Reference
447
448 @lspdef ffi:allocate-foreign-object
449 @defun ffi:allocate-foreign-object type &optional size
450
451 Allocates an instance of a foreign object
452
453 @table @var
454 @item type
455 The type of foreign object to allocate. This parameter is evaluated.
456 @item size
457 An optional size parameter that is evaluated. If specified, allocates
458 and returns an array of @var{type} that is @var{size} members long. This
459 parameter is evaluated.
460 @item returns
461 A pointer to the foreign object.
462 @end table
463
464 @paragraph Description
465 Allocates an instance of a foreign object. It returns a pointer to the
466 object.
467
468 @paragraph Examples
469 @exindex @coderef{ffi:allocate-foreign-object} allocating structure object
470 @lisp
471 (ffi:def-struct ab (a :int) (b :double))
472 ;; => (:STRUCT (A :INT) (B :DOUBLE))
473 (ffi:allocate-foreign-object 'ab)
474 ;; => #<foreign AB>
475 @end lisp
476 @end defun
477
478 @lspdef ffi:free-foreign-object
479 @defun ffi:free-foreign-object ptr
480
481 Frees memory that was allocated for a foreign object
482
483 @table @var
484 @item ptr
485 A pointer to the allocated foreign object to free.
486 @end table
487
488 @paragraph Description
489 Frees memory that was allocated for a foreign object.
490 @end defun
491
492 @lspdef ffi:with-foreign-object
493 @defmac ffi:with-foreign-object (var type) &body body
494
495 Wraps the allocation, binding and destruction of a foreign object around
496 a body of code
497
498 @table @var
499 @item var
500 Variable name to bind.
501 @item type
502 Type of foreign object to allocate. This parameter is evaluated.
503 @item body
504 Code to be evaluated.
505 @item returns
506 The result of evaluating the body.
507 @end table
508
509 @paragraph Description
510 This function wraps the allocation, binding, and destruction of a
511 foreign object around the body of code.
512
513 @paragraph Examples
514 @exindex @coderef{ffi:with-foreign-object} macro usage
515 @lisp
516 (defun gethostname2 ()
517 "Returns the hostname"
518 (ffi:with-foreign-object (name '(:array :unsigned-char 256))
519 (if (zerop (c-gethostname (ffi:char-array-to-pointer name) 256))
520 (ffi:convert-from-foreign-string name)
521 (error "gethostname() failed."))))
522 @end lisp
523 @end defmac
524
525 @lspdef ffi:size-of-foreign-type
526 @defmac ffi:size-of-foreign-type ftype
527
528 Returns the number of data bytes used by a foreign object type
529
530 @table @var
531 @item ftype
532 A foreign type specifier. This parameter is evaluated.
533 @item returns
534 Number of data bytes used by a foreign object @var{ftype}.
535 @end table
536
537 @paragraph Description
538 Returns the number of data bytes used by a foreign object type. This
539 does not include any Lisp storage overhead.
540
541 @paragraph Examples
542 @exindex @coderef{ffi:size-of-foreign-type}
543 @lisp
544 (ffi:size-of-foreign-type :unsigned-byte)
545 ;; => 1
546 (ffi:size-of-foreign-type 'my-100-byte-vector-type)
547 ;; => 100
548 @end lisp
549 @end defmac
550
551 @lspdef ffi:pointer-address
552 @defun ffi:pointer-address ptr
553
554 Returns the address of a pointer
555
556 @table @var
557 @item ptr
558 A pointer to a foreign object.
559 @item returns
560 An integer representing the pointer's address.
561 @end table
562
563 @paragraph Description
564 Returns the address as an integer of a pointer.
565 @end defun
566
567 @lspdef ffi:deref-pointer
568 @defun ffi:deref-pointer ptr ftype
569
570 Dereferences a pointer
571
572 @table @var
573 @item ptr
574 Pointer to a foreign object.
575 @item ftype
576 Foreign type of the object being pointed to.
577 @item returns
578 The value of the object where the pointer points.
579 @end table
580
581 @paragraph Description
582 Returns the object to which a pointer points. @code{setf}-able.
583
584 @paragraph Notes
585 Casting of the pointer may be performed with
586 @coderef{ffi:with-cast-pointer} together with
587 @coderef{ffi:deref-pointer}/@coderef{ffi:deref-array}.
588
589 @paragraph Examples
590 @exindex @coderef{ffi:deref-pointer}
591 @lisp
592 (let ((intp (ffi:allocate-foreign-object :int)))
593 (setf (ffi:deref-pointer intp :int) 10)
594 (prog1
595 (ffi:deref-pointer intp :int)
596 (ffi:free-foreign-object intp)))
597 ;; => 10
598 @end lisp
599 @end defun
600
601 @lspdef ffi:ensure-char-character
602 @defun ffi:ensure-char-character object
603
604 Ensures that a dereferenced @code{:char} pointer is a character
605
606 @table @var
607 @item object
608 Either a character or a integer specifying a character code.
609 @item returns
610 A character.
611 @end table
612
613 @paragraph Description
614 Ensures that an objects obtained by dereferencing @code{:char} and
615 @code{:unsigned-char} pointers is a lisp character.
616
617 @paragraph Examples
618 @exindex @coderef{ffi:ensure-char-character}
619 @lisp
620 (let ((fs (ffi:convert-to-foreign-string "a")))
621 (prog1
622 (ffi:ensure-char-character (ffi:deref-pointer fs :char))
623 (ffi:free-foreign-object fs)))
624 ;; => #\a
625 @end lisp
626
627 @paragraph Exceptional Situations
628 Depending upon the implementation and what UFFI expects, this macro may
629 signal an error if the object is not a character or integer.
630 @end defun
631
632 @lspdef ffi:ensure-char-integer
633 @defun ffi:ensure-char-integer object
634
635 Ensures that a dereferenced @code{:char} pointer is an integer
636
637 @table @var
638 @item object
639 Either a character or a integer specifying a character code.
640 @item returns
641 An integer.
642 @end table
643
644 @paragraph Description
645 Ensures that an objects obtained by dereferencing @code{:char} and
646 @code{:unsigned-char} pointers is a lisp integer.
647
648 @paragraph Examples
649 @exindex @coderef{ffi:ensure-char-integer}
650 @lisp
651 (let ((fs (ffi:convert-to-foreign-string "a")))
652 (prog1
653 (ffi:ensure-char-integer (ffi:deref-pointer fs :char))
654 (ffi:free-foreign-object fs)))
655 ;; => 96
656 @end lisp
657
658 @paragraph Exceptional Situations
659 Depending upon the implementation and what UFFI expects, this macro may
660 signal an error if the object is not a character or integer.
661 @end defun
662
663 @lspdef ffi:make-null-pointer
664 @defun ffi:make-null-pointer ftype
665
666 Create a NULL pointer of a specified type
667
668 @table @var
669 @item ftype
670 A type of object to which the pointer refers.
671 @item returns
672 The NULL pointer of type @var{ftype}.
673 @end table
674 @end defun
675
676 @lspdef ffi:null-pointer-p
677 @defun ffi:null-pointer-p ptr
678
679 Tests a pointer for NULL value
680
681 @table @var
682 @item ptr
683 A foreign object pointer.
684 @item returns
685 The boolean flag.
686 @end table
687 @end defun
688
689 @lspdef ffi:+null-cstring-pointer+
690 @defvr Variable {ffi:+null-cstring-pointer+}
691 A NULL cstring pointer. This can be used for testing if a cstring
692 returned by a function is NULL.
693 @end defvr
694
695 @lspdef ffi:with-cast-pointer
696 @defmac ffi:with-cast-pointer (var ptr ftype) &body body
697
698 Wraps a body of code with a pointer cast to a new type
699
700 @table @var
701 @item var
702 Symbol which will be bound to the casted object.
703 @item ptr
704 Pointer to a foreign object.
705 @item ftype
706 A foreign type of the object being pointed to.
707 @item returns
708 The value of the object where the pointer points.
709 @end table
710
711 @paragraph Description
712 Executes @var{body} with @var{ptr} cast to be a pointer to type
713 @var{ftype}. @var{var} will be bound to this value during the
714 execution of @var{body}.
715
716 @paragraph Examples
717 @exindex @coderef{ffi:with-cast-pointer}
718 @lisp
719 (ffi:with-foreign-object (size :int)
720 ;; FOO is a foreign function returning a :POINTER-VOID
721 (let ((memory (foo size)))
722 (when (mumble)
723 ;; at this point we know for some reason that MEMORY points
724 ;; to an array of unsigned bytes
725 (ffi:with-cast-pointer (memory :unsigned-byte)
726 (dotimes (i (deref-pointer size :int))
727 (do-something-with
728 (ffi:deref-array memory '(:array :unsigned-byte) i)))))))
729 @end lisp
730 @end defmac
731
732 @lspdef ffi:def-foreign-var
733 @defmac ffi:def-foreign-var name type module
734
735 Defines a symbol macro to access a variable in foreign code
736
737 @table @var
738 @item name
739 A string or list specifying the symbol macro's name. If it is a
740 string, that names the foreign variable. A Lisp name is created by
741 translating @code{#\_} to @code{#\-} and by converting to upper-case.
742
743 If it is a list, the first item is a string specifying the foreign
744 variable name and the second it is a symbol stating the Lisp name.
745 @item type
746 A foreign type of the foreign variable.
747 @item module
748 Either a string specifying the module (or library) the foreign
749 variable resides in, @code{:default} if no module needs to be loaded
750 or @code{nil} to use SFFI.
751 @end table
752
753 @paragraph Description
754 Defines a symbol macro which can be used to access (get and set) the
755 value of a variable in foreign code.
756
757 @paragraph Examples
758 @exindex @coderef{ffi:def-foreign-var} places in foreign world
759
760 C code defining foreign structure, standalone integer and the accessor:
761 @example
762 @verbatim
763 int baz = 3;
764
765 typedef struct {
766 int x;
767 double y;
768 } foo_struct;
769
770 foo_struct the_struct = { 42, 3.2 };
771
772 int foo () {
773 return baz;
774 }
775 @end verbatim
776 @end example
777
778 Lisp code defining C structure, function and a variable:
779 @lisp
780 (ffi:def-struct foo-struct
781 (x :int)
782 (y :double))
783
784 (ffi:def-function ("foo" foo) ()
785 :returning :int
786 :module "foo")
787
788 (ffi:def-foreign-var ("baz" *baz*) :int "foo")
789 (ffi:def-foreign-var ("the_struct" *the-struct*) foo-struct "foo")
790
791 *baz* ;; => 3
792 (incf *baz*) ;; => 4
793 (foo) ;; => 4
794 @end lisp
795 @end defmac
796
797 @node Foreign Strings
798 @subsubsection Foreign Strings
799 @cindex Foreign strings
800
801 @subsubheading Overview
802 @cindex cstring and foreign string differences
803
804 UFFI has functions to two types of C-compatible strings: cstrings and
805 foreign strings. cstrings are used only as parameters to and from
806 functions. In some implementations a cstring is not a foreign type but
807 rather the Lisp string itself. On other platforms a cstring is a newly
808 allocated foreign vector for storing characters. The following is an
809 example of using cstrings to both send and return a value.
810
811 @exindex cstring used to send and return a value
812 @lisp
813 (ffi:def-function ("getenv" c-getenv)
814 ((name :cstring))
815 :returning :cstring)
816
817 (defun my-getenv (key)
818 "Returns an environment variable, or NIL if it does not exist"
819 (check-type key string)
820 (ffi:with-cstring (key-native key)
821 (ffi:convert-from-cstring (c-getenv key-native))))
822 @end lisp
823
824 In contrast, foreign strings are always a foreign vector of characters
825 which have memory allocated. Thus, if you need to allocate memory to
826 hold the return value of a string, you must use a foreign string and not
827 a cstring. The following is an example of using a foreign string for a
828 return value.
829
830 @exindex foreign string used to send and return a value
831 @lisp
832 (ffi:def-function ("gethostname" c-gethostname)
833 ((name (* :unsigned-char))
834 (len :int))
835 :returning :int)
836
837 (defun gethostname ()
838 "Returns the hostname"
839 (let* ((name (ffi:allocate-foreign-string 256))
840 (result-code (c-gethostname name 256))
841 (hostname (when (zerop result-code)
842 (ffi:convert-from-foreign-string name))))
843 ;; UFFI does not yet provide a universal way to free
844 ;; memory allocated by C's malloc. At this point, a program
845 ;; needs to call C's free function to free such memory.
846 (unless (zerop result-code)
847 (error "gethostname() failed."))))
848 @end lisp
849
850 Foreign functions that return pointers to freshly allocated strings
851 should in general not return cstrings, but foreign strings. (There is
852 no portable way to release such cstrings from Lisp.) The following is
853 an example of handling such a function.
854
855 @exindex Conversion between foreign string and cstring
856 @lisp
857 (ffi:def-function ("readline" c-readline)
858 ((prompt :cstring))
859 :returning (* :char))
860
861 (defun readline (prompt)
862 "Reads a string from console with line-editing."
863 (ffi:with-cstring (c-prompt prompt)
864 (let* ((c-str (c-readline c-prompt))
865 (str (ffi:convert-from-foreign-string c-str)))
866 (ffi:free-foreign-object c-str)
867 str)))
868 @end lisp
869
870 @subsubheading Reference
871
872 @lspdef ffi:convert-from-cstring
873 @defmac ffi:convert-from-cstring object
874 Converts a @code{cstring} to a Lisp string
875 @table @var
876 @item object
877 A @code{cstring}
878 @item returns
879 A Lisp string
880 @end table
881
882 @paragraph Description
883 Converts a Lisp string to a cstring. This is most often used when
884 processing the results of a foreign function that returns a cstring.
885 @end defmac
886
887 @lspdef ffi:convert-to-cstring
888 @defmac ffi:convert-to-cstring object
889 Converts a Lisp string to a @code{cstring}
890 @table @var
891 @item object
892 A Lisp string
893 @item returns
894 A @code{cstring}
895 @end table
896
897 @paragraph Description
898 Converts a Lisp string to a @code{cstring}. The @code{cstring} should
899 be freed with @coderef{ffi:free-cstring}.
900 @paragraph Side Effects
901 This function allocates memory.
902 @end defmac
903
904
905 @lspdef ffi:free-cstring
906 @defmac ffi:convert-from-cstring cstring
907 Free memory used by @var{cstring}
908 @table @var
909 @item cstring
910 @code{cstring} to be freed.
911 @end table
912 @paragraph Description
913 Frees any memory possibly allocated by @coderef{ffi:convert-to-cstring}.
914 On ECL, a @code{cstring} is just the Lisp string itself.
915 @end defmac
916
917
918 @lspdef ffi:with-cstring
919 @defmac ffi:with-cstring (cstring string) &body body
920 Binds a newly created @code{cstring}
921 @table @var
922 @item cstring
923 A symbol naming the @code{cstring} to be created.
924 @item string
925 A Lisp string that will be translated to a @code{cstring}.
926 @item body
927 The body of where the @var{cstring} will be bound.
928 @item returns
929 Result of evaluating the @var{body}.
930 @end table
931 @paragraph Description
932 Binds a symbol to a @code{cstring} created from conversion of a
933 @var{string}. Automatically frees the @var{cstring}.
934 @paragraph Examples
935 @exindex @code{with-cstring}
936 @lisp
937 (ffi:def-function ("getenv" c-getenv)
938 ((name :cstring))
939 :returning :cstring)
940
941 (defun getenv (key)
942 "Returns an environment variable, or NIL if it does not exist"
943 (check-type key string)
944 (ffi:with-cstring (key-cstring key)
945 (ffi:convert-from-cstring (c-getenv key-cstring))))
946 @end lisp
947 @end defmac
948
949 @lspdef ffi:with-cstrings
950 @defmac ffi:with-cstrings bindings &body body
951 Binds a newly created @code{cstrings}
952 @table @var
953 @item bindings
954 List of pairs @var{(cstring string)}, where @var{cstring} is a name
955 for a @code{cstring} translated from Lisp string @var{string}.
956 @item body
957 The body of where the @var{bindings} will be bound.
958 @item returns
959 Result of evaluating the @var{body}.
960 @end table
961 @paragraph Description
962 Binds a symbols to a @code{cstring}s created from conversion of a
963 @var{string}s. Automatically frees the @var{cstring}s. This macro works
964 similar to @code{let*}. Based on @code{with-cstring}.
965 @end defmac
966
967 @lspdef ffi:convert-from-foreign-string
968 @defmac ffi:convert-from-foreign-string foreign-string &key length (null-terminated-p t)
969 Converts a foreign string into a Lisp string
970 @table @var
971 @item foreign-string
972 A foreign string.
973 @item length
974 The length of the foreign string to convert. The default is the length
975 of the string until a NULL character is reached.
976 @item null-terminated-p
977 A boolean flag with a default value of @code{t}. When true, the string is
978 converted until the first NULL character is reached.
979 @item returns
980 A Lisp string.
981 @end table
982 @paragraph Description
983 Returns a Lisp string from a foreign string. Can translate ASCII and
984 binary strings.
985 @end defmac
986
987 @lspdef ffi:convert-to-foreign-string
988 @defmac ffi:convert-to-foreign-string string
989 Converts a Lisp string to a foreign string
990 @table @var
991 @item string
992 A Lisp string.
993 @item returns
994 A foreign string.
995 @end table
996 @paragraph Description
997 Converts a Lisp string to a foreign string. Memory should be freed with
998 @coderef{ffi:free-foreign-object}.
999 @end defmac
1000
1001 @lspdef ffi:allocate-foreign-string
1002 @defmac ffi:allocate-foreign-string size &key unsigned
1003 Allocates space for a foreign string
1004 @table @var
1005 @item size
1006 The size of the space to be allocated in bytes.
1007 @item unsigned
1008 A boolean flag with a default value of @code{t}. When true, marks the pointer
1009 as an @code{:unsigned-char}.
1010 @item returns
1011 A foreign string which has undefined contents.
1012 @end table
1013 @paragraph Description
1014 Allocates space for a foreign string. Memory should be freed with
1015 @coderef{ffi:free-foreign-object}.
1016 @end defmac
1017
1018 @lspdef ffi:with-foreign-string
1019 @defmac ffi:with-foreign-string (foreign-string string) &body body
1020 Binds a newly allocated @code{foreign-string}
1021 @table @var
1022 @item foreign-string
1023 A symbol naming the @code{foreign string} to be created.
1024 @item string
1025 A Lisp string that will be translated to a @code{foreign string}.
1026 @item body
1027 The body of where the @var{foreign-string} will be bound.
1028 @item returns
1029 Result of evaluating the @var{body}.
1030 @end table
1031 @paragraph Description
1032 Binds a symbol to a @code{foreign-string} created from conversion of a
1033 @var{string}. Automatically deallocates the @var{foreign-string}.
1034 @paragraph Examples
1035 @end defmac
1036
1037 @lspdef ffi:with-foreign-strings
1038 @defmac ffi:with-foreign-strings bindings &body body
1039 Binds a newly created @code{foreign string}
1040 @table @var
1041 @item bindings
1042 List of pairs @var{(foreign-string string)}, where @var{foreign-string}
1043 is a name for a @code{foreign string} translated from Lisp string
1044 @var{string}.
1045 @item body
1046 The body of where the @var{bindings} will be bound.
1047 @item returns
1048 Result of evaluating the @var{body}.
1049 @end table
1050 @paragraph Description
1051 Binds a symbols to a @code{foreign-string}s created from conversion of a
1052 @var{string}s. Automatically frees the @var{foreign-string}s. This macro
1053 works similar to @code{let*}. Based on @coderef{ffi:with-foreign-string}.
1054 @end defmac
1055
1056 @node Functions and Libraries
1057 @subsubsection Functions and Libraries
1058 @cindex Foreign functions and libraries
1059
1060 @subsubheading Reference
1061
1062 @lspdef ffi:def-function
1063 @defmac ffi:def-function name args &key module (returning :void) (call :cdecl)
1064 @table @var
1065 @item name
1066 A string or list specifying the function name. If it is a string, that
1067 names the foreign function. A Lisp name is created by translating
1068 @code{#\_} to @code{#\-} and by converting to upper-case in
1069 case-insensitive Lisp implementations. If it is a list, the first item
1070 is a string specifying the foreign function name and the second it is a
1071 symbol stating the Lisp name.
1072 @item args
1073 A list of argument declarations. If @code{nil}, indicates that the function
1074 does not take any arguments.
1075 @item module
1076 Either a string specifying which module (or library) that the foreign
1077 function resides, @code{:default} if no module needs to be loaded or
1078 @code{nil} to use SFFI.
1079 @item call
1080 Function calling convention. May be one of @code{:default}, @code{:cdecl},
1081 @code{:sysv}, @code{:stdcall}, @code{:win64} and
1082 @code{:unix64}.
1083
1084 This argument is used only when we're using the dynamic function
1085 interface. If ECL is built without the DFFI support, then it uses SFFI
1086 the @var{call} argument is ignored.
1087 @item returning
1088 A declaration specifying the result type of the foreign function.
1089 @code{:void} indicates that the function does not return any value.
1090 @end table
1091 @paragraph Description
1092 Declares a foreign function.
1093 @paragraph Examples
1094 @exindex @coderef{ffi:def-function}
1095 @lisp
1096 (ffi:def-function "gethostname"
1097 ((name (* :unsigned-char))
1098 (len :int))
1099 :returning :int)
1100 @end lisp
1101 @end defmac
1102
1103 @lspdef ffi:load-foreign-library
1104 @defmac ffi:load-foreign-library filename &key module supporting-libraries force-load system-library
1105 @table @var
1106 @item filename
1107 A string or pathname specifying the library location in the filesystem.
1108 @item module
1109 @strong{IGNORED} A string designating the name of the module to apply to
1110 functions in this library.
1111 @item supporting-libraries
1112 @strong{IGNORED} A list of strings naming the libraries required to link
1113 the foreign library.
1114 @item force-load
1115 @strong{IGNORED} Forces the loading of the library if it has been
1116 previously loaded.
1117 @item system-library
1118 Denotes if the loaded library is a system library (accessible with the
1119 correct linker flags). If @code{t}, then SFFI is used and the linking is
1120 performed after compilation of the module. Otherwise (default) both SFFI
1121 and DFFI are used, but SFFI only during the compilation.
1122 @item returns
1123 A generalized boolean @emph{true} if the library was able to be loaded
1124 successfully or if the library has been previously loaded, otherwise
1125 @code{nil}.
1126 @end table
1127 @paragraph Description
1128 Loads a foreign library. Ensures that a library is only loaded once
1129 during a session.
1130 @paragraph Examples
1131 @exindex @coderef{ffi:load-foreign-library}
1132 @lisp
1133 (ffi:load-foreign-library #p"/usr/lib/libmagic.so.1")
1134 ;; => #<codeblock "/usr/lib/libmagic.so">
1135 @end lisp
1136 @paragraph Side Effects
1137 Loads the foreign code into the Lisp system.
1138 @paragraph Affected by
1139 Ability to load the file.
1140 @end defmac
1141
1142 @lspdef ffi:find-foreign-library
1143 @defun ffi:find-foreign-library names directories &key drive-letters types
1144 Finds a foreign library file
1145 @table @var
1146 @item names
1147 A string or list of strings containing the base name of the library
1148 file.
1149 @item directories
1150 A string or list of strings containing the directory the library file.
1151 @item drive-letters
1152 A string or list of strings containing the drive letters for the library
1153 file.
1154 @item types
1155 A string or list of strings containing the file type of the library
1156 file. Default is @code{nil}. If @code{nil}, will use a default type
1157 based on the currently running implementation.
1158 @item returns
1159 A path containing the path to the @emph{first} file found, or
1160 @code{nil} if the library file was not found.
1161 @end table
1162 @paragraph Description
1163 Finds a foreign library by searching through a number of possible
1164 locations. Returns the path of the first found file.
1165 @paragraph Examples
1166 @exindex @coderef{ffi:find-foreign-library}
1167 @lisp
1168 (ffi:find-foreign-library '("libz" "libmagic")
1169 '("/usr/local/lib/" "/usr/lib/")
1170 :types '("so" "dll"))
1171 ;; => #P"/usr/lib/libz.so.1.2.8"
1172 @end lisp
1173 @end defun
1174
1175 @c @lspindex ffi:
1176 @c @defmac ffi:
1177
1178 @c desc
1179
1180 @c @table @var
1181 @c @item arg-1
1182 @c description
1183 @c @item arg-2
1184 @c description
1185 @c @item returns
1186 @c One value? More?
1187 @c @end table
1188
1189 @c @paragraph Description
1190 @c Description here
1191
1192 @c @paragraph Examples
1193 @c @exindex @code{ffi:} sample run
1194 @c @lisp
1195
1196 @c @end lisp
1197
1198 @c @paragraph Side effects
1199 @c foo bar
1200 @c @end defmac