1 <?xml version='1.0' ?> <!-- Mode: Docbook -->
3 <reference id="ref_declarations">
4 <title>Declarations</title>
8 <title>Overview</title>
9 <para>Declarations are used to give the compiler optimizing
10 information about foreign types. Currently, only &cmucl;
11 supports declarations. On &acl; and &lw;, these expressions
12 declare the type generically as &t;
17 <refentry id="def-type">
19 <refname>def-type</refname>
20 <refpurpose>Defines a Common Lisp type.
22 <refclass>Macro</refclass>
27 <function>def-type</function> <replaceable>name type</replaceable>
31 <title>Arguments and Values</title>
34 <term><parameter>name</parameter></term>
36 <para>A symbol naming the type</para>
40 <term><parameter>type</parameter></term>
42 <para>A form that is evaluated that specifies the &uffi; type.
49 <title>Description</title>
50 <para>Defines a Common Lisp type based on a &uffi; type.
54 <title>Examples</title>
56 (def-type char-ptr '(* :char))
59 (declare (type char-ptr ptr))
64 <title>Side Effects</title>
65 <para>Defines a new &cl; type.</para>
68 <title>Affected by</title>
72 <title>Exceptional Situations</title>
80 <title>Primitive Types</title>
82 <title>Overview</title>
84 Primitive types have a single value, these include
85 characters, numbers, and pointers. They are all symbols in
90 <para><constant>:char</constant> - Signed 8-bits. A
91 dereferenced :char pointer returns an character.</para>
94 <para><constant>:unsigned-char</constant> - Unsigned 8-bits. A dereferenced :unsigned-char
95 pointer returns an character.</para>
98 <para><constant>:byte</constant> - Signed 8-bits. A
99 dereferenced :byte pointer returns an integer.</para>
102 <para><constant>:unsigned-byte</constant> - Unsigned 8-bits. A
103 dereferenced :unsigned-byte pointer returns an integer.</para>
106 <para><constant>:short</constant> - Signed 16-bits.</para>
109 <para><constant>:unsigned-short</constant> - Unsigned 16-bits.</para>
112 <para><constant>:int</constant> - Signed 32-bits.</para>
115 <para><constant>:unsigned-int</constant> - Unsigned 32-bits.</para>
118 <para><constant>:long</constant> - Signed 32 or 64 bits, depending upon the platform.</para>
121 <para><constant>:unsigned-long</constant> - Unsigned 32 or 64 bits, depending upon the platform.</para>
124 <para><constant>:float</constant> - 32-bit floating point.</para>
127 <para><constant>:double</constant> - 64-bit floating point.</para>
130 <para><constant>:cstring</constant> -
131 A &null; terminated string used for passing and returning characters strings with a &c; function.
135 <para><constant>:void</constant> -
136 The absence of a value. Used to indicate that a function does not return a value.</para>
139 <para><constant>:pointer-void</constant> -
140 Points to a generic object.</para>
143 <para><constant>*</constant> - Used to declare a pointer to an object</para>
148 <refentry id="def-constant">
150 <refname>def-constant</refname>
151 <refpurpose>Binds a symbol to a constant.
153 <refclass>Macro</refclass>
156 <title>Syntax</title>
158 <function>def-constant</function> <replaceable>name value &key export</replaceable>
162 <title>Arguments and Values</title>
165 <term><parameter>name</parameter></term>
167 <para>A symbol that will be bound to the value.
172 <term><parameter>value</parameter></term>
174 <para>An evaluated form that is bound the the name.
179 <term><parameter>export</parameter></term>
181 <para>When &t;, the name is exported from the current package. The default is &nil;</para>
187 <title>Description</title>
189 This is a thin wrapper around
190 <function>defconstant</function>. It evaluates at
191 compile-time and optionally exports the symbol from the package.
195 <title>Examples</title>
197 (def-constant pi2 (* 2 pi))
198 (def-constant exported-pi2 (* 2 pi) :export t)
202 <title>Side Effects</title>
203 <para>Creates a new special variable..</para>
206 <title>Affected by</title>
210 <title>Exceptional Situations</title>
215 <refentry id="def-foreign-type">
217 <refname>def-foreign-type</refname>
218 <refpurpose>Defines a new foreign type.
220 <refclass>Macro</refclass>
223 <title>Syntax</title>
225 <function>def-foreign-type</function> <replaceable>name type</replaceable>
229 <title>Arguments and Values</title>
232 <term><parameter>name</parameter></term>
234 <para>A symbol naming the new foreign type.
239 <term><parameter>value</parameter></term>
241 <para>A form that is not evaluated that defines the new
249 <title>Description</title>
250 <para>Defines a new foreign type.
254 <title>Examples</title>
256 (def-foreign-type my-generic-pointer :pointer-void)
257 (def-foreign-type a-double-float :double-float)
258 (def-foreign-type char-ptr (* :char))
262 <title>Side Effects</title>
263 <para>Defines a new foreign type.
267 <title>Affected by</title>
271 <title>Exceptional Situations</title>
276 <refentry id="null-char-p">
278 <refname>null-char-p</refname>
279 <refpurpose>Tests a character for &null; value.
281 <refclass>Macro</refclass>
284 <title>Syntax</title>
286 <function>null-char-p</function> <replaceable>char</replaceable> => <returnvalue>is-null</returnvalue>
290 <title>Arguments and Values</title>
293 <term><parameter>char</parameter></term>
295 <para>A character or integer.
300 <term><parameter>is-null</parameter></term>
302 <para>A boolean flag indicating if char is a &null; value.
309 <title>Description</title>
311 A predicate testing if a character or integer is &null;. This
312 abstracts the difference in implementations where some return a
313 <computeroutput>character</computeroutput> and some return a
314 <computeroutput>integer</computeroutput> whence dereferencing a
315 <computeroutput>C</computeroutput> character pointer.
319 <title>Examples</title>
321 (def-array-pointer ca :unsigned-char)
322 (let ((fs (convert-to-foreign-string "ab")))
323 (values (null-char-p (deref-array fs 'ca 0))
324 (null-char-p (deref-array fs 'ca 2))))
330 <title>Side Effects</title>
335 <title>Affected by</title>
339 <title>Exceptional Situations</title>
346 <title>Aggregate Types</title>
348 <title>Overview</title>
350 Aggregate types are comprised of one or more primitive types.
354 <refentry id="def-enum">
356 <refname>def-enum</refname>
357 <refpurpose>Defines a &c; enumeration.
359 <refclass>Macro</refclass>
362 <title>Syntax</title>
364 <function>def-enum</function> <replaceable>name fields &key separator-string</replaceable>
368 <title>Arguments and Values</title>
371 <term><parameter>name</parameter></term>
373 <para>A symbol that names the enumeration.
378 <term><parameter>fields</parameter></term>
380 <para>A list of field defintions. Each definition can be
381 a symbol or a list of two elements. Symbols get assigned a value of the
382 current counter which starts at <computeroutput>0</computeroutput> and
383 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
384 position is the value to assign the the symbol. The current counter gets set
385 to <computeroutput>1+</computeroutput> this value.
390 <term><parameter>separator-string</parameter></term>
392 <para>A string that governs the creation of constants. The
393 default is <computeroutput>"#"</computeroutput>.</para>
399 <title>Description</title>
401 Declares a &c; enumeration. It generates constants with integer values for the elements of the enumeration. The symbols for the these constant
402 values are created by the <function>concatenation</function> of the
403 enumeration name, separator-string, and field symbol. Also creates
404 a foreign type with the name <parameter>name</parameter> of type
405 <constant>:int</constant>.
409 <title>Examples</title>
411 (def-enum abc (:a :b :c))
412 ;; Creates constants abc#a (1), abc#b (2), abc#c (3) and defines
413 ;; the foreign type "abc" to be :int
415 (def-enum efoo (:e1 (:e2 10) :e3) :separator-string "-")
416 ;; Creates constants efoo-e1 (1), efoo-e2 (10), efoo-e3 (11) and defines
417 ;; the foreign type efoo to be :int
421 <title>Side Effects</title>
422 <para>Creates a :int foreign type, defines constants.</para>
425 <title>Affected by</title>
429 <title>Exceptional Situations</title>
435 <refentry id="def-struct">
437 <refname>def-struct</refname>
438 <refpurpose>Defines a &c; structure.
440 <refclass>Macro</refclass>
443 <title>Syntax</title>
445 <function>def-struct</function> <replaceable>name &rest fields</replaceable>
449 <title>Arguments and Values</title>
452 <term><parameter>name</parameter></term>
454 <para>A symbol that names the structure.
459 <term><parameter>fields</parameter></term>
461 <para>A variable number of field defintions. Each definition is a list consisting of a symbol naming the field followed by its foreign type.
468 <title>Description</title>
470 Declares a structure. A special type is available as a slot
471 in the field. It is a pointer that points to an instance of the parent
472 structure. It's type is <constant>:pointer-self</constant>.
477 <title>Examples</title>
479 (def-struct foo (a :unsigned-int)
482 (next :pointer-self))
486 <title>Side Effects</title>
487 <para>Creates a foreign type.</para>
490 <title>Affected by</title>
494 <title>Exceptional Situations</title>
500 <refentry id="get-slot-value">
502 <refname>get-slot-value</refname>
503 <refpurpose>Retrieves a value from a slot of a structure.
505 <refclass>Macro</refclass>
508 <title>Syntax</title>
510 <function>get-slot-value</function> <replaceable>obj type field</replaceable> => <returnvalue>value</returnvalue>
514 <title>Arguments and Values</title>
517 <term><parameter>obj</parameter></term>
519 <para>A pointer to foreign structure.
524 <term><parameter>type</parameter></term>
526 <para>A name of the foreign structure.
531 <term><parameter>field</parameter></term>
533 <para>A name of the desired field in foreign structure.
538 <term><returnvalue>value</returnvalue></term>
540 <para>The value of the field in the structure.
547 <title>Description</title>
549 Accesses a slot value from a structure.
553 <title>Examples</title>
555 (get-slot-value foo-ptr 'foo-structure 'field-name)
559 <title>Side Effects</title>
563 <title>Affected by</title>
567 <title>Exceptional Situations</title>
572 <refentry id="get-slot-pointer">
574 <refname>get-slot-pointer</refname>
575 <refpurpose>Retrieves a pointer from a slot of a structure.
577 <refclass>Macro</refclass>
580 <title>Syntax</title>
582 <function>get-slot-pointer</function> <replaceable>obj type field</replaceable> => <returnvalue>pointer</returnvalue>
586 <title>Arguments and Values</title>
589 <term><parameter>obj</parameter></term>
591 <para>A pointer to foreign structure.
596 <term><parameter>type</parameter></term>
598 <para>A name of the foreign structure.
603 <term><parameter>field</parameter></term>
605 <para>A name of the desired field in foreign structure.
610 <term><returnvalue>pointer</returnvalue></term>
612 <para>The value of the field in the structure.
619 <title>Description</title>
621 This is similar to <function>get-slot-value</function>. It
622 is used when the value of a slot is a pointer type.
626 <title>Examples</title>
628 (get-slot-pointer foo-ptr 'foo-structure 'my-char-ptr)
632 <title>Side Effects</title>
636 <title>Affected by</title>
640 <title>Exceptional Situations</title>
646 <refentry id="def-array-pointer">
648 <refname>def-array-pointer</refname>
649 <refpurpose>Defines a pointer to a array of type.
651 <refclass>Macro</refclass>
654 <title>Syntax</title>
656 <function>def-array-pointer</function> <replaceable>name type</replaceable>
660 <title>Arguments and Values</title>
663 <term><parameter>name</parameter></term>
665 <para>A name of the new foreign type.
670 <term><parameter>type</parameter></term>
672 <para>The foreign type of the array elements.
679 <title>Description</title>
681 Defines a type tat is a pointer to an array of type.
685 <title>Examples</title>
687 (def-array-pointer byte-array-pointer :unsigned-char)
691 <title>Side Effects</title>
692 <para>Defines a new foreign type.</para>
695 <title>Affected by</title>
699 <title>Exceptional Situations</title>
705 <refentry id="deref-array">
707 <refname>deref-array</refname>
708 <refpurpose>Deference an array.
710 <refclass>Macro</refclass>
713 <title>Syntax</title>
715 <function>deref-array</function> <replaceable>array type positon</replaceable> => <returnvalue>value</returnvalue>
719 <title>Arguments and Values</title>
722 <term><parameter>array</parameter></term>
724 <para>A foreign array.
729 <term><parameter>type</parameter></term>
731 <para>The foreign type of the array.
736 <term><parameter>position</parameter></term>
738 <para>An integer specifying the position to retrieve from
744 <term><returnvalue>value</returnvalue></term>
746 <para>The value stored in the position of the array.
753 <title>Description</title>
755 Dereferences (retrieves) the value of an array element.
759 <title>Examples</title>
761 (def-array-pointer ca :char)
762 (let ((fs (convert-to-foreign-string "ab")))
763 (values (null-char-p (deref-array fs 'ca 0))
764 (null-char-p (deref-array fs 'ca 2))))
772 The TYPE argument is ignored for CL implementations other than
773 AllegroCL. If you want to cast a pointer to another type use
774 WITH-CAST-POINTER together with DEREF-POINTER/DEREF-ARRAY.
778 <title>Side Effects</title>
782 <title>Affected by</title>
786 <title>Exceptional Situations</title>
791 <refentry id="def-union">
793 <refname>def-union</refname>
794 <refpurpose>Defines a foreign union type.
796 <refclass>Macro</refclass>
799 <title>Syntax</title>
801 <function>def-union</function> <replaceable>name &rest fields</replaceable>
805 <title>Arguments and Values</title>
808 <term><parameter>name</parameter></term>
810 <para>A name of the new union type.
815 <term><parameter>fields</parameter></term>
817 <para>A list of fields of the union.
824 <title>Description</title>
826 Defines a foreign union type.
830 <title>Examples</title>
832 (def-union test-union
836 (let ((u (allocate-foreign-object 'test-union))
837 (setf (get-slot-value u 'test-union 'an-int) (+ 65 (* 66 256)))
839 (ensure-char-character (get-slot-value u 'test-union 'a-char))
840 (free-foreign-object u)))
845 <title>Side Effects</title>
846 <para>Defines a new foreign type.</para>
849 <title>Affected by</title>
853 <title>Exceptional Situations</title>
862 <title>Objects</title>
864 <title>Overview</title>
866 Objects are entities that can allocated, referred to by pointers, and
872 <refentry id="allocate-foreign-object">
874 <refname>allocate-foreign-object</refname>
875 <refpurpose>Allocates an instance of a foreign object.
877 <refclass>Macro</refclass>
880 <title>Syntax</title>
882 <function>allocate-foreign-object</function> <replaceable>type &optional size</replaceable> => <returnvalue>ptr</returnvalue>
886 <title>Arguments and Values</title>
889 <term><parameter>type</parameter></term>
891 <para>The type of foreign object to allocate. This parameter is evaluated.
896 <term><parameter>size</parameter></term>
898 <para>An optional size parameter that is evaluated. If specified, allocates and returns an
899 array of <parameter>type</parameter> that is <parameter>size</parameter> members long. This parameter is evaluated.
904 <term><returnvalue>ptr</returnvalue></term>
906 <para>A pointer to the foreign object.
913 <title>Description</title>
915 Allocates an instance of a foreign object. It returns a pointer to the object.
919 <title>Examples</title>
921 (def-struct ab (a :int) (b :double))
922 (allocate-foreign-object 'ab)
927 <title>Side Effects</title>
931 <title>Affected by</title>
935 <title>Exceptional Situations</title>
941 <refentry id="free-foreign-object">
943 <refname>free-foreign-object</refname>
944 <refpurpose>Frees memory that was allocated for a foreign boject.
946 <refclass>Macro</refclass>
949 <title>Syntax</title>
951 <function>free-foreign-object</function> <replaceable>ptr</replaceable>
955 <title>Arguments and Values</title>
958 <term><parameter>ptr</parameter></term>
960 <para>A pointer to the allocated foreign object to free.
967 <title>Description</title>
969 Frees the memory used by the allocation of a foreign object.
973 <title>Side Effects</title>
977 <title>Affected by</title>
981 <title>Exceptional Situations</title>
987 <refentry id="with-foreign-object">
989 <refname>with-foreign-object</refname>
990 <refpurpose>Wraps the allocation of a foreign object around a body of code.
992 <refclass>Macro</refclass>
995 <title>Syntax</title>
997 <function>with-foreign-object</function> <replaceable>(var type) &body body</replaceable> => <returnvalue>form-return</returnvalue>
1001 <title>Arguments and Values</title>
1004 <term><parameter>var</parameter></term>
1006 <para>The variable name to bind.
1011 <term><parameter>type</parameter></term>
1013 <para>The type of foreign object to allocate. This parameter is evaluated.
1018 <term><returnvalue>form-return</returnvalue></term>
1020 <para>The result of evaluating the <parameter>body</parameter>.
1027 <title>Description</title>
1029 This function wraps the allocation, binding, and destruction of a foreign object.
1031 &lw; platforms the object is stack allocated for efficiency. Benchmarks show that &acl; performs
1032 much better with static allocation.
1036 <title>Examples</title>
1038 (defun gethostname2 ()
1039 "Returns the hostname"
1040 (uffi:with-foreign-object (name '(:array :unsigned-char 256))
1041 (if (zerop (c-gethostname (uffi:char-array-to-pointer name) 256))
1042 (uffi:convert-from-foreign-string name)
1043 (error "gethostname() failed."))))
1047 <title>Side Effects</title>
1051 <title>Affected by</title>
1055 <title>Exceptional Situations</title>
1060 <refentry id="size-of-foreign-type">
1062 <refname>size-of-foreign-type</refname>
1063 <refpurpose>Returns the number of data bytes used by a foreign object type.
1065 <refclass>Macro</refclass>
1068 <title>Syntax</title>
1070 <function>size-of-foreign-type</function> <replaceable>ftype</replaceable>
1074 <title>Arguments and Values</title>
1077 <term><parameter>ftype</parameter></term>
1079 <para>A foreign type specifier. This parameter is evaluated.
1086 <title>Description</title>
1088 Returns the number of data bytes used by a foreign object type. This does not include any Lisp storage overhead.
1092 <title>Examples</title>
1095 (size-of-foreign-object :unsigned-byte)
1097 (size-of-foreign-object 'my-100-byte-vector-type)
1103 <title>Side Effects</title>
1105 </refsect1> <refsect1>
1106 <title>Affected by</title>
1110 <title>Exceptional Situations</title>
1115 <refentry id="pointer-address">
1117 <refname>pointer-address</refname>
1118 <refpurpose>Returns the address of a pointer.
1120 <refclass>Macro</refclass>
1123 <title>Syntax</title>
1125 <function>pointer-address</function> <replaceable>ptr</replaceable> => <returnvalue>address</returnvalue>
1129 <title>Arguments and Values</title>
1132 <term><parameter>ptr</parameter></term>
1134 <para>A pointer to a foreign object.
1139 <term><parameter>address</parameter></term>
1141 <para>An integer representing the pointer's address.
1148 <title>Description</title>
1150 Returns the address as an integer of a pointer.
1154 <title>Side Effects</title>
1158 <title>Affected by</title>
1162 <title>Exceptional Situations</title>
1168 <refentry id="deref-pointer">
1170 <refname>deref-pointer</refname>
1171 <refpurpose>Deferences a pointer.
1173 <refclass>Macro</refclass>
1176 <title>Syntax</title>
1178 <function>deref-pointer</function> <replaceable>ptr type</replaceable> => <returnvalue>value</returnvalue>
1182 <title>Arguments and Values</title>
1185 <term><parameter>ptr</parameter></term>
1187 <para>A pointer to a foreign object.
1192 <term><parameter>type</parameter></term>
1194 <para>A foreign type of the object being pointed to.
1199 <term><returnvalue>value</returnvalue></term>
1201 <para>The value of the object where the pointer points.
1208 <title>Description</title>
1210 Returns the object to which a pointer points.
1214 <title>Examples</title>
1217 (let ((intp (allocate-foreign-object :int)))
1218 (setf (deref-pointer intp :int) 10)
1220 (deref-pointer intp :int)
1221 (free-foreign-object intp)))
1227 <title>Notes</title>
1229 The TYPE argument is ignored for CL implementations other than
1230 AllegroCL. If you want to cast a pointer to another type use
1231 WITH-CAST-POINTER together with DEREF-POINTER/DEREF-ARRAY.
1235 <title>Side Effects</title>
1239 <title>Affected by</title>
1243 <title>Exceptional Situations</title>
1248 <refentry id="ensure-char-character">
1250 <refname>ensure-char-character</refname>
1251 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1254 <refclass>Macro</refclass>
1257 <title>Syntax</title>
1259 <function>ensure-char-character</function> <replaceable>object</replaceable> => <returnvalue>char</returnvalue>
1263 <title>Arguments and Values</title>
1266 <term><parameter>object</parameter></term>
1268 <para>Either a character or a integer specifying a character code.
1273 <term><returnvalue>char</returnvalue></term>
1282 <title>Description</title>
1284 Ensures that an objects obtained by dereferencing
1285 <constant>:char</constant> and <constant>:unsigned-char</constant>
1286 pointers are a lisp character.
1290 <title>Examples</title>
1293 (let ((fs (convert-to-foreign-string "a")))
1295 (ensure-char-character (deref-pointer fs :char))
1296 (free-foreign-object fs)))
1302 <title>Side Effects</title>
1306 <title>Affected by</title>
1310 <title>Exceptional Situations</title>
1311 <para>Depending upon the implementation and what &uffi; expects, this
1312 macro may signal an error if the object is not a character or
1317 <refentry id="ensure-char-integer">
1319 <refname>ensure-char-integer</refname>
1320 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1323 <refclass>Macro</refclass>
1326 <title>Syntax</title>
1328 <function>ensure-char-integer</function> <replaceable>object</replaceable> => <returnvalue>int</returnvalue>
1332 <title>Arguments and Values</title>
1335 <term><parameter>object</parameter></term>
1337 <para>Either a character or a integer specifying a character code.
1342 <term><returnvalue>int</returnvalue></term>
1351 <title>Description</title>
1353 Ensures that an object obtained by dereferencing a
1354 <constant>:char</constant> pointer is an integer.
1358 <title>Examples</title>
1361 (let ((fs (convert-to-foreign-string "a")))
1363 (ensure-char-integer (deref-pointer fs :char))
1364 (free-foreign-object fs)))
1370 <title>Side Effects</title>
1374 <title>Affected by</title>
1378 <title>Exceptional Situations</title>
1379 <para>Depending upon the implementation and what &uffi; expects, this
1380 macro may signal an error if the object is not a character or
1385 <refentry id="make-null-pointer">
1387 <refname>make-null-pointer</refname>
1388 <refpurpose>Create a &null; pointer.
1390 <refclass>Macro</refclass>
1393 <title>Syntax</title>
1395 <function>make-null-pointer</function> <replaceable>type</replaceable> => <returnvalue>ptr</returnvalue>
1399 <title>Arguments and Values</title>
1402 <term><parameter>type</parameter></term>
1404 <para>A type of object to which the pointer refers.
1409 <term><parameter>ptr</parameter></term>
1411 <para>The &null; pointer of type <parameter>type</parameter>.
1418 <title>Description</title>
1420 Creates a &null; pointer of a specified type.
1424 <title>Side Effects</title>
1428 <title>Affected by</title>
1432 <title>Exceptional Situations</title>
1438 <refentry id="null-pointer-p">
1440 <refname>null-pointer-p</refname>
1441 <refpurpose>Tests a pointer for &null; value.
1443 <refclass>Macro</refclass>
1446 <title>Syntax</title>
1448 <function>null-pointer-p</function> <replaceable>ptr</replaceable> => <returnvalue>is-null</returnvalue>
1452 <title>Arguments and Values</title>
1455 <term><parameter>ptr</parameter></term>
1457 <para>A foreign object pointer.
1462 <term><returnvalue>is-null</returnvalue></term>
1464 <para>The boolean flag.
1471 <title>Description</title>
1473 A predicate testing if a pointer is has a &null; value.
1477 <title>Side Effects</title>
1481 <title>Affected by</title>
1485 <title>Exceptional Situations</title>
1491 <refentry id="null-cstring-pointer">
1493 <refname>+null-cstring-pointer+</refname>
1494 <refpurpose>A constant &null; cstring pointer.
1496 <refclass>Constant</refclass>
1499 <title>Description</title>
1501 A &null; cstring pointer. This can be used for testing
1502 if a cstring returned by a function is &null;.
1507 <refentry id="with-cast-pointer">
1509 <refname>with-cast-pointer</refname>
1510 <refpurpose>Wraps a body of code with a pointer cast to a new type.
1512 <refclass>Macro</refclass>
1515 <title>Syntax</title>
1517 <function>with-cast-pointer</function> (<replaceable>binding-name ptr type) & body body</replaceable> => <returnvalue>value</returnvalue>
1521 <title>Arguments and Values</title>
1524 <term><parameter>ptr</parameter></term>
1526 <para>A pointer to a foreign object.
1531 <term><parameter>type</parameter></term>
1533 <para>A foreign type of the object being pointed to.
1538 <term><returnvalue>value</returnvalue></term>
1540 <para>The value of the object where the pointer points.
1547 <title>Description</title>
1549 Executes BODY with POINTER cast to be a pointer to type TYPE. If
1550 BINDING-NAME is provided the cast pointer will be bound to this
1551 name during the execution of BODY. If BINDING-NAME is not provided
1552 POINTER must be a name bound to the pointer which should be
1553 cast. This name will be bound to the cast pointer during the
1556 This is a no-op in AllegroCL but will wrap BODY in a LET form if
1557 BINDING-NAME is provided.
1559 This macro is meant to be used in conjunction with DEREF-POINTER or
1560 DEREF-ARRAY. In Allegro CL the "cast" will actually take place in
1561 DEREF-POINTER or DEREF-ARRAY.
1565 <title>Examples</title>
1567 (with-foreign-object (size :int)
1568 ;; FOO is a foreign function returning a :POINTER-VOID
1569 (let ((memory (foo size)))
1571 ;; at this point we know for some reason that MEMORY points
1572 ;; to an array of unsigned bytes
1573 (with-cast-pointer (memory :unsigned-byte)
1574 (dotimes (i (deref-pointer size :int))
1576 (deref-array memory '(:array :unsigned-byte) i)))))))
1580 <title>Side Effects</title>
1584 <title>Affected by</title>
1588 <title>Exceptional Situations</title>
1593 <refentry id="def-foreign-var">
1595 <refname>def-foreign-var</refname>
1597 Defines a symbol macro to access a variable in foreign code
1599 <refclass>Macro</refclass>
1602 <title>Syntax</title>
1604 <function>def-foreign-var</function> <replaceable>name type module</replaceable>
1608 <title>Arguments and Values</title>
1611 <term><parameter>name</parameter></term>
1614 A string or list specificying the symbol macro's name. If it is a
1615 string, that names the foreign variable. A Lisp name is created
1616 by translating #\_ to #\- and by converting to upper-case in
1617 case-insensitive Lisp implementations. If it is a list, the first
1618 item is a string specifying the foreign variable name and the
1619 second it is a symbol stating the Lisp name.
1624 <term><parameter>type</parameter></term>
1626 <para>A foreign type of the foreign variable.
1631 <term><returnvalue>module</returnvalue></term>
1634 A string specifying the module (or library) the foreign variable
1635 resides in. (Required by Lispworks)
1642 <title>Description</title>
1644 Defines a symbol macro which can be used to access (get and set) the
1645 value of a variable in foreign code.
1649 <title>Examples</title>
1651 <title>C code</title>
1660 foo_struct the_struct = { 42, 3.2 };
1668 <title>Lisp code</title>
1670 (uffi:def-struct foo-struct
1674 (uffi:def-function ("foo" foo)
1679 (uffi:def-foreign-var ("baz" *baz*) :int "foo")
1680 (uffi:def-foreign-var ("the_struct" *the-struct*) foo-struct "foo")
1695 <title>Side Effects</title>
1699 <title>Affected by</title>
1703 <title>Exceptional Situations</title>
1711 <title>Strings</title>
1713 <title>Overview</title>
1716 &uffi; has functions to two types of
1717 <varname>C</varname>-compatible
1718 strings: <emphasis>cstring</emphasis> and
1719 <emphasis>foreign</emphasis> strings.
1721 cstrings are used <emphasis>only</emphasis> as parameters to and from
1722 functions. In some implementations a cstring is not a foreign type but
1723 rather the Lisp string itself. On other platforms a cstring is a newly
1724 allocated foreign vector for storing characters. The following is an
1725 example of using cstrings to both send and return a value.
1729 (uffi:def-function ("getenv" c-getenv)
1731 :returning :cstring)
1733 (defun my-getenv (key)
1734 "Returns an environment variable, or NIL if it does not exist"
1735 (check-type key string)
1736 (uffi:with-cstring (key-native key)
1737 (uffi:convert-from-cstring (c-getenv key-native))))
1740 <para> In contrast, foreign strings are always a foreign vector of
1741 characters which have memory allocated. Thus, if you need to allocate
1742 memory to hold the return value of a string, you must use a foreign
1743 string and not a cstring. The following is an example of using a foreign
1744 string for a return value. </para>
1747 (uffi:def-function ("gethostname" c-gethostname)
1748 ((name (* :unsigned-char))
1752 (defun gethostname ()
1753 "Returns the hostname"
1754 (let* ((name (uffi:allocate-foreign-string 256))
1755 (result-code (c-gethostname name 256))
1756 (hostname (when (zerop result-code)
1757 (uffi:convert-from-foreign-string name))))
1758 (uffi:free-foreign-object name)
1759 (unless (zerop result-code)
1760 (error "gethostname() failed."))))
1763 <para> Foreign functions that return pointers to freshly allocated
1764 strings should in general not return cstrings, but foreign strings.
1765 (There is no portable way to release such cstrings from Lisp.) The
1766 following is an example of handling such a function. </para>
1769 (uffi:def-function ("readline" c-readline)
1771 :returning (* :char))
1773 (defun readline (prompt)
1774 "Reads a string from console with line-editing."
1775 (with-cstring (c-prompt prompt)
1776 (let* ((c-str (c-readline c-prompt))
1777 (str (convert-from-foreign-string c-str)))
1778 (uffi:free-foreign-object c-str)
1784 <refentry id="convert-from-cstring">
1786 <refname>convert-from-cstring</refname>
1787 <refpurpose>Converts a cstring to a Lisp string.
1789 <refclass>Macro</refclass>
1792 <title>Syntax</title>
1794 <function>convert-from-cstring</function> <replaceable>cstring</replaceable> => <returnvalue>string</returnvalue>
1798 <title>Arguments and Values</title>
1801 <term><parameter>cstring</parameter></term>
1808 <term><returnvalue>string</returnvalue></term>
1810 <para>A Lisp string.
1817 <title>Description</title>
1819 Converts a Lisp string to a <constant>cstring</constant>. This is
1820 most often used when processing the results of a foreign function
1821 that returns a cstring.
1825 <title>Side Effects</title>
1829 <title>Affected by</title>
1833 <title>Exceptional Situations</title>
1839 <refentry id="convert-to-cstring">
1841 <refname>convert-to-cstring</refname>
1842 <refpurpose>Converts a Lisp string to a cstring.
1844 <refclass>Macro</refclass>
1847 <title>Syntax</title>
1849 <function>convert-to-cstring</function> <replaceable>string</replaceable> => <returnvalue>cstring</returnvalue>
1853 <title>Arguments and Values</title>
1856 <term><parameter>string</parameter></term>
1858 <para>A Lisp string.
1863 <term><returnvalue>cstring</returnvalue></term>
1872 <title>Description</title>
1874 Converts a Lisp string to a
1875 <varname>cstring</varname>. The
1876 <varname>cstring</varname> should be freed with
1877 <function>free-cstring</function>.
1881 <title>Side Effects</title>
1882 <para>On some implementations, this function allocates memory.</para>
1885 <title>Affected by</title>
1889 <title>Exceptional Situations</title>
1895 <refentry id="free-cstring">
1897 <refname>free-cstring</refname>
1898 <refpurpose>Free memory used by cstring.
1900 <refclass>Macro</refclass>
1903 <title>Syntax</title>
1905 <function>free-cstring</function> <replaceable>cstring</replaceable>
1909 <title>Arguments and Values</title>
1912 <term><parameter>cstring</parameter></term>
1921 <title>Description</title>
1923 Frees any memory possibly allocated by
1924 <function>convert-to-cstring</function>. On some implementions, a cstring is just the Lisp string itself.
1928 <title>Side Effects</title>
1932 <title>Affected by</title>
1936 <title>Exceptional Situations</title>
1942 <refentry id="with-cstring">
1944 <refname>with-cstring</refname>
1945 <refpurpose>Binds a newly created cstring.
1947 <refclass>Macro</refclass>
1950 <title>Syntax</title>
1952 <function>with-cstring</function> <replaceable>(cstring string) {body}</replaceable>
1956 <title>Arguments and Values</title>
1959 <term><parameter>cstring</parameter></term>
1961 <para>A symbol naming the cstring to be created.
1966 <term><parameter>string</parameter></term>
1968 <para>A Lisp string that will be translated to a cstring.
1973 <term><parameter>body</parameter></term>
1975 <para>The body of where the cstring will be bound.
1982 <title>Description</title>
1984 Binds a symbol to a cstring created from conversion of a string. Automatically frees the <varname>cstring</varname>.
1988 <title>Examples</title>
1991 (def-function ("getenv" c-getenv)
1993 :returning :cstring)
1996 "Returns an environment variable, or NIL if it does not exist"
1997 (check-type key string)
1998 (with-cstring (key-cstring key)
1999 (convert-from-cstring (c-getenv key-cstring))))
2004 <title>Side Effects</title>
2008 <title>Affected by</title>
2012 <title>Exceptional Situations</title>
2018 <refentry id="convert-from-foreign-string">
2020 <refname>convert-from-foreign-string</refname>
2021 <refpurpose>Converts a foreign string into a Lisp string.
2023 <refclass>Macro</refclass>
2026 <title>Syntax</title>
2028 <function>convert-from-foreign-string</function> <replaceable>foreign-string &key length null-terminated-p</replaceable> => <returnvalue>string</returnvalue>
2032 <title>Arguments and Values</title>
2035 <term><parameter>foreign-string</parameter></term>
2037 <para>A foreign string.
2042 <term><parameter>length</parameter></term>
2044 <para>The length of the foreign string to
2045 convert. The default is the length of the string until a &null;
2046 character is reached.
2051 <term><parameter>null-terminated-p</parameter></term>
2053 <para>A boolean flag with a default value of &t; When true,
2054 the string is converted until the first &null; character is reached.
2059 <term><returnvalue>string</returnvalue></term>
2061 <para>A Lisp string.
2068 <title>Description</title>
2070 Returns a Lisp string from a foreign string.
2071 Can translated ASCII and binary strings.
2075 <title>Side Effects</title>
2079 <title>Affected by</title>
2083 <title>Exceptional Situations</title>
2089 <refentry id="convert-to-foreign-string">
2091 <refname>convert-to-foreign-string</refname>
2092 <refpurpose>Converts a Lisp string to a foreign string.
2094 <refclass>Macro</refclass>
2097 <title>Syntax</title>
2099 <function>convert-to-foreign-string</function> <replaceable>string</replaceable> => <returnvalue>foreign-string</returnvalue>
2103 <title>Arguments and Values</title>
2106 <term><parameter>string</parameter></term>
2108 <para>A Lisp string.
2113 <term><returnvalue>foreign-string</returnvalue></term>
2115 <para>A foreign string.
2122 <title>Description</title>
2124 Converts a Lisp string to a foreign string. Memory should be
2125 freed with <function>free-foreign-object</function>.
2129 <title>Side Effects</title>
2133 <title>Affected by</title>
2137 <title>Exceptional Situations</title>
2144 <refentry id="allocate-foreign-string">
2146 <refname>allocate-foreign-string</refname>
2147 <refpurpose>Allocates space for a foreign string.
2149 <refclass>Macro</refclass>
2152 <title>Syntax</title>
2154 <function>allocate-foreign-string</function> <replaceable>size &key unsigned</replaceable> => <returnvalue>foreign-string</returnvalue>
2158 <title>Arguments and Values</title>
2161 <term><parameter>size</parameter></term>
2163 <para>The size of the space to be allocated in bytes.
2168 <term><parameter>unsigned</parameter></term>
2170 <para>A boolean flag with a default value of &t;. When true,
2171 marks the pointer as an <constant>:unsigned-char</constant>.
2176 <term><returnvalue>foreign-string</returnvalue></term>
2178 <para>A foreign string which has undefined contents.
2185 <title>Description</title>
2187 Allocates space for a foreign string. Memory should
2188 be freed with <function>free-foreign-object</function>.
2192 <title>Side Effects</title>
2196 <title>Affected by</title>
2200 <title>Exceptional Situations</title>
2208 <title>Functions & Libraries</title>
2210 <refentry id="def-function">
2212 <refname>def-function</refname>
2213 <refpurpose>Declares a function.
2215 <refclass>Macro</refclass>
2218 <title>Syntax</title>
2220 <function>def-function</function> <replaceable>name args &key module returning</replaceable>
2224 <title>Arguments and Values</title>
2227 <term><parameter>name</parameter></term>
2229 <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.
2234 <term><parameter>args</parameter></term>
2236 <para>A list of argument declarations. If &nil;, indicates that the function does not take any arguments.
2241 <term><parameter>module</parameter></term>
2243 <para>A string specifying which module (or library) that the foreign function resides. (Required by Lispworks)</para>
2247 <term><returnvalue>returning</returnvalue></term>
2249 <para>A declaration specifying the result type of the
2250 foreign function. If <constant>:void</constant> indicates module does not return any value.
2257 <title>Description</title>
2258 <para>Declares a foreign function.
2262 <title>Examples</title>
2264 (def-function "gethostname"
2265 ((name (* :unsigned-char))
2271 <title>Side Effects</title>
2275 <title>Affected by</title>
2279 <title>Exceptional Situations</title>
2284 <refentry id="load-foreign-library">
2286 <refname>load-foreign-library</refname>
2287 <refpurpose>Loads a foreign library.
2289 <refclass>Function</refclass>
2292 <title>Syntax</title>
2294 <function>load-foreign-library</function> <replaceable>filename &key module supporting-libraries force-load</replaceable> => <returnvalue>success</returnvalue>
2298 <title>Arguments and Values</title>
2301 <term><parameter>filename</parameter></term>
2303 <para>A string or pathname specifying the library location
2304 in the filesystem. At least one implementation (&lw;) can not
2305 accept a logical pathname.
2310 <term><parameter>module</parameter></term>
2312 <para>A string designating the name of the module to apply
2313 to functions in this library. (Required for Lispworks)
2318 <term><parameter>supporting-libraries</parameter></term>
2320 <para>A list of strings naming the libraries required to
2321 link the foreign library. (Required by CMUCL)
2326 <term><parameter>force-load</parameter></term>
2328 <para>Forces the loading of the library if it has been previously loaded.
2333 <term><returnvalue>success</returnvalue></term>
2335 <para>A boolean flag, &t; if the library was able to be
2336 loaded successfully or if the library has been previously loaded,
2344 <title>Description</title>
2345 <para>Loads a foreign library. Applies a module name to functions
2346 within the library. Ensures that a library is only loaded once during
2347 a session. A library can be reloaded by using the <symbol>:force-load</symbol> key.
2351 <title>Examples</title>
2353 (load-foreign-library #p"/usr/lib/libmysqlclient.so"
2355 :supporting-libraries '("c"))
2360 <title>Side Effects</title>
2361 <para>Loads the foreign code into the Lisp system.
2365 <title>Affected by</title>
2366 <para>Ability to load the file.</para>
2369 <title>Exceptional Situations</title>
2374 <refentry id="find-foreign-library">
2376 <refname>find-foreign-library</refname>
2377 <refpurpose>Finds a foreign library file.
2379 <refclass>Function</refclass>
2382 <title>Syntax</title>
2384 <function>find-foreign-library</function> <replaceable>names directories & drive-letters types</replaceable> => <returnvalue>path</returnvalue>
2388 <title>Arguments and Values</title>
2391 <term><parameter>names</parameter></term>
2393 <para>A string or list of strings containing the base name of the library file.
2398 <term><parameter>directories</parameter></term>
2400 <para>A string or list of strings containing the directory the library file.
2405 <term><parameter>drive-letters</parameter></term>
2407 <para>A string or list of strings containing the drive letters for the library file.
2412 <term><parameter>types</parameter></term>
2414 <para>A string or list of strings containing the file type of the library file. Default
2415 is &nil;. If &nil;, will use a default type based on the currently running implementation.
2420 <term><returnvalue>path</returnvalue></term>
2422 <para>A path containing the path found, or &nil; if the library file was not found.
2429 <title>Description</title>
2430 <para>Finds a foreign library by searching through a number of possible locations. Returns
2431 the path of the first found file.
2435 <title>Examples</title>
2437 (find-foreign-library '("libmysqlclient" "libmysql")
2438 '("/opt/mysql/lib/mysql/" "/usr/local/lib/" "/usr/lib/" "/mysql/lib/opt/")
2439 :types '("so" "dll")
2440 :drive-letters '("C" "D" "E"))
2441 => #P"D:\\mysql\\lib\\opt\\libmysql.dll"
2445 <title>Side Effects</title>
2450 <title>Affected by</title>
2454 <title>Exceptional Situations</title>