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> - Unsigned 8-bits. A
100 dereferenced :byte pointer returns an integer.</para>
103 <para><constant>:short</constant> - Signed 16-bits.</para>
106 <para><constant>:unsigned-short</constant> - Unsigned 16-bits.</para>
109 <para><constant>:int</constant> - Signed 32-bits.</para>
112 <para><constant>:unsigned-int</constant> - Unsigned 32-bits.</para>
115 <para><constant>:long</constant> - Signed 32-bits.</para>
118 <para><constant>:unsigned-long</constant> - Unsigned 32-bits.</para>
121 <para><constant>:float</constant> - 32-bit floating point.</para>
124 <para><constant>:double</constant> - 64-bit floating point.</para>
127 <para><constant>:cstring</constant> -
128 A &null; terminated string used for passing and returning characters strings with a &c; function.
132 <para><constant>:void</constant> -
133 The absence of a value. Used to indicate that a function does not return a value.</para>
136 <para><constant>:pointer-void</constant> -
137 Points to a generic object.</para>
140 <para><constant>*</constant> - Used to declare a pointer to an object</para>
145 <refentry id="def-constant">
147 <refname>def-constant</refname>
148 <refpurpose>Binds a symbol to a constant.
150 <refclass>Macro</refclass>
153 <title>Syntax</title>
155 <function>def-constant</function> <replaceable>name value &key export</replaceable>
159 <title>Arguments and Values</title>
162 <term><parameter>name</parameter></term>
164 <para>A symbol that will be bound to the value.
169 <term><parameter>value</parameter></term>
171 <para>An evaluated form that is bound the the name.
176 <term><parameter>export</parameter></term>
178 <para>When &t;, the name is exported from the current package. The default is &nil;</para>
184 <title>Description</title>
186 This is a thin wrapper around
187 <function>defconstant</function>. It evaluates at
188 compile-time and optionally exports the symbol from the package.
192 <title>Examples</title>
194 (def-constant pi2 (* 2 pi))
195 (def-constant exported-pi2 (* 2 pi) :export t)
199 <title>Side Effects</title>
200 <para>Creates a new special variable..</para>
203 <title>Affected by</title>
207 <title>Exceptional Situations</title>
212 <refentry id="def-foreign-type">
214 <refname>def-foreign-type</refname>
215 <refpurpose>Defines a new foreign type.
217 <refclass>Macro</refclass>
220 <title>Syntax</title>
222 <function>def-foreign-type</function> <replaceable>name type</replaceable>
226 <title>Arguments and Values</title>
229 <term><parameter>name</parameter></term>
231 <para>A symbol naming the new foreign type.
236 <term><parameter>value</parameter></term>
238 <para>A form that is not evaluated that defines the new
246 <title>Description</title>
247 <para>Defines a new foreign type.
251 <title>Examples</title>
253 (def-foreign-type my-generic-pointer :pointer-void)
254 (def-foreign-type a-double-float :double-float)
255 (def-foreign-type char-ptr (* :char))
259 <title>Side Effects</title>
260 <para>Defines a new foreign type.
264 <title>Affected by</title>
268 <title>Exceptional Situations</title>
273 <refentry id="null-char-p">
275 <refname>null-char-p</refname>
276 <refpurpose>Tests a character for &null; value.
278 <refclass>Macro</refclass>
281 <title>Syntax</title>
283 <function>null-char-p</function> <replaceable>char</replaceable> => <returnvalue>is-null</returnvalue>
287 <title>Arguments and Values</title>
290 <term><parameter>char</parameter></term>
292 <para>A character or integer.
297 <term><parameter>is-null</parameter></term>
299 <para>A boolean flag indicating if char is a &null; value.
306 <title>Description</title>
308 A predicate testing if a character or integer is &null;. This
309 abstracts the difference in implementations where some return a
310 <computeroutput>character</computeroutput> and some return a
311 <computeroutput>integer</computeroutput> whence dereferencing a
312 <computeroutput>C</computeroutput> character pointer.
316 <title>Examples</title>
318 (def-array-pointer ca :unsigned-char)
319 (let ((fs (convert-to-foreign-string "ab")))
320 (values (null-char-p (deref-array fs 'ca 0))
321 (null-char-p (deref-array fs 'ca 2))))
327 <title>Side Effects</title>
332 <title>Affected by</title>
336 <title>Exceptional Situations</title>
343 <title>Aggregate Types</title>
345 <title>Overview</title>
347 Aggregate types are comprised of one or more primitive types.
351 <refentry id="def-enum">
353 <refname>def-enum</refname>
354 <refpurpose>Defines a &c; enumeration.
356 <refclass>Macro</refclass>
359 <title>Syntax</title>
361 <function>def-enum</function> <replaceable>name fields &key separator-string</replaceable>
365 <title>Arguments and Values</title>
368 <term><parameter>name</parameter></term>
370 <para>A symbol that names the enumeration.
375 <term><parameter>fields</parameter></term>
377 <para>A list of field defintions. Each definition can be
378 a symbol or a list of two elements. Symbols get assigned a value of the
379 current counter which starts at <computeroutput>0</computeroutput> and
380 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
381 position is the value to assign the the symbol. The current counter gets set
382 to <computeroutput>1+</computeroutput> this value.
387 <term><parameter>separator-string</parameter></term>
389 <para>A string that governs the creation of constants. The
390 default is <computeroutput>"#"</computeroutput>.</para>
396 <title>Description</title>
398 Declares a &c; enumeration. It generates constants with integer values for the elements of the enumeration. The symbols for the these constant
399 values are created by the <function>concatenation</function> of the
400 enumeration name, separator-string, and field symbol. Also creates
401 a foreign type with the name <parameter>name</parameter> of type
402 <constant>:int</constant>.
406 <title>Examples</title>
408 (def-enum abc (:a :b :c))
409 ;; Creates constants abc#a (1), abc#b (2), abc#c (3) and defines
410 ;; the foreign type "abc" to be :int
412 (def-enum efoo (:e1 (:e2 10) :e3) :separator-string "-")
413 ;; Creates constants efoo-e1 (1), efoo-e2 (10), efoo-e3 (11) and defines
414 ;; the foreign type efoo to be :int
418 <title>Side Effects</title>
419 <para>Creates a :int foreign type, defines constants.</para>
422 <title>Affected by</title>
426 <title>Exceptional Situations</title>
432 <refentry id="def-struct">
434 <refname>def-struct</refname>
435 <refpurpose>Defines a &c; structure.
437 <refclass>Macro</refclass>
440 <title>Syntax</title>
442 <function>def-struct</function> <replaceable>name &rest fields</replaceable>
446 <title>Arguments and Values</title>
449 <term><parameter>name</parameter></term>
451 <para>A symbol that names the structure.
456 <term><parameter>fields</parameter></term>
458 <para>A variable number of field defintions. Each definition is a list consisting of a symbol naming the field followed by its foreign type.
465 <title>Description</title>
467 Declares a structure. A special type is available as a slot
468 in the field. It is a pointer that points to an instance of the parent
469 structure. It's type is <constant>:pointer-self</constant>.
474 <title>Examples</title>
476 (def-struct foo (a :unsigned-int)
479 (next :pointer-self))
483 <title>Side Effects</title>
484 <para>Creates a foreign type.</para>
487 <title>Affected by</title>
491 <title>Exceptional Situations</title>
497 <refentry id="get-slot-value">
499 <refname>get-slot-value</refname>
500 <refpurpose>Retrieves a value from a slot of a structure.
502 <refclass>Macro</refclass>
505 <title>Syntax</title>
507 <function>get-slot-value</function> <replaceable>obj type field</replaceable> => <returnvalue>value</returnvalue>
511 <title>Arguments and Values</title>
514 <term><parameter>obj</parameter></term>
516 <para>A pointer to foreign structure.
521 <term><parameter>type</parameter></term>
523 <para>A name of the foreign structure.
528 <term><parameter>field</parameter></term>
530 <para>A name of the desired field in foreign structure.
535 <term><returnvalue>value</returnvalue></term>
537 <para>The value of the field in the structure.
544 <title>Description</title>
546 Accesses a slot value from a structure.
550 <title>Examples</title>
552 (get-slot-value foo-ptr 'foo-structure 'field-name)
556 <title>Side Effects</title>
560 <title>Affected by</title>
564 <title>Exceptional Situations</title>
569 <refentry id="get-slot-pointer">
571 <refname>get-slot-pointer</refname>
572 <refpurpose>Retrieves a pointer from a slot of a structure.
574 <refclass>Macro</refclass>
577 <title>Syntax</title>
579 <function>get-slot-pointer</function> <replaceable>obj type field</replaceable> => <returnvalue>pointer</returnvalue>
583 <title>Arguments and Values</title>
586 <term><parameter>obj</parameter></term>
588 <para>A pointer to foreign structure.
593 <term><parameter>type</parameter></term>
595 <para>A name of the foreign structure.
600 <term><parameter>field</parameter></term>
602 <para>A name of the desired field in foreign structure.
607 <term><returnvalue>pointer</returnvalue></term>
609 <para>The value of the field in the structure.
616 <title>Description</title>
618 This is similar to <function>get-slot-value</function>. It
619 is used when the value of a slot is a pointer type.
623 <title>Examples</title>
625 (get-slot-pointer foo-ptr 'foo-structure 'my-char-ptr)
629 <title>Side Effects</title>
633 <title>Affected by</title>
637 <title>Exceptional Situations</title>
643 <refentry id="def-array-pointer">
645 <refname>def-array-pointer</refname>
646 <refpurpose>Defines a pointer to a array of type.
648 <refclass>Macro</refclass>
651 <title>Syntax</title>
653 <function>def-array-pointer</function> <replaceable>name type</replaceable>
657 <title>Arguments and Values</title>
660 <term><parameter>name</parameter></term>
662 <para>A name of the new foreign type.
667 <term><parameter>type</parameter></term>
669 <para>The foreign type of the array elements.
676 <title>Description</title>
678 Defines a type tat is a pointer to an array of type.
682 <title>Examples</title>
684 (def-array-pointer byte-array-pointer :unsigned-char)
688 <title>Side Effects</title>
689 <para>Defines a new foreign type.</para>
692 <title>Affected by</title>
696 <title>Exceptional Situations</title>
702 <refentry id="deref-array">
704 <refname>deref-array</refname>
705 <refpurpose>Deference an array.
707 <refclass>Macro</refclass>
710 <title>Syntax</title>
712 <function>deref-array</function> <replaceable>array type positon</replaceable> => <returnvalue>value</returnvalue>
716 <title>Arguments and Values</title>
719 <term><parameter>array</parameter></term>
721 <para>A foreign array.
726 <term><parameter>type</parameter></term>
728 <para>The foreign type of the array.
733 <term><parameter>position</parameter></term>
735 <para>An integer specifying the position to retrieve from
741 <term><returnvalue>value</returnvalue></term>
743 <para>The value stored in the position of the array.
750 <title>Description</title>
752 Dereferences (retrieves) the value of an array element.
756 <title>Examples</title>
759 (let ((fs (convert-to-foreign-string "ab")))
760 (values (null-char-p (deref-array fs 'ca 0))
761 (null-char-p (deref-array fs 'ca 2))))
767 <title>Side Effects</title>
771 <title>Affected by</title>
775 <title>Exceptional Situations</title>
780 <refentry id="def-union">
782 <refname>def-union</refname>
783 <refpurpose>Defines a foreign union type.
785 <refclass>Macro</refclass>
788 <title>Syntax</title>
790 <function>def-union</function> <replaceable>name &rest fields</replaceable>
794 <title>Arguments and Values</title>
797 <term><parameter>name</parameter></term>
799 <para>A name of the new union type.
804 <term><parameter>fields</parameter></term>
806 <para>A list of fields of the union.
813 <title>Description</title>
815 Defines a foreign union type.
819 <title>Examples</title>
821 (def-union test-union
825 (let ((u (allocate-foreign-object 'test-union))
826 (setf (get-slot-value u 'test-union 'an-int) (+ 65 (* 66 256)))
828 (ensure-char-character (get-slot-value u 'test-union 'a-char))
829 (free-foreign-object u)))
834 <title>Side Effects</title>
835 <para>Defines a new foreign type.</para>
838 <title>Affected by</title>
842 <title>Exceptional Situations</title>
851 <title>Objects</title>
853 <title>Overview</title>
855 Objects are entities that can allocated, referred to by pointers, and
861 <refentry id="allocate-foreign-object">
863 <refname>allocate-foreign-object</refname>
864 <refpurpose>Allocates an instance of a foreign object.
866 <refclass>Macro</refclass>
869 <title>Syntax</title>
871 <function>allocate-foreign-object</function> <replaceable>type &optional size</replaceable> => <returnvalue>ptr</returnvalue>
875 <title>Arguments and Values</title>
878 <term><parameter>type</parameter></term>
880 <para>The type of foreign object to allocate. This parameter is evaluated.
885 <term><parameter>size</parameter></term>
887 <para>An optional size parameter that is evaluated. If specified, allocates and returns an
888 array of <parameter>type</parameter> that is <parameter>size</parameter> members long. This parameter is evaluated.
893 <term><returnvalue>ptr</returnvalue></term>
895 <para>A pointer to the foreign object.
902 <title>Description</title>
904 Allocates an instance of a foreign object. It returns a pointer to the object.
908 <title>Examples</title>
910 (def-struct ab (a :int) (b :double))
911 (allocate-foreign-object 'ab)
916 <title>Side Effects</title>
920 <title>Affected by</title>
924 <title>Exceptional Situations</title>
930 <refentry id="free-foreign-object">
932 <refname>free-foreign-object</refname>
933 <refpurpose>Frees memory that was allocated for a foreign boject.
935 <refclass>Macro</refclass>
938 <title>Syntax</title>
940 <function>free-foreign-object</function> <replaceable>ptr</replaceable>
944 <title>Arguments and Values</title>
947 <term><parameter>ptr</parameter></term>
949 <para>A pointer to the allocated foreign object to free.
956 <title>Description</title>
958 Frees the memory used by the allocation of a foreign object.
962 <title>Side Effects</title>
966 <title>Affected by</title>
970 <title>Exceptional Situations</title>
976 <refentry id="with-foreign-object">
978 <refname>with-foreign-object</refname>
979 <refpurpose>Wraps the allocation of a foreign object around a body of code.
981 <refclass>Macro</refclass>
984 <title>Syntax</title>
986 <function>with-foreign-object</function> <replaceable>(var type) &body body</replaceable> => <returnvalue>form-return</returnvalue>
990 <title>Arguments and Values</title>
993 <term><parameter>var</parameter></term>
995 <para>The variable name to bind.
1000 <term><parameter>type</parameter></term>
1002 <para>The type of foreign object to allocate. This parameter is evaluated.
1007 <term><returnvalue>form-return</returnvalue></term>
1009 <para>The result of evaluating the <parameter>body</parameter>.
1016 <title>Description</title>
1018 This function wraps the allocation, binding, and destruction of a foreign object.
1020 &lw; platforms the object is stack allocated for efficiency. Benchmarks show that &acl; performs
1021 much better with static allocation.
1025 <title>Examples</title>
1027 (defun gethostname2 ()
1028 "Returns the hostname"
1029 (uffi:with-foreign-object (name '(:array :unsigned-char 256))
1030 (if (zerop (c-gethostname (uffi:char-array-to-pointer name) 256))
1031 (uffi:convert-from-foreign-string name)
1032 (error "gethostname() failed."))))
1036 <title>Side Effects</title>
1040 <title>Affected by</title>
1044 <title>Exceptional Situations</title>
1049 <refentry id="pointer-address">
1051 <refname>pointer-address</refname>
1052 <refpurpose>Returns the address of a pointer.
1054 <refclass>Macro</refclass>
1057 <title>Syntax</title>
1059 <function>pointer-address</function> <replaceable>ptr</replaceable> => <returnvalue>address</returnvalue>
1063 <title>Arguments and Values</title>
1066 <term><parameter>ptr</parameter></term>
1068 <para>A pointer to a foreign object.
1073 <term><parameter>address</parameter></term>
1075 <para>An integer representing the pointer's address.
1082 <title>Description</title>
1084 Returns the address as an integer of a pointer.
1088 <title>Side Effects</title>
1092 <title>Affected by</title>
1096 <title>Exceptional Situations</title>
1102 <refentry id="deref-pointer">
1104 <refname>deref-pointer</refname>
1105 <refpurpose>Deferences a pointer.
1107 <refclass>Macro</refclass>
1110 <title>Syntax</title>
1112 <function>deref-pointer</function> <replaceable>ptr type</replaceable> => <returnvalue>value</returnvalue>
1116 <title>Arguments and Values</title>
1119 <term><parameter>ptr</parameter></term>
1121 <para>A pointer to a foreign object.
1126 <term><parameter>type</parameter></term>
1128 <para>A foreign type of the object being pointed to.
1133 <term><returnvalue>value</returnvalue></term>
1135 <para>The value of the object where the pointer points.
1142 <title>Description</title>
1144 Returns the object to which a pointer points.
1148 <title>Examples</title>
1151 (let ((intp (allocate-foreign-object :int)))
1152 (setf (deref-pointer intp :int) 10)
1154 (deref-pointer intp :int)
1155 (free-foreign-object intp)))
1161 <title>Side Effects</title>
1165 <title>Affected by</title>
1169 <title>Exceptional Situations</title>
1174 <refentry id="ensure-char-character">
1176 <refname>ensure-char-character</refname>
1177 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1180 <refclass>Macro</refclass>
1183 <title>Syntax</title>
1185 <function>ensure-char-character</function> <replaceable>object</replaceable> => <returnvalue>char</returnvalue>
1189 <title>Arguments and Values</title>
1192 <term><parameter>object</parameter></term>
1194 <para>Either a character or a integer specifying a character code.
1199 <term><returnvalue>char</returnvalue></term>
1208 <title>Description</title>
1210 Ensures that an object obtained by dereferencing a
1211 <constant>:char</constant> pointer is a character.
1215 <title>Examples</title>
1218 (let ((fs (convert-to-foreign-string "a")))
1220 (ensure-char-character (deref-pointer fs :char))
1221 (free-foreign-object fs)))
1227 <title>Side Effects</title>
1231 <title>Affected by</title>
1235 <title>Exceptional Situations</title>
1236 <para>Depending upon the implementation and what &uffi; expects, this
1237 macro may signal an error if the object is not a character or
1242 <refentry id="ensure-char-integer">
1244 <refname>ensure-char-integer</refname>
1245 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1248 <refclass>Macro</refclass>
1251 <title>Syntax</title>
1253 <function>ensure-char-integer</function> <replaceable>object</replaceable> => <returnvalue>int</returnvalue>
1257 <title>Arguments and Values</title>
1260 <term><parameter>object</parameter></term>
1262 <para>Either a character or a integer specifying a character code.
1267 <term><returnvalue>int</returnvalue></term>
1276 <title>Description</title>
1278 Ensures that an object obtained by dereferencing a
1279 <constant>:char</constant> pointer is an integer.
1283 <title>Examples</title>
1286 (let ((fs (convert-to-foreign-string "a")))
1288 (ensure-char-integer (deref-pointer fs :char))
1289 (free-foreign-object fs)))
1295 <title>Side Effects</title>
1299 <title>Affected by</title>
1303 <title>Exceptional Situations</title>
1304 <para>Depending upon the implementation and what &uffi; expects, this
1305 macro may signal an error if the object is not a character or
1310 <refentry id="make-null-pointer">
1312 <refname>make-null-pointer</refname>
1313 <refpurpose>Create a &null; pointer.
1315 <refclass>Macro</refclass>
1318 <title>Syntax</title>
1320 <function>make-null-pointer</function> <replaceable>type</replaceable> => <returnvalue>ptr</returnvalue>
1324 <title>Arguments and Values</title>
1327 <term><parameter>type</parameter></term>
1329 <para>A type of object to which the pointer refers.
1334 <term><parameter>ptr</parameter></term>
1336 <para>The &null; pointer of type <parameter>type</parameter>.
1343 <title>Description</title>
1345 Creates a &null; pointer of a specified type.
1349 <title>Side Effects</title>
1353 <title>Affected by</title>
1357 <title>Exceptional Situations</title>
1363 <refentry id="null-pointer-p">
1365 <refname>null-pointer-p</refname>
1366 <refpurpose>Tests a pointer for &null; value.
1368 <refclass>Macro</refclass>
1371 <title>Syntax</title>
1373 <function>null-pointer-p</function> <replaceable>ptr</replaceable> => <returnvalue>is-null</returnvalue>
1377 <title>Arguments and Values</title>
1380 <term><parameter>ptr</parameter></term>
1382 <para>A foreign object pointer.
1387 <term><returnvalue>is-null</returnvalue></term>
1389 <para>The boolean flag.
1396 <title>Description</title>
1398 A predicate testing if a pointer is has a &null; value.
1402 <title>Side Effects</title>
1406 <title>Affected by</title>
1410 <title>Exceptional Situations</title>
1416 <refentry id="null-cstring-pointer">
1418 <refname>+null-cstring-pointer+</refname>
1419 <refpurpose>A constant &null; cstring pointer.
1421 <refclass>Constant</refclass>
1424 <title>Description</title>
1426 A &null; cstring pointer. This can be used for testing
1427 if a cstring returned by a function is &null;.
1435 <title>Strings</title>
1437 <title>Overview</title>
1439 &uffi; has functions to two types of <varname>C</varname>-compatible
1440 strings, <emphasis>cstring</emphasis> and <emphasis>foreign</emphasis> strings.
1441 cstrings are used as parameters to and from functions. An implementation, such as CMUCL,
1442 may not convert these to a foreign type for efficiency sake. Thus, it is not
1443 possible to "allocate" a cstring. In contrast, foreign strings
1444 always need to have memory for them.
1448 <refentry id="convert-from-cstring">
1450 <refname>convert-from-cstring</refname>
1451 <refpurpose>Converts a cstring to a Lisp string.
1453 <refclass>Macro</refclass>
1456 <title>Syntax</title>
1458 <function>convert-from-cstring</function> <replaceable>cstring</replaceable> => <returnvalue>string</returnvalue>
1462 <title>Arguments and Values</title>
1465 <term><parameter>cstring</parameter></term>
1472 <term><returnvalue>string</returnvalue></term>
1474 <para>A Lisp string.
1481 <title>Description</title>
1483 Converts a Lisp string to a <constant>cstring</constant>. This is
1484 most often used when processing the results of a foreign function
1485 that returns a cstring.
1489 <title>Side Effects</title>
1493 <title>Affected by</title>
1497 <title>Exceptional Situations</title>
1503 <refentry id="convert-to-cstring">
1505 <refname>convert-to-cstring</refname>
1506 <refpurpose>Converts a Lisp string to a cstring.
1508 <refclass>Macro</refclass>
1511 <title>Syntax</title>
1513 <function>convert-to-cstring</function> <replaceable>string</replaceable> => <returnvalue>cstring</returnvalue>
1517 <title>Arguments and Values</title>
1520 <term><parameter>string</parameter></term>
1522 <para>A Lisp string.
1527 <term><returnvalue>cstring</returnvalue></term>
1536 <title>Description</title>
1538 Converts a Lisp string to a
1539 <varname>cstring</varname>. The
1540 <varname>cstring</varname> should be freed with
1541 <function>free-cstring</function>.
1545 <title>Side Effects</title>
1549 <title>Affected by</title>
1553 <title>Exceptional Situations</title>
1559 <refentry id="free-cstring">
1561 <refname>free-cstring</refname>
1562 <refpurpose>Free memory used by cstring.
1564 <refclass>Macro</refclass>
1567 <title>Syntax</title>
1569 <function>free-cstring</function> <replaceable>cstring</replaceable>
1573 <title>Arguments and Values</title>
1576 <term><parameter>cstring</parameter></term>
1585 <title>Description</title>
1587 Frees any memory possibly allocated by
1588 <function>convert-to-cstring</function>.
1592 <title>Side Effects</title>
1596 <title>Affected by</title>
1600 <title>Exceptional Situations</title>
1606 <refentry id="with-cstring">
1608 <refname>with-cstring</refname>
1609 <refpurpose>Binds a newly created cstring.
1611 <refclass>Macro</refclass>
1614 <title>Syntax</title>
1616 <function>with-cstring</function> <replaceable>(cstring string) {body}</replaceable>
1620 <title>Arguments and Values</title>
1623 <term><parameter>cstring</parameter></term>
1625 <para>A symbol naming the cstring to be created.
1630 <term><parameter>string</parameter></term>
1632 <para>A Lisp string that will be translated to a cstring.
1637 <term><parameter>body</parameter></term>
1639 <para>The body of where the cstring will be bound.
1646 <title>Description</title>
1648 Binds a lexical variable to a newly allocated <varname>cstring</varname>. Automatically frees <varname>cstring</varname>.
1652 <title>Examples</title>
1655 (def-function ("getenv" c-getenv)
1657 :returning :cstring)
1660 "Returns an environment variable, or NIL if it does not exist"
1661 (check-type key string)
1662 (with-cstring (key-cstring key)
1663 (convert-from-cstring (c-getenv key-cstring))))
1668 <title>Side Effects</title>
1672 <title>Affected by</title>
1676 <title>Exceptional Situations</title>
1682 <refentry id="convert-from-foreign-string">
1684 <refname>convert-from-foreign-string</refname>
1685 <refpurpose>Converts a foreign string into a Lisp string.
1687 <refclass>Macro</refclass>
1690 <title>Syntax</title>
1692 <function>convert-from-foreign-string</function> <replaceable>foreign-string &key length null-terminated-p</replaceable> => <returnvalue>string</returnvalue>
1696 <title>Arguments and Values</title>
1699 <term><parameter>foreign-string</parameter></term>
1701 <para>A foreign string.
1706 <term><parameter>length</parameter></term>
1708 <para>The length of the foreign string to
1709 convert. The default is the length of the string until a &null;
1710 character is reached.
1715 <term><parameter>null-terminated-p</parameter></term>
1717 <para>A boolean flag with a default value of &t; When true,
1718 the string is converted until the first &null; character is reached.
1723 <term><returnvalue>string</returnvalue></term>
1725 <para>A Lisp string.
1732 <title>Description</title>
1734 Returns a Lisp string from a foreign string.
1735 Can translated ASCII and binary strings.
1739 <title>Side Effects</title>
1743 <title>Affected by</title>
1747 <title>Exceptional Situations</title>
1753 <refentry id="convert-to-foreign-string">
1755 <refname>convert-to-foreign-string</refname>
1756 <refpurpose>Converts a Lisp string to a foreign string.
1758 <refclass>Macro</refclass>
1761 <title>Syntax</title>
1763 <function>convert-to-foreign-string</function> <replaceable>string</replaceable> => <returnvalue>foreign-string</returnvalue>
1767 <title>Arguments and Values</title>
1770 <term><parameter>string</parameter></term>
1772 <para>A Lisp string.
1777 <term><returnvalue>foreign-string</returnvalue></term>
1779 <para>A foreign string.
1786 <title>Description</title>
1788 Converts a Lisp string to a foreign string. Memory should be
1789 freed with <function>free-foreign-object</function>.
1793 <title>Side Effects</title>
1797 <title>Affected by</title>
1801 <title>Exceptional Situations</title>
1808 <refentry id="allocate-foreign-string">
1810 <refname>allocate-foreign-string</refname>
1811 <refpurpose>Allocates space for a foreign string.
1813 <refclass>Macro</refclass>
1816 <title>Syntax</title>
1818 <function>allocate-foreign-string</function> <replaceable>size &key unsigned</replaceable> => <returnvalue>foreign-string</returnvalue>
1822 <title>Arguments and Values</title>
1825 <term><parameter>size</parameter></term>
1827 <para>The size of the space to be allocated in bytes.
1832 <term><parameter>unsigned</parameter></term>
1834 <para>A boolean flag with a default value of &t;. When true,
1835 marks the pointer as an <constant>:unsigned-char</constant>.
1840 <term><returnvalue>foreign-string</returnvalue></term>
1842 <para>A foreign string which has undefined contents.
1849 <title>Description</title>
1851 Allocates space for a foreign string. Memory should
1852 be freed with <function>free-foreign-object</function>.
1856 <title>Side Effects</title>
1860 <title>Affected by</title>
1864 <title>Exceptional Situations</title>
1872 <title>Functions & Libraries</title>
1874 <refentry id="def-function">
1876 <refname>def-function</refname>
1877 <refpurpose>Declares a function.
1879 <refclass>Macro</refclass>
1882 <title>Syntax</title>
1884 <function>def-function</function> <replaceable>name args &key module returning</replaceable>
1888 <title>Arguments and Values</title>
1891 <term><parameter>name</parameter></term>
1893 <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.
1898 <term><parameter>args</parameter></term>
1900 <para>A list of argument declarations. If &nil;, indicates that the function does not take any arguments.
1905 <term><parameter>module</parameter></term>
1907 <para>A string specifying which module (or library) that the foreign function resides. (Required by Lispworks)</para>
1911 <term><returnvalue>returning</returnvalue></term>
1913 <para>A declaration specifying the result type of the
1914 foreign function. If <constant>:void</constant> indicates module does not return any value.
1921 <title>Description</title>
1922 <para>Declares a foreign function.
1926 <title>Examples</title>
1928 (def-function "gethostname"
1929 ((name (* :unsigned-char))
1935 <title>Side Effects</title>
1939 <title>Affected by</title>
1943 <title>Exceptional Situations</title>
1948 <refentry id="load-foreign-library">
1950 <refname>load-foreign-library</refname>
1951 <refpurpose>Loads a foreign library.
1953 <refclass>Function</refclass>
1956 <title>Syntax</title>
1958 <function>load-foreign-library</function> <replaceable>filename &key module supporting-libraries</replaceable> => <returnvalue>success</returnvalue>
1962 <title>Arguments and Values</title>
1965 <term><parameter>filename</parameter></term>
1967 <para>A string or pathname specifying the library location
1968 in the filesystem. At least one implementation (&lw;) can not
1969 accept a logical pathname.
1974 <term><parameter>module</parameter></term>
1976 <para>A string designating the name of the module to apply
1977 to functions in this library. (Required for Lispworks)
1982 <term><parameter>supporting-libraries</parameter></term>
1984 <para>A list of strings naming the libraries required to
1985 link the foreign library. (Required by CMUCL)
1990 <term><returnvalue>success</returnvalue></term>
1992 <para>A boolean flag, &t; if the library was able to be
1993 loaded successfully or if the library has been previously loaded,
2001 <title>Description</title>
2002 <para>Loads a foreign library. Applies a module name to functions
2003 within the library. Ensures that a library is only loaded once during
2008 <title>Examples</title>
2010 (load-foreign-library #p"/usr/lib/libmysqlclient.so"
2012 :supporting-libraries '("c"))
2017 <title>Side Effects</title>
2018 <para>Loads the foreign code into the Lisp system.
2022 <title>Affected by</title>
2023 <para>Ability to load the file.</para>
2026 <title>Exceptional Situations</title>
2031 <refentry id="find-foreign-library">
2033 <refname>find-foreign-library</refname>
2034 <refpurpose>Finds a foreign library file.
2036 <refclass>Function</refclass>
2039 <title>Syntax</title>
2041 <function>find-foreign-library</function> <replaceable>names directories & drive-letters types</replaceable> => <returnvalue>path</returnvalue>
2045 <title>Arguments and Values</title>
2048 <term><parameter>names</parameter></term>
2050 <para>A string or list of strings containing the base name of the library file.
2055 <term><parameter>directories</parameter></term>
2057 <para>A string or list of strings containing the directory the library file.
2062 <term><parameter>drive-letters</parameter></term>
2064 <para>A string or list of strings containing the drive letters for the library file.
2069 <term><parameter>types</parameter></term>
2071 <para>A string or list of strings containing the file type of the library file. Default
2072 is &nil;. If &nil;, will use a default type based on the currently running implementation.
2077 <term><returnvalue>path</returnvalue></term>
2079 <para>A path containing the path found, or &nil; if the library file was not found.
2086 <title>Description</title>
2087 <para>Finds a foreign library by searching through a number of possible locations. Returns
2088 the path of the first found file.
2092 <title>Examples</title>
2094 (find-foreign-library '("libmysqlclient" "libmysql")
2095 '("/opt/mysql/lib/mysql/" "/usr/local/lib/" "/usr/lib/" "/mysql/lib/opt/")
2096 :types '("so" "dll")
2097 :drive-letters '("C" "D" "E"))
2098 => #P"D:\\mysql\\lib\\opt\\libmysql.dll"
2102 <title>Side Effects</title>
2107 <title>Affected by</title>
2111 <title>Exceptional Situations</title>