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>
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))))
771 <title>Side Effects</title>
775 <title>Affected by</title>
779 <title>Exceptional Situations</title>
784 <refentry id="def-union">
786 <refname>def-union</refname>
787 <refpurpose>Defines a foreign union type.
789 <refclass>Macro</refclass>
792 <title>Syntax</title>
794 <function>def-union</function> <replaceable>name &rest fields</replaceable>
798 <title>Arguments and Values</title>
801 <term><parameter>name</parameter></term>
803 <para>A name of the new union type.
808 <term><parameter>fields</parameter></term>
810 <para>A list of fields of the union.
817 <title>Description</title>
819 Defines a foreign union type.
823 <title>Examples</title>
825 (def-union test-union
829 (let ((u (allocate-foreign-object 'test-union))
830 (setf (get-slot-value u 'test-union 'an-int) (+ 65 (* 66 256)))
832 (ensure-char-character (get-slot-value u 'test-union 'a-char))
833 (free-foreign-object u)))
838 <title>Side Effects</title>
839 <para>Defines a new foreign type.</para>
842 <title>Affected by</title>
846 <title>Exceptional Situations</title>
855 <title>Objects</title>
857 <title>Overview</title>
859 Objects are entities that can allocated, referred to by pointers, and
865 <refentry id="allocate-foreign-object">
867 <refname>allocate-foreign-object</refname>
868 <refpurpose>Allocates an instance of a foreign object.
870 <refclass>Macro</refclass>
873 <title>Syntax</title>
875 <function>allocate-foreign-object</function> <replaceable>type &optional size</replaceable> => <returnvalue>ptr</returnvalue>
879 <title>Arguments and Values</title>
882 <term><parameter>type</parameter></term>
884 <para>The type of foreign object to allocate. This parameter is evaluated.
889 <term><parameter>size</parameter></term>
891 <para>An optional size parameter that is evaluated. If specified, allocates and returns an
892 array of <parameter>type</parameter> that is <parameter>size</parameter> members long. This parameter is evaluated.
897 <term><returnvalue>ptr</returnvalue></term>
899 <para>A pointer to the foreign object.
906 <title>Description</title>
908 Allocates an instance of a foreign object. It returns a pointer to the object.
912 <title>Examples</title>
914 (def-struct ab (a :int) (b :double))
915 (allocate-foreign-object 'ab)
920 <title>Side Effects</title>
924 <title>Affected by</title>
928 <title>Exceptional Situations</title>
934 <refentry id="free-foreign-object">
936 <refname>free-foreign-object</refname>
937 <refpurpose>Frees memory that was allocated for a foreign boject.
939 <refclass>Macro</refclass>
942 <title>Syntax</title>
944 <function>free-foreign-object</function> <replaceable>ptr</replaceable>
948 <title>Arguments and Values</title>
951 <term><parameter>ptr</parameter></term>
953 <para>A pointer to the allocated foreign object to free.
960 <title>Description</title>
962 Frees the memory used by the allocation of a foreign object.
966 <title>Side Effects</title>
970 <title>Affected by</title>
974 <title>Exceptional Situations</title>
980 <refentry id="with-foreign-object">
982 <refname>with-foreign-object</refname>
983 <refpurpose>Wraps the allocation of a foreign object around a body of code.
985 <refclass>Macro</refclass>
988 <title>Syntax</title>
990 <function>with-foreign-object</function> <replaceable>(var type) &body body</replaceable> => <returnvalue>form-return</returnvalue>
994 <title>Arguments and Values</title>
997 <term><parameter>var</parameter></term>
999 <para>The variable name to bind.
1004 <term><parameter>type</parameter></term>
1006 <para>The type of foreign object to allocate. This parameter is evaluated.
1011 <term><returnvalue>form-return</returnvalue></term>
1013 <para>The result of evaluating the <parameter>body</parameter>.
1020 <title>Description</title>
1022 This function wraps the allocation, binding, and destruction of a foreign object.
1024 &lw; platforms the object is stack allocated for efficiency. Benchmarks show that &acl; performs
1025 much better with static allocation.
1029 <title>Examples</title>
1031 (defun gethostname2 ()
1032 "Returns the hostname"
1033 (uffi:with-foreign-object (name '(:array :unsigned-char 256))
1034 (if (zerop (c-gethostname (uffi:char-array-to-pointer name) 256))
1035 (uffi:convert-from-foreign-string name)
1036 (error "gethostname() failed."))))
1040 <title>Side Effects</title>
1044 <title>Affected by</title>
1048 <title>Exceptional Situations</title>
1053 <refentry id="size-of-foreign-type">
1055 <refname>size-of-foreign-type</refname>
1056 <refpurpose>Returns the number of data bytes used by a foreign object type.
1058 <refclass>Macro</refclass>
1061 <title>Syntax</title>
1063 <function>size-of-foreign-type</function> <replaceable>ftype</replaceable>
1067 <title>Arguments and Values</title>
1070 <term><parameter>ftype</parameter></term>
1072 <para>A foreign type specifier. This parameter is evaluated.
1079 <title>Description</title>
1081 Returns the number of data bytes used by a foreign object type. This does not include any Lisp storage overhead.
1085 <title>Examples</title>
1088 (size-of-foreign-object :unsigned-byte)
1090 (size-of-foreign-object 'my-100-byte-vector-type)
1096 <title>Side Effects</title>
1098 </refsect1> <refsect1>
1099 <title>Affected by</title>
1103 <title>Exceptional Situations</title>
1108 <refentry id="pointer-address">
1110 <refname>pointer-address</refname>
1111 <refpurpose>Returns the address of a pointer.
1113 <refclass>Macro</refclass>
1116 <title>Syntax</title>
1118 <function>pointer-address</function> <replaceable>ptr</replaceable> => <returnvalue>address</returnvalue>
1122 <title>Arguments and Values</title>
1125 <term><parameter>ptr</parameter></term>
1127 <para>A pointer to a foreign object.
1132 <term><parameter>address</parameter></term>
1134 <para>An integer representing the pointer's address.
1141 <title>Description</title>
1143 Returns the address as an integer of a pointer.
1147 <title>Side Effects</title>
1151 <title>Affected by</title>
1155 <title>Exceptional Situations</title>
1161 <refentry id="deref-pointer">
1163 <refname>deref-pointer</refname>
1164 <refpurpose>Deferences a pointer.
1166 <refclass>Macro</refclass>
1169 <title>Syntax</title>
1171 <function>deref-pointer</function> <replaceable>ptr type</replaceable> => <returnvalue>value</returnvalue>
1175 <title>Arguments and Values</title>
1178 <term><parameter>ptr</parameter></term>
1180 <para>A pointer to a foreign object.
1185 <term><parameter>type</parameter></term>
1187 <para>A foreign type of the object being pointed to.
1192 <term><returnvalue>value</returnvalue></term>
1194 <para>The value of the object where the pointer points.
1201 <title>Description</title>
1203 Returns the object to which a pointer points.
1207 <title>Examples</title>
1210 (let ((intp (allocate-foreign-object :int)))
1211 (setf (deref-pointer intp :int) 10)
1213 (deref-pointer intp :int)
1214 (free-foreign-object intp)))
1220 <title>Side Effects</title>
1224 <title>Affected by</title>
1228 <title>Exceptional Situations</title>
1233 <refentry id="ensure-char-character">
1235 <refname>ensure-char-character</refname>
1236 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1239 <refclass>Macro</refclass>
1242 <title>Syntax</title>
1244 <function>ensure-char-character</function> <replaceable>object</replaceable> => <returnvalue>char</returnvalue>
1248 <title>Arguments and Values</title>
1251 <term><parameter>object</parameter></term>
1253 <para>Either a character or a integer specifying a character code.
1258 <term><returnvalue>char</returnvalue></term>
1267 <title>Description</title>
1269 Ensures that an object obtained by dereferencing a
1270 <constant>:char</constant> pointer is a character.
1274 <title>Examples</title>
1277 (let ((fs (convert-to-foreign-string "a")))
1279 (ensure-char-character (deref-pointer fs :char))
1280 (free-foreign-object fs)))
1286 <title>Side Effects</title>
1290 <title>Affected by</title>
1294 <title>Exceptional Situations</title>
1295 <para>Depending upon the implementation and what &uffi; expects, this
1296 macro may signal an error if the object is not a character or
1301 <refentry id="ensure-char-integer">
1303 <refname>ensure-char-integer</refname>
1304 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1307 <refclass>Macro</refclass>
1310 <title>Syntax</title>
1312 <function>ensure-char-integer</function> <replaceable>object</replaceable> => <returnvalue>int</returnvalue>
1316 <title>Arguments and Values</title>
1319 <term><parameter>object</parameter></term>
1321 <para>Either a character or a integer specifying a character code.
1326 <term><returnvalue>int</returnvalue></term>
1335 <title>Description</title>
1337 Ensures that an object obtained by dereferencing a
1338 <constant>:char</constant> pointer is an integer.
1342 <title>Examples</title>
1345 (let ((fs (convert-to-foreign-string "a")))
1347 (ensure-char-integer (deref-pointer fs :char))
1348 (free-foreign-object fs)))
1354 <title>Side Effects</title>
1358 <title>Affected by</title>
1362 <title>Exceptional Situations</title>
1363 <para>Depending upon the implementation and what &uffi; expects, this
1364 macro may signal an error if the object is not a character or
1369 <refentry id="make-null-pointer">
1371 <refname>make-null-pointer</refname>
1372 <refpurpose>Create a &null; pointer.
1374 <refclass>Macro</refclass>
1377 <title>Syntax</title>
1379 <function>make-null-pointer</function> <replaceable>type</replaceable> => <returnvalue>ptr</returnvalue>
1383 <title>Arguments and Values</title>
1386 <term><parameter>type</parameter></term>
1388 <para>A type of object to which the pointer refers.
1393 <term><parameter>ptr</parameter></term>
1395 <para>The &null; pointer of type <parameter>type</parameter>.
1402 <title>Description</title>
1404 Creates a &null; pointer of a specified type.
1408 <title>Side Effects</title>
1412 <title>Affected by</title>
1416 <title>Exceptional Situations</title>
1422 <refentry id="null-pointer-p">
1424 <refname>null-pointer-p</refname>
1425 <refpurpose>Tests a pointer for &null; value.
1427 <refclass>Macro</refclass>
1430 <title>Syntax</title>
1432 <function>null-pointer-p</function> <replaceable>ptr</replaceable> => <returnvalue>is-null</returnvalue>
1436 <title>Arguments and Values</title>
1439 <term><parameter>ptr</parameter></term>
1441 <para>A foreign object pointer.
1446 <term><returnvalue>is-null</returnvalue></term>
1448 <para>The boolean flag.
1455 <title>Description</title>
1457 A predicate testing if a pointer is has a &null; value.
1461 <title>Side Effects</title>
1465 <title>Affected by</title>
1469 <title>Exceptional Situations</title>
1475 <refentry id="null-cstring-pointer">
1477 <refname>+null-cstring-pointer+</refname>
1478 <refpurpose>A constant &null; cstring pointer.
1480 <refclass>Constant</refclass>
1483 <title>Description</title>
1485 A &null; cstring pointer. This can be used for testing
1486 if a cstring returned by a function is &null;.
1494 <title>Strings</title>
1496 <title>Overview</title>
1499 &uffi; has functions to two types of
1500 <varname>C</varname>-compatible
1501 strings: <emphasis>cstring</emphasis> and
1502 <emphasis>foreign</emphasis> strings.
1504 cstrings are used <emphasis>only</emphasis> as parameters to and from
1505 functions. In some implementations a cstring is not a foreign type but
1506 rather the Lisp string itself. On other platforms a cstring is a newly
1507 allocated foreign vector for storing characters. The following is an
1508 example of using cstrings to both send and return a value.
1512 (uffi:def-function ("getenv" c-getenv)
1514 :returning :cstring)
1516 (defun my-getenv (key)
1517 "Returns an environment variable, or NIL if it does not exist"
1518 (check-type key string)
1519 (uffi:with-cstring (key-native key)
1520 (uffi:convert-from-cstring (c-getenv key-native))))
1523 <para> In contrast, foreign strings are always a foreign vector of
1524 characters which have memory allocated. Thus, if you need to allocate
1525 memory to hold the return value of a string, you must use a foreign
1526 string and not a cstring. The following is an example of using a foreign
1527 string for a return value. </para>
1530 (uffi:def-function ("gethostname" c-gethostname)
1531 ((name (* :unsigned-char))
1535 (defun gethostname ()
1536 "Returns the hostname"
1537 (let* ((name (uffi:allocate-foreign-string 256))
1538 (result-code (c-gethostname name 256))
1539 (hostname (when (zerop result-code)
1540 (uffi:convert-from-foreign-string name))))
1541 (uffi:free-foreign-object name)
1542 (unless (zerop result-code)
1543 (error "gethostname() failed."))))
1548 <refentry id="convert-from-cstring">
1550 <refname>convert-from-cstring</refname>
1551 <refpurpose>Converts a cstring to a Lisp string.
1553 <refclass>Macro</refclass>
1556 <title>Syntax</title>
1558 <function>convert-from-cstring</function> <replaceable>cstring</replaceable> => <returnvalue>string</returnvalue>
1562 <title>Arguments and Values</title>
1565 <term><parameter>cstring</parameter></term>
1572 <term><returnvalue>string</returnvalue></term>
1574 <para>A Lisp string.
1581 <title>Description</title>
1583 Converts a Lisp string to a <constant>cstring</constant>. This is
1584 most often used when processing the results of a foreign function
1585 that returns a cstring.
1589 <title>Side Effects</title>
1593 <title>Affected by</title>
1597 <title>Exceptional Situations</title>
1603 <refentry id="convert-to-cstring">
1605 <refname>convert-to-cstring</refname>
1606 <refpurpose>Converts a Lisp string to a cstring.
1608 <refclass>Macro</refclass>
1611 <title>Syntax</title>
1613 <function>convert-to-cstring</function> <replaceable>string</replaceable> => <returnvalue>cstring</returnvalue>
1617 <title>Arguments and Values</title>
1620 <term><parameter>string</parameter></term>
1622 <para>A Lisp string.
1627 <term><returnvalue>cstring</returnvalue></term>
1636 <title>Description</title>
1638 Converts a Lisp string to a
1639 <varname>cstring</varname>. The
1640 <varname>cstring</varname> should be freed with
1641 <function>free-cstring</function>.
1645 <title>Side Effects</title>
1646 <para>On some implementations, this function allocates memory.</para>
1649 <title>Affected by</title>
1653 <title>Exceptional Situations</title>
1659 <refentry id="free-cstring">
1661 <refname>free-cstring</refname>
1662 <refpurpose>Free memory used by cstring.
1664 <refclass>Macro</refclass>
1667 <title>Syntax</title>
1669 <function>free-cstring</function> <replaceable>cstring</replaceable>
1673 <title>Arguments and Values</title>
1676 <term><parameter>cstring</parameter></term>
1685 <title>Description</title>
1687 Frees any memory possibly allocated by
1688 <function>convert-to-cstring</function>. On some implementions, a cstring is just the Lisp string itself.
1692 <title>Side Effects</title>
1696 <title>Affected by</title>
1700 <title>Exceptional Situations</title>
1706 <refentry id="with-cstring">
1708 <refname>with-cstring</refname>
1709 <refpurpose>Binds a newly created cstring.
1711 <refclass>Macro</refclass>
1714 <title>Syntax</title>
1716 <function>with-cstring</function> <replaceable>(cstring string) {body}</replaceable>
1720 <title>Arguments and Values</title>
1723 <term><parameter>cstring</parameter></term>
1725 <para>A symbol naming the cstring to be created.
1730 <term><parameter>string</parameter></term>
1732 <para>A Lisp string that will be translated to a cstring.
1737 <term><parameter>body</parameter></term>
1739 <para>The body of where the cstring will be bound.
1746 <title>Description</title>
1748 Binds a symbol to a cstring created from conversion of a string. Automatically frees the <varname>cstring</varname>.
1752 <title>Examples</title>
1755 (def-function ("getenv" c-getenv)
1757 :returning :cstring)
1760 "Returns an environment variable, or NIL if it does not exist"
1761 (check-type key string)
1762 (with-cstring (key-cstring key)
1763 (convert-from-cstring (c-getenv key-cstring))))
1768 <title>Side Effects</title>
1772 <title>Affected by</title>
1776 <title>Exceptional Situations</title>
1782 <refentry id="convert-from-foreign-string">
1784 <refname>convert-from-foreign-string</refname>
1785 <refpurpose>Converts a foreign string into a Lisp string.
1787 <refclass>Macro</refclass>
1790 <title>Syntax</title>
1792 <function>convert-from-foreign-string</function> <replaceable>foreign-string &key length null-terminated-p</replaceable> => <returnvalue>string</returnvalue>
1796 <title>Arguments and Values</title>
1799 <term><parameter>foreign-string</parameter></term>
1801 <para>A foreign string.
1806 <term><parameter>length</parameter></term>
1808 <para>The length of the foreign string to
1809 convert. The default is the length of the string until a &null;
1810 character is reached.
1815 <term><parameter>null-terminated-p</parameter></term>
1817 <para>A boolean flag with a default value of &t; When true,
1818 the string is converted until the first &null; character is reached.
1823 <term><returnvalue>string</returnvalue></term>
1825 <para>A Lisp string.
1832 <title>Description</title>
1834 Returns a Lisp string from a foreign string.
1835 Can translated ASCII and binary strings.
1839 <title>Side Effects</title>
1843 <title>Affected by</title>
1847 <title>Exceptional Situations</title>
1853 <refentry id="convert-to-foreign-string">
1855 <refname>convert-to-foreign-string</refname>
1856 <refpurpose>Converts a Lisp string to a foreign string.
1858 <refclass>Macro</refclass>
1861 <title>Syntax</title>
1863 <function>convert-to-foreign-string</function> <replaceable>string</replaceable> => <returnvalue>foreign-string</returnvalue>
1867 <title>Arguments and Values</title>
1870 <term><parameter>string</parameter></term>
1872 <para>A Lisp string.
1877 <term><returnvalue>foreign-string</returnvalue></term>
1879 <para>A foreign string.
1886 <title>Description</title>
1888 Converts a Lisp string to a foreign string. Memory should be
1889 freed with <function>free-foreign-object</function>.
1893 <title>Side Effects</title>
1897 <title>Affected by</title>
1901 <title>Exceptional Situations</title>
1908 <refentry id="allocate-foreign-string">
1910 <refname>allocate-foreign-string</refname>
1911 <refpurpose>Allocates space for a foreign string.
1913 <refclass>Macro</refclass>
1916 <title>Syntax</title>
1918 <function>allocate-foreign-string</function> <replaceable>size &key unsigned</replaceable> => <returnvalue>foreign-string</returnvalue>
1922 <title>Arguments and Values</title>
1925 <term><parameter>size</parameter></term>
1927 <para>The size of the space to be allocated in bytes.
1932 <term><parameter>unsigned</parameter></term>
1934 <para>A boolean flag with a default value of &t;. When true,
1935 marks the pointer as an <constant>:unsigned-char</constant>.
1940 <term><returnvalue>foreign-string</returnvalue></term>
1942 <para>A foreign string which has undefined contents.
1949 <title>Description</title>
1951 Allocates space for a foreign string. Memory should
1952 be freed with <function>free-foreign-object</function>.
1956 <title>Side Effects</title>
1960 <title>Affected by</title>
1964 <title>Exceptional Situations</title>
1972 <title>Functions & Libraries</title>
1974 <refentry id="def-function">
1976 <refname>def-function</refname>
1977 <refpurpose>Declares a function.
1979 <refclass>Macro</refclass>
1982 <title>Syntax</title>
1984 <function>def-function</function> <replaceable>name args &key module returning</replaceable>
1988 <title>Arguments and Values</title>
1991 <term><parameter>name</parameter></term>
1993 <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.
1998 <term><parameter>args</parameter></term>
2000 <para>A list of argument declarations. If &nil;, indicates that the function does not take any arguments.
2005 <term><parameter>module</parameter></term>
2007 <para>A string specifying which module (or library) that the foreign function resides. (Required by Lispworks)</para>
2011 <term><returnvalue>returning</returnvalue></term>
2013 <para>A declaration specifying the result type of the
2014 foreign function. If <constant>:void</constant> indicates module does not return any value.
2021 <title>Description</title>
2022 <para>Declares a foreign function.
2026 <title>Examples</title>
2028 (def-function "gethostname"
2029 ((name (* :unsigned-char))
2035 <title>Side Effects</title>
2039 <title>Affected by</title>
2043 <title>Exceptional Situations</title>
2048 <refentry id="load-foreign-library">
2050 <refname>load-foreign-library</refname>
2051 <refpurpose>Loads a foreign library.
2053 <refclass>Function</refclass>
2056 <title>Syntax</title>
2058 <function>load-foreign-library</function> <replaceable>filename &key module supporting-libraries</replaceable> => <returnvalue>success</returnvalue>
2062 <title>Arguments and Values</title>
2065 <term><parameter>filename</parameter></term>
2067 <para>A string or pathname specifying the library location
2068 in the filesystem. At least one implementation (&lw;) can not
2069 accept a logical pathname.
2074 <term><parameter>module</parameter></term>
2076 <para>A string designating the name of the module to apply
2077 to functions in this library. (Required for Lispworks)
2082 <term><parameter>supporting-libraries</parameter></term>
2084 <para>A list of strings naming the libraries required to
2085 link the foreign library. (Required by CMUCL)
2090 <term><returnvalue>success</returnvalue></term>
2092 <para>A boolean flag, &t; if the library was able to be
2093 loaded successfully or if the library has been previously loaded,
2101 <title>Description</title>
2102 <para>Loads a foreign library. Applies a module name to functions
2103 within the library. Ensures that a library is only loaded once during
2108 <title>Examples</title>
2110 (load-foreign-library #p"/usr/lib/libmysqlclient.so"
2112 :supporting-libraries '("c"))
2117 <title>Side Effects</title>
2118 <para>Loads the foreign code into the Lisp system.
2122 <title>Affected by</title>
2123 <para>Ability to load the file.</para>
2126 <title>Exceptional Situations</title>
2131 <refentry id="find-foreign-library">
2133 <refname>find-foreign-library</refname>
2134 <refpurpose>Finds a foreign library file.
2136 <refclass>Function</refclass>
2139 <title>Syntax</title>
2141 <function>find-foreign-library</function> <replaceable>names directories & drive-letters types</replaceable> => <returnvalue>path</returnvalue>
2145 <title>Arguments and Values</title>
2148 <term><parameter>names</parameter></term>
2150 <para>A string or list of strings containing the base name of the library file.
2155 <term><parameter>directories</parameter></term>
2157 <para>A string or list of strings containing the directory the library file.
2162 <term><parameter>drive-letters</parameter></term>
2164 <para>A string or list of strings containing the drive letters for the library file.
2169 <term><parameter>types</parameter></term>
2171 <para>A string or list of strings containing the file type of the library file. Default
2172 is &nil;. If &nil;, will use a default type based on the currently running implementation.
2177 <term><returnvalue>path</returnvalue></term>
2179 <para>A path containing the path found, or &nil; if the library file was not found.
2186 <title>Description</title>
2187 <para>Finds a foreign library by searching through a number of possible locations. Returns
2188 the path of the first found file.
2192 <title>Examples</title>
2194 (find-foreign-library '("libmysqlclient" "libmysql")
2195 '("/opt/mysql/lib/mysql/" "/usr/local/lib/" "/usr/lib/" "/mysql/lib/opt/")
2196 :types '("so" "dll")
2197 :drive-letters '("C" "D" "E"))
2198 => #P"D:\\mysql\\lib\\opt\\libmysql.dll"
2202 <title>Side Effects</title>
2207 <title>Affected by</title>
2211 <title>Exceptional Situations</title>