r5497: Automatic commit for debian_version_1_3_0-1
[uffi.git] / doc / ref.sgml
1 <!-- -*- DocBook -*- -->
2
3   
4 <reference>
5   <title>Declarations</title>
6
7   <partintro>
8     <sect1>
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;
14       </para>
15     </sect1>
16   </partintro>
17
18       <refentry id="def-type">
19         <refnamediv>
20           <refname>def-type</refname>
21         <refpurpose>Defines a Common Lisp type. 
22         </refpurpose>
23         <refclass>Macro</refclass>
24       </refnamediv>
25       <refsynopsisdiv>
26         <title>Syntax</title>
27         <synopsis>
28           <function>def-type</function> <replaceable>name type</replaceable>
29         </synopsis>
30       </refsynopsisdiv>
31       <refsect1>
32         <title>Arguments and Values</title>
33         <variablelist>
34           <varlistentry>
35             <term><parameter>name</parameter></term>
36             <listitem>
37               <para>A symbol naming the type</para>
38             </listitem>
39           </varlistentry>
40           <varlistentry>
41             <term><parameter>type</parameter></term>
42             <listitem>
43               <para>A form that is evaluated that specifies the &uffi; type.
44               </para>
45             </listitem>
46           </varlistentry>
47         </variablelist>
48       </refsect1>
49       <refsect1>
50         <title>Description</title>
51         <para>Defines a Common Lisp type based on a &uffi; type.
52         </para>
53       </refsect1>
54       <refsect1>
55         <title>Examples</title>
56         <programlisting>
57 (def-type char-ptr '(* :char))
58 ...
59 (defun foo (ptr)
60   (declare (type char-ptr ptr))
61   ...
62         </programlisting>
63       </refsect1>
64       <refsect1>
65         <title>Side Effects</title>
66         <para>Defines a new &cl; type.</para>
67       </refsect1>
68       <refsect1>
69         <title>Affected by</title>
70         <para>None.</para>
71       </refsect1>
72       <refsect1>
73         <title>Exceptional Situations</title>
74         <para>None.</para>
75       </refsect1>
76     </refentry>
77   </reference>
78
79
80   <reference>
81     <title>Primitive Types</title>
82     <partintro>
83       <title>Overview</title>
84         <para>
85           Primitive types have a single value, these include
86           characters, numbers, and pointers. They are all symbols in
87           the keyword package.
88         </para>
89         <itemizedlist>
90           <listitem>
91             <para><constant>:char</constant> - Signed 8-bits. A
92 dereferenced :char pointer returns an character.</para>
93           </listitem>
94           <listitem>
95             <para><constant>:unsigned-char</constant> - Unsigned 8-bits. A dereferenced :unsigned-char
96 pointer returns an character.</para>
97           </listitem>
98           <listitem>
99             <para><constant>:byte</constant> - Signed 8-bits. A
100 dereferenced :byte pointer returns an integer.</para>
101           </listitem>
102           <listitem>
103             <para><constant>:unsigned-byte</constant> - Unsigned 8-bits. A
104 dereferenced :unsigned-byte pointer returns an integer.</para>
105           </listitem>
106           <listitem>
107             <para><constant>:short</constant> - Signed 16-bits.</para>
108           </listitem>
109           <listitem>
110             <para><constant>:unsigned-short</constant> - Unsigned 16-bits.</para>
111           </listitem>
112           <listitem>
113             <para><constant>:int</constant> - Signed 32-bits.</para>
114           </listitem>
115           <listitem>
116             <para><constant>:unsigned-int</constant> - Unsigned 32-bits.</para>
117           </listitem>
118           <listitem>
119             <para><constant>:long</constant> - Signed 32-bits.</para>
120           </listitem>
121           <listitem>
122             <para><constant>:unsigned-long</constant> - Unsigned 32-bits.</para>
123           </listitem>
124           <listitem>
125             <para><constant>:float</constant> - 32-bit floating point.</para>
126           </listitem>
127           <listitem>
128             <para><constant>:double</constant> - 64-bit floating point.</para>
129           </listitem>
130           <listitem>
131             <para><constant>:cstring</constant> - 
132 A &null; terminated string used for passing and returning characters strings with a &c; function.
133             </para>
134           </listitem>
135           <listitem>
136             <para><constant>:void</constant> - 
137 The absence of a value. Used to indicate that a function does not return a value.</para>
138           </listitem>
139           <listitem>
140             <para><constant>:pointer-void</constant> - 
141 Points to a generic object.</para>
142           </listitem>
143           <listitem>
144             <para><constant>*</constant> - Used to declare a pointer to an object</para>
145           </listitem>
146         </itemizedlist>
147       </partintro>
148
149       <refentry id="def-constant">
150         <refnamediv>
151           <refname>def-constant</refname>
152         <refpurpose>Binds a symbol to a constant. 
153         </refpurpose>
154         <refclass>Macro</refclass>
155       </refnamediv>
156       <refsynopsisdiv>
157         <title>Syntax</title>
158         <synopsis>
159           <function>def-constant</function> <replaceable>name value &amp;key export</replaceable>
160         </synopsis>
161       </refsynopsisdiv>
162       <refsect1>
163         <title>Arguments and Values</title>
164         <variablelist>
165           <varlistentry>
166             <term><parameter>name</parameter></term>
167             <listitem>
168               <para>A symbol that will be bound to the value.
169               </para>
170             </listitem>
171           </varlistentry>
172           <varlistentry>
173             <term><parameter>value</parameter></term>
174             <listitem>
175               <para>An evaluated form that is bound the the name.
176               </para>
177             </listitem>
178           </varlistentry>
179           <varlistentry>
180             <term><parameter>export</parameter></term>
181             <listitem>
182               <para>When &t;, the name is exported from the current package. The default is &nil;</para>
183             </listitem>
184           </varlistentry>
185         </variablelist>
186       </refsect1>
187       <refsect1>
188         <title>Description</title>
189         <para>
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.
193         </para>
194       </refsect1>
195       <refsect1>
196         <title>Examples</title>
197         <programlisting>
198 (def-constant pi2 (* 2 pi))
199 (def-constant exported-pi2 (* 2 pi) :export t)
200         </programlisting>
201       </refsect1>
202       <refsect1>
203         <title>Side Effects</title>
204         <para>Creates a new special variable..</para>
205       </refsect1>
206       <refsect1>
207         <title>Affected by</title>
208         <para>None.</para>
209       </refsect1>
210       <refsect1>
211         <title>Exceptional Situations</title>
212         <para>None.</para>
213       </refsect1>
214     </refentry>
215
216     <refentry id="def-foreign-type">
217         <refnamediv>
218           <refname>def-foreign-type</refname>
219         <refpurpose>Defines a new foreign type. 
220         </refpurpose>
221         <refclass>Macro</refclass>
222       </refnamediv>
223       <refsect1>
224         <title>Syntax</title>
225 <synopsis>
226           <function>def-foreign-type</function> <replaceable>name type</replaceable>
227 </synopsis>
228       </refsect1>
229       <refsect1>
230         <title>Arguments and Values</title>
231         <variablelist>
232           <varlistentry>
233             <term><parameter>name</parameter></term>
234             <listitem>
235               <para>A symbol naming the new foreign type.
236               </para>
237             </listitem>
238           </varlistentry>
239           <varlistentry>
240             <term><parameter>value</parameter></term>
241             <listitem>
242               <para>A form that is not evaluated that defines the new
243 foreign type.
244               </para>
245             </listitem>
246           </varlistentry>
247         </variablelist>
248       </refsect1>
249       <refsect1>
250         <title>Description</title>
251         <para>Defines a new foreign type.
252         </para>
253       </refsect1>
254       <refsect1>
255         <title>Examples</title>
256         <programlisting>
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))
260         </programlisting>
261       </refsect1>
262       <refsect1>
263         <title>Side Effects</title>
264         <para>Defines a new foreign type.
265         </para>
266       </refsect1>
267       <refsect1>
268         <title>Affected by</title>
269         <para>None.</para>
270       </refsect1>
271       <refsect1>
272         <title>Exceptional Situations</title>
273         <para>None.</para>
274       </refsect1>
275     </refentry>
276
277     <refentry id="null-char-p">
278         <refnamediv>
279           <refname>null-char-p</refname>
280         <refpurpose>Tests a character for &null; value.
281         </refpurpose>
282         <refclass>Macro</refclass>
283       </refnamediv>
284       <refsect1>
285         <title>Syntax</title>
286 <synopsis>
287           <function>null-char-p</function> <replaceable>char</replaceable> => <returnvalue>is-null</returnvalue>
288 </synopsis>
289       </refsect1>
290       <refsect1>
291         <title>Arguments and Values</title>
292         <variablelist>
293           <varlistentry>
294             <term><parameter>char</parameter></term>
295             <listitem>
296               <para>A character or integer.
297               </para>
298             </listitem>
299           </varlistentry>
300           <varlistentry>
301             <term><parameter>is-null</parameter></term>
302             <listitem>
303               <para>A boolean flag indicating if char is a &null; value.
304               </para>
305             </listitem>
306           </varlistentry>
307         </variablelist>
308       </refsect1>
309       <refsect1>
310         <title>Description</title>
311         <para>
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.
317         </para>
318       </refsect1>
319       <refsect1>
320         <title>Examples</title>
321         <programlisting>
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))))
326 => &nil;
327    &t;
328         </programlisting>
329       </refsect1>
330       <refsect1>
331         <title>Side Effects</title>
332         <para>None.
333         </para>
334       </refsect1>
335       <refsect1>
336         <title>Affected by</title>
337         <para>None.</para>
338       </refsect1>
339       <refsect1>
340         <title>Exceptional Situations</title>
341         <para>None.</para>
342       </refsect1>
343     </refentry>
344   </reference>
345
346   <reference>
347     <title>Aggregate Types</title>
348     <partintro>
349       <title>Overview</title>
350       <para>
351         Aggregate types are comprised of one or more primitive types.
352       </para>
353     </partintro>
354
355     <refentry id="def-enum">
356       <refnamediv>
357         <refname>def-enum</refname>
358         <refpurpose>Defines a &c; enumeration.
359         </refpurpose>
360         <refclass>Macro</refclass>
361       </refnamediv>
362       <refsynopsisdiv>
363         <title>Syntax</title>
364         <synopsis>
365           <function>def-enum</function> <replaceable>name fields &amp;key separator-string</replaceable>
366         </synopsis>
367       </refsynopsisdiv>
368       <refsect1>
369         <title>Arguments and Values</title>
370         <variablelist>
371           <varlistentry>
372             <term><parameter>name</parameter></term>
373             <listitem>
374               <para>A symbol that names the enumeration.
375               </para>
376             </listitem>
377           </varlistentry>
378           <varlistentry>
379             <term><parameter>fields</parameter></term>
380             <listitem>
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.
387               </para>
388             </listitem>
389           </varlistentry>
390           <varlistentry>
391             <term><parameter>separator-string</parameter></term>
392             <listitem>
393               <para>A string that governs the creation of constants. The
394 default is <computeroutput>"#"</computeroutput>.</para>
395             </listitem>
396           </varlistentry>
397         </variablelist>
398       </refsect1>
399       <refsect1>
400         <title>Description</title>
401         <para>
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>.
407         </para>
408       </refsect1>
409       <refsect1>
410         <title>Examples</title>
411         <programlisting>
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
415
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
419         </programlisting>
420       </refsect1>
421       <refsect1>
422         <title>Side Effects</title>
423         <para>Creates a :int foreign type, defines constants.</para>
424       </refsect1>
425       <refsect1>
426         <title>Affected by</title>
427         <para>None.</para>
428       </refsect1>
429       <refsect1>
430         <title>Exceptional Situations</title>
431         <para>None.</para>
432       </refsect1>
433     </refentry>
434
435
436       <refentry id="def-struct">
437         <refnamediv>
438           <refname>def-struct</refname>
439         <refpurpose>Defines a &c; structure.
440         </refpurpose>
441         <refclass>Macro</refclass>
442       </refnamediv>
443       <refsynopsisdiv>
444         <title>Syntax</title>
445         <synopsis>
446           <function>def-struct</function> <replaceable>name &amp;rest fields</replaceable>
447         </synopsis>
448       </refsynopsisdiv>
449       <refsect1>
450         <title>Arguments and Values</title>
451         <variablelist>
452           <varlistentry>
453             <term><parameter>name</parameter></term>
454             <listitem>
455               <para>A symbol that names the structure.
456               </para>
457             </listitem>
458           </varlistentry>
459           <varlistentry>
460             <term><parameter>fields</parameter></term>
461             <listitem>
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.
463               </para>
464             </listitem>
465           </varlistentry>
466         </variablelist>
467       </refsect1>
468       <refsect1>
469         <title>Description</title>
470         <para>
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>.
474
475         </para>
476       </refsect1>
477       <refsect1>
478         <title>Examples</title>
479         <programlisting>
480 (def-struct foo (a :unsigned-int) 
481                 (b (* :char)) 
482                 (c (:array :int 10)) 
483                 (next :pointer-self))
484         </programlisting>
485       </refsect1>
486       <refsect1>
487         <title>Side Effects</title>
488         <para>Creates a foreign type.</para>
489       </refsect1>
490       <refsect1>
491         <title>Affected by</title>
492         <para>None.</para>
493       </refsect1>
494       <refsect1>
495         <title>Exceptional Situations</title>
496         <para>None.</para>
497       </refsect1>
498     </refentry>
499
500
501     <refentry id="get-slot-value">
502       <refnamediv>
503         <refname>get-slot-value</refname>
504         <refpurpose>Retrieves a value from a slot of a structure.
505         </refpurpose>
506         <refclass>Macro</refclass>
507       </refnamediv>
508       <refsynopsisdiv>
509         <title>Syntax</title>
510         <synopsis>
511           <function>get-slot-value</function> <replaceable>obj type field</replaceable> => <returnvalue>value</returnvalue>
512         </synopsis>
513       </refsynopsisdiv>
514       <refsect1>
515         <title>Arguments and Values</title>
516         <variablelist>
517           <varlistentry>
518             <term><parameter>obj</parameter></term>
519             <listitem>
520               <para>A pointer to foreign structure.
521               </para>
522             </listitem>
523           </varlistentry>
524           <varlistentry>
525             <term><parameter>type</parameter></term>
526             <listitem>
527               <para>A name of the foreign structure.
528               </para>
529             </listitem>
530           </varlistentry>
531           <varlistentry>
532             <term><parameter>field</parameter></term>
533             <listitem>
534               <para>A name of the desired field in foreign structure.
535               </para>
536             </listitem>
537           </varlistentry>
538           <varlistentry>
539             <term><returnvalue>value</returnvalue></term>
540             <listitem>
541               <para>The value of the field in the structure.
542               </para>
543             </listitem>
544           </varlistentry>
545         </variablelist>
546       </refsect1>
547       <refsect1>
548         <title>Description</title>
549         <para>
550           Accesses a slot value from a structure.
551         </para>
552       </refsect1>
553       <refsect1>
554         <title>Examples</title>
555         <programlisting>
556 (get-slot-value foo-ptr 'foo-structure 'field-name)
557         </programlisting>
558       </refsect1>
559       <refsect1>
560         <title>Side Effects</title>
561         <para>None.</para>
562       </refsect1>
563       <refsect1>
564         <title>Affected by</title>
565         <para>None.</para>
566       </refsect1>
567       <refsect1>
568         <title>Exceptional Situations</title>
569         <para>None.</para>
570       </refsect1>
571     </refentry>
572
573     <refentry id="get-slot-pointer">
574       <refnamediv>
575         <refname>get-slot-pointer</refname>
576         <refpurpose>Retrieves a pointer from a slot of a structure.
577         </refpurpose>
578         <refclass>Macro</refclass>
579       </refnamediv>
580       <refsynopsisdiv>
581         <title>Syntax</title>
582         <synopsis>
583           <function>get-slot-pointer</function> <replaceable>obj type field</replaceable> => <returnvalue>pointer</returnvalue>
584         </synopsis>
585       </refsynopsisdiv>
586       <refsect1>
587         <title>Arguments and Values</title>
588         <variablelist>
589           <varlistentry>
590             <term><parameter>obj</parameter></term>
591             <listitem>
592               <para>A pointer to foreign structure.
593               </para>
594             </listitem>
595           </varlistentry>
596           <varlistentry>
597             <term><parameter>type</parameter></term>
598             <listitem>
599               <para>A name of the foreign structure.
600               </para>
601             </listitem>
602           </varlistentry>
603           <varlistentry>
604             <term><parameter>field</parameter></term>
605             <listitem>
606               <para>A name of the desired field in foreign structure.
607               </para>
608             </listitem>
609           </varlistentry>
610           <varlistentry>
611             <term><returnvalue>pointer</returnvalue></term>
612             <listitem>
613               <para>The value of the field in the structure.
614               </para>
615             </listitem>
616           </varlistentry>
617         </variablelist>
618       </refsect1>
619       <refsect1>
620         <title>Description</title>
621         <para>
622           This is similar to <function>get-slot-value</function>. It
623           is used when the value of a slot is a pointer type.
624         </para>
625       </refsect1>
626       <refsect1>
627         <title>Examples</title>
628         <programlisting>
629 (get-slot-pointer foo-ptr 'foo-structure 'my-char-ptr)
630         </programlisting>
631       </refsect1>
632       <refsect1>
633         <title>Side Effects</title>
634         <para>None.</para>
635       </refsect1>
636       <refsect1>
637         <title>Affected by</title>
638         <para>None.</para>
639       </refsect1>
640       <refsect1>
641         <title>Exceptional Situations</title>
642         <para>None.</para>
643       </refsect1>
644     </refentry>
645
646
647     <refentry id="def-array-pointer">
648       <refnamediv>
649         <refname>def-array-pointer</refname>
650         <refpurpose>Defines a pointer to a array of type.
651         </refpurpose>
652         <refclass>Macro</refclass>
653       </refnamediv>
654       <refsynopsisdiv>
655         <title>Syntax</title>
656         <synopsis>
657           <function>def-array-pointer</function> <replaceable>name type</replaceable>
658         </synopsis>
659       </refsynopsisdiv>
660       <refsect1>
661         <title>Arguments and Values</title>
662         <variablelist>
663           <varlistentry>
664             <term><parameter>name</parameter></term>
665             <listitem>
666               <para>A name of the new foreign type.
667               </para>
668             </listitem>
669           </varlistentry>
670           <varlistentry>
671             <term><parameter>type</parameter></term>
672             <listitem>
673               <para>The foreign type of the array elements.
674               </para>
675             </listitem>
676           </varlistentry>
677         </variablelist>
678       </refsect1>
679       <refsect1>
680         <title>Description</title>
681         <para>
682           Defines a type tat is a pointer to an array of type.
683         </para>
684       </refsect1>
685       <refsect1>
686         <title>Examples</title>
687         <programlisting>
688 (def-array-pointer byte-array-pointer :unsigned-char)
689         </programlisting>
690       </refsect1>
691       <refsect1>
692         <title>Side Effects</title>
693         <para>Defines a new foreign type.</para>
694       </refsect1>
695       <refsect1>
696         <title>Affected by</title>
697         <para>None.</para>
698       </refsect1>
699       <refsect1>
700         <title>Exceptional Situations</title>
701         <para>None.</para>
702       </refsect1>
703     </refentry>
704
705
706     <refentry id="deref-array">
707       <refnamediv>
708         <refname>deref-array</refname>
709         <refpurpose>Deference an array.
710         </refpurpose>
711         <refclass>Macro</refclass>
712       </refnamediv>
713       <refsynopsisdiv>
714         <title>Syntax</title>
715         <synopsis>
716           <function>deref-array</function> <replaceable>array type positon</replaceable> => <returnvalue>value</returnvalue>
717         </synopsis>
718       </refsynopsisdiv>
719       <refsect1>
720         <title>Arguments and Values</title>
721         <variablelist>
722           <varlistentry>
723             <term><parameter>array</parameter></term>
724             <listitem>
725               <para>A foreign array.
726               </para>
727             </listitem>
728           </varlistentry>
729           <varlistentry>
730             <term><parameter>type</parameter></term>
731             <listitem>
732               <para>The foreign type of the array.
733               </para>
734             </listitem>
735           </varlistentry>
736           <varlistentry>
737             <term><parameter>position</parameter></term>
738             <listitem>
739               <para>An integer specifying the position to retrieve from
740 the array.
741               </para>
742             </listitem>
743           </varlistentry>
744           <varlistentry>
745             <term><returnvalue>value</returnvalue></term>
746             <listitem>
747               <para>The value stored in the position of the array.
748               </para>
749             </listitem>
750           </varlistentry>
751         </variablelist>
752       </refsect1>
753       <refsect1>
754         <title>Description</title>
755         <para>
756           Dereferences (retrieves) the value of an array element.
757         </para>
758       </refsect1>
759       <refsect1>
760         <title>Examples</title>
761         <programlisting>
762 (def-array-pointer ca :char)
763 (let ((fs (convert-to-foreign-string "ab")))
764    (values (null-char-p (deref-array fs 'ca 0))
765            (null-char-p (deref-array fs 'ca 2))))
766 => &nil;
767    &t;
768         </programlisting>
769       </refsect1>
770       <refsect1>
771         <title>Notes</title>
772         <para>
773   The TYPE argument is ignored for CL implementations other than
774   AllegroCL. If you want to cast a pointer to another type use
775   WITH-CAST-POINTER together with DEREF-POINTER/DEREF-ARRAY.
776         </para>
777       </refsect1>
778       <refsect1>
779         <title>Side Effects</title>
780         <para>None.</para>
781       </refsect1>
782       <refsect1>
783         <title>Affected by</title>
784         <para>None.</para>
785       </refsect1>
786       <refsect1>
787         <title>Exceptional Situations</title>
788         <para>None.</para>
789       </refsect1>
790     </refentry>
791
792     <refentry id="def-union">
793       <refnamediv>
794         <refname>def-union</refname>
795         <refpurpose>Defines a foreign union type.
796         </refpurpose>
797         <refclass>Macro</refclass>
798       </refnamediv>
799       <refsynopsisdiv>
800         <title>Syntax</title>
801         <synopsis>
802           <function>def-union</function> <replaceable>name &amp;rest fields</replaceable>
803         </synopsis>
804       </refsynopsisdiv>
805       <refsect1>
806         <title>Arguments and Values</title>
807         <variablelist>
808           <varlistentry>
809             <term><parameter>name</parameter></term>
810             <listitem>
811               <para>A name of the new union type.
812               </para>
813             </listitem>
814           </varlistentry>
815           <varlistentry>
816             <term><parameter>fields</parameter></term>
817             <listitem>
818               <para>A list of fields of the union.
819               </para>
820             </listitem>
821           </varlistentry>
822         </variablelist>
823       </refsect1>
824       <refsect1>
825         <title>Description</title>
826         <para>
827           Defines a foreign union type.
828         </para>
829       </refsect1>
830       <refsect1>
831         <title>Examples</title>
832         <programlisting>
833 (def-union test-union
834   (a-char :char)
835   (an-int :int))
836
837 (let ((u (allocate-foreign-object 'test-union))
838   (setf (get-slot-value u 'test-union 'an-int) (+ 65 (* 66 256)))
839   (prog1 
840     (ensure-char-character (get-slot-value u 'test-union 'a-char))
841     (free-foreign-object u)))
842 => #\A
843         </programlisting>
844       </refsect1>
845       <refsect1>
846         <title>Side Effects</title>
847         <para>Defines a new foreign type.</para>
848       </refsect1>
849       <refsect1>
850         <title>Affected by</title>
851         <para>None.</para>
852       </refsect1>
853       <refsect1>
854         <title>Exceptional Situations</title>
855         <para>None.</para>
856       </refsect1>
857     </refentry>
858
859
860 </reference>
861
862 <reference>
863     <title>Objects</title>
864 <partintro>
865 <title>Overview</title>
866     <para>
867       Objects are entities that can allocated, referred to by pointers, and
868 can be freed.
869     </para>
870 </partintro>
871
872
873     <refentry id="allocate-foreign-object">
874       <refnamediv>
875         <refname>allocate-foreign-object</refname>
876         <refpurpose>Allocates an instance of a foreign object.
877         </refpurpose>
878         <refclass>Macro</refclass>
879       </refnamediv>
880       <refsynopsisdiv>
881         <title>Syntax</title>
882         <synopsis>
883           <function>allocate-foreign-object</function> <replaceable>type &amp;optional size</replaceable> => <returnvalue>ptr</returnvalue>
884         </synopsis>
885       </refsynopsisdiv>
886       <refsect1>
887         <title>Arguments and Values</title>
888         <variablelist>
889           <varlistentry>
890             <term><parameter>type</parameter></term>
891             <listitem>
892               <para>The type of foreign object to allocate. This parameter is evaluated.
893               </para>
894             </listitem>
895           </varlistentry>
896           <varlistentry>
897             <term><parameter>size</parameter></term>
898             <listitem>
899               <para>An optional size parameter that is evaluated. If specified, allocates and returns an
900 array of <parameter>type</parameter> that is <parameter>size</parameter> members long. This parameter is evaluated.
901               </para>
902             </listitem>
903           </varlistentry>
904           <varlistentry>
905             <term><returnvalue>ptr</returnvalue></term>
906             <listitem>
907               <para>A pointer to the foreign object.
908               </para>
909             </listitem>
910           </varlistentry>
911         </variablelist>
912       </refsect1>
913       <refsect1>
914         <title>Description</title>
915         <para>
916           Allocates an instance of a foreign object. It returns a pointer to the object.
917         </para>
918       </refsect1>
919       <refsect1>
920         <title>Examples</title>
921         <programlisting>
922 (def-struct ab (a :int) (b :double))
923 (allocate-foreign-object 'ab)
924 => #&lt;ptr&gt;
925         </programlisting>
926       </refsect1>
927       <refsect1>
928         <title>Side Effects</title>
929         <para>None.</para>
930       </refsect1>
931       <refsect1>
932         <title>Affected by</title>
933         <para>None.</para>
934       </refsect1>
935       <refsect1>
936         <title>Exceptional Situations</title>
937         <para>None.</para>
938       </refsect1>
939     </refentry>
940
941
942     <refentry id="free-foreign-object">
943       <refnamediv>
944         <refname>free-foreign-object</refname>
945         <refpurpose>Frees memory that was allocated for a foreign boject.
946         </refpurpose>
947         <refclass>Macro</refclass>
948       </refnamediv>
949       <refsynopsisdiv>
950         <title>Syntax</title>
951         <synopsis>
952           <function>free-foreign-object</function> <replaceable>ptr</replaceable>
953         </synopsis>
954       </refsynopsisdiv>
955       <refsect1>
956         <title>Arguments and Values</title>
957         <variablelist>
958           <varlistentry>
959             <term><parameter>ptr</parameter></term>
960             <listitem>
961               <para>A pointer to the allocated foreign object to free.
962               </para>
963             </listitem>
964           </varlistentry>
965         </variablelist>
966       </refsect1>
967       <refsect1>
968         <title>Description</title>
969         <para>
970           Frees the memory used by the allocation of a foreign object.
971         </para>
972       </refsect1>
973       <refsect1>
974         <title>Side Effects</title>
975         <para>None.</para>
976       </refsect1>
977       <refsect1>
978         <title>Affected by</title>
979         <para>None.</para>
980       </refsect1>
981       <refsect1>
982         <title>Exceptional Situations</title>
983         <para>None.</para>
984       </refsect1>
985     </refentry>
986
987
988     <refentry id="with-foreign-object">
989       <refnamediv>
990         <refname>with-foreign-object</refname>
991         <refpurpose>Wraps the allocation of a foreign object around a body of code.
992         </refpurpose>
993         <refclass>Macro</refclass>
994       </refnamediv>
995       <refsynopsisdiv>
996         <title>Syntax</title>
997         <synopsis>
998           <function>with-foreign-object</function> <replaceable>(var type) &amp;body body</replaceable> => <returnvalue>form-return</returnvalue>
999         </synopsis>
1000       </refsynopsisdiv>
1001       <refsect1>
1002         <title>Arguments and Values</title>
1003         <variablelist>
1004           <varlistentry>
1005             <term><parameter>var</parameter></term>
1006             <listitem>
1007               <para>The variable name to bind.
1008               </para>
1009             </listitem>
1010           </varlistentry>
1011           <varlistentry>
1012             <term><parameter>type</parameter></term>
1013             <listitem>
1014               <para>The type of foreign object to allocate. This parameter is evaluated.
1015               </para>
1016             </listitem>
1017           </varlistentry>
1018           <varlistentry>
1019             <term><returnvalue>form-return</returnvalue></term>
1020             <listitem>
1021               <para>The result of evaluating the <parameter>body</parameter>.
1022               </para>
1023             </listitem>
1024           </varlistentry>
1025         </variablelist>
1026       </refsect1>
1027       <refsect1>
1028         <title>Description</title>
1029         <para>
1030 This function wraps the allocation, binding, and destruction of a foreign object.
1031 On &cmucl; and
1032 &lw; platforms the object is stack allocated for efficiency. Benchmarks show that &acl; performs
1033 much better with static allocation.
1034         </para>
1035       </refsect1>
1036       <refsect1>
1037         <title>Examples</title>
1038         <programlisting>
1039 (defun gethostname2 ()
1040   "Returns the hostname"
1041   (uffi:with-foreign-object (name '(:array :unsigned-char 256))
1042     (if (zerop (c-gethostname (uffi:char-array-to-pointer name) 256))
1043         (uffi:convert-from-foreign-string name)
1044         (error "gethostname() failed."))))
1045         </programlisting>
1046       </refsect1>
1047       <refsect1>
1048         <title>Side Effects</title>
1049         <para>None.</para>
1050       </refsect1>
1051       <refsect1>
1052         <title>Affected by</title>
1053         <para>None.</para>
1054       </refsect1>
1055       <refsect1>
1056         <title>Exceptional Situations</title>
1057         <para>None.</para>
1058       </refsect1>
1059     </refentry>
1060
1061     <refentry id="size-of-foreign-type">
1062       <refnamediv>
1063         <refname>size-of-foreign-type</refname>
1064         <refpurpose>Returns the number of data bytes used by a foreign object type.
1065         </refpurpose>
1066         <refclass>Macro</refclass>
1067       </refnamediv>
1068       <refsynopsisdiv>
1069         <title>Syntax</title>
1070         <synopsis>
1071           <function>size-of-foreign-type</function> <replaceable>ftype</replaceable>
1072         </synopsis>
1073       </refsynopsisdiv>
1074       <refsect1>
1075         <title>Arguments and Values</title>
1076         <variablelist>
1077           <varlistentry>
1078             <term><parameter>ftype</parameter></term>
1079             <listitem>
1080               <para>A foreign type specifier. This parameter is evaluated.
1081               </para>
1082             </listitem>
1083           </varlistentry>
1084         </variablelist>
1085       </refsect1>
1086       <refsect1>
1087         <title>Description</title>
1088         <para>
1089           Returns the number of data bytes used by a foreign object type. This does not include any Lisp storage overhead.
1090         </para>
1091       </refsect1>
1092       <refsect1>
1093         <title>Examples</title>
1094         <para>
1095 <programlisting>
1096 (size-of-foreign-object :unsigned-byte)
1097 => 1
1098 (size-of-foreign-object 'my-100-byte-vector-type)
1099 => 100
1100 </programlisting>
1101         </para>
1102       </refsect1>
1103       <refsect1>
1104         <title>Side Effects</title>
1105         <para>None.</para>
1106       </refsect1>      <refsect1>
1107         <title>Affected by</title>
1108         <para>None.</para>
1109       </refsect1>
1110       <refsect1>
1111         <title>Exceptional Situations</title>
1112         <para>None.</para>
1113       </refsect1>
1114     </refentry>
1115
1116     <refentry id="pointer-address">
1117       <refnamediv>
1118         <refname>pointer-address</refname>
1119         <refpurpose>Returns the address of a pointer.
1120         </refpurpose>
1121         <refclass>Macro</refclass>
1122       </refnamediv>
1123       <refsynopsisdiv>
1124         <title>Syntax</title>
1125         <synopsis>
1126           <function>pointer-address</function> <replaceable>ptr</replaceable> => <returnvalue>address</returnvalue>
1127         </synopsis>
1128       </refsynopsisdiv>
1129       <refsect1>
1130         <title>Arguments and Values</title>
1131         <variablelist>
1132           <varlistentry>
1133             <term><parameter>ptr</parameter></term>
1134             <listitem>
1135               <para>A pointer to a foreign object.
1136               </para>
1137             </listitem>
1138           </varlistentry>
1139           <varlistentry>
1140             <term><parameter>address</parameter></term>
1141             <listitem>
1142               <para>An integer representing the pointer's address.
1143               </para>
1144             </listitem>
1145           </varlistentry>
1146         </variablelist>
1147       </refsect1>
1148       <refsect1>
1149         <title>Description</title>
1150         <para>
1151           Returns the address as an integer of a pointer.
1152         </para>
1153       </refsect1>
1154       <refsect1>
1155         <title>Side Effects</title>
1156         <para>None.</para>
1157       </refsect1>
1158       <refsect1>
1159         <title>Affected by</title>
1160         <para>None.</para>
1161       </refsect1>
1162       <refsect1>
1163         <title>Exceptional Situations</title>
1164         <para>None.</para>
1165       </refsect1>
1166     </refentry>
1167
1168
1169     <refentry id="deref-pointer">
1170       <refnamediv>
1171         <refname>deref-pointer</refname>
1172         <refpurpose>Deferences a pointer.
1173         </refpurpose>
1174         <refclass>Macro</refclass>
1175       </refnamediv>
1176       <refsynopsisdiv>
1177         <title>Syntax</title>
1178         <synopsis>
1179           <function>deref-pointer</function> <replaceable>ptr type</replaceable> => <returnvalue>value</returnvalue>
1180         </synopsis>
1181       </refsynopsisdiv>
1182       <refsect1>
1183         <title>Arguments and Values</title>
1184         <variablelist>
1185           <varlistentry>
1186             <term><parameter>ptr</parameter></term>
1187             <listitem>
1188               <para>A pointer to a foreign object.
1189               </para>
1190             </listitem>
1191           </varlistentry>
1192           <varlistentry>
1193             <term><parameter>type</parameter></term>
1194             <listitem>
1195               <para>A foreign type of the object being pointed to.
1196               </para>
1197             </listitem>
1198           </varlistentry>
1199           <varlistentry>
1200             <term><returnvalue>value</returnvalue></term>
1201             <listitem>
1202               <para>The value of the object where the pointer points.
1203               </para>
1204             </listitem>
1205           </varlistentry>
1206         </variablelist>
1207       </refsect1>
1208       <refsect1>
1209         <title>Description</title>
1210         <para>
1211           Returns the object to which a pointer points.
1212         </para>
1213       </refsect1>
1214       <refsect1>
1215         <title>Examples</title>
1216         <para>
1217 <programlisting>
1218 (let ((intp (allocate-foreign-object :int)))
1219   (setf (deref-pointer intp :int) 10)
1220   (prog1
1221     (deref-pointer intp :int)
1222     (free-foreign-object intp)))
1223 => 10
1224 </programlisting>
1225         </para>
1226       </refsect1>
1227       <refsect1>
1228         <title>Notes</title>
1229         <para>
1230   The TYPE argument is ignored for CL implementations other than
1231   AllegroCL. If you want to cast a pointer to another type use
1232   WITH-CAST-POINTER together with DEREF-POINTER/DEREF-ARRAY.
1233         </para>
1234       </refsect1>
1235       <refsect1>
1236         <title>Side Effects</title>
1237         <para>None.</para>
1238       </refsect1>
1239       <refsect1>
1240         <title>Affected by</title>
1241         <para>None.</para>
1242       </refsect1>
1243       <refsect1>
1244         <title>Exceptional Situations</title>
1245         <para>None.</para>
1246       </refsect1>
1247     </refentry>
1248
1249     <refentry id="ensure-char-character">
1250       <refnamediv>
1251         <refname>ensure-char-character</refname>
1252         <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1253 a character.
1254         </refpurpose>
1255         <refclass>Macro</refclass>
1256       </refnamediv>
1257       <refsynopsisdiv>
1258         <title>Syntax</title>
1259         <synopsis>
1260           <function>ensure-char-character</function> <replaceable>object</replaceable> => <returnvalue>char</returnvalue>
1261         </synopsis>
1262       </refsynopsisdiv>
1263       <refsect1>
1264         <title>Arguments and Values</title>
1265         <variablelist>
1266           <varlistentry>
1267             <term><parameter>object</parameter></term>
1268             <listitem>
1269               <para>Either a character or a integer specifying a character code.
1270               </para>
1271             </listitem>
1272           </varlistentry>
1273           <varlistentry>
1274             <term><returnvalue>char</returnvalue></term>
1275             <listitem>
1276               <para>A character.
1277               </para>
1278             </listitem>
1279           </varlistentry>
1280         </variablelist>
1281       </refsect1>
1282       <refsect1>
1283         <title>Description</title>
1284         <para>
1285           Ensures that an objects obtained by dereferencing  
1286 <constant>:char</constant> and <constant>:unsigned-char</constant>
1287 pointers are a lisp character.
1288         </para>
1289       </refsect1>
1290       <refsect1>
1291         <title>Examples</title>
1292         <para>
1293 <programlisting>
1294 (let ((fs (convert-to-foreign-string "a")))
1295   (prog1 
1296     (ensure-char-character (deref-pointer fs :char))
1297     (free-foreign-object fs)))
1298 => #\a
1299 </programlisting>
1300         </para>
1301       </refsect1>
1302       <refsect1>
1303         <title>Side Effects</title>
1304         <para>None.</para>
1305       </refsect1>
1306       <refsect1>
1307         <title>Affected by</title>
1308         <para>None.</para>
1309       </refsect1>
1310       <refsect1>
1311         <title>Exceptional Situations</title>
1312         <para>Depending upon the implementation and what &uffi; expects, this
1313 macro may signal an error if the object is not a character or
1314 integer.</para>
1315       </refsect1>
1316     </refentry>
1317
1318     <refentry id="ensure-char-integer">
1319       <refnamediv>
1320         <refname>ensure-char-integer</refname>
1321         <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
1322 an integer.
1323         </refpurpose>
1324         <refclass>Macro</refclass>
1325       </refnamediv>
1326       <refsynopsisdiv>
1327         <title>Syntax</title>
1328         <synopsis>
1329           <function>ensure-char-integer</function> <replaceable>object</replaceable> => <returnvalue>int</returnvalue>
1330         </synopsis>
1331       </refsynopsisdiv>
1332       <refsect1>
1333         <title>Arguments and Values</title>
1334         <variablelist>
1335           <varlistentry>
1336             <term><parameter>object</parameter></term>
1337             <listitem>
1338               <para>Either a character or a integer specifying a character code.
1339               </para>
1340             </listitem>
1341           </varlistentry>
1342           <varlistentry>
1343             <term><returnvalue>int</returnvalue></term>
1344             <listitem>
1345               <para>An integer.
1346               </para>
1347             </listitem>
1348           </varlistentry>
1349         </variablelist>
1350       </refsect1>
1351       <refsect1>
1352         <title>Description</title>
1353         <para>
1354           Ensures that an object obtained by dereferencing a 
1355 <constant>:char</constant> pointer is an integer.
1356         </para>
1357       </refsect1>
1358       <refsect1>
1359         <title>Examples</title>
1360         <para>
1361 <programlisting>
1362 (let ((fs (convert-to-foreign-string "a")))
1363   (prog1 
1364     (ensure-char-integer (deref-pointer fs :char))
1365     (free-foreign-object fs)))
1366 => 96
1367 </programlisting>
1368         </para>
1369       </refsect1>
1370       <refsect1>
1371         <title>Side Effects</title>
1372         <para>None.</para>
1373       </refsect1>
1374       <refsect1>
1375         <title>Affected by</title>
1376         <para>None.</para>
1377       </refsect1>
1378       <refsect1>
1379         <title>Exceptional Situations</title>
1380         <para>Depending upon the implementation and what &uffi; expects, this
1381 macro may signal an error if the object is not a character or
1382 integer.</para>
1383       </refsect1>
1384     </refentry>
1385
1386     <refentry id="make-null-pointer">
1387       <refnamediv>
1388         <refname>make-null-pointer</refname>
1389         <refpurpose>Create a &null; pointer.
1390         </refpurpose>
1391         <refclass>Macro</refclass>
1392       </refnamediv>
1393       <refsynopsisdiv>
1394         <title>Syntax</title>
1395         <synopsis>
1396           <function>make-null-pointer</function> <replaceable>type</replaceable> => <returnvalue>ptr</returnvalue>
1397         </synopsis>
1398       </refsynopsisdiv>
1399       <refsect1>
1400         <title>Arguments and Values</title>
1401         <variablelist>
1402           <varlistentry>
1403             <term><parameter>type</parameter></term>
1404             <listitem>
1405               <para>A type of object to which the pointer refers.
1406               </para>
1407             </listitem>
1408           </varlistentry>
1409           <varlistentry>
1410             <term><parameter>ptr</parameter></term>
1411             <listitem>
1412               <para>The &null; pointer of type <parameter>type</parameter>.
1413               </para>
1414             </listitem>
1415           </varlistentry>
1416         </variablelist>
1417       </refsect1>
1418       <refsect1>
1419         <title>Description</title>
1420         <para>
1421           Creates a &null; pointer of a specified type.
1422         </para>
1423       </refsect1>
1424       <refsect1>
1425         <title>Side Effects</title>
1426         <para>None.</para>
1427       </refsect1>
1428       <refsect1>
1429         <title>Affected by</title>
1430         <para>None.</para>
1431       </refsect1>
1432       <refsect1>
1433         <title>Exceptional Situations</title>
1434         <para>None.</para>
1435       </refsect1>
1436     </refentry>
1437
1438
1439     <refentry id="null-pointer-p">
1440       <refnamediv>
1441         <refname>null-pointer-p</refname>
1442         <refpurpose>Tests a pointer for &null; value.
1443         </refpurpose>
1444         <refclass>Macro</refclass>
1445       </refnamediv>
1446       <refsynopsisdiv>
1447         <title>Syntax</title>
1448         <synopsis>
1449           <function>null-pointer-p</function> <replaceable>ptr</replaceable> => <returnvalue>is-null</returnvalue>
1450         </synopsis>
1451       </refsynopsisdiv>
1452       <refsect1>
1453         <title>Arguments and Values</title>
1454         <variablelist>
1455           <varlistentry>
1456             <term><parameter>ptr</parameter></term>
1457             <listitem>
1458               <para>A foreign object pointer.
1459               </para>
1460             </listitem>
1461           </varlistentry>
1462           <varlistentry>
1463             <term><returnvalue>is-null</returnvalue></term>
1464             <listitem>
1465               <para>The boolean flag.
1466               </para>
1467             </listitem>
1468           </varlistentry>
1469         </variablelist>
1470       </refsect1>
1471       <refsect1>
1472         <title>Description</title>
1473         <para>
1474           A predicate testing if a pointer is has a &null; value.
1475         </para>
1476       </refsect1>
1477       <refsect1>
1478         <title>Side Effects</title>
1479         <para>None.</para>
1480       </refsect1>
1481       <refsect1>
1482         <title>Affected by</title>
1483         <para>None.</para>
1484       </refsect1>
1485       <refsect1>
1486         <title>Exceptional Situations</title>
1487         <para>None.</para>
1488       </refsect1>
1489     </refentry>
1490
1491
1492     <refentry id="null-cstring-pointer">
1493       <refnamediv>
1494         <refname>+null-cstring-pointer+</refname>
1495         <refpurpose>A constant &null; cstring pointer.
1496         </refpurpose>
1497         <refclass>Constant</refclass>
1498       </refnamediv>
1499       <refsect1>
1500         <title>Description</title>
1501         <para>
1502           A &null; cstring pointer. This can be used for testing
1503 if a cstring returned by a function is &null;.
1504         </para>
1505       </refsect1>
1506     </refentry>
1507
1508     <refentry id="with-cast-pointer">
1509       <refnamediv>
1510         <refname>with-cast-pointer</refname>
1511         <refpurpose>Wraps a body of code with a pointer cast to a new type.
1512         </refpurpose>
1513         <refclass>Macro</refclass>
1514       </refnamediv>
1515       <refsynopsisdiv>
1516         <title>Syntax</title>
1517         <synopsis>
1518           <function>with-cast-pointer</function> (<replaceable>binding-name ptr type) &amp; body body</replaceable> => <returnvalue>value</returnvalue>
1519         </synopsis>
1520       </refsynopsisdiv>
1521       <refsect1>
1522         <title>Arguments and Values</title>
1523         <variablelist>
1524           <varlistentry>
1525             <term><parameter>ptr</parameter></term>
1526             <listitem>
1527               <para>A pointer to a foreign object.
1528               </para>
1529             </listitem>
1530           </varlistentry>
1531           <varlistentry>
1532             <term><parameter>type</parameter></term>
1533             <listitem>
1534               <para>A foreign type of the object being pointed to.
1535               </para>
1536             </listitem>
1537           </varlistentry>
1538           <varlistentry>
1539             <term><returnvalue>value</returnvalue></term>
1540             <listitem>
1541               <para>The value of the object where the pointer points.
1542               </para>
1543             </listitem>
1544           </varlistentry>
1545         </variablelist>
1546       </refsect1>
1547       <refsect1>
1548         <title>Description</title>
1549         <para>
1550   Executes BODY with POINTER cast to be a pointer to type TYPE. If
1551   BINDING-NAME is provided the cast pointer will be bound to this
1552   name during the execution of BODY. If BINDING-NAME is not provided
1553   POINTER must be a name bound to the pointer which should be
1554   cast. This name will be bound to the cast pointer during the
1555   execution of BODY.
1556
1557   This is a no-op in AllegroCL but will wrap BODY in a LET form if
1558   BINDING-NAME is provided.
1559
1560   This macro is meant to be used in conjunction with DEREF-POINTER or
1561   DEREF-ARRAY. In Allegro CL the "cast" will actually take place in
1562   DEREF-POINTER or DEREF-ARRAY.
1563         </para>
1564       </refsect1>
1565       <refsect1>
1566         <title>Examples</title>
1567 <programlisting>
1568 (with-foreign-object (size :int)
1569    ;; FOO is a foreign function returning a :POINTER-VOID
1570    (let ((memory (foo size)))
1571       (when (mumble)
1572          ;; at this point we know for some reason that MEMORY points
1573          ;; to an array of unsigned bytes
1574          (with-cast-pointer (memory :unsigned-byte)
1575            (dotimes (i (deref-pointer size :int))
1576             (do-something-with
1577               (deref-array memory '(:array :unsigned-byte) i)))))))
1578 </programlisting>
1579       </refsect1>
1580       <refsect1>
1581         <title>Side Effects</title>
1582         <para>None.</para>
1583       </refsect1>
1584       <refsect1>
1585         <title>Affected by</title>
1586         <para>None.</para>
1587       </refsect1>
1588       <refsect1>
1589         <title>Exceptional Situations</title>
1590         <para>None.</para>
1591       </refsect1>
1592     </refentry>
1593
1594 </reference>
1595
1596     <reference>
1597       <title>Strings</title>
1598 <partintro>
1599 <title>Overview</title>
1600 <para>
1601
1602           &uffi; has functions to two types of
1603 <varname>C</varname>-compatible
1604           strings: <emphasis>cstring</emphasis> and
1605 <emphasis>foreign</emphasis> strings.
1606
1607 cstrings are used <emphasis>only</emphasis> as parameters to and from
1608 functions. In some implementations a cstring is not a foreign type but
1609 rather the Lisp string itself. On other platforms a cstring is a newly
1610 allocated foreign vector for storing characters. The following is an
1611 example of using cstrings to both send and return a value.  
1612 </para>
1613
1614 <programlisting>
1615 (uffi:def-function ("getenv" c-getenv) 
1616     ((name :cstring))
1617   :returning :cstring)
1618
1619 (defun my-getenv (key)
1620   "Returns an environment variable, or NIL if it does not exist"
1621   (check-type key string)
1622   (uffi:with-cstring (key-native key)
1623     (uffi:convert-from-cstring (c-getenv key-native))))
1624 </programlisting>
1625
1626 <para> In contrast, foreign strings are always a foreign vector of
1627 characters which have memory allocated. Thus, if you need to allocate
1628 memory to hold the return value of a string, you must use a foreign
1629 string and not a cstring.  The following is an example of using a foreign
1630 string for a return value.  </para>
1631
1632 <programlisting>
1633 (uffi:def-function ("gethostname" c-gethostname)
1634     ((name (* :unsigned-char))
1635      (len :int))
1636   :returning :int)
1637
1638 (defun gethostname ()
1639   "Returns the hostname"
1640   (let* ((name (uffi:allocate-foreign-string 256))
1641          (result-code (c-gethostname name 256))
1642          (hostname (when (zerop result-code)
1643                      (uffi:convert-from-foreign-string name))))
1644     (uffi:free-foreign-object name)
1645     (unless (zerop result-code)
1646       (error "gethostname() failed."))))
1647 </programlisting>
1648
1649 <para>  Foreign functions that return pointers to freshly allocated
1650 strings should in general not return cstrings, but foreign strings.
1651 (There is no portable way to release such cstrings from Lisp.)  The
1652 following is an example of handling such a function.  </para>
1653
1654 <programlisting>
1655 (uffi:def-function ("readline" c-readline)
1656     ((prompt :cstring))
1657   :returning (* :char))
1658
1659 (defun readline (prompt)
1660   "Reads a string from console with line-editing."
1661   (with-cstring (c-prompt prompt)
1662       (let* ((c-str (c-readline c-prompt))
1663              (str (convert-from-foreign-string c-str)))
1664         (uffi:free-foreign-object c-str)
1665         str)))
1666 </programlisting>
1667
1668 </partintro>
1669
1670     <refentry id="convert-from-cstring">
1671       <refnamediv>
1672         <refname>convert-from-cstring</refname>
1673         <refpurpose>Converts a cstring to a Lisp string.
1674         </refpurpose>
1675         <refclass>Macro</refclass>
1676       </refnamediv>
1677       <refsynopsisdiv>
1678         <title>Syntax</title>
1679         <synopsis>
1680           <function>convert-from-cstring</function> <replaceable>cstring</replaceable> => <returnvalue>string</returnvalue>
1681         </synopsis>
1682       </refsynopsisdiv>
1683       <refsect1>
1684         <title>Arguments and Values</title>
1685         <variablelist>
1686           <varlistentry>
1687             <term><parameter>cstring</parameter></term>
1688             <listitem>
1689               <para>A cstring.
1690               </para>
1691             </listitem>
1692           </varlistentry>
1693           <varlistentry>
1694             <term><returnvalue>string</returnvalue></term>
1695             <listitem>
1696               <para>A Lisp string.
1697               </para>
1698             </listitem>
1699           </varlistentry>
1700         </variablelist>
1701       </refsect1>
1702       <refsect1>
1703         <title>Description</title>
1704         <para>
1705           Converts a Lisp string to a <constant>cstring</constant>. This is
1706 most often used when processing the results of a foreign function
1707 that returns a cstring.
1708         </para>
1709       </refsect1>
1710       <refsect1>
1711         <title>Side Effects</title>
1712         <para>None.</para>
1713       </refsect1>
1714       <refsect1>
1715         <title>Affected by</title>
1716         <para>None.</para>
1717       </refsect1>
1718       <refsect1>
1719         <title>Exceptional Situations</title>
1720         <para>None.</para>
1721       </refsect1>
1722     </refentry>
1723
1724
1725     <refentry id="convert-to-cstring">
1726       <refnamediv>
1727         <refname>convert-to-cstring</refname>
1728         <refpurpose>Converts a Lisp string to a cstring.
1729         </refpurpose>
1730         <refclass>Macro</refclass>
1731       </refnamediv>
1732       <refsynopsisdiv>
1733         <title>Syntax</title>
1734         <synopsis>
1735           <function>convert-to-cstring</function> <replaceable>string</replaceable> => <returnvalue>cstring</returnvalue>
1736         </synopsis>
1737       </refsynopsisdiv>
1738       <refsect1>
1739         <title>Arguments and Values</title>
1740         <variablelist>
1741           <varlistentry>
1742             <term><parameter>string</parameter></term>
1743             <listitem>
1744               <para>A Lisp string.
1745               </para>
1746             </listitem>
1747           </varlistentry>
1748           <varlistentry>
1749             <term><returnvalue>cstring</returnvalue></term>
1750             <listitem>
1751               <para>A cstring.
1752               </para>
1753             </listitem>
1754           </varlistentry>
1755         </variablelist>
1756       </refsect1>
1757       <refsect1>
1758         <title>Description</title>
1759         <para>
1760           Converts a Lisp string to a
1761           <varname>cstring</varname>. The
1762           <varname>cstring</varname> should be freed with
1763           <function>free-cstring</function>.
1764         </para>
1765       </refsect1>
1766       <refsect1>
1767         <title>Side Effects</title>
1768         <para>On some implementations, this function allocates memory.</para>
1769       </refsect1>
1770       <refsect1>
1771         <title>Affected by</title>
1772         <para>None.</para>
1773       </refsect1>
1774       <refsect1>
1775         <title>Exceptional Situations</title>
1776         <para>None.</para>
1777       </refsect1>
1778     </refentry>
1779
1780
1781     <refentry id="free-cstring">
1782       <refnamediv>
1783         <refname>free-cstring</refname>
1784         <refpurpose>Free memory used by cstring.
1785         </refpurpose>
1786         <refclass>Macro</refclass>
1787       </refnamediv>
1788       <refsynopsisdiv>
1789         <title>Syntax</title>
1790         <synopsis>
1791           <function>free-cstring</function> <replaceable>cstring</replaceable>
1792         </synopsis>
1793       </refsynopsisdiv>
1794       <refsect1>
1795         <title>Arguments and Values</title>
1796         <variablelist>
1797           <varlistentry>
1798             <term><parameter>cstring</parameter></term>
1799             <listitem>
1800               <para>A cstring.
1801               </para>
1802             </listitem>
1803           </varlistentry>
1804         </variablelist>
1805       </refsect1>
1806       <refsect1>
1807         <title>Description</title>
1808         <para>
1809           Frees any memory possibly allocated by
1810           <function>convert-to-cstring</function>. On some implementions, a cstring is just the Lisp string itself.
1811         </para>
1812       </refsect1>
1813       <refsect1>
1814         <title>Side Effects</title>
1815         <para>None.</para>
1816       </refsect1>
1817       <refsect1>
1818         <title>Affected by</title>
1819         <para>None.</para>
1820       </refsect1>
1821       <refsect1>
1822         <title>Exceptional Situations</title>
1823         <para>None.</para>
1824       </refsect1>
1825     </refentry>
1826
1827
1828     <refentry id="with-cstring">
1829       <refnamediv>
1830         <refname>with-cstring</refname>
1831         <refpurpose>Binds a newly created cstring.
1832         </refpurpose>
1833         <refclass>Macro</refclass>
1834       </refnamediv>
1835       <refsynopsisdiv>
1836         <title>Syntax</title>
1837         <synopsis>
1838           <function>with-cstring</function> <replaceable>(cstring string) {body}</replaceable>
1839         </synopsis>
1840       </refsynopsisdiv>
1841       <refsect1>
1842         <title>Arguments and Values</title>
1843         <variablelist>
1844           <varlistentry>
1845             <term><parameter>cstring</parameter></term>
1846             <listitem>
1847               <para>A symbol naming the cstring to be created.
1848               </para>
1849             </listitem>
1850           </varlistentry>
1851           <varlistentry>
1852             <term><parameter>string</parameter></term>
1853             <listitem>
1854               <para>A Lisp string that will be translated to a cstring.
1855               </para>
1856             </listitem>
1857           </varlistentry>
1858           <varlistentry>
1859             <term><parameter>body</parameter></term>
1860             <listitem>
1861               <para>The body of where the cstring will be bound.
1862               </para>
1863             </listitem>
1864           </varlistentry>
1865         </variablelist>
1866       </refsect1>
1867       <refsect1>
1868         <title>Description</title>
1869         <para>
1870           Binds a symbol to a cstring created from conversion of a string. Automatically frees the <varname>cstring</varname>.
1871         </para>
1872       </refsect1>
1873       <refsect1>
1874         <title>Examples</title>
1875         <para>
1876             <programlisting>
1877 (def-function ("getenv" c-getenv) 
1878    ((name :cstring))
1879    :returning :cstring)
1880
1881 (defun getenv (key)
1882   "Returns an environment variable, or NIL if it does not exist"
1883   (check-type key string)
1884   (with-cstring (key-cstring key)
1885     (convert-from-cstring (c-getenv key-cstring))))
1886             </programlisting>
1887           </para>
1888         </refsect1>
1889       <refsect1>
1890         <title>Side Effects</title>
1891         <para>None.</para>
1892       </refsect1>
1893       <refsect1>
1894         <title>Affected by</title>
1895         <para>None.</para>
1896       </refsect1>
1897       <refsect1>
1898         <title>Exceptional Situations</title>
1899         <para>None.</para>
1900       </refsect1>
1901     </refentry>
1902
1903
1904     <refentry id="convert-from-foreign-string">
1905       <refnamediv>
1906         <refname>convert-from-foreign-string</refname>
1907         <refpurpose>Converts a foreign string into a Lisp string.
1908         </refpurpose>
1909         <refclass>Macro</refclass>
1910       </refnamediv>
1911       <refsynopsisdiv>
1912         <title>Syntax</title>
1913         <synopsis>
1914           <function>convert-from-foreign-string</function> <replaceable>foreign-string &amp;key length null-terminated-p</replaceable> => <returnvalue>string</returnvalue>
1915         </synopsis>
1916       </refsynopsisdiv>
1917       <refsect1>
1918         <title>Arguments and Values</title>
1919         <variablelist>
1920           <varlistentry>
1921             <term><parameter>foreign-string</parameter></term>
1922             <listitem>
1923               <para>A foreign string.
1924               </para>
1925             </listitem>
1926           </varlistentry>
1927           <varlistentry>
1928             <term><parameter>length</parameter></term>
1929             <listitem>
1930               <para>The length of the foreign string to
1931 convert. The default is the length of the string until a &null;
1932 character is reached.
1933               </para>
1934             </listitem>
1935           </varlistentry>
1936           <varlistentry>
1937             <term><parameter>null-terminated-p</parameter></term>
1938             <listitem>
1939               <para>A boolean flag with a default value of &t; When true,
1940 the string is converted until the first &null; character is reached.
1941               </para>
1942             </listitem>
1943           </varlistentry>
1944           <varlistentry>
1945             <term><returnvalue>string</returnvalue></term>
1946             <listitem>
1947               <para>A Lisp string.
1948               </para>
1949             </listitem>
1950           </varlistentry>
1951         </variablelist>
1952       </refsect1>
1953       <refsect1>
1954         <title>Description</title>
1955         <para>
1956           Returns a Lisp string from a foreign string. 
1957 Can translated ASCII and binary strings.
1958         </para>
1959       </refsect1>
1960       <refsect1>
1961         <title>Side Effects</title>
1962         <para>None.</para>
1963       </refsect1>
1964       <refsect1>
1965         <title>Affected by</title>
1966         <para>None.</para>
1967       </refsect1>
1968       <refsect1>
1969         <title>Exceptional Situations</title>
1970         <para>None.</para>
1971       </refsect1>
1972     </refentry>
1973
1974
1975     <refentry id="convert-to-foreign-string">
1976       <refnamediv>
1977         <refname>convert-to-foreign-string</refname>
1978         <refpurpose>Converts a Lisp string to a foreign string.
1979         </refpurpose>
1980         <refclass>Macro</refclass>
1981       </refnamediv>
1982       <refsynopsisdiv>
1983         <title>Syntax</title>
1984         <synopsis>
1985           <function>convert-to-foreign-string</function> <replaceable>string</replaceable> => <returnvalue>foreign-string</returnvalue>
1986         </synopsis>
1987       </refsynopsisdiv>
1988       <refsect1>
1989         <title>Arguments and Values</title>
1990         <variablelist>
1991           <varlistentry>
1992             <term><parameter>string</parameter></term>
1993             <listitem>
1994               <para>A Lisp string.
1995               </para>
1996             </listitem>
1997           </varlistentry>
1998           <varlistentry>
1999             <term><returnvalue>foreign-string</returnvalue></term>
2000             <listitem>
2001               <para>A foreign string.
2002               </para>
2003             </listitem>
2004           </varlistentry>
2005         </variablelist>
2006       </refsect1>
2007       <refsect1>
2008         <title>Description</title>
2009         <para>
2010           Converts a Lisp string to a foreign string. Memory should be
2011           freed with <function>free-foreign-object</function>.
2012         </para>
2013       </refsect1>
2014       <refsect1>
2015         <title>Side Effects</title>
2016         <para>None.</para>
2017       </refsect1>
2018       <refsect1>
2019         <title>Affected by</title>
2020         <para>None.</para>
2021       </refsect1>
2022       <refsect1>
2023         <title>Exceptional Situations</title>
2024         <para>None.</para>
2025       </refsect1>
2026     </refentry>
2027
2028
2029
2030     <refentry id="allocate-foreign-string">
2031       <refnamediv>
2032         <refname>allocate-foreign-string</refname>
2033         <refpurpose>Allocates space for a foreign string.
2034         </refpurpose>
2035         <refclass>Macro</refclass>
2036       </refnamediv>
2037       <refsynopsisdiv>
2038         <title>Syntax</title>
2039         <synopsis>
2040           <function>allocate-foreign-string</function> <replaceable>size &amp;key unsigned</replaceable> => <returnvalue>foreign-string</returnvalue>
2041         </synopsis>
2042       </refsynopsisdiv>
2043       <refsect1>
2044         <title>Arguments and Values</title>
2045         <variablelist>
2046           <varlistentry>
2047             <term><parameter>size</parameter></term>
2048             <listitem>
2049               <para>The size of the space to be allocated in bytes.
2050               </para>
2051             </listitem>
2052           </varlistentry>
2053           <varlistentry>
2054             <term><parameter>unsigned</parameter></term>
2055             <listitem>
2056               <para>A boolean flag with a default value of &t;. When true,
2057 marks the pointer as an <constant>:unsigned-char</constant>.
2058               </para>
2059             </listitem>
2060           </varlistentry>
2061           <varlistentry>
2062             <term><returnvalue>foreign-string</returnvalue></term>
2063             <listitem>
2064               <para>A foreign string which has undefined contents.
2065               </para>
2066             </listitem>
2067           </varlistentry>
2068         </variablelist>
2069       </refsect1>
2070       <refsect1>
2071         <title>Description</title>
2072         <para>
2073           Allocates space for a foreign string. Memory should
2074           be freed with <function>free-foreign-object</function>.
2075         </para>
2076       </refsect1>
2077       <refsect1>
2078         <title>Side Effects</title>
2079         <para>None.</para>
2080       </refsect1>
2081       <refsect1>
2082         <title>Affected by</title>
2083         <para>None.</para>
2084       </refsect1>
2085       <refsect1>
2086         <title>Exceptional Situations</title>
2087         <para>None.</para>
2088       </refsect1>
2089     </refentry>
2090
2091   </reference>
2092
2093   <reference>
2094       <title>Functions &amp; Libraries</title>
2095
2096       <refentry id="def-function">
2097         <refnamediv>
2098           <refname>def-function</refname>
2099         <refpurpose>Declares a function. 
2100         </refpurpose>
2101         <refclass>Macro</refclass>
2102       </refnamediv>
2103       <refsynopsisdiv>
2104         <title>Syntax</title>
2105         <synopsis>
2106           <function>def-function</function> <replaceable>name args &amp;key module returning</replaceable>
2107         </synopsis>
2108       </refsynopsisdiv>
2109       <refsect1>
2110         <title>Arguments and Values</title>
2111         <variablelist>
2112           <varlistentry>
2113             <term><parameter>name</parameter></term>
2114             <listitem>
2115               <para>A string or list specificying the function name. If it is a string, that names the foreign function. A Lisp name is created by translating #\_ to #\- and by converting to upper-case in case-insensitive Lisp implementations. If it is a list, the first item is a string specifying the foreign function name and the second it is a symbol stating the Lisp name.
2116               </para>
2117             </listitem>
2118           </varlistentry>
2119           <varlistentry>
2120             <term><parameter>args</parameter></term>
2121             <listitem>
2122               <para>A list of argument declarations. If &nil;, indicates that the function does not take any arguments.
2123               </para>
2124             </listitem>
2125           </varlistentry>
2126           <varlistentry>
2127             <term><parameter>module</parameter></term>
2128             <listitem>
2129               <para>A string specifying which module (or library) that the foreign function resides. (Required by Lispworks)</para>
2130             </listitem>
2131           </varlistentry>
2132           <varlistentry>
2133             <term><returnvalue>returning</returnvalue></term>
2134             <listitem>
2135               <para>A declaration specifying the result type of the
2136 foreign function. If <constant>:void</constant> indicates module does not return any value.
2137               </para>
2138             </listitem>
2139           </varlistentry>
2140         </variablelist>
2141       </refsect1>
2142       <refsect1>
2143         <title>Description</title>
2144         <para>Declares a foreign function.
2145         </para>
2146       </refsect1>
2147       <refsect1>
2148         <title>Examples</title>
2149         <programlisting>
2150 (def-function "gethostname" 
2151   ((name (* :unsigned-char))
2152    (len :int))
2153   :returning :int)
2154         </programlisting>
2155       </refsect1>
2156       <refsect1>
2157         <title>Side Effects</title>
2158         <para>None.</para>
2159       </refsect1>
2160       <refsect1>
2161         <title>Affected by</title>
2162         <para>None.</para>
2163       </refsect1>
2164       <refsect1>
2165         <title>Exceptional Situations</title>
2166         <para>None.</para>
2167       </refsect1>
2168     </refentry>
2169
2170       <refentry id="load-foreign-library">
2171         <refnamediv>
2172           <refname>load-foreign-library</refname>
2173         <refpurpose>Loads a foreign library. 
2174         </refpurpose>
2175         <refclass>Function</refclass>
2176       </refnamediv>
2177       <refsect1>
2178         <title>Syntax</title>
2179 <synopsis>
2180           <function>load-foreign-library</function> <replaceable>filename &amp;key module supporting-libraries force-load</replaceable> => <returnvalue>success</returnvalue>
2181 </synopsis>
2182       </refsect1>
2183       <refsect1>
2184         <title>Arguments and Values</title>
2185         <variablelist>
2186           <varlistentry>
2187             <term><parameter>filename</parameter></term>
2188             <listitem>
2189               <para>A string or pathname specifying the library location
2190 in the filesystem. At least one implementation (&lw;) can not
2191 accept a logical pathname.
2192               </para>
2193             </listitem>
2194           </varlistentry>
2195           <varlistentry>
2196             <term><parameter>module</parameter></term>
2197             <listitem>
2198               <para>A string designating the name of the module to apply
2199 to functions in this library. (Required for Lispworks)
2200               </para>
2201             </listitem>
2202           </varlistentry>
2203           <varlistentry>
2204             <term><parameter>supporting-libraries</parameter></term>
2205             <listitem>
2206               <para>A list of strings naming the libraries required to
2207 link the foreign library. (Required by CMUCL)
2208               </para>
2209             </listitem>
2210           </varlistentry>
2211           <varlistentry>
2212             <term><parameter>force-load</parameter></term>
2213             <listitem>
2214               <para>Forces the loading of the library if it has been previously loaded. 
2215               </para>
2216             </listitem>
2217           </varlistentry>
2218           <varlistentry>
2219             <term><returnvalue>success</returnvalue></term>
2220             <listitem>
2221               <para>A boolean flag, &t; if the library was able to be
2222 loaded successfully or if the library has been previously loaded,
2223 otherwise &nil;.
2224               </para>
2225             </listitem>
2226           </varlistentry>
2227         </variablelist>
2228       </refsect1>
2229       <refsect1>
2230         <title>Description</title>
2231         <para>Loads a foreign library. Applies a module name to functions
2232 within the library. Ensures that a library is only loaded once during
2233 a session. A library can be reloaded by using the <symbol>:force-load</symbol> key.
2234         </para>
2235       </refsect1>
2236       <refsect1>
2237         <title>Examples</title>
2238         <programlisting>
2239   (load-foreign-library #p"/usr/lib/libmysqlclient.so" 
2240                         :module "mysql" 
2241                         :supporting-libraries '("c"))
2242     => T
2243         </programlisting>
2244       </refsect1>
2245       <refsect1>
2246         <title>Side Effects</title>
2247         <para>Loads the foreign code into the Lisp system.
2248         </para>
2249       </refsect1>
2250       <refsect1>
2251         <title>Affected by</title>
2252         <para>Ability to load the file.</para>
2253       </refsect1>
2254       <refsect1>
2255         <title>Exceptional Situations</title>
2256         <para>None.</para>
2257       </refsect1>
2258     </refentry>
2259
2260       <refentry id="find-foreign-library">
2261         <refnamediv>
2262           <refname>find-foreign-library</refname>
2263         <refpurpose>Finds a foreign library file.
2264         </refpurpose>
2265         <refclass>Function</refclass>
2266       </refnamediv>
2267       <refsect1>
2268         <title>Syntax</title>
2269 <synopsis>
2270           <function>find-foreign-library</function> <replaceable>names directories &amp; drive-letters types</replaceable> => <returnvalue>path</returnvalue>
2271 </synopsis>
2272       </refsect1>
2273       <refsect1>
2274         <title>Arguments and Values</title>
2275         <variablelist>
2276           <varlistentry>
2277             <term><parameter>names</parameter></term>
2278             <listitem>
2279               <para>A string or list of strings containing the base name of the library file.
2280               </para>
2281             </listitem>
2282           </varlistentry>
2283           <varlistentry>
2284             <term><parameter>directories</parameter></term>
2285             <listitem>
2286               <para>A string or list of strings containing the directory the library file.
2287               </para>
2288             </listitem>
2289           </varlistentry>
2290           <varlistentry>
2291             <term><parameter>drive-letters</parameter></term>
2292             <listitem>
2293               <para>A string or list of strings containing the drive letters for the library file.
2294               </para>
2295             </listitem>
2296           </varlistentry>
2297           <varlistentry>
2298             <term><parameter>types</parameter></term>
2299             <listitem>
2300               <para>A string or list of strings containing the file type of the library file. Default
2301 is &nil;. If &nil;, will use a default type based on the currently running implementation.
2302               </para>
2303             </listitem>
2304           </varlistentry>
2305           <varlistentry>
2306             <term><returnvalue>path</returnvalue></term>
2307             <listitem>
2308               <para>A path containing the path found, or &nil; if the library file was not found.
2309               </para>
2310             </listitem>
2311           </varlistentry>
2312         </variablelist>
2313       </refsect1>
2314       <refsect1>
2315         <title>Description</title>
2316         <para>Finds a foreign library by searching through a number of possible locations. Returns
2317 the path of the first found file.
2318         </para>
2319       </refsect1>
2320       <refsect1>
2321         <title>Examples</title>
2322         <programlisting>
2323 (find-foreign-library '("libmysqlclient" "libmysql")
2324     '("/opt/mysql/lib/mysql/" "/usr/local/lib/" "/usr/lib/" "/mysql/lib/opt/")
2325     :types '("so" "dll")
2326     :drive-letters '("C" "D" "E"))
2327 => #P"D:\\mysql\\lib\\opt\\libmysql.dll"
2328         </programlisting>
2329       </refsect1>
2330       <refsect1>
2331         <title>Side Effects</title>
2332         <para>None.
2333         </para>
2334       </refsect1>
2335       <refsect1>
2336         <title>Affected by</title>
2337         <para>None.</para>
2338       </refsect1>
2339       <refsect1>
2340         <title>Exceptional Situations</title>
2341         <para>None.</para>
2342       </refsect1>
2343     </refentry>
2344
2345 </reference>
2346
2347