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>A unevaluated type of foreign object to allocate.
885 <term><parameter>size</parameter></term>
887 <para>An optional size parameter. If specified, allocates and returns an
888 array of <parameter>type</parameter> that is <parameter>size</parameter> members long.
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="pointer-address">
978 <refname>pointer-address</refname>
979 <refpurpose>Returns the address of a pointer.
981 <refclass>Macro</refclass>
984 <title>Syntax</title>
986 <function>pointer-address</function> <replaceable>ptr</replaceable> => <returnvalue>address</returnvalue>
990 <title>Arguments and Values</title>
993 <term><parameter>ptr</parameter></term>
995 <para>A pointer to a foreign object.
1000 <term><parameter>address</parameter></term>
1002 <para>An integer representing the pointer's address.
1009 <title>Description</title>
1011 Returns the address as an integer of a pointer.
1015 <title>Side Effects</title>
1019 <title>Affected by</title>
1023 <title>Exceptional Situations</title>
1029 <refentry id="deref-pointer">
1031 <refname>deref-pointer</refname>
1032 <refpurpose>Deferences a pointer.
1034 <refclass>Macro</refclass>
1037 <title>Syntax</title>
1039 <function>deref-pointer</function> <replaceable>ptr type</replaceable> => <returnvalue>value</returnvalue>
1043 <title>Arguments and Values</title>
1046 <term><parameter>ptr</parameter></term>
1048 <para>A pointer to a foreign object.
1053 <term><parameter>type</parameter></term>
1055 <para>A foreign type of the object being pointed to.
1060 <term><returnvalue>value</returnvalue></term>
1062 <para>The value of the object where the pointer points.
1069 <title>Description</title>
1071 Returns the object to which a pointer points.
1075 <title>Examples</title>
1078 (let ((intp (allocate-foreign-object :int)))
1079 (setf (deref-pointer intp :int) 10)
1081 (deref-pointer intp :int)
1082 (free-foreign-object intp)))
1088 <title>Side Effects</title>
1092 <title>Affected by</title>
1096 <title>Exceptional Situations</title>
1101 <refentry id="ensure-char-character">
1103 <refname>ensure-char-character</refname>
1104 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1107 <refclass>Macro</refclass>
1110 <title>Syntax</title>
1112 <function>ensure-char-character</function> <replaceable>object</replaceable> => <returnvalue>char</returnvalue>
1116 <title>Arguments and Values</title>
1119 <term><parameter>object</parameter></term>
1121 <para>Either a character or a integer specifying a character code.
1126 <term><returnvalue>char</returnvalue></term>
1135 <title>Description</title>
1137 Ensures that an object obtained by dereferencing a
1138 <constant>:char</constant> pointer is a character.
1142 <title>Examples</title>
1145 (let ((fs (convert-to-foreign-string "a")))
1147 (ensure-char-character (deref-pointer fs :char))
1148 (free-foreign-object fs)))
1154 <title>Side Effects</title>
1158 <title>Affected by</title>
1162 <title>Exceptional Situations</title>
1163 <para>Depending upon the implementation and what &uffi; expects, this
1164 macro may signal an error if the object is not a character or
1169 <refentry id="ensure-char-integer">
1171 <refname>ensure-char-integer</refname>
1172 <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1175 <refclass>Macro</refclass>
1178 <title>Syntax</title>
1180 <function>ensure-char-integer</function> <replaceable>object</replaceable> => <returnvalue>int</returnvalue>
1184 <title>Arguments and Values</title>
1187 <term><parameter>object</parameter></term>
1189 <para>Either a character or a integer specifying a character code.
1194 <term><returnvalue>int</returnvalue></term>
1203 <title>Description</title>
1205 Ensures that an object obtained by dereferencing a
1206 <constant>:char</constant> pointer is an integer.
1210 <title>Examples</title>
1213 (let ((fs (convert-to-foreign-string "a")))
1215 (ensure-char-integer (deref-pointer fs :char))
1216 (free-foreign-object fs)))
1222 <title>Side Effects</title>
1226 <title>Affected by</title>
1230 <title>Exceptional Situations</title>
1231 <para>Depending upon the implementation and what &uffi; expects, this
1232 macro may signal an error if the object is not a character or
1237 <refentry id="make-null-pointer">
1239 <refname>make-null-pointer</refname>
1240 <refpurpose>Create a &null; pointer.
1242 <refclass>Macro</refclass>
1245 <title>Syntax</title>
1247 <function>make-null-pointer</function> <replaceable>type</replaceable> => <returnvalue>ptr</returnvalue>
1251 <title>Arguments and Values</title>
1254 <term><parameter>type</parameter></term>
1256 <para>A type of object to which the pointer refers.
1261 <term><parameter>ptr</parameter></term>
1263 <para>The &null; pointer of type <parameter>type</parameter>.
1270 <title>Description</title>
1272 Creates a &null; pointer of a specified type.
1276 <title>Side Effects</title>
1280 <title>Affected by</title>
1284 <title>Exceptional Situations</title>
1290 <refentry id="null-pointer-p">
1292 <refname>null-pointer-p</refname>
1293 <refpurpose>Tests a pointer for &null; value.
1295 <refclass>Macro</refclass>
1298 <title>Syntax</title>
1300 <function>null-pointer-p</function> <replaceable>ptr</replaceable> => <returnvalue>is-null</returnvalue>
1304 <title>Arguments and Values</title>
1307 <term><parameter>ptr</parameter></term>
1309 <para>A foreign object pointer.
1314 <term><returnvalue>is-null</returnvalue></term>
1316 <para>The boolean flag.
1323 <title>Description</title>
1325 A predicate testing if a pointer is has a &null; value.
1329 <title>Side Effects</title>
1333 <title>Affected by</title>
1337 <title>Exceptional Situations</title>
1343 <refentry id="null-cstring-pointer">
1345 <refname>+null-cstring-pointer+</refname>
1346 <refpurpose>A constant &null; cstring pointer.
1348 <refclass>Constant</refclass>
1351 <title>Description</title>
1353 A &null; cstring pointer. This can be used for testing
1354 if a cstring returned by a function is &null;.
1362 <title>Strings</title>
1364 <title>Overview</title>
1366 &uffi; has functions to two types of <varname>C</varname>-compatible
1367 strings, <emphasis>cstring</emphasis> and <emphasis>foreign</emphasis> strings.
1368 cstrings are used as parameters to and from functions. An implementation, such as CMUCL,
1369 may not convert these to a foreign type for efficiency sake. Thus, it is not
1370 possible to "allocate" a cstring. In contrast, foreign strings
1371 always need to have memory for them.
1375 <refentry id="convert-from-cstring">
1377 <refname>convert-from-cstring</refname>
1378 <refpurpose>Converts a cstring to a Lisp string.
1380 <refclass>Macro</refclass>
1383 <title>Syntax</title>
1385 <function>convert-from-cstring</function> <replaceable>cstring</replaceable> => <returnvalue>string</returnvalue>
1389 <title>Arguments and Values</title>
1392 <term><parameter>cstring</parameter></term>
1399 <term><returnvalue>string</returnvalue></term>
1401 <para>A Lisp string.
1408 <title>Description</title>
1410 Converts a Lisp string to a <constant>cstring</constant>. This is
1411 most often used when processing the results of a foreign function
1412 that returns a cstring.
1416 <title>Side Effects</title>
1420 <title>Affected by</title>
1424 <title>Exceptional Situations</title>
1430 <refentry id="convert-to-cstring">
1432 <refname>convert-to-cstring</refname>
1433 <refpurpose>Converts a Lisp string to a cstring.
1435 <refclass>Macro</refclass>
1438 <title>Syntax</title>
1440 <function>convert-to-cstring</function> <replaceable>string</replaceable> => <returnvalue>cstring</returnvalue>
1444 <title>Arguments and Values</title>
1447 <term><parameter>string</parameter></term>
1449 <para>A Lisp string.
1454 <term><returnvalue>cstring</returnvalue></term>
1463 <title>Description</title>
1465 Converts a Lisp string to a
1466 <varname>cstring</varname>. The
1467 <varname>cstring</varname> should be freed with
1468 <function>free-cstring</function>.
1472 <title>Side Effects</title>
1476 <title>Affected by</title>
1480 <title>Exceptional Situations</title>
1486 <refentry id="free-cstring">
1488 <refname>free-cstring</refname>
1489 <refpurpose>Free memory used by cstring.
1491 <refclass>Macro</refclass>
1494 <title>Syntax</title>
1496 <function>free-cstring</function> <replaceable>cstring</replaceable>
1500 <title>Arguments and Values</title>
1503 <term><parameter>cstring</parameter></term>
1512 <title>Description</title>
1514 Frees any memory possibly allocated by
1515 <function>convert-to-cstring</function>.
1519 <title>Side Effects</title>
1523 <title>Affected by</title>
1527 <title>Exceptional Situations</title>
1533 <refentry id="with-cstring">
1535 <refname>with-cstring</refname>
1536 <refpurpose>Binds a newly created cstring.
1538 <refclass>Macro</refclass>
1541 <title>Syntax</title>
1543 <function>with-cstring</function> <replaceable>(cstring string) {body}</replaceable>
1547 <title>Arguments and Values</title>
1550 <term><parameter>cstring</parameter></term>
1552 <para>A symbol naming the cstring to be created.
1557 <term><parameter>string</parameter></term>
1559 <para>A Lisp string that will be translated to a cstring.
1564 <term><parameter>body</parameter></term>
1566 <para>The body of where the cstring will be bound.
1573 <title>Description</title>
1575 Binds a lexical variable to a newly allocated <varname>cstring</varname>. Automatically frees <varname>cstring</varname>.
1579 <title>Examples</title>
1582 (def-function ("getenv" c-getenv)
1584 :returning :cstring)
1587 "Returns an environment variable, or NIL if it does not exist"
1588 (check-type key string)
1589 (with-cstring (key-cstring key)
1590 (convert-from-cstring (c-getenv key-cstring))))
1595 <title>Side Effects</title>
1599 <title>Affected by</title>
1603 <title>Exceptional Situations</title>
1609 <refentry id="convert-from-foreign-string">
1611 <refname>convert-from-foreign-string</refname>
1612 <refpurpose>Converts a foreign string into a Lisp string.
1614 <refclass>Macro</refclass>
1617 <title>Syntax</title>
1619 <function>convert-from-foreign-string</function> <replaceable>foreign-string &key length null-terminated-p</replaceable> => <returnvalue>string</returnvalue>
1623 <title>Arguments and Values</title>
1626 <term><parameter>foreign-string</parameter></term>
1628 <para>A foreign string.
1633 <term><parameter>length</parameter></term>
1635 <para>The length of the foreign string to
1636 convert. The default is the length of the string until a &null;
1637 character is reached.
1642 <term><parameter>null-terminated-p</parameter></term>
1644 <para>A boolean flag with a default value of &t; When true,
1645 the string is converted until the first &null; character is reached.
1650 <term><returnvalue>string</returnvalue></term>
1652 <para>A Lisp string.
1659 <title>Description</title>
1661 Returns a Lisp string from a foreign string.
1662 Can translated ASCII and binary strings.
1666 <title>Side Effects</title>
1670 <title>Affected by</title>
1674 <title>Exceptional Situations</title>
1680 <refentry id="convert-to-foreign-string">
1682 <refname>convert-to-foreign-string</refname>
1683 <refpurpose>Converts a Lisp string to a foreign string.
1685 <refclass>Macro</refclass>
1688 <title>Syntax</title>
1690 <function>convert-to-foreign-string</function> <replaceable>string</replaceable> => <returnvalue>foreign-string</returnvalue>
1694 <title>Arguments and Values</title>
1697 <term><parameter>string</parameter></term>
1699 <para>A Lisp string.
1704 <term><returnvalue>foreign-string</returnvalue></term>
1706 <para>A foreign string.
1713 <title>Description</title>
1715 Converts a Lisp string to a foreign string. Memory should be
1716 freed with <function>free-foreign-object</function>.
1720 <title>Side Effects</title>
1724 <title>Affected by</title>
1728 <title>Exceptional Situations</title>
1735 <refentry id="allocate-foreign-string">
1737 <refname>allocate-foreign-string</refname>
1738 <refpurpose>Allocates space for a foreign string.
1740 <refclass>Macro</refclass>
1743 <title>Syntax</title>
1745 <function>allocate-foreign-string</function> <replaceable>size &key unsigned</replaceable> => <returnvalue>foreign-string</returnvalue>
1749 <title>Arguments and Values</title>
1752 <term><parameter>size</parameter></term>
1754 <para>The size of the space to be allocated in bytes.
1759 <term><parameter>unsigned</parameter></term>
1761 <para>A boolean flag with a default value of &t;. When true,
1762 marks the pointer as an <constant>:unsigned-char</constant>.
1767 <term><returnvalue>foreign-string</returnvalue></term>
1769 <para>A foreign string which has undefined contents.
1776 <title>Description</title>
1778 Allocates space for a foreign string. Memory should
1779 be freed with <function>free-foreign-object</function>.
1783 <title>Side Effects</title>
1787 <title>Affected by</title>
1791 <title>Exceptional Situations</title>
1799 <title>Functions & Libraries</title>
1801 <refentry id="def-function">
1803 <refname>def-function</refname>
1804 <refpurpose>Declares a function.
1806 <refclass>Macro</refclass>
1809 <title>Syntax</title>
1811 <function>def-function</function> <replaceable>name args &key module returning</replaceable>
1815 <title>Arguments and Values</title>
1818 <term><parameter>name</parameter></term>
1820 <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.
1825 <term><parameter>args</parameter></term>
1827 <para>A list of argument declarations. If &nil;, indicates that the function does not take any arguments.
1832 <term><parameter>module</parameter></term>
1834 <para>A string specifying which module (or library) that the foreign function resides. (Required by Lispworks)</para>
1838 <term><returnvalue>returning</returnvalue></term>
1840 <para>A declaration specifying the result type of the
1841 foreign function. If <constant>:void</constant> indicates module does not return any value.
1848 <title>Description</title>
1849 <para>Declares a foreign function.
1853 <title>Examples</title>
1855 (def-function "gethostname"
1856 ((name (* :unsigned-char))
1862 <title>Side Effects</title>
1866 <title>Affected by</title>
1870 <title>Exceptional Situations</title>
1875 <refentry id="load-foreign-library">
1877 <refname>load-foreign-library</refname>
1878 <refpurpose>Loads a foreign library.
1880 <refclass>Function</refclass>
1883 <title>Syntax</title>
1885 <function>load-foreign-library</function> <replaceable>filename &key module supporting-libraries</replaceable> => <returnvalue>success</returnvalue>
1889 <title>Arguments and Values</title>
1892 <term><parameter>filename</parameter></term>
1894 <para>A string or pathname specifying the library location
1895 in the filesystem. At least one implementation (&lw;) can not
1896 accept a logical pathname.
1901 <term><parameter>module</parameter></term>
1903 <para>A string designating the name of the module to apply
1904 to functions in this library. (Required for Lispworks)
1909 <term><parameter>supporting-libraries</parameter></term>
1911 <para>A list of strings naming the libraries required to
1912 link the foreign library. (Required by CMUCL)
1917 <term><returnvalue>success</returnvalue></term>
1919 <para>A boolean flag, &t; if the library was able to be
1920 loaded successfully or if the library has been previously loaded,
1928 <title>Description</title>
1929 <para>Loads a foreign library. Applies a module name to functions
1930 within the library. Ensures that a library is only loaded once during
1935 <title>Examples</title>
1937 (load-foreign-library #p"/usr/lib/libmysqlclient.so"
1939 :supporting-libraries '("c"))
1944 <title>Side Effects</title>
1945 <para>Loads the foreign code into the Lisp system.
1949 <title>Affected by</title>
1950 <para>Ability to load the file.</para>
1953 <title>Exceptional Situations</title>