Partially sync files.el from XEmacs 21.5 for wildcard support.
[sxemacs] / src / effi.h
1 /*
2  * effi.h --- Header for Foreign Function Interface.
3  *
4  * Copyright (C) 2004 Evgeny Zajcev.
5  * Copyright (C) 2008 Steve Youngs.
6  *
7 This file is part of SXEmacs
8
9 SXEmacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 SXEmacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
21
22 #ifndef _SXEMACS_EFFI_H_
23 #define _SXEMACS_EFFI_H_
24
25 #ifndef FFI_STACK_SIZE
26 #define FFI_STACK_SIZE 4096
27 #endif  /* FFI_STACK_SIZE */
28
29 /** Errors **/
30 #define EFFI_OK  0
31 #define EFFI_ARG 1
32 #define EFFI_ERR 2
33
34 struct Lisp_EffiObject {
35 #ifdef SXEMACS
36         struct lcrecord_header header;
37 #else
38         struct old_lcrecord_header header;
39 #endif  /* SXEMACS */
40         Lisp_Object type;               /* type of ffi object */
41         Lisp_Object size;               /* size of ffi object type */
42
43         Lisp_Object plist;              /* properties list */
44
45         /* Foreign stuff */
46         int fotype;
47 #define EFFI_FOT_NONE  0                /* fop is not used */
48 #define EFFI_FOT_BIND  2                /* fop is reference to foreign data */
49 #define EFFI_FOT_FUNC  3                /* fop is pointer to function */
50
51         /*
52          * Declared as union just for the style, there no problem to
53          * always use fop.generic to access pointer.
54          */
55         union {
56                 void *ptr;              /* pointer to foreign data */
57                 void *fun;              /* pointer to foreign function */
58                 void *generic;          /* generic storer */
59         } fop;
60
61         size_t storage_size;            /* size of storage */
62         char fostorage[0];              /* storage */
63 };
64
65 #define EFFIO_HAS_FOP(fo) ((fo)->fotype != EFFI_FOT_NONE)
66
67 typedef struct Lisp_EffiObject Lisp_EffiObject;
68
69 DECLARE_LRECORD(ffiobject, Lisp_EffiObject);
70 #define XEFFIO(x) XRECORD (x, ffiobject, Lisp_EffiObject)
71 #ifdef SXEMACS
72 #  define XSETEFFIO(x, p) XSETRECORD (x, p, ffiobject)
73 #else
74 #  define wrap_effio(p) wrap_record (p, ffiobject)
75 #endif  /* SXEMACS */
76 #define EFFIOP(x) RECORDP (x, ffiobject)
77 #define CHECK_EFFIO(x) CHECK_RECORD (x, ffiobject)
78 #define CONCHECK_EFFIO(x) CONCHECK_RECORD (x, ffiobject)
79
80 EXFUN(Fffi_slot_offset, 2);
81 EXFUN(Fffi_size_of_type, 1);
82 EXFUN(Fffi_plist, 1);
83
84 #ifdef EF_USE_ASYNEQ
85 #include "events/event-queue.h"
86
87 typedef struct ffi_job_s *ffi_job_t;
88 struct ffi_job_s {
89         sxe_mutex_t mtx;
90         event_queue_t queue;
91
92         /* the foreign function and its args */
93         Lisp_Object fof;
94         int fof_nargs;
95         Lisp_Object *fof_args;
96
97         /* the sentinel and its args */
98         Lisp_Object sntnl;
99         int sntnl_nargs;
100         Lisp_Object *sntnl_args;
101
102         /* the result fo */
103         Lisp_Object retfo;
104         Lisp_Object result;
105 };
106
107 #define ffi_job(_x)             (((ffi_job_t)worker_job_data(_x)))
108 #define ffi_job_sentinel(_x)    (ffi_job(_x)->sntnl)
109 #define ffi_job_result(_x)      (ffi_job(_x)->result)
110 #define XFFI_JOB(_x)            ((ffi_job_t)(XWORKER_JOB_DATA(_x)))
111 #define FFI_JOBP(_x)                                                    \
112         (WORKER_JOBP(_x) && XWORKER_JOB_HANDLER(_x) == &ffi_job_handler)
113 #define CHECK_FFI_JOB(_x)                                               \
114         do {                                                            \
115                 if (!FFI_JOBP(_x))                                      \
116                         dead_wrong_type_argument(Qffi_jobp, _x);        \
117         } while (0)
118 #define CONCHECK_FFI_JOB(_x)                                            \
119         do {                                                            \
120                 if (!FFI_JOBP(_x))                                      \
121                         return wrong_type_argument(Qffi_jobp, _x);      \
122         } while (0)
123
124 #endif  /* EF_USE_ASYNEQ */
125
126 #endif /* _SXEMACS_FFI_H_ */