1 <!-- -*- DocBook -*- -->
5 <title>Declarations</title>
9 <title>Overview</title>
10 <para>Declarations are used to give the compiler optimizing
11 information about foreign types. Currently, only &cmucl;
12 supports declarations. On &acl; and &lw;, these expressions
13 declare the type generically as &t;
18 <refentry id="def-type">
20 <refname>def-type</refname>
21 <refpurpose>Defines a Common Lisp type.
23 <refclass>Macro</refclass>
28 <function>def-type</function> <replaceable>name type</replaceable>
32 <title>Arguments and Values</title>
35 <term><parameter>name</parameter></term>
37 <para>A symbol naming the type</para>
41 <term><parameter>type</parameter></term>
43 <para>A form that is evaluated that specifies the &uffi; type.
50 <title>Description</title>
51 <para>Defines a Common Lisp type based on a &uffi; type.
55 <title>Examples</title>
57 (def-type char-ptr '(* :char))
60 (declare (type char-ptr ptr))
65 <title>Side Effects</title>
66 <para>Defines a new &cl; type.</para>
69 <title>Affected by</title>
73 <title>Exceptional Situations</title>
81 <title>Primitive Types</title>
83 <title>Overview</title>
85 Primitive types have a single value, these include
86 characters, numbers, and pointers. They are all symbols in
91 <para><constant>:char</constant> - Signed 8-bits. A
92 dereferenced :char pointer returns an character.</para>
95 <para><constant>:unsigned-char</constant> - Unsigned 8-bits. A dereferenced :unsigned-char
96 pointer returns an character.</para>
99 <para><constant>:byte</constant> - Signed 8-bits. A
100 dereferenced :byte pointer returns an integer.</para>
103 <para><constant>:unsigned-byte</constant> - Unsigned 8-bits. A
104 dereferenced :unsigned-byte pointer returns an integer.</para>
107 <para><constant>:short</constant> - Signed 16-bits.</para>
110 <para><constant>:unsigned-short</constant> - Unsigned 16-bits.</para>
113 <para><constant>:int</constant> - Signed 32-bits.</para>
116 <para><constant>:unsigned-int</constant> - Unsigned 32-bits.</para>
119 <para><constant>:long</constant> - Signed 32-bits.</para>
122 <para><constant>:unsigned-long</constant> - Unsigned 32-bits.</para>
125 <para><constant>:float</constant> - 32-bit floating point.</para>
128 <para><constant>:double</constant> - 64-bit floating point.</para>
131 <para><constant>:cstring</constant> -
132 A &null; terminated string used for passing and returning characters strings with a &c; function.
136 <para><constant>:void</constant> -
137 The absence of a value. Used to indicate that a function does not return a value.</para>
140 <para><constant>:pointer-void</constant> -
141 Points to a generic object.</para>
144 <para><constant>*</constant> - Used to declare a pointer to an object</para>
149 <refentry id="def-constant">
151 <refname>def-constant</refname>
152 <refpurpose>Binds a symbol to a constant.
154 <refclass>Macro</refclass>
157 <title>Syntax</title>
159 <function>def-constant</function> <replaceable>name value &key export</replaceable>
163 <title>Arguments and Values</title>
166 <term><parameter>name</parameter></term>
168 <para>A symbol that will be bound to the value.
173 <term><parameter>value</parameter></term>
175 <para>An evaluated form that is bound the the name.
180 <term><parameter>export</parameter></term>
182 <para>When &t;, the name is exported from the current package. The default is &nil;</para>
188 <title>Description</title>
190 This is a thin wrapper around
191 <function>defconstant</function>. It evaluates at
192 compile-time and optionally exports the symbol from the package.
196 <title>Examples</title>
198 (def-constant pi2 (* 2 pi))
199 (def-constant exported-pi2 (* 2 pi) :export t)
203 <title>Side Effects</title>
204 <para>Creates a new special variable..</para>
207 <title>Affected by</title>
211 <title>Exceptional Situations</title>
216 <refentry id="def-foreign-type">
218 <refname>def-foreign-type</refname>
219 <refpurpose>Defines a new foreign type.
221 <refclass>Macro</refclass>
224 <title>Syntax</title>
226 <function>def-foreign-type</function> <replaceable>name type</replaceable>
230 <title>Arguments and Values</title>
233 <term><parameter>name</parameter></term>
235 <para>A symbol naming the new foreign type.
240 <term><parameter>value</parameter></term>
242 <para>A form that is not evaluated that defines the new
250 <title>Description</title>
251 <para>Defines a new foreign type.
255 <title>Examples</title>
257 (def-foreign-type my-generic-pointer :pointer-void)
258 (def-foreign-type a-double-float :double-float)
259 (def-foreign-type char-ptr (* :char))
263 <title>Side Effects</title>
264 <para>Defines a new foreign type.
268 <title>Affected by</title>
272 <title>Exceptional Situations</title>
277 <refentry id="null-char-p">
279 <refname>null-char-p</refname>
280 <refpurpose>Tests a character for &null; value.
282 <refclass>Macro</refclass>
285 <title>Syntax</title>
287 <function>null-char-p</function> <replaceable>char</replaceable> => <returnvalue>is-null</returnvalue>
291 <title>Arguments and Values</title>
294 <term><parameter>char</parameter></term>
296 <para>A character or integer.
301 <term><parameter>is-null</parameter></term>
303 <para>A boolean flag indicating if char is a &null; value.
310 <title>Description</title>
312 A predicate testing if a character or integer is &null;. This
313 abstracts the difference in implementations where some return a
314 <computeroutput>character</computeroutput> and some return a
315 <computeroutput>integer</computeroutput> whence dereferencing a
316 <computeroutput>C</computeroutput> character pointer.
320 <title>Examples</title>
322 (def-array-pointer ca :unsigned-char)
323 (let ((fs (convert-to-foreign-string "ab")))
324 (values (null-char-p (deref-array fs 'ca 0))
325 (null-char-p (deref-array fs 'ca 2))))
331 <title>Side Effects</title>
336 <title>Affected by</title>
340 <title>Exceptional Situations</title>
347 <title>Aggregate Types</title>
349 <title>Overview</title>
351 Aggregate types are comprised of one or more primitive types.
355 <refentry id="def-enum">
357 <refname>def-enum</refname>
358 <refpurpose>Defines a &c; enumeration.
360 <refclass>Macro</refclass>
363 <title>Syntax</title>
365 <function>def-enum</function> <replaceable>name fields &key separator-string</replaceable>
369 <title>Arguments and Values</title>
372 <term><parameter>name</parameter></term>
374 <para>A symbol that names the enumeration.
379 <term><parameter>fields</parameter></term>
381 <para>A list of field defintions. Each definition can be
382 a symbol or a list of two elements. Symbols get assigned a value of the
383 current counter which starts at <computeroutput>0</computeroutput> and
384 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
385 position is the value to assign the the symbol. The current counter gets set
386 to <computeroutput>1+</computeroutput> this value.
391 <term><parameter>separator-string</parameter></term>
393 <para>A string that governs the creation of constants. The
394 default is <computeroutput>"#"</computeroutput>.</para>
400 <title>Description</title>
402 Declares a &c; enumeration. It generates constants with integer values for the elements of the enumeration. The symbols for the these constant
403 values are created by the <function>concatenation</function> of the
404 enumeration name, separator-string, and field symbol. Also creates
405 a foreign type with the name <parameter>name</parameter> of type
406 <constant>:int</constant>.
410 <title>Examples</title>
412 (def-enum abc (:a :b :c))
413 ;; Creates constants abc#a (1), abc#b (2), abc#c (3) and defines
414 ;; the foreign type "abc" to be :int
416 (def-enum efoo (:e1 (:e2 10) :e3) :separator-string "-")
417 ;; Creates constants efoo-e1 (1), efoo-e2 (10), efoo-e3 (11) and defines
418 ;; the foreign type efoo to be :int
422 <title>Side Effects</title>
423 <para>Creates a :int foreign type, defines constants.</para>
426 <title>Affected by</title>
430 <title>Exceptional Situations</title>
436 <refentry id="def-struct">
438 <refname>def-struct</refname>
439 <refpurpose>Defines a &c; structure.
441 <refclass>Macro</refclass>
444 <title>Syntax</title>
446 <function>def-struct</function> <replaceable>name &rest fields</replaceable>
450 <title>Arguments and Values</title>
453 <term><parameter>name</parameter></term>
455 <para>A symbol that names the structure.
460 <term><parameter>fields</parameter></term>
462 <para>A variable number of field defintions. Each definition is a list consisting of a symbol naming the field followed by its foreign type.
469 <title>Description</title>
471 Declares a structure. A special type is available as a slot
472 in the field. It is a pointer that points to an instance of the parent
473 structure. It's type is <constant>:pointer-self</constant>.
478 <title>Examples</title>
480 (def-struct foo (a :unsigned-int)
483 (next :pointer-self))
487 <title>Side Effects</title>
488 <para>Creates a foreign type.</para>
491 <title>Affected by</title>
495 <title>Exceptional Situations</title>
501 <refentry id="get-slot-value">
503 <refname>get-slot-value</refname>
504 <refpurpose>Retrieves a value from a slot of a structure.
506 <refclass>Macro</refclass>
509 <title>Syntax</title>
511 <function>get-slot-value</function> <replaceable>obj type field</replaceable> => <returnvalue>value</returnvalue>
515 <title>Arguments and Values</title>
518 <term><parameter>obj</parameter></term>
520 <para>A pointer to foreign structure.
525 <term><parameter>type</parameter></term>
527 <para>A name of the foreign structure.
532 <term><parameter>field</parameter></term>
534 <para>A name of the desired field in foreign structure.
539 <term><returnvalue>value</returnvalue></term>
541 <para>The value of the field in the structure.
548 <title>Description</title>
550 Accesses a slot value from a structure.
554 <title>Examples</title>
556 (get-slot-value foo-ptr 'foo-structure 'field-name)
560 <title>Side Effects</title>
564 <title>Affected by</title>
568 <title>Exceptional Situations</title>
573 <refentry id="get-slot-pointer">
575 <refname>get-slot-pointer</refname>
576 <refpurpose>Retrieves a pointer from a slot of a structure.
578 <refclass>Macro</refclass>
581 <title>Syntax</title>
583 <function>get-slot-pointer</function> <replaceable>obj type field</replaceable> => <returnvalue>pointer</returnvalue>
587 <title>Arguments and Values</title>
590 <term><parameter>obj</parameter></term>
592 <para>A pointer to foreign structure.
597 <term><parameter>type</parameter></term>
599 <para>A name of the foreign structure.
604 <term><parameter>field</parameter></term>
606 <para>A name of the desired field in foreign structure.
611 <term><returnvalue>pointer</returnvalue></term>
613 <para>The value of the field in the structure.
620 <title>Description</title>
622 This is similar to <function>get-slot-value</function>. It
623 is used when the value of a slot is a pointer type.
627 <title>Examples</title>
629 (get-slot-pointer foo-ptr 'foo-structure 'my-char-ptr)
633 <title>Side Effects</title>
637 <title>Affected by</title>
641 <title>Exceptional Situations</title>
647 <refentry id="def-array-pointer">
649 <refname>def-array-pointer</refname>
650 <refpurpose>Defines a pointer to a array of type.
652 <refclass>Macro</refclass>
655 <title>Syntax</title>
657 <function>def-array-pointer</function> <replaceable>name type</replaceable>
661 <title>Arguments and Values</title>
664 <term><parameter>name</parameter></term>
666 <para>A name of the new foreign type.
671 <term><parameter>type</parameter></term>
673 <para>The foreign type of the array elements.
680 <title>Description</title>
682 Defines a type tat is a pointer to an array of type.
686 <title>Examples</title>
688 (def-array-pointer byte-array-pointer :unsigned-char)
692 <title>Side Effects</title>
693 <para>Defines a new foreign type.</para>
696 <title>Affected by</title>
700 <title>Exceptional Situations</title>
706 <refentry id="deref-array">
708 <refname>deref-array</refname>
709 <refpurpose>Deference an array.
711 <refclass>Macro</refclass>
714 <title>Syntax</title>
716 <function>deref-array</function> <replaceable>array type positon</replaceable> => <returnvalue>value</returnvalue>
720 <title>Arguments and Values</title>
723 <term><parameter>array</parameter></term>
725 <para>A foreign array.
730 <term><parameter>type</parameter></term>
732 <para>The foreign type of the array.
737 <term><parameter>position</parameter></term>
739 <para>An integer specifying the position to retrieve from
745 <term><returnvalue>value</returnvalue></term>
747 <para>The value stored in the position of the array.
754 <title>Description</title>
756 Dereferences (retrieves) the value of an array element.
760 <title>Examples</title>
762 (def-array-pointer ca :char)
763 (let ((fs (convert-to-foreign-string "ab")))
764 (values (null-char-p (deref-array fs 'ca 0))
765 (null-char-p (deref-array fs 'ca 2))))
773 The TYPE argument is ignored for CL implementations other than
774 AllegroCL. If you want to cast a pointer to another type use
775 WITH-CAST-POINTER together with DEREF-POINTER/DEREF-ARRAY.
779 <title>Side Effects</title>
783 <title>Affected by</title>
787 <title>Exceptional Situations</title>
792 <refentry id="def-union">
794 <refname>def-union</refname>
795 <refpurpose>Defines a foreign union type.
797 <refclass>Macro</refclass>
800 <title>Syntax</title>
802 <function>def-union</function> <replaceable>name &rest fields</replaceable>
806 <title>Arguments and Values</title>
809 <term><parameter>name</parameter></term>
811 <para>A name of the new union type.
816 <term><parameter>fields</parameter></term>
818 <para>A list of fields of the union.
825 <title>Description</title>
827 Defines a foreign union type.
831 <title>Examples</title>
833 (def-union test-union
837 (let ((u (allocate-foreign-object 'test-union))
838 (setf (get-slot-value u 'test-union 'an-int) (+ 65 (* 66 256)))
840 (ensure-char-character (get-slot-value u 'test-union 'a-char))
841 (free-foreign-object u)))
846 <title>Side Effects</title>
847 <para>Defines a new foreign type.</para>
850 <title>Affected by</title>
854 <title>Exceptional Situations</title>
863 <title>Objects</title>
865 <title>Overview</title>
867 Objects are entities that can allocated, referred to by pointers, and
873 <refentry id="allocate-foreign-object">
875 <refname>allocate-foreign-object</refname>
876 <refpurpose>Allocates an instance of a foreign object.
878 <refclass>Macro</refclass>
881 <title>Syntax</title>
883 <function>allocate-foreign-object</function> <replaceable>type &optional size</replaceable> => <returnvalue>ptr</returnvalue>
887 <title>Arguments and Values</title>
890 <term><parameter>type</parameter></term>
892 <para>The type of foreign object to allocate. This parameter is evaluated.
897 <term><parameter>size</parameter></term>
899 <para>An optional size parameter that is evaluated. If specified, allocates and returns an
900 array of <parameter>type</parameter> that is <parameter>size</parameter> members long. This parameter is evaluated.
905 <term><returnvalue>ptr</returnvalue></term>
907 <para>A pointer to the foreign object.
914 <title>Description</title>
916 Allocates an instance of a foreign object. It returns a pointer to the object.
920 <title>Examples</title>
922 (def-struct ab (a :int) (b :double))
923 (allocate-foreign-object 'ab)
928 <title>Side Effects</title>
932 <title>Affected by</title>
936 <title>Exceptional Situations</title>
942 <refentry id="free-foreign-object">
944 <refname>free-foreign-object</refname>
945 <refpurpose>Frees memory that was allocated for a foreign boject.
947 <refclass>Macro</refclass>
950 <title>Syntax</title>
952 <function>free-foreign-object</function> <replaceable>ptr</replaceable>
956 <title>Arguments and Values</title>
959 <term><parameter>ptr</parameter></term>
961 <para>A pointer to the allocated foreign object to free.
968 <title>Description</title>
970 Frees the memory used by the allocation of a foreign object.
974 <title>Side Effects</title>
978 <title>Affected by</title>
982 <title>Exceptional Situations</title>
988 <refentry id="with-foreign-object">
990 <refname>with-foreign-object</refname>
991 <refpurpose>Wraps the allocation of a foreign object around a body of code.
993 <refclass>Macro</refclass>
996 <title>Syntax</title>
998 <function>with-foreign-object</function> <replaceable>(var type) &body body</replaceable> => <returnvalue>form-return</returnvalue>
1002 <title>Arguments and Values</title>
1005 <term><parameter>var</parameter></term>
1007 <para>The variable name to bind.
1012 <term><parameter>type</parameter></term>
1014 <para>The type of foreign object to allocate. This parameter is evaluated.
1019 <term><returnvalue>form-return</returnvalue></term>
1021 <para>The result of evaluating the <parameter>body</parameter>.
1028 <title>Description</title>
1030 This function wraps the allocation, binding, and destruction of a foreign object.
1032 &lw; platforms the object is stack allocated for efficiency. Benchmarks show that &acl; performs
1033 much better with static allocation.
1037 <title>Examples</title>
1039 (defun gethostname2 ()
1040 "Returns the hostname"
1041 (uffi:with-foreign-object (name '(:array :unsigned-char 256))
1042 (if (zerop (c-gethostname (uffi:char-array-to-pointer name) 256))
1043 (uffi:convert-from-foreign-string name)
1044 (error "gethostname() failed."))))
1048 <title>Side Effects</title>
1052 <title>Affected by</title>
1056 <title>Exceptional Situations</title>
1061 <refentry id="size-of-foreign-type">
1063 <refname>size-of-foreign-type</refname>
1064 <refpurpose>Returns the number of data bytes used by a foreign object type.
1066 <refclass>Macro</refclass>
1069 <title>Syntax</title>
1071 <function>size-of-foreign-type</function> <replaceable>ftype</replaceable>
1075 <title>Arguments and Values</title>
1078 <term><parameter>ftype</parameter></term>
1080 <para>A foreign type specifier. This parameter is evaluated.
1087 <title>Description</title>
1089 Returns the number of data bytes used by a foreign object type. This does not include any Lisp storage overhead.
1093 <title>Examples</title>
1096 (size-of-foreign-object :unsigned-byte)
1098 (size-of-foreign-object 'my-100-byte-vector-type)
1104 <title>Side Effects</title>
1106 </refsect1> <refsect1>
1107 <title>Affected by</title>
1111 <title>Exceptional Situations</title>
1116 <refentry id="pointer-address">
1118 <refname>pointer-address</refname>
1119 <refpurpose>Returns the address of a pointer.
1121 <refclass>Macro</refclass>
1124 <title>Syntax</title>
1126 <function>pointer-address</function> <replaceable>ptr</replaceable> => <returnvalue>address</returnvalue>
1130 <title>Arguments and Values</title>
1133 <term><parameter>ptr</parameter></term>
1135 <para>A pointer to a foreign object.
1140 <term><parameter>address</parameter></term>
1142 <para>An integer representing the pointer's address.
1149 <title>Description</title>
1151 Returns the address as an integer of a pointer.
1155 <title>Side Effects</title>
1159 <title>Affected by</title>
1163 <title>Exceptional Situations</title>
1169 <refentry id="deref-pointer">
1171 <refname>deref-pointer</refname>
1172 <refpurpose>Deferences a pointer.
1174 <refclass>Macro</refclass>
1177 <title>Syntax</title>
1179 <function>deref-pointer</function> <replaceable>ptr type</replaceable> => <returnvalue>value</returnvalue>
1183 <title>Arguments and Values</title>
1186 <term><parameter>ptr</parameter></term>
1188 <para>A pointer to a foreign object.
1193 <term><parameter>type</parameter></term>
1195 <para>A foreign type of the object being pointed to.
1200 <term><returnvalue>value</returnvalue></term>
1202 <para>The value of the object where the pointer points.
1209 <title>Description</title>
1211 Returns the object to which a pointer points.
1215 <title>Examples</title>
1218 (let ((intp (allocate-foreign-object :int)))
1219 (setf (deref-pointer intp :int) 10)
1221 (deref-pointer intp :int)
1222 (free-foreign-object intp)))
1228 <title>Notes</title>
1230 The TYPE argument is ignored for CL implementations other than
1231 AllegroCL. If you want to cast a pointer to another type use
1232 WITH-CAST-POINTER together with DEREF-POINTER/DEREF-ARRAY.
1236 <title>Side Effects</title>
1240 <title>Affected by</title>
1244 <title>Exceptional Situations</title>
1249 <refentry id="ensure-char-character">
1251 <refname>ensure-char-character</refname>
1252 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1255 <refclass>Macro</refclass>
1258 <title>Syntax</title>
1260 <function>ensure-char-character</function> <replaceable>object</replaceable> => <returnvalue>char</returnvalue>
1264 <title>Arguments and Values</title>
1267 <term><parameter>object</parameter></term>
1269 <para>Either a character or a integer specifying a character code.
1274 <term><returnvalue>char</returnvalue></term>
1283 <title>Description</title>
1285 Ensures that an objects obtained by dereferencing
1286 <constant>:char</constant> and <constant>:unsigned-char</constant>
1287 pointers are a lisp character.
1291 <title>Examples</title>
1294 (let ((fs (convert-to-foreign-string "a")))
1296 (ensure-char-character (deref-pointer fs :char))
1297 (free-foreign-object fs)))
1303 <title>Side Effects</title>
1307 <title>Affected by</title>
1311 <title>Exceptional Situations</title>
1312 <para>Depending upon the implementation and what &uffi; expects, this
1313 macro may signal an error if the object is not a character or
1318 <refentry id="ensure-char-integer">
1320 <refname>ensure-char-integer</refname>
1321 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1324 <refclass>Macro</refclass>
1327 <title>Syntax</title>
1329 <function>ensure-char-integer</function> <replaceable>object</replaceable> => <returnvalue>int</returnvalue>
1333 <title>Arguments and Values</title>
1336 <term><parameter>object</parameter></term>
1338 <para>Either a character or a integer specifying a character code.
1343 <term><returnvalue>int</returnvalue></term>
1352 <title>Description</title>
1354 Ensures that an object obtained by dereferencing a
1355 <constant>:char</constant> pointer is an integer.
1359 <title>Examples</title>
1362 (let ((fs (convert-to-foreign-string "a")))
1364 (ensure-char-integer (deref-pointer fs :char))
1365 (free-foreign-object fs)))
1371 <title>Side Effects</title>
1375 <title>Affected by</title>
1379 <title>Exceptional Situations</title>
1380 <para>Depending upon the implementation and what &uffi; expects, this
1381 macro may signal an error if the object is not a character or
1386 <refentry id="make-null-pointer">
1388 <refname>make-null-pointer</refname>
1389 <refpurpose>Create a &null; pointer.
1391 <refclass>Macro</refclass>
1394 <title>Syntax</title>
1396 <function>make-null-pointer</function> <replaceable>type</replaceable> => <returnvalue>ptr</returnvalue>
1400 <title>Arguments and Values</title>
1403 <term><parameter>type</parameter></term>
1405 <para>A type of object to which the pointer refers.
1410 <term><parameter>ptr</parameter></term>
1412 <para>The &null; pointer of type <parameter>type</parameter>.
1419 <title>Description</title>
1421 Creates a &null; pointer of a specified type.
1425 <title>Side Effects</title>
1429 <title>Affected by</title>
1433 <title>Exceptional Situations</title>
1439 <refentry id="null-pointer-p">
1441 <refname>null-pointer-p</refname>
1442 <refpurpose>Tests a pointer for &null; value.
1444 <refclass>Macro</refclass>
1447 <title>Syntax</title>
1449 <function>null-pointer-p</function> <replaceable>ptr</replaceable> => <returnvalue>is-null</returnvalue>
1453 <title>Arguments and Values</title>
1456 <term><parameter>ptr</parameter></term>
1458 <para>A foreign object pointer.
1463 <term><returnvalue>is-null</returnvalue></term>
1465 <para>The boolean flag.
1472 <title>Description</title>
1474 A predicate testing if a pointer is has a &null; value.
1478 <title>Side Effects</title>
1482 <title>Affected by</title>
1486 <title>Exceptional Situations</title>
1492 <refentry id="null-cstring-pointer">
1494 <refname>+null-cstring-pointer+</refname>
1495 <refpurpose>A constant &null; cstring pointer.
1497 <refclass>Constant</refclass>
1500 <title>Description</title>
1502 A &null; cstring pointer. This can be used for testing
1503 if a cstring returned by a function is &null;.
1508 <refentry id="with-cast-pointer">
1510 <refname>with-cast-pointer</refname>
1511 <refpurpose>Wraps a body of code with a pointer cast to a new type.
1513 <refclass>Macro</refclass>
1516 <title>Syntax</title>
1518 <function>with-cast-pointer</function> (<replaceable>binding-name ptr type) & body body</replaceable> => <returnvalue>value</returnvalue>
1522 <title>Arguments and Values</title>
1525 <term><parameter>ptr</parameter></term>
1527 <para>A pointer to a foreign object.
1532 <term><parameter>type</parameter></term>
1534 <para>A foreign type of the object being pointed to.
1539 <term><returnvalue>value</returnvalue></term>
1541 <para>The value of the object where the pointer points.
1548 <title>Description</title>
1550 Executes BODY with POINTER cast to be a pointer to type TYPE. If
1551 BINDING-NAME is provided the cast pointer will be bound to this
1552 name during the execution of BODY. If BINDING-NAME is not provided
1553 POINTER must be a name bound to the pointer which should be
1554 cast. This name will be bound to the cast pointer during the
1557 This is a no-op in AllegroCL but will wrap BODY in a LET form if
1558 BINDING-NAME is provided.
1560 This macro is meant to be used in conjunction with DEREF-POINTER or
1561 DEREF-ARRAY. In Allegro CL the "cast" will actually take place in
1562 DEREF-POINTER or DEREF-ARRAY.
1566 <title>Examples</title>
1568 (with-foreign-object (size :int)
1569 ;; FOO is a foreign function returning a :POINTER-VOID
1570 (let ((memory (foo size)))
1572 ;; at this point we know for some reason that MEMORY points
1573 ;; to an array of unsigned bytes
1574 (with-cast-pointer (memory :unsigned-byte)
1575 (dotimes (i (deref-pointer size :int))
1577 (deref-array memory '(:array :unsigned-byte) i)))))))
1581 <title>Side Effects</title>
1585 <title>Affected by</title>
1589 <title>Exceptional Situations</title>
1597 <title>Strings</title>
1599 <title>Overview</title>
1602 &uffi; has functions to two types of
1603 <varname>C</varname>-compatible
1604 strings: <emphasis>cstring</emphasis> and
1605 <emphasis>foreign</emphasis> strings.
1607 cstrings are used <emphasis>only</emphasis> as parameters to and from
1608 functions. In some implementations a cstring is not a foreign type but
1609 rather the Lisp string itself. On other platforms a cstring is a newly
1610 allocated foreign vector for storing characters. The following is an
1611 example of using cstrings to both send and return a value.
1615 (uffi:def-function ("getenv" c-getenv)
1617 :returning :cstring)
1619 (defun my-getenv (key)
1620 "Returns an environment variable, or NIL if it does not exist"
1621 (check-type key string)
1622 (uffi:with-cstring (key-native key)
1623 (uffi:convert-from-cstring (c-getenv key-native))))
1626 <para> In contrast, foreign strings are always a foreign vector of
1627 characters which have memory allocated. Thus, if you need to allocate
1628 memory to hold the return value of a string, you must use a foreign
1629 string and not a cstring. The following is an example of using a foreign
1630 string for a return value. </para>
1633 (uffi:def-function ("gethostname" c-gethostname)
1634 ((name (* :unsigned-char))
1638 (defun gethostname ()
1639 "Returns the hostname"
1640 (let* ((name (uffi:allocate-foreign-string 256))
1641 (result-code (c-gethostname name 256))
1642 (hostname (when (zerop result-code)
1643 (uffi:convert-from-foreign-string name))))
1644 (uffi:free-foreign-object name)
1645 (unless (zerop result-code)
1646 (error "gethostname() failed."))))
1649 <para> Foreign functions that return pointers to freshly allocated
1650 strings should in general not return cstrings, but foreign strings.
1651 (There is no portable way to release such cstrings from Lisp.) The
1652 following is an example of handling such a function. </para>
1655 (uffi:def-function ("readline" c-readline)
1657 :returning (* :char))
1659 (defun readline (prompt)
1660 "Reads a string from console with line-editing."
1661 (with-cstring (c-prompt prompt)
1662 (let* ((c-str (c-readline c-prompt))
1663 (str (convert-from-foreign-string c-str)))
1664 (uffi:free-foreign-object c-str)
1670 <refentry id="convert-from-cstring">
1672 <refname>convert-from-cstring</refname>
1673 <refpurpose>Converts a cstring to a Lisp string.
1675 <refclass>Macro</refclass>
1678 <title>Syntax</title>
1680 <function>convert-from-cstring</function> <replaceable>cstring</replaceable> => <returnvalue>string</returnvalue>
1684 <title>Arguments and Values</title>
1687 <term><parameter>cstring</parameter></term>
1694 <term><returnvalue>string</returnvalue></term>
1696 <para>A Lisp string.
1703 <title>Description</title>
1705 Converts a Lisp string to a <constant>cstring</constant>. This is
1706 most often used when processing the results of a foreign function
1707 that returns a cstring.
1711 <title>Side Effects</title>
1715 <title>Affected by</title>
1719 <title>Exceptional Situations</title>
1725 <refentry id="convert-to-cstring">
1727 <refname>convert-to-cstring</refname>
1728 <refpurpose>Converts a Lisp string to a cstring.
1730 <refclass>Macro</refclass>
1733 <title>Syntax</title>
1735 <function>convert-to-cstring</function> <replaceable>string</replaceable> => <returnvalue>cstring</returnvalue>
1739 <title>Arguments and Values</title>
1742 <term><parameter>string</parameter></term>
1744 <para>A Lisp string.
1749 <term><returnvalue>cstring</returnvalue></term>
1758 <title>Description</title>
1760 Converts a Lisp string to a
1761 <varname>cstring</varname>. The
1762 <varname>cstring</varname> should be freed with
1763 <function>free-cstring</function>.
1767 <title>Side Effects</title>
1768 <para>On some implementations, this function allocates memory.</para>
1771 <title>Affected by</title>
1775 <title>Exceptional Situations</title>
1781 <refentry id="free-cstring">
1783 <refname>free-cstring</refname>
1784 <refpurpose>Free memory used by cstring.
1786 <refclass>Macro</refclass>
1789 <title>Syntax</title>
1791 <function>free-cstring</function> <replaceable>cstring</replaceable>
1795 <title>Arguments and Values</title>
1798 <term><parameter>cstring</parameter></term>
1807 <title>Description</title>
1809 Frees any memory possibly allocated by
1810 <function>convert-to-cstring</function>. On some implementions, a cstring is just the Lisp string itself.
1814 <title>Side Effects</title>
1818 <title>Affected by</title>
1822 <title>Exceptional Situations</title>
1828 <refentry id="with-cstring">
1830 <refname>with-cstring</refname>
1831 <refpurpose>Binds a newly created cstring.
1833 <refclass>Macro</refclass>
1836 <title>Syntax</title>
1838 <function>with-cstring</function> <replaceable>(cstring string) {body}</replaceable>
1842 <title>Arguments and Values</title>
1845 <term><parameter>cstring</parameter></term>
1847 <para>A symbol naming the cstring to be created.
1852 <term><parameter>string</parameter></term>
1854 <para>A Lisp string that will be translated to a cstring.
1859 <term><parameter>body</parameter></term>
1861 <para>The body of where the cstring will be bound.
1868 <title>Description</title>
1870 Binds a symbol to a cstring created from conversion of a string. Automatically frees the <varname>cstring</varname>.
1874 <title>Examples</title>
1877 (def-function ("getenv" c-getenv)
1879 :returning :cstring)
1882 "Returns an environment variable, or NIL if it does not exist"
1883 (check-type key string)
1884 (with-cstring (key-cstring key)
1885 (convert-from-cstring (c-getenv key-cstring))))
1890 <title>Side Effects</title>
1894 <title>Affected by</title>
1898 <title>Exceptional Situations</title>
1904 <refentry id="convert-from-foreign-string">
1906 <refname>convert-from-foreign-string</refname>
1907 <refpurpose>Converts a foreign string into a Lisp string.
1909 <refclass>Macro</refclass>
1912 <title>Syntax</title>
1914 <function>convert-from-foreign-string</function> <replaceable>foreign-string &key length null-terminated-p</replaceable> => <returnvalue>string</returnvalue>
1918 <title>Arguments and Values</title>
1921 <term><parameter>foreign-string</parameter></term>
1923 <para>A foreign string.
1928 <term><parameter>length</parameter></term>
1930 <para>The length of the foreign string to
1931 convert. The default is the length of the string until a &null;
1932 character is reached.
1937 <term><parameter>null-terminated-p</parameter></term>
1939 <para>A boolean flag with a default value of &t; When true,
1940 the string is converted until the first &null; character is reached.
1945 <term><returnvalue>string</returnvalue></term>
1947 <para>A Lisp string.
1954 <title>Description</title>
1956 Returns a Lisp string from a foreign string.
1957 Can translated ASCII and binary strings.
1961 <title>Side Effects</title>
1965 <title>Affected by</title>
1969 <title>Exceptional Situations</title>
1975 <refentry id="convert-to-foreign-string">
1977 <refname>convert-to-foreign-string</refname>
1978 <refpurpose>Converts a Lisp string to a foreign string.
1980 <refclass>Macro</refclass>
1983 <title>Syntax</title>
1985 <function>convert-to-foreign-string</function> <replaceable>string</replaceable> => <returnvalue>foreign-string</returnvalue>
1989 <title>Arguments and Values</title>
1992 <term><parameter>string</parameter></term>
1994 <para>A Lisp string.
1999 <term><returnvalue>foreign-string</returnvalue></term>
2001 <para>A foreign string.
2008 <title>Description</title>
2010 Converts a Lisp string to a foreign string. Memory should be
2011 freed with <function>free-foreign-object</function>.
2015 <title>Side Effects</title>
2019 <title>Affected by</title>
2023 <title>Exceptional Situations</title>
2030 <refentry id="allocate-foreign-string">
2032 <refname>allocate-foreign-string</refname>
2033 <refpurpose>Allocates space for a foreign string.
2035 <refclass>Macro</refclass>
2038 <title>Syntax</title>
2040 <function>allocate-foreign-string</function> <replaceable>size &key unsigned</replaceable> => <returnvalue>foreign-string</returnvalue>
2044 <title>Arguments and Values</title>
2047 <term><parameter>size</parameter></term>
2049 <para>The size of the space to be allocated in bytes.
2054 <term><parameter>unsigned</parameter></term>
2056 <para>A boolean flag with a default value of &t;. When true,
2057 marks the pointer as an <constant>:unsigned-char</constant>.
2062 <term><returnvalue>foreign-string</returnvalue></term>
2064 <para>A foreign string which has undefined contents.
2071 <title>Description</title>
2073 Allocates space for a foreign string. Memory should
2074 be freed with <function>free-foreign-object</function>.
2078 <title>Side Effects</title>
2082 <title>Affected by</title>
2086 <title>Exceptional Situations</title>
2094 <title>Functions & Libraries</title>
2096 <refentry id="def-function">
2098 <refname>def-function</refname>
2099 <refpurpose>Declares a function.
2101 <refclass>Macro</refclass>
2104 <title>Syntax</title>
2106 <function>def-function</function> <replaceable>name args &key module returning</replaceable>
2110 <title>Arguments and Values</title>
2113 <term><parameter>name</parameter></term>
2115 <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.
2120 <term><parameter>args</parameter></term>
2122 <para>A list of argument declarations. If &nil;, indicates that the function does not take any arguments.
2127 <term><parameter>module</parameter></term>
2129 <para>A string specifying which module (or library) that the foreign function resides. (Required by Lispworks)</para>
2133 <term><returnvalue>returning</returnvalue></term>
2135 <para>A declaration specifying the result type of the
2136 foreign function. If <constant>:void</constant> indicates module does not return any value.
2143 <title>Description</title>
2144 <para>Declares a foreign function.
2148 <title>Examples</title>
2150 (def-function "gethostname"
2151 ((name (* :unsigned-char))
2157 <title>Side Effects</title>
2161 <title>Affected by</title>
2165 <title>Exceptional Situations</title>
2170 <refentry id="load-foreign-library">
2172 <refname>load-foreign-library</refname>
2173 <refpurpose>Loads a foreign library.
2175 <refclass>Function</refclass>
2178 <title>Syntax</title>
2180 <function>load-foreign-library</function> <replaceable>filename &key module supporting-libraries force-load</replaceable> => <returnvalue>success</returnvalue>
2184 <title>Arguments and Values</title>
2187 <term><parameter>filename</parameter></term>
2189 <para>A string or pathname specifying the library location
2190 in the filesystem. At least one implementation (&lw;) can not
2191 accept a logical pathname.
2196 <term><parameter>module</parameter></term>
2198 <para>A string designating the name of the module to apply
2199 to functions in this library. (Required for Lispworks)
2204 <term><parameter>supporting-libraries</parameter></term>
2206 <para>A list of strings naming the libraries required to
2207 link the foreign library. (Required by CMUCL)
2212 <term><parameter>force-load</parameter></term>
2214 <para>Forces the loading of the library if it has been previously loaded.
2219 <term><returnvalue>success</returnvalue></term>
2221 <para>A boolean flag, &t; if the library was able to be
2222 loaded successfully or if the library has been previously loaded,
2230 <title>Description</title>
2231 <para>Loads a foreign library. Applies a module name to functions
2232 within the library. Ensures that a library is only loaded once during
2233 a session. A library can be reloaded by using the <symbol>:force-load</symbol> key.
2237 <title>Examples</title>
2239 (load-foreign-library #p"/usr/lib/libmysqlclient.so"
2241 :supporting-libraries '("c"))
2246 <title>Side Effects</title>
2247 <para>Loads the foreign code into the Lisp system.
2251 <title>Affected by</title>
2252 <para>Ability to load the file.</para>
2255 <title>Exceptional Situations</title>
2260 <refentry id="find-foreign-library">
2262 <refname>find-foreign-library</refname>
2263 <refpurpose>Finds a foreign library file.
2265 <refclass>Function</refclass>
2268 <title>Syntax</title>
2270 <function>find-foreign-library</function> <replaceable>names directories & drive-letters types</replaceable> => <returnvalue>path</returnvalue>
2274 <title>Arguments and Values</title>
2277 <term><parameter>names</parameter></term>
2279 <para>A string or list of strings containing the base name of the library file.
2284 <term><parameter>directories</parameter></term>
2286 <para>A string or list of strings containing the directory the library file.
2291 <term><parameter>drive-letters</parameter></term>
2293 <para>A string or list of strings containing the drive letters for the library file.
2298 <term><parameter>types</parameter></term>
2300 <para>A string or list of strings containing the file type of the library file. Default
2301 is &nil;. If &nil;, will use a default type based on the currently running implementation.
2306 <term><returnvalue>path</returnvalue></term>
2308 <para>A path containing the path found, or &nil; if the library file was not found.
2315 <title>Description</title>
2316 <para>Finds a foreign library by searching through a number of possible locations. Returns
2317 the path of the first found file.
2321 <title>Examples</title>
2323 (find-foreign-library '("libmysqlclient" "libmysql")
2324 '("/opt/mysql/lib/mysql/" "/usr/local/lib/" "/usr/lib/" "/mysql/lib/opt/")
2325 :types '("so" "dll")
2326 :drive-letters '("C" "D" "E"))
2327 => #P"D:\\mysql\\lib\\opt\\libmysql.dll"
2331 <title>Side Effects</title>
2336 <title>Affected by</title>
2340 <title>Exceptional Situations</title>