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="pointer-address">
1055 <refname>pointer-address</refname>
1056 <refpurpose>Returns the address of a pointer.
1058 <refclass>Macro</refclass>
1061 <title>Syntax</title>
1063 <function>pointer-address</function> <replaceable>ptr</replaceable> => <returnvalue>address</returnvalue>
1067 <title>Arguments and Values</title>
1070 <term><parameter>ptr</parameter></term>
1072 <para>A pointer to a foreign object.
1077 <term><parameter>address</parameter></term>
1079 <para>An integer representing the pointer's address.
1086 <title>Description</title>
1088 Returns the address as an integer of a pointer.
1092 <title>Side Effects</title>
1096 <title>Affected by</title>
1100 <title>Exceptional Situations</title>
1106 <refentry id="deref-pointer">
1108 <refname>deref-pointer</refname>
1109 <refpurpose>Deferences a pointer.
1111 <refclass>Macro</refclass>
1114 <title>Syntax</title>
1116 <function>deref-pointer</function> <replaceable>ptr type</replaceable> => <returnvalue>value</returnvalue>
1120 <title>Arguments and Values</title>
1123 <term><parameter>ptr</parameter></term>
1125 <para>A pointer to a foreign object.
1130 <term><parameter>type</parameter></term>
1132 <para>A foreign type of the object being pointed to.
1137 <term><returnvalue>value</returnvalue></term>
1139 <para>The value of the object where the pointer points.
1146 <title>Description</title>
1148 Returns the object to which a pointer points.
1152 <title>Examples</title>
1155 (let ((intp (allocate-foreign-object :int)))
1156 (setf (deref-pointer intp :int) 10)
1158 (deref-pointer intp :int)
1159 (free-foreign-object intp)))
1165 <title>Side Effects</title>
1169 <title>Affected by</title>
1173 <title>Exceptional Situations</title>
1178 <refentry id="ensure-char-character">
1180 <refname>ensure-char-character</refname>
1181 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1184 <refclass>Macro</refclass>
1187 <title>Syntax</title>
1189 <function>ensure-char-character</function> <replaceable>object</replaceable> => <returnvalue>char</returnvalue>
1193 <title>Arguments and Values</title>
1196 <term><parameter>object</parameter></term>
1198 <para>Either a character or a integer specifying a character code.
1203 <term><returnvalue>char</returnvalue></term>
1212 <title>Description</title>
1214 Ensures that an object obtained by dereferencing a
1215 <constant>:char</constant> pointer is a character.
1219 <title>Examples</title>
1222 (let ((fs (convert-to-foreign-string "a")))
1224 (ensure-char-character (deref-pointer fs :char))
1225 (free-foreign-object fs)))
1231 <title>Side Effects</title>
1235 <title>Affected by</title>
1239 <title>Exceptional Situations</title>
1240 <para>Depending upon the implementation and what &uffi; expects, this
1241 macro may signal an error if the object is not a character or
1246 <refentry id="ensure-char-integer">
1248 <refname>ensure-char-integer</refname>
1249 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1252 <refclass>Macro</refclass>
1255 <title>Syntax</title>
1257 <function>ensure-char-integer</function> <replaceable>object</replaceable> => <returnvalue>int</returnvalue>
1261 <title>Arguments and Values</title>
1264 <term><parameter>object</parameter></term>
1266 <para>Either a character or a integer specifying a character code.
1271 <term><returnvalue>int</returnvalue></term>
1280 <title>Description</title>
1282 Ensures that an object obtained by dereferencing a
1283 <constant>:char</constant> pointer is an integer.
1287 <title>Examples</title>
1290 (let ((fs (convert-to-foreign-string "a")))
1292 (ensure-char-integer (deref-pointer fs :char))
1293 (free-foreign-object fs)))
1299 <title>Side Effects</title>
1303 <title>Affected by</title>
1307 <title>Exceptional Situations</title>
1308 <para>Depending upon the implementation and what &uffi; expects, this
1309 macro may signal an error if the object is not a character or
1314 <refentry id="make-null-pointer">
1316 <refname>make-null-pointer</refname>
1317 <refpurpose>Create a &null; pointer.
1319 <refclass>Macro</refclass>
1322 <title>Syntax</title>
1324 <function>make-null-pointer</function> <replaceable>type</replaceable> => <returnvalue>ptr</returnvalue>
1328 <title>Arguments and Values</title>
1331 <term><parameter>type</parameter></term>
1333 <para>A type of object to which the pointer refers.
1338 <term><parameter>ptr</parameter></term>
1340 <para>The &null; pointer of type <parameter>type</parameter>.
1347 <title>Description</title>
1349 Creates a &null; pointer of a specified type.
1353 <title>Side Effects</title>
1357 <title>Affected by</title>
1361 <title>Exceptional Situations</title>
1367 <refentry id="null-pointer-p">
1369 <refname>null-pointer-p</refname>
1370 <refpurpose>Tests a pointer for &null; value.
1372 <refclass>Macro</refclass>
1375 <title>Syntax</title>
1377 <function>null-pointer-p</function> <replaceable>ptr</replaceable> => <returnvalue>is-null</returnvalue>
1381 <title>Arguments and Values</title>
1384 <term><parameter>ptr</parameter></term>
1386 <para>A foreign object pointer.
1391 <term><returnvalue>is-null</returnvalue></term>
1393 <para>The boolean flag.
1400 <title>Description</title>
1402 A predicate testing if a pointer is has a &null; value.
1406 <title>Side Effects</title>
1410 <title>Affected by</title>
1414 <title>Exceptional Situations</title>
1420 <refentry id="null-cstring-pointer">
1422 <refname>+null-cstring-pointer+</refname>
1423 <refpurpose>A constant &null; cstring pointer.
1425 <refclass>Constant</refclass>
1428 <title>Description</title>
1430 A &null; cstring pointer. This can be used for testing
1431 if a cstring returned by a function is &null;.
1439 <title>Strings</title>
1441 <title>Overview</title>
1443 &uffi; has functions to two types of <varname>C</varname>-compatible
1444 strings, <emphasis>cstring</emphasis> and <emphasis>foreign</emphasis> strings.
1445 cstrings are used as parameters to and from functions. An implementation, such as CMUCL,
1446 may not convert these to a foreign type for efficiency sake. Thus, it is not
1447 possible to "allocate" a cstring. In contrast, foreign strings
1448 always need to have memory for them.
1452 <refentry id="convert-from-cstring">
1454 <refname>convert-from-cstring</refname>
1455 <refpurpose>Converts a cstring to a Lisp string.
1457 <refclass>Macro</refclass>
1460 <title>Syntax</title>
1462 <function>convert-from-cstring</function> <replaceable>cstring</replaceable> => <returnvalue>string</returnvalue>
1466 <title>Arguments and Values</title>
1469 <term><parameter>cstring</parameter></term>
1476 <term><returnvalue>string</returnvalue></term>
1478 <para>A Lisp string.
1485 <title>Description</title>
1487 Converts a Lisp string to a <constant>cstring</constant>. This is
1488 most often used when processing the results of a foreign function
1489 that returns a cstring.
1493 <title>Side Effects</title>
1497 <title>Affected by</title>
1501 <title>Exceptional Situations</title>
1507 <refentry id="convert-to-cstring">
1509 <refname>convert-to-cstring</refname>
1510 <refpurpose>Converts a Lisp string to a cstring.
1512 <refclass>Macro</refclass>
1515 <title>Syntax</title>
1517 <function>convert-to-cstring</function> <replaceable>string</replaceable> => <returnvalue>cstring</returnvalue>
1521 <title>Arguments and Values</title>
1524 <term><parameter>string</parameter></term>
1526 <para>A Lisp string.
1531 <term><returnvalue>cstring</returnvalue></term>
1540 <title>Description</title>
1542 Converts a Lisp string to a
1543 <varname>cstring</varname>. The
1544 <varname>cstring</varname> should be freed with
1545 <function>free-cstring</function>.
1549 <title>Side Effects</title>
1553 <title>Affected by</title>
1557 <title>Exceptional Situations</title>
1563 <refentry id="free-cstring">
1565 <refname>free-cstring</refname>
1566 <refpurpose>Free memory used by cstring.
1568 <refclass>Macro</refclass>
1571 <title>Syntax</title>
1573 <function>free-cstring</function> <replaceable>cstring</replaceable>
1577 <title>Arguments and Values</title>
1580 <term><parameter>cstring</parameter></term>
1589 <title>Description</title>
1591 Frees any memory possibly allocated by
1592 <function>convert-to-cstring</function>.
1596 <title>Side Effects</title>
1600 <title>Affected by</title>
1604 <title>Exceptional Situations</title>
1610 <refentry id="with-cstring">
1612 <refname>with-cstring</refname>
1613 <refpurpose>Binds a newly created cstring.
1615 <refclass>Macro</refclass>
1618 <title>Syntax</title>
1620 <function>with-cstring</function> <replaceable>(cstring string) {body}</replaceable>
1624 <title>Arguments and Values</title>
1627 <term><parameter>cstring</parameter></term>
1629 <para>A symbol naming the cstring to be created.
1634 <term><parameter>string</parameter></term>
1636 <para>A Lisp string that will be translated to a cstring.
1641 <term><parameter>body</parameter></term>
1643 <para>The body of where the cstring will be bound.
1650 <title>Description</title>
1652 Binds a lexical variable to a newly allocated <varname>cstring</varname>. Automatically frees <varname>cstring</varname>.
1656 <title>Examples</title>
1659 (def-function ("getenv" c-getenv)
1661 :returning :cstring)
1664 "Returns an environment variable, or NIL if it does not exist"
1665 (check-type key string)
1666 (with-cstring (key-cstring key)
1667 (convert-from-cstring (c-getenv key-cstring))))
1672 <title>Side Effects</title>
1676 <title>Affected by</title>
1680 <title>Exceptional Situations</title>
1686 <refentry id="convert-from-foreign-string">
1688 <refname>convert-from-foreign-string</refname>
1689 <refpurpose>Converts a foreign string into a Lisp string.
1691 <refclass>Macro</refclass>
1694 <title>Syntax</title>
1696 <function>convert-from-foreign-string</function> <replaceable>foreign-string &key length null-terminated-p</replaceable> => <returnvalue>string</returnvalue>
1700 <title>Arguments and Values</title>
1703 <term><parameter>foreign-string</parameter></term>
1705 <para>A foreign string.
1710 <term><parameter>length</parameter></term>
1712 <para>The length of the foreign string to
1713 convert. The default is the length of the string until a &null;
1714 character is reached.
1719 <term><parameter>null-terminated-p</parameter></term>
1721 <para>A boolean flag with a default value of &t; When true,
1722 the string is converted until the first &null; character is reached.
1727 <term><returnvalue>string</returnvalue></term>
1729 <para>A Lisp string.
1736 <title>Description</title>
1738 Returns a Lisp string from a foreign string.
1739 Can translated ASCII and binary strings.
1743 <title>Side Effects</title>
1747 <title>Affected by</title>
1751 <title>Exceptional Situations</title>
1757 <refentry id="convert-to-foreign-string">
1759 <refname>convert-to-foreign-string</refname>
1760 <refpurpose>Converts a Lisp string to a foreign string.
1762 <refclass>Macro</refclass>
1765 <title>Syntax</title>
1767 <function>convert-to-foreign-string</function> <replaceable>string</replaceable> => <returnvalue>foreign-string</returnvalue>
1771 <title>Arguments and Values</title>
1774 <term><parameter>string</parameter></term>
1776 <para>A Lisp string.
1781 <term><returnvalue>foreign-string</returnvalue></term>
1783 <para>A foreign string.
1790 <title>Description</title>
1792 Converts a Lisp string to a foreign string. Memory should be
1793 freed with <function>free-foreign-object</function>.
1797 <title>Side Effects</title>
1801 <title>Affected by</title>
1805 <title>Exceptional Situations</title>
1812 <refentry id="allocate-foreign-string">
1814 <refname>allocate-foreign-string</refname>
1815 <refpurpose>Allocates space for a foreign string.
1817 <refclass>Macro</refclass>
1820 <title>Syntax</title>
1822 <function>allocate-foreign-string</function> <replaceable>size &key unsigned</replaceable> => <returnvalue>foreign-string</returnvalue>
1826 <title>Arguments and Values</title>
1829 <term><parameter>size</parameter></term>
1831 <para>The size of the space to be allocated in bytes.
1836 <term><parameter>unsigned</parameter></term>
1838 <para>A boolean flag with a default value of &t;. When true,
1839 marks the pointer as an <constant>:unsigned-char</constant>.
1844 <term><returnvalue>foreign-string</returnvalue></term>
1846 <para>A foreign string which has undefined contents.
1853 <title>Description</title>
1855 Allocates space for a foreign string. Memory should
1856 be freed with <function>free-foreign-object</function>.
1860 <title>Side Effects</title>
1864 <title>Affected by</title>
1868 <title>Exceptional Situations</title>
1876 <title>Functions & Libraries</title>
1878 <refentry id="def-function">
1880 <refname>def-function</refname>
1881 <refpurpose>Declares a function.
1883 <refclass>Macro</refclass>
1886 <title>Syntax</title>
1888 <function>def-function</function> <replaceable>name args &key module returning</replaceable>
1892 <title>Arguments and Values</title>
1895 <term><parameter>name</parameter></term>
1897 <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.
1902 <term><parameter>args</parameter></term>
1904 <para>A list of argument declarations. If &nil;, indicates that the function does not take any arguments.
1909 <term><parameter>module</parameter></term>
1911 <para>A string specifying which module (or library) that the foreign function resides. (Required by Lispworks)</para>
1915 <term><returnvalue>returning</returnvalue></term>
1917 <para>A declaration specifying the result type of the
1918 foreign function. If <constant>:void</constant> indicates module does not return any value.
1925 <title>Description</title>
1926 <para>Declares a foreign function.
1930 <title>Examples</title>
1932 (def-function "gethostname"
1933 ((name (* :unsigned-char))
1939 <title>Side Effects</title>
1943 <title>Affected by</title>
1947 <title>Exceptional Situations</title>
1952 <refentry id="load-foreign-library">
1954 <refname>load-foreign-library</refname>
1955 <refpurpose>Loads a foreign library.
1957 <refclass>Function</refclass>
1960 <title>Syntax</title>
1962 <function>load-foreign-library</function> <replaceable>filename &key module supporting-libraries</replaceable> => <returnvalue>success</returnvalue>
1966 <title>Arguments and Values</title>
1969 <term><parameter>filename</parameter></term>
1971 <para>A string or pathname specifying the library location
1972 in the filesystem. At least one implementation (&lw;) can not
1973 accept a logical pathname.
1978 <term><parameter>module</parameter></term>
1980 <para>A string designating the name of the module to apply
1981 to functions in this library. (Required for Lispworks)
1986 <term><parameter>supporting-libraries</parameter></term>
1988 <para>A list of strings naming the libraries required to
1989 link the foreign library. (Required by CMUCL)
1994 <term><returnvalue>success</returnvalue></term>
1996 <para>A boolean flag, &t; if the library was able to be
1997 loaded successfully or if the library has been previously loaded,
2005 <title>Description</title>
2006 <para>Loads a foreign library. Applies a module name to functions
2007 within the library. Ensures that a library is only loaded once during
2012 <title>Examples</title>
2014 (load-foreign-library #p"/usr/lib/libmysqlclient.so"
2016 :supporting-libraries '("c"))
2021 <title>Side Effects</title>
2022 <para>Loads the foreign code into the Lisp system.
2026 <title>Affected by</title>
2027 <para>Ability to load the file.</para>
2030 <title>Exceptional Situations</title>
2035 <refentry id="find-foreign-library">
2037 <refname>find-foreign-library</refname>
2038 <refpurpose>Finds a foreign library file.
2040 <refclass>Function</refclass>
2043 <title>Syntax</title>
2045 <function>find-foreign-library</function> <replaceable>names directories & drive-letters types</replaceable> => <returnvalue>path</returnvalue>
2049 <title>Arguments and Values</title>
2052 <term><parameter>names</parameter></term>
2054 <para>A string or list of strings containing the base name of the library file.
2059 <term><parameter>directories</parameter></term>
2061 <para>A string or list of strings containing the directory the library file.
2066 <term><parameter>drive-letters</parameter></term>
2068 <para>A string or list of strings containing the drive letters for the library file.
2073 <term><parameter>types</parameter></term>
2075 <para>A string or list of strings containing the file type of the library file. Default
2076 is &nil;. If &nil;, will use a default type based on the currently running implementation.
2081 <term><returnvalue>path</returnvalue></term>
2083 <para>A path containing the path found, or &nil; if the library file was not found.
2090 <title>Description</title>
2091 <para>Finds a foreign library by searching through a number of possible locations. Returns
2092 the path of the first found file.
2096 <title>Examples</title>
2098 (find-foreign-library '("libmysqlclient" "libmysql")
2099 '("/opt/mysql/lib/mysql/" "/usr/local/lib/" "/usr/lib/" "/mysql/lib/opt/")
2100 :types '("so" "dll")
2101 :drive-letters '("C" "D" "E"))
2102 => #P"D:\\mysql\\lib\\opt\\libmysql.dll"
2106 <title>Side Effects</title>
2111 <title>Affected by</title>
2115 <title>Exceptional Situations</title>