1 <?xml version='1.0' ?> <!-- Mode: Docbook -->
2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "file:///usr/share/sgml/docbook/dtd/xml/4.2/docbookx.dtd" [
4 <!ENTITY % myents SYSTEM "entities.xml">
8 <reference id="ref_declarations">
9 <title>Declarations</title>
13 <title>Overview</title>
14 <para>Declarations are used to give the compiler optimizing
15 information about foreign types. Currently, only &cmucl;
16 supports declarations. On &acl; and &lw;, these expressions
17 declare the type generically as &t;
22 <refentry id="def-type">
24 <refname>def-type</refname>
25 <refpurpose>Defines a Common Lisp type.
27 <refclass>Macro</refclass>
32 <function>def-type</function> <replaceable>name type</replaceable>
36 <title>Arguments and Values</title>
39 <term><parameter>name</parameter></term>
41 <para>A symbol naming the type</para>
45 <term><parameter>type</parameter></term>
47 <para>A form that is evaluated that specifies the &uffi; type.
54 <title>Description</title>
55 <para>Defines a Common Lisp type based on a &uffi; type.
59 <title>Examples</title>
61 (def-type char-ptr '(* :char))
64 (declare (type char-ptr ptr))
69 <title>Side Effects</title>
70 <para>Defines a new &cl; type.</para>
73 <title>Affected by</title>
77 <title>Exceptional Situations</title>
85 <title>Primitive Types</title>
87 <title>Overview</title>
89 Primitive types have a single value, these include
90 characters, numbers, and pointers. They are all symbols in
95 <para><constant>:char</constant> - Signed 8-bits. A
96 dereferenced :char pointer returns an character.</para>
99 <para><constant>:unsigned-char</constant> - Unsigned 8-bits. A dereferenced :unsigned-char
100 pointer returns an character.</para>
103 <para><constant>:byte</constant> - Signed 8-bits. A
104 dereferenced :byte pointer returns an integer.</para>
107 <para><constant>:unsigned-byte</constant> - Unsigned 8-bits. A
108 dereferenced :unsigned-byte pointer returns an integer.</para>
111 <para><constant>:short</constant> - Signed 16-bits.</para>
114 <para><constant>:unsigned-short</constant> - Unsigned 16-bits.</para>
117 <para><constant>:int</constant> - Signed 32-bits.</para>
120 <para><constant>:unsigned-int</constant> - Unsigned 32-bits.</para>
123 <para><constant>:long</constant> - Signed 32 or 64 bits, depending upon the platform.</para>
126 <para><constant>:unsigned-long</constant> - Unsigned 32 or 64 bits, depending upon the platform.</para>
129 <para><constant>:float</constant> - 32-bit floating point.</para>
132 <para><constant>:double</constant> - 64-bit floating point.</para>
135 <para><constant>:cstring</constant> -
136 A &null; terminated string used for passing and returning characters strings with a &c; function.
140 <para><constant>:void</constant> -
141 The absence of a value. Used to indicate that a function does not return a value.</para>
144 <para><constant>:pointer-void</constant> -
145 Points to a generic object.</para>
148 <para><constant>*</constant> - Used to declare a pointer to an object</para>
153 <refentry id="def-constant">
155 <refname>def-constant</refname>
156 <refpurpose>Binds a symbol to a constant.
158 <refclass>Macro</refclass>
161 <title>Syntax</title>
163 <function>def-constant</function> <replaceable>name value &key export</replaceable>
167 <title>Arguments and Values</title>
170 <term><parameter>name</parameter></term>
172 <para>A symbol that will be bound to the value.
177 <term><parameter>value</parameter></term>
179 <para>An evaluated form that is bound the the name.
184 <term><parameter>export</parameter></term>
186 <para>When &t;, the name is exported from the current package. The default is &nil;</para>
192 <title>Description</title>
194 This is a thin wrapper around
195 <function>defconstant</function>. It evaluates at
196 compile-time and optionally exports the symbol from the package.
200 <title>Examples</title>
202 (def-constant pi2 (* 2 pi))
203 (def-constant exported-pi2 (* 2 pi) :export t)
207 <title>Side Effects</title>
208 <para>Creates a new special variable..</para>
211 <title>Affected by</title>
215 <title>Exceptional Situations</title>
220 <refentry id="def-foreign-type">
222 <refname>def-foreign-type</refname>
223 <refpurpose>Defines a new foreign type.
225 <refclass>Macro</refclass>
228 <title>Syntax</title>
230 <function>def-foreign-type</function> <replaceable>name type</replaceable>
234 <title>Arguments and Values</title>
237 <term><parameter>name</parameter></term>
239 <para>A symbol naming the new foreign type.
244 <term><parameter>value</parameter></term>
246 <para>A form that is not evaluated that defines the new
254 <title>Description</title>
255 <para>Defines a new foreign type.
259 <title>Examples</title>
261 (def-foreign-type my-generic-pointer :pointer-void)
262 (def-foreign-type a-double-float :double-float)
263 (def-foreign-type char-ptr (* :char))
267 <title>Side Effects</title>
268 <para>Defines a new foreign type.
272 <title>Affected by</title>
276 <title>Exceptional Situations</title>
281 <refentry id="null-char-p">
283 <refname>null-char-p</refname>
284 <refpurpose>Tests a character for &null; value.
286 <refclass>Macro</refclass>
289 <title>Syntax</title>
291 <function>null-char-p</function> <replaceable>char</replaceable> => <returnvalue>is-null</returnvalue>
295 <title>Arguments and Values</title>
298 <term><parameter>char</parameter></term>
300 <para>A character or integer.
305 <term><parameter>is-null</parameter></term>
307 <para>A boolean flag indicating if char is a &null; value.
314 <title>Description</title>
316 A predicate testing if a character or integer is &null;. This
317 abstracts the difference in implementations where some return a
318 <computeroutput>character</computeroutput> and some return a
319 <computeroutput>integer</computeroutput> whence dereferencing a
320 <computeroutput>C</computeroutput> character pointer.
324 <title>Examples</title>
326 (def-array-pointer ca :unsigned-char)
327 (let ((fs (convert-to-foreign-string "ab")))
328 (values (null-char-p (deref-array fs 'ca 0))
329 (null-char-p (deref-array fs 'ca 2))))
335 <title>Side Effects</title>
340 <title>Affected by</title>
344 <title>Exceptional Situations</title>
351 <title>Aggregate Types</title>
353 <title>Overview</title>
355 Aggregate types are comprised of one or more primitive types.
359 <refentry id="def-enum">
361 <refname>def-enum</refname>
362 <refpurpose>Defines a &c; enumeration.
364 <refclass>Macro</refclass>
367 <title>Syntax</title>
369 <function>def-enum</function> <replaceable>name fields &key separator-string</replaceable>
373 <title>Arguments and Values</title>
376 <term><parameter>name</parameter></term>
378 <para>A symbol that names the enumeration.
383 <term><parameter>fields</parameter></term>
385 <para>A list of field defintions. Each definition can be
386 a symbol or a list of two elements. Symbols get assigned a value of the
387 current counter which starts at <computeroutput>0</computeroutput> and
388 increments by <computeroutput>1</computeroutput> for each subsequent symbol. It the field definition is a list, the first position is the symbol and the second
389 position is the value to assign the the symbol. The current counter gets set
390 to <computeroutput>1+</computeroutput> this value.
395 <term><parameter>separator-string</parameter></term>
397 <para>A string that governs the creation of constants. The
398 default is <computeroutput>"#"</computeroutput>.</para>
404 <title>Description</title>
406 Declares a &c; enumeration. It generates constants with integer values for the elements of the enumeration. The symbols for the these constant
407 values are created by the <function>concatenation</function> of the
408 enumeration name, separator-string, and field symbol. Also creates
409 a foreign type with the name <parameter>name</parameter> of type
410 <constant>:int</constant>.
414 <title>Examples</title>
416 (def-enum abc (:a :b :c))
417 ;; Creates constants abc#a (1), abc#b (2), abc#c (3) and defines
418 ;; the foreign type "abc" to be :int
420 (def-enum efoo (:e1 (:e2 10) :e3) :separator-string "-")
421 ;; Creates constants efoo-e1 (1), efoo-e2 (10), efoo-e3 (11) and defines
422 ;; the foreign type efoo to be :int
426 <title>Side Effects</title>
427 <para>Creates a :int foreign type, defines constants.</para>
430 <title>Affected by</title>
434 <title>Exceptional Situations</title>
440 <refentry id="def-struct">
442 <refname>def-struct</refname>
443 <refpurpose>Defines a &c; structure.
445 <refclass>Macro</refclass>
448 <title>Syntax</title>
450 <function>def-struct</function> <replaceable>name &rest fields</replaceable>
454 <title>Arguments and Values</title>
457 <term><parameter>name</parameter></term>
459 <para>A symbol that names the structure.
464 <term><parameter>fields</parameter></term>
466 <para>A variable number of field defintions. Each definition is a list consisting of a symbol naming the field followed by its foreign type.
473 <title>Description</title>
475 Declares a structure. A special type is available as a slot
476 in the field. It is a pointer that points to an instance of the parent
477 structure. It's type is <constant>:pointer-self</constant>.
482 <title>Examples</title>
484 (def-struct foo (a :unsigned-int)
487 (next :pointer-self))
491 <title>Side Effects</title>
492 <para>Creates a foreign type.</para>
495 <title>Affected by</title>
499 <title>Exceptional Situations</title>
505 <refentry id="get-slot-value">
507 <refname>get-slot-value</refname>
508 <refpurpose>Retrieves a value from a slot of a structure.
510 <refclass>Macro</refclass>
513 <title>Syntax</title>
515 <function>get-slot-value</function> <replaceable>obj type field</replaceable> => <returnvalue>value</returnvalue>
519 <title>Arguments and Values</title>
522 <term><parameter>obj</parameter></term>
524 <para>A pointer to foreign structure.
529 <term><parameter>type</parameter></term>
531 <para>A name of the foreign structure.
536 <term><parameter>field</parameter></term>
538 <para>A name of the desired field in foreign structure.
543 <term><returnvalue>value</returnvalue></term>
545 <para>The value of the field in the structure.
552 <title>Description</title>
554 Accesses a slot value from a structure.
558 <title>Examples</title>
560 (get-slot-value foo-ptr 'foo-structure 'field-name)
564 <title>Side Effects</title>
568 <title>Affected by</title>
572 <title>Exceptional Situations</title>
577 <refentry id="get-slot-pointer">
579 <refname>get-slot-pointer</refname>
580 <refpurpose>Retrieves a pointer from a slot of a structure.
582 <refclass>Macro</refclass>
585 <title>Syntax</title>
587 <function>get-slot-pointer</function> <replaceable>obj type field</replaceable> => <returnvalue>pointer</returnvalue>
591 <title>Arguments and Values</title>
594 <term><parameter>obj</parameter></term>
596 <para>A pointer to foreign structure.
601 <term><parameter>type</parameter></term>
603 <para>A name of the foreign structure.
608 <term><parameter>field</parameter></term>
610 <para>A name of the desired field in foreign structure.
615 <term><returnvalue>pointer</returnvalue></term>
617 <para>The value of the field in the structure.
624 <title>Description</title>
626 This is similar to <function>get-slot-value</function>. It
627 is used when the value of a slot is a pointer type.
631 <title>Examples</title>
633 (get-slot-pointer foo-ptr 'foo-structure 'my-char-ptr)
637 <title>Side Effects</title>
641 <title>Affected by</title>
645 <title>Exceptional Situations</title>
651 <refentry id="def-array-pointer">
653 <refname>def-array-pointer</refname>
654 <refpurpose>Defines a pointer to a array of type.
656 <refclass>Macro</refclass>
659 <title>Syntax</title>
661 <function>def-array-pointer</function> <replaceable>name type</replaceable>
665 <title>Arguments and Values</title>
668 <term><parameter>name</parameter></term>
670 <para>A name of the new foreign type.
675 <term><parameter>type</parameter></term>
677 <para>The foreign type of the array elements.
684 <title>Description</title>
686 Defines a type tat is a pointer to an array of type.
690 <title>Examples</title>
692 (def-array-pointer byte-array-pointer :unsigned-char)
696 <title>Side Effects</title>
697 <para>Defines a new foreign type.</para>
700 <title>Affected by</title>
704 <title>Exceptional Situations</title>
710 <refentry id="deref-array">
712 <refname>deref-array</refname>
713 <refpurpose>Deference an array.
715 <refclass>Macro</refclass>
718 <title>Syntax</title>
720 <function>deref-array</function> <replaceable>array type positon</replaceable> => <returnvalue>value</returnvalue>
724 <title>Arguments and Values</title>
727 <term><parameter>array</parameter></term>
729 <para>A foreign array.
734 <term><parameter>type</parameter></term>
736 <para>The foreign type of the array.
741 <term><parameter>position</parameter></term>
743 <para>An integer specifying the position to retrieve from
749 <term><returnvalue>value</returnvalue></term>
751 <para>The value stored in the position of the array.
758 <title>Description</title>
760 Dereferences (retrieves) the value of an array element.
764 <title>Examples</title>
766 (def-array-pointer ca :char)
767 (let ((fs (convert-to-foreign-string "ab")))
768 (values (null-char-p (deref-array fs 'ca 0))
769 (null-char-p (deref-array fs 'ca 2))))
777 The TYPE argument is ignored for CL implementations other than
778 AllegroCL. If you want to cast a pointer to another type use
779 WITH-CAST-POINTER together with DEREF-POINTER/DEREF-ARRAY.
783 <title>Side Effects</title>
787 <title>Affected by</title>
791 <title>Exceptional Situations</title>
796 <refentry id="def-union">
798 <refname>def-union</refname>
799 <refpurpose>Defines a foreign union type.
801 <refclass>Macro</refclass>
804 <title>Syntax</title>
806 <function>def-union</function> <replaceable>name &rest fields</replaceable>
810 <title>Arguments and Values</title>
813 <term><parameter>name</parameter></term>
815 <para>A name of the new union type.
820 <term><parameter>fields</parameter></term>
822 <para>A list of fields of the union.
829 <title>Description</title>
831 Defines a foreign union type.
835 <title>Examples</title>
837 (def-union test-union
841 (let ((u (allocate-foreign-object 'test-union))
842 (setf (get-slot-value u 'test-union 'an-int) (+ 65 (* 66 256)))
844 (ensure-char-character (get-slot-value u 'test-union 'a-char))
845 (free-foreign-object u)))
850 <title>Side Effects</title>
851 <para>Defines a new foreign type.</para>
854 <title>Affected by</title>
858 <title>Exceptional Situations</title>
867 <title>Objects</title>
869 <title>Overview</title>
871 Objects are entities that can allocated, referred to by pointers, and
877 <refentry id="allocate-foreign-object">
879 <refname>allocate-foreign-object</refname>
880 <refpurpose>Allocates an instance of a foreign object.
882 <refclass>Macro</refclass>
885 <title>Syntax</title>
887 <function>allocate-foreign-object</function> <replaceable>type &optional size</replaceable> => <returnvalue>ptr</returnvalue>
891 <title>Arguments and Values</title>
894 <term><parameter>type</parameter></term>
896 <para>The type of foreign object to allocate. This parameter is evaluated.
901 <term><parameter>size</parameter></term>
903 <para>An optional size parameter that is evaluated. If specified, allocates and returns an
904 array of <parameter>type</parameter> that is <parameter>size</parameter> members long. This parameter is evaluated.
909 <term><returnvalue>ptr</returnvalue></term>
911 <para>A pointer to the foreign object.
918 <title>Description</title>
920 Allocates an instance of a foreign object. It returns a pointer to the object.
924 <title>Examples</title>
926 (def-struct ab (a :int) (b :double))
927 (allocate-foreign-object 'ab)
932 <title>Side Effects</title>
936 <title>Affected by</title>
940 <title>Exceptional Situations</title>
946 <refentry id="free-foreign-object">
948 <refname>free-foreign-object</refname>
949 <refpurpose>Frees memory that was allocated for a foreign boject.
951 <refclass>Macro</refclass>
954 <title>Syntax</title>
956 <function>free-foreign-object</function> <replaceable>ptr</replaceable>
960 <title>Arguments and Values</title>
963 <term><parameter>ptr</parameter></term>
965 <para>A pointer to the allocated foreign object to free.
972 <title>Description</title>
974 Frees the memory used by the allocation of a foreign object.
978 <title>Side Effects</title>
982 <title>Affected by</title>
986 <title>Exceptional Situations</title>
992 <refentry id="with-foreign-object">
994 <refname>with-foreign-object</refname>
995 <refpurpose>Wraps the allocation of a foreign object around a body of code.
997 <refclass>Macro</refclass>
1000 <title>Syntax</title>
1002 <function>with-foreign-object</function> <replaceable>(var type) &body body</replaceable> => <returnvalue>form-return</returnvalue>
1006 <title>Arguments and Values</title>
1009 <term><parameter>var</parameter></term>
1011 <para>The variable name to bind.
1016 <term><parameter>type</parameter></term>
1018 <para>The type of foreign object to allocate. This parameter is evaluated.
1023 <term><returnvalue>form-return</returnvalue></term>
1025 <para>The result of evaluating the <parameter>body</parameter>.
1032 <title>Description</title>
1034 This function wraps the allocation, binding, and destruction of a foreign object.
1036 &lw; platforms the object is stack allocated for efficiency. Benchmarks show that &acl; performs
1037 much better with static allocation.
1041 <title>Examples</title>
1043 (defun gethostname2 ()
1044 "Returns the hostname"
1045 (uffi:with-foreign-object (name '(:array :unsigned-char 256))
1046 (if (zerop (c-gethostname (uffi:char-array-to-pointer name) 256))
1047 (uffi:convert-from-foreign-string name)
1048 (error "gethostname() failed."))))
1052 <title>Side Effects</title>
1056 <title>Affected by</title>
1060 <title>Exceptional Situations</title>
1065 <refentry id="size-of-foreign-type">
1067 <refname>size-of-foreign-type</refname>
1068 <refpurpose>Returns the number of data bytes used by a foreign object type.
1070 <refclass>Macro</refclass>
1073 <title>Syntax</title>
1075 <function>size-of-foreign-type</function> <replaceable>ftype</replaceable>
1079 <title>Arguments and Values</title>
1082 <term><parameter>ftype</parameter></term>
1084 <para>A foreign type specifier. This parameter is evaluated.
1091 <title>Description</title>
1093 Returns the number of data bytes used by a foreign object type. This does not include any Lisp storage overhead.
1097 <title>Examples</title>
1100 (size-of-foreign-object :unsigned-byte)
1102 (size-of-foreign-object 'my-100-byte-vector-type)
1108 <title>Side Effects</title>
1110 </refsect1> <refsect1>
1111 <title>Affected by</title>
1115 <title>Exceptional Situations</title>
1120 <refentry id="pointer-address">
1122 <refname>pointer-address</refname>
1123 <refpurpose>Returns the address of a pointer.
1125 <refclass>Macro</refclass>
1128 <title>Syntax</title>
1130 <function>pointer-address</function> <replaceable>ptr</replaceable> => <returnvalue>address</returnvalue>
1134 <title>Arguments and Values</title>
1137 <term><parameter>ptr</parameter></term>
1139 <para>A pointer to a foreign object.
1144 <term><parameter>address</parameter></term>
1146 <para>An integer representing the pointer's address.
1153 <title>Description</title>
1155 Returns the address as an integer of a pointer.
1159 <title>Side Effects</title>
1163 <title>Affected by</title>
1167 <title>Exceptional Situations</title>
1173 <refentry id="deref-pointer">
1175 <refname>deref-pointer</refname>
1176 <refpurpose>Deferences a pointer.
1178 <refclass>Macro</refclass>
1181 <title>Syntax</title>
1183 <function>deref-pointer</function> <replaceable>ptr type</replaceable> => <returnvalue>value</returnvalue>
1187 <title>Arguments and Values</title>
1190 <term><parameter>ptr</parameter></term>
1192 <para>A pointer to a foreign object.
1197 <term><parameter>type</parameter></term>
1199 <para>A foreign type of the object being pointed to.
1204 <term><returnvalue>value</returnvalue></term>
1206 <para>The value of the object where the pointer points.
1213 <title>Description</title>
1215 Returns the object to which a pointer points.
1219 <title>Examples</title>
1222 (let ((intp (allocate-foreign-object :int)))
1223 (setf (deref-pointer intp :int) 10)
1225 (deref-pointer intp :int)
1226 (free-foreign-object intp)))
1232 <title>Notes</title>
1234 The TYPE argument is ignored for CL implementations other than
1235 AllegroCL. If you want to cast a pointer to another type use
1236 WITH-CAST-POINTER together with DEREF-POINTER/DEREF-ARRAY.
1240 <title>Side Effects</title>
1244 <title>Affected by</title>
1248 <title>Exceptional Situations</title>
1253 <refentry id="ensure-char-character">
1255 <refname>ensure-char-character</refname>
1256 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1259 <refclass>Macro</refclass>
1262 <title>Syntax</title>
1264 <function>ensure-char-character</function> <replaceable>object</replaceable> => <returnvalue>char</returnvalue>
1268 <title>Arguments and Values</title>
1271 <term><parameter>object</parameter></term>
1273 <para>Either a character or a integer specifying a character code.
1278 <term><returnvalue>char</returnvalue></term>
1287 <title>Description</title>
1289 Ensures that an objects obtained by dereferencing
1290 <constant>:char</constant> and <constant>:unsigned-char</constant>
1291 pointers are a lisp character.
1295 <title>Examples</title>
1298 (let ((fs (convert-to-foreign-string "a")))
1300 (ensure-char-character (deref-pointer fs :char))
1301 (free-foreign-object fs)))
1307 <title>Side Effects</title>
1311 <title>Affected by</title>
1315 <title>Exceptional Situations</title>
1316 <para>Depending upon the implementation and what &uffi; expects, this
1317 macro may signal an error if the object is not a character or
1322 <refentry id="ensure-char-integer">
1324 <refname>ensure-char-integer</refname>
1325 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1328 <refclass>Macro</refclass>
1331 <title>Syntax</title>
1333 <function>ensure-char-integer</function> <replaceable>object</replaceable> => <returnvalue>int</returnvalue>
1337 <title>Arguments and Values</title>
1340 <term><parameter>object</parameter></term>
1342 <para>Either a character or a integer specifying a character code.
1347 <term><returnvalue>int</returnvalue></term>
1356 <title>Description</title>
1358 Ensures that an object obtained by dereferencing a
1359 <constant>:char</constant> pointer is an integer.
1363 <title>Examples</title>
1366 (let ((fs (convert-to-foreign-string "a")))
1368 (ensure-char-integer (deref-pointer fs :char))
1369 (free-foreign-object fs)))
1375 <title>Side Effects</title>
1379 <title>Affected by</title>
1383 <title>Exceptional Situations</title>
1384 <para>Depending upon the implementation and what &uffi; expects, this
1385 macro may signal an error if the object is not a character or
1390 <refentry id="make-null-pointer">
1392 <refname>make-null-pointer</refname>
1393 <refpurpose>Create a &null; pointer.
1395 <refclass>Macro</refclass>
1398 <title>Syntax</title>
1400 <function>make-null-pointer</function> <replaceable>type</replaceable> => <returnvalue>ptr</returnvalue>
1404 <title>Arguments and Values</title>
1407 <term><parameter>type</parameter></term>
1409 <para>A type of object to which the pointer refers.
1414 <term><parameter>ptr</parameter></term>
1416 <para>The &null; pointer of type <parameter>type</parameter>.
1423 <title>Description</title>
1425 Creates a &null; pointer of a specified type.
1429 <title>Side Effects</title>
1433 <title>Affected by</title>
1437 <title>Exceptional Situations</title>
1443 <refentry id="null-pointer-p">
1445 <refname>null-pointer-p</refname>
1446 <refpurpose>Tests a pointer for &null; value.
1448 <refclass>Macro</refclass>
1451 <title>Syntax</title>
1453 <function>null-pointer-p</function> <replaceable>ptr</replaceable> => <returnvalue>is-null</returnvalue>
1457 <title>Arguments and Values</title>
1460 <term><parameter>ptr</parameter></term>
1462 <para>A foreign object pointer.
1467 <term><returnvalue>is-null</returnvalue></term>
1469 <para>The boolean flag.
1476 <title>Description</title>
1478 A predicate testing if a pointer is has a &null; value.
1482 <title>Side Effects</title>
1486 <title>Affected by</title>
1490 <title>Exceptional Situations</title>
1496 <refentry id="null-cstring-pointer">
1498 <refname>+null-cstring-pointer+</refname>
1499 <refpurpose>A constant &null; cstring pointer.
1501 <refclass>Constant</refclass>
1504 <title>Description</title>
1506 A &null; cstring pointer. This can be used for testing
1507 if a cstring returned by a function is &null;.
1512 <refentry id="with-cast-pointer">
1514 <refname>with-cast-pointer</refname>
1515 <refpurpose>Wraps a body of code with a pointer cast to a new type.
1517 <refclass>Macro</refclass>
1520 <title>Syntax</title>
1522 <function>with-cast-pointer</function> (<replaceable>binding-name ptr type) & body body</replaceable> => <returnvalue>value</returnvalue>
1526 <title>Arguments and Values</title>
1529 <term><parameter>ptr</parameter></term>
1531 <para>A pointer to a foreign object.
1536 <term><parameter>type</parameter></term>
1538 <para>A foreign type of the object being pointed to.
1543 <term><returnvalue>value</returnvalue></term>
1545 <para>The value of the object where the pointer points.
1552 <title>Description</title>
1554 Executes BODY with POINTER cast to be a pointer to type TYPE. If
1555 BINDING-NAME is provided the cast pointer will be bound to this
1556 name during the execution of BODY. If BINDING-NAME is not provided
1557 POINTER must be a name bound to the pointer which should be
1558 cast. This name will be bound to the cast pointer during the
1561 This is a no-op in AllegroCL but will wrap BODY in a LET form if
1562 BINDING-NAME is provided.
1564 This macro is meant to be used in conjunction with DEREF-POINTER or
1565 DEREF-ARRAY. In Allegro CL the "cast" will actually take place in
1566 DEREF-POINTER or DEREF-ARRAY.
1570 <title>Examples</title>
1572 (with-foreign-object (size :int)
1573 ;; FOO is a foreign function returning a :POINTER-VOID
1574 (let ((memory (foo size)))
1576 ;; at this point we know for some reason that MEMORY points
1577 ;; to an array of unsigned bytes
1578 (with-cast-pointer (memory :unsigned-byte)
1579 (dotimes (i (deref-pointer size :int))
1581 (deref-array memory '(:array :unsigned-byte) i)))))))
1585 <title>Side Effects</title>
1589 <title>Affected by</title>
1593 <title>Exceptional Situations</title>
1598 <refentry id="def-foreign-var">
1600 <refname>def-foreign-var</refname>
1602 Defines a symbol macro to access a variable in foreign code
1604 <refclass>Macro</refclass>
1607 <title>Syntax</title>
1609 <function>def-foreign-var</function> <replaceable>name type module</replaceable>
1613 <title>Arguments and Values</title>
1616 <term><parameter>name</parameter></term>
1619 A string or list specificying the symbol macro's name. If it is a
1620 string, that names the foreign variable. A Lisp name is created
1621 by translating #\_ to #\- and by converting to upper-case in
1622 case-insensitive Lisp implementations. If it is a list, the first
1623 item is a string specifying the foreign variable name and the
1624 second it is a symbol stating the Lisp name.
1629 <term><parameter>type</parameter></term>
1631 <para>A foreign type of the foreign variable.
1636 <term><returnvalue>module</returnvalue></term>
1639 A string specifying the module (or library) the foreign variable
1640 resides in. (Required by Lispworks)
1647 <title>Description</title>
1649 Defines a symbol macro which can be used to access (get and set) the
1650 value of a variable in foreign code.
1654 <title>Examples</title>
1656 <title>C code</title>
1665 foo_struct the_struct = { 42, 3.2 };
1673 <title>Lisp code</title>
1675 (uffi:def-struct foo-struct
1679 (uffi:def-function ("foo" foo)
1684 (uffi:def-foreign-var ("baz" *baz*) :int "foo")
1685 (uffi:def-foreign-var ("the_struct" *the-struct*) foo-struct "foo")
1700 <title>Side Effects</title>
1704 <title>Affected by</title>
1708 <title>Exceptional Situations</title>
1716 <title>Strings</title>
1718 <title>Overview</title>
1721 &uffi; has functions to two types of
1722 <varname>C</varname>-compatible
1723 strings: <emphasis>cstring</emphasis> and
1724 <emphasis>foreign</emphasis> strings.
1726 cstrings are used <emphasis>only</emphasis> as parameters to and from
1727 functions. In some implementations a cstring is not a foreign type but
1728 rather the Lisp string itself. On other platforms a cstring is a newly
1729 allocated foreign vector for storing characters. The following is an
1730 example of using cstrings to both send and return a value.
1734 (uffi:def-function ("getenv" c-getenv)
1736 :returning :cstring)
1738 (defun my-getenv (key)
1739 "Returns an environment variable, or NIL if it does not exist"
1740 (check-type key string)
1741 (uffi:with-cstring (key-native key)
1742 (uffi:convert-from-cstring (c-getenv key-native))))
1745 <para> In contrast, foreign strings are always a foreign vector of
1746 characters which have memory allocated. Thus, if you need to allocate
1747 memory to hold the return value of a string, you must use a foreign
1748 string and not a cstring. The following is an example of using a foreign
1749 string for a return value. </para>
1752 (uffi:def-function ("gethostname" c-gethostname)
1753 ((name (* :unsigned-char))
1757 (defun gethostname ()
1758 "Returns the hostname"
1759 (let* ((name (uffi:allocate-foreign-string 256))
1760 (result-code (c-gethostname name 256))
1761 (hostname (when (zerop result-code)
1762 (uffi:convert-from-foreign-string name))))
1763 (uffi:free-foreign-object name)
1764 (unless (zerop result-code)
1765 (error "gethostname() failed."))))
1768 <para> Foreign functions that return pointers to freshly allocated
1769 strings should in general not return cstrings, but foreign strings.
1770 (There is no portable way to release such cstrings from Lisp.) The
1771 following is an example of handling such a function. </para>
1774 (uffi:def-function ("readline" c-readline)
1776 :returning (* :char))
1778 (defun readline (prompt)
1779 "Reads a string from console with line-editing."
1780 (with-cstring (c-prompt prompt)
1781 (let* ((c-str (c-readline c-prompt))
1782 (str (convert-from-foreign-string c-str)))
1783 (uffi:free-foreign-object c-str)
1789 <refentry id="convert-from-cstring">
1791 <refname>convert-from-cstring</refname>
1792 <refpurpose>Converts a cstring to a Lisp string.
1794 <refclass>Macro</refclass>
1797 <title>Syntax</title>
1799 <function>convert-from-cstring</function> <replaceable>cstring</replaceable> => <returnvalue>string</returnvalue>
1803 <title>Arguments and Values</title>
1806 <term><parameter>cstring</parameter></term>
1813 <term><returnvalue>string</returnvalue></term>
1815 <para>A Lisp string.
1822 <title>Description</title>
1824 Converts a Lisp string to a <constant>cstring</constant>. This is
1825 most often used when processing the results of a foreign function
1826 that returns a cstring.
1830 <title>Side Effects</title>
1834 <title>Affected by</title>
1838 <title>Exceptional Situations</title>
1844 <refentry id="convert-to-cstring">
1846 <refname>convert-to-cstring</refname>
1847 <refpurpose>Converts a Lisp string to a cstring.
1849 <refclass>Macro</refclass>
1852 <title>Syntax</title>
1854 <function>convert-to-cstring</function> <replaceable>string</replaceable> => <returnvalue>cstring</returnvalue>
1858 <title>Arguments and Values</title>
1861 <term><parameter>string</parameter></term>
1863 <para>A Lisp string.
1868 <term><returnvalue>cstring</returnvalue></term>
1877 <title>Description</title>
1879 Converts a Lisp string to a
1880 <varname>cstring</varname>. The
1881 <varname>cstring</varname> should be freed with
1882 <function>free-cstring</function>.
1886 <title>Side Effects</title>
1887 <para>On some implementations, this function allocates memory.</para>
1890 <title>Affected by</title>
1894 <title>Exceptional Situations</title>
1900 <refentry id="free-cstring">
1902 <refname>free-cstring</refname>
1903 <refpurpose>Free memory used by cstring.
1905 <refclass>Macro</refclass>
1908 <title>Syntax</title>
1910 <function>free-cstring</function> <replaceable>cstring</replaceable>
1914 <title>Arguments and Values</title>
1917 <term><parameter>cstring</parameter></term>
1926 <title>Description</title>
1928 Frees any memory possibly allocated by
1929 <function>convert-to-cstring</function>. On some implementions, a cstring is just the Lisp string itself.
1933 <title>Side Effects</title>
1937 <title>Affected by</title>
1941 <title>Exceptional Situations</title>
1947 <refentry id="with-cstring">
1949 <refname>with-cstring</refname>
1950 <refpurpose>Binds a newly created cstring.
1952 <refclass>Macro</refclass>
1955 <title>Syntax</title>
1957 <function>with-cstring</function> <replaceable>(cstring string) {body}</replaceable>
1961 <title>Arguments and Values</title>
1964 <term><parameter>cstring</parameter></term>
1966 <para>A symbol naming the cstring to be created.
1971 <term><parameter>string</parameter></term>
1973 <para>A Lisp string that will be translated to a cstring.
1978 <term><parameter>body</parameter></term>
1980 <para>The body of where the cstring will be bound.
1987 <title>Description</title>
1989 Binds a symbol to a cstring created from conversion of a string. Automatically frees the <varname>cstring</varname>.
1993 <title>Examples</title>
1996 (def-function ("getenv" c-getenv)
1998 :returning :cstring)
2001 "Returns an environment variable, or NIL if it does not exist"
2002 (check-type key string)
2003 (with-cstring (key-cstring key)
2004 (convert-from-cstring (c-getenv key-cstring))))
2009 <title>Side Effects</title>
2013 <title>Affected by</title>
2017 <title>Exceptional Situations</title>
2023 <refentry id="convert-from-foreign-string">
2025 <refname>convert-from-foreign-string</refname>
2026 <refpurpose>Converts a foreign string into a Lisp string.
2028 <refclass>Macro</refclass>
2031 <title>Syntax</title>
2033 <function>convert-from-foreign-string</function> <replaceable>foreign-string &key length null-terminated-p</replaceable> => <returnvalue>string</returnvalue>
2037 <title>Arguments and Values</title>
2040 <term><parameter>foreign-string</parameter></term>
2042 <para>A foreign string.
2047 <term><parameter>length</parameter></term>
2049 <para>The length of the foreign string to
2050 convert. The default is the length of the string until a &null;
2051 character is reached.
2056 <term><parameter>null-terminated-p</parameter></term>
2058 <para>A boolean flag with a default value of &t; When true,
2059 the string is converted until the first &null; character is reached.
2064 <term><returnvalue>string</returnvalue></term>
2066 <para>A Lisp string.
2073 <title>Description</title>
2075 Returns a Lisp string from a foreign string.
2076 Can translated ASCII and binary strings.
2080 <title>Side Effects</title>
2084 <title>Affected by</title>
2088 <title>Exceptional Situations</title>
2094 <refentry id="convert-to-foreign-string">
2096 <refname>convert-to-foreign-string</refname>
2097 <refpurpose>Converts a Lisp string to a foreign string.
2099 <refclass>Macro</refclass>
2102 <title>Syntax</title>
2104 <function>convert-to-foreign-string</function> <replaceable>string</replaceable> => <returnvalue>foreign-string</returnvalue>
2108 <title>Arguments and Values</title>
2111 <term><parameter>string</parameter></term>
2113 <para>A Lisp string.
2118 <term><returnvalue>foreign-string</returnvalue></term>
2120 <para>A foreign string.
2127 <title>Description</title>
2129 Converts a Lisp string to a foreign string. Memory should be
2130 freed with <function>free-foreign-object</function>.
2134 <title>Side Effects</title>
2138 <title>Affected by</title>
2142 <title>Exceptional Situations</title>
2149 <refentry id="allocate-foreign-string">
2151 <refname>allocate-foreign-string</refname>
2152 <refpurpose>Allocates space for a foreign string.
2154 <refclass>Macro</refclass>
2157 <title>Syntax</title>
2159 <function>allocate-foreign-string</function> <replaceable>size &key unsigned</replaceable> => <returnvalue>foreign-string</returnvalue>
2163 <title>Arguments and Values</title>
2166 <term><parameter>size</parameter></term>
2168 <para>The size of the space to be allocated in bytes.
2173 <term><parameter>unsigned</parameter></term>
2175 <para>A boolean flag with a default value of &t;. When true,
2176 marks the pointer as an <constant>:unsigned-char</constant>.
2181 <term><returnvalue>foreign-string</returnvalue></term>
2183 <para>A foreign string which has undefined contents.
2190 <title>Description</title>
2192 Allocates space for a foreign string. Memory should
2193 be freed with <function>free-foreign-object</function>.
2197 <title>Side Effects</title>
2201 <title>Affected by</title>
2205 <title>Exceptional Situations</title>
2213 <title>Functions & Libraries</title>
2215 <refentry id="def-function">
2217 <refname>def-function</refname>
2218 <refpurpose>Declares a function.
2220 <refclass>Macro</refclass>
2223 <title>Syntax</title>
2225 <function>def-function</function> <replaceable>name args &key module returning</replaceable>
2229 <title>Arguments and Values</title>
2232 <term><parameter>name</parameter></term>
2234 <para>A string or list specificying the function name. If it is a string, that names the foreign function. A Lisp name is created by translating #\_ to #\- and by converting to upper-case in case-insensitive Lisp implementations. If it is a list, the first item is a string specifying the foreign function name and the second it is a symbol stating the Lisp name.
2239 <term><parameter>args</parameter></term>
2241 <para>A list of argument declarations. If &nil;, indicates that the function does not take any arguments.
2246 <term><parameter>module</parameter></term>
2248 <para>A string specifying which module (or library) that the foreign function resides. (Required by Lispworks)</para>
2252 <term><returnvalue>returning</returnvalue></term>
2254 <para>A declaration specifying the result type of the
2255 foreign function. If <constant>:void</constant> indicates module does not return any value.
2262 <title>Description</title>
2263 <para>Declares a foreign function.
2267 <title>Examples</title>
2269 (def-function "gethostname"
2270 ((name (* :unsigned-char))
2276 <title>Side Effects</title>
2280 <title>Affected by</title>
2284 <title>Exceptional Situations</title>
2289 <refentry id="load-foreign-library">
2291 <refname>load-foreign-library</refname>
2292 <refpurpose>Loads a foreign library.
2294 <refclass>Function</refclass>
2297 <title>Syntax</title>
2299 <function>load-foreign-library</function> <replaceable>filename &key module supporting-libraries force-load</replaceable> => <returnvalue>success</returnvalue>
2303 <title>Arguments and Values</title>
2306 <term><parameter>filename</parameter></term>
2308 <para>A string or pathname specifying the library location
2309 in the filesystem. At least one implementation (&lw;) can not
2310 accept a logical pathname.
2315 <term><parameter>module</parameter></term>
2317 <para>A string designating the name of the module to apply
2318 to functions in this library. (Required for Lispworks)
2323 <term><parameter>supporting-libraries</parameter></term>
2325 <para>A list of strings naming the libraries required to
2326 link the foreign library. (Required by CMUCL)
2331 <term><parameter>force-load</parameter></term>
2333 <para>Forces the loading of the library if it has been previously loaded.
2338 <term><returnvalue>success</returnvalue></term>
2340 <para>A boolean flag, &t; if the library was able to be
2341 loaded successfully or if the library has been previously loaded,
2349 <title>Description</title>
2350 <para>Loads a foreign library. Applies a module name to functions
2351 within the library. Ensures that a library is only loaded once during
2352 a session. A library can be reloaded by using the <symbol>:force-load</symbol> key.
2356 <title>Examples</title>
2358 (load-foreign-library #p"/usr/lib/libmysqlclient.so"
2360 :supporting-libraries '("c"))
2365 <title>Side Effects</title>
2366 <para>Loads the foreign code into the Lisp system.
2370 <title>Affected by</title>
2371 <para>Ability to load the file.</para>
2374 <title>Exceptional Situations</title>
2379 <refentry id="find-foreign-library">
2381 <refname>find-foreign-library</refname>
2382 <refpurpose>Finds a foreign library file.
2384 <refclass>Function</refclass>
2387 <title>Syntax</title>
2389 <function>find-foreign-library</function> <replaceable>names directories & drive-letters types</replaceable> => <returnvalue>path</returnvalue>
2393 <title>Arguments and Values</title>
2396 <term><parameter>names</parameter></term>
2398 <para>A string or list of strings containing the base name of the library file.
2403 <term><parameter>directories</parameter></term>
2405 <para>A string or list of strings containing the directory the library file.
2410 <term><parameter>drive-letters</parameter></term>
2412 <para>A string or list of strings containing the drive letters for the library file.
2417 <term><parameter>types</parameter></term>
2419 <para>A string or list of strings containing the file type of the library file. Default
2420 is &nil;. If &nil;, will use a default type based on the currently running implementation.
2425 <term><returnvalue>path</returnvalue></term>
2427 <para>A path containing the path found, or &nil; if the library file was not found.
2434 <title>Description</title>
2435 <para>Finds a foreign library by searching through a number of possible locations. Returns
2436 the path of the first found file.
2440 <title>Examples</title>
2442 (find-foreign-library '("libmysqlclient" "libmysql")
2443 '("/opt/mysql/lib/mysql/" "/usr/local/lib/" "/usr/lib/" "/mysql/lib/opt/")
2444 :types '("so" "dll")
2445 :drive-letters '("C" "D" "E"))
2446 => #P"D:\\mysql\\lib\\opt\\libmysql.dll"
2450 <title>Side Effects</title>
2455 <title>Affected by</title>
2459 <title>Exceptional Situations</title>