12 the following conditions:
13
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 ----------------------------------------------------------------------- */
26
27 /* This file defines generic functions for use with the raw api. */
28
29 #include <ffi.h>
30 #include <ffi_common.h>
31
32 #if !FFI_NO_RAW_API
33
34 size_t
35 ffi_raw_size (ffi_cif *cif)
36 {
37 size_t result = 0;
38 int i;
39
40 ffi_type **at = cif->arg_types;
41
42 for (i = cif->nargs-1; i >= 0; i--, at++)
43 {
44 #if !FFI_NO_STRUCTS
45 if ((*at)->type == FFI_TYPE_STRUCT)
46 result += ALIGN (sizeof (void*), FFI_SIZEOF_ARG);
47 else
48 #endif
49 result += ALIGN ((*at)->size, FFI_SIZEOF_ARG);
50 }
51
52 return result;
53 }
54
55
56 void
57 ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args)
58 {
59 unsigned i;
60 ffi_type **tp = cif->arg_types;
61
62 #if WORDS_BIGENDIAN
63
64 for (i = 0; i < cif->nargs; i++, tp++, args++)
65 {
66 switch ((*tp)->type)
67 {
68 case FFI_TYPE_UINT8:
69 case FFI_TYPE_SINT8:
81 *args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 4);
82 break;
83 #endif
84
85 #if !FFI_NO_STRUCTS
86 case FFI_TYPE_STRUCT:
87 *args = (raw++)->ptr;
88 break;
89 #endif
90
91 case FFI_TYPE_COMPLEX:
92 *args = (raw++)->ptr;
93 break;
94
95 case FFI_TYPE_POINTER:
96 *args = (void*) &(raw++)->ptr;
97 break;
98
99 default:
100 *args = raw;
101 raw += ALIGN ((*tp)->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
102 }
103 }
104
105 #else /* WORDS_BIGENDIAN */
106
107 #if !PDP
108
109 /* then assume little endian */
110 for (i = 0; i < cif->nargs; i++, tp++, args++)
111 {
112 #if !FFI_NO_STRUCTS
113 if ((*tp)->type == FFI_TYPE_STRUCT)
114 {
115 *args = (raw++)->ptr;
116 }
117 else
118 #endif
119 if ((*tp)->type == FFI_TYPE_COMPLEX)
120 {
121 *args = (raw++)->ptr;
122 }
123 else
124 {
125 *args = (void*) raw;
126 raw += ALIGN ((*tp)->size, sizeof (void*)) / sizeof (void*);
127 }
128 }
129
130 #else
131 #error "pdp endian not supported"
132 #endif /* ! PDP */
133
134 #endif /* WORDS_BIGENDIAN */
135 }
136
137 void
138 ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw)
139 {
140 unsigned i;
141 ffi_type **tp = cif->arg_types;
142
143 for (i = 0; i < cif->nargs; i++, tp++, args++)
144 {
145 switch ((*tp)->type)
146 {
169 (raw++)->sint = *(SINT32*) (*args);
170 break;
171 #endif
172
173 #if !FFI_NO_STRUCTS
174 case FFI_TYPE_STRUCT:
175 (raw++)->ptr = *args;
176 break;
177 #endif
178
179 case FFI_TYPE_COMPLEX:
180 (raw++)->ptr = *args;
181 break;
182
183 case FFI_TYPE_POINTER:
184 (raw++)->ptr = **(void***) args;
185 break;
186
187 default:
188 memcpy ((void*) raw->data, (void*)*args, (*tp)->size);
189 raw += ALIGN ((*tp)->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
190 }
191 }
192 }
193
194 #if !FFI_NATIVE_RAW_API
195
196
197 /* This is a generic definition of ffi_raw_call, to be used if the
198 * native system does not provide a machine-specific implementation.
199 * Having this, allows code to be written for the raw API, without
200 * the need for system-specific code to handle input in that format;
201 * these following couple of functions will handle the translation forth
202 * and back automatically. */
203
204 void ffi_raw_call (ffi_cif *cif, void (*fn)(void), void *rvalue, ffi_raw *raw)
205 {
206 void **avalue = (void**) alloca (cif->nargs * sizeof (void*));
207 ffi_raw_to_ptrarray (cif, raw, avalue);
208 ffi_call (cif, fn, rvalue, avalue);
209 }
|
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 ----------------------------------------------------------------------- */
26
27 /* This file defines generic functions for use with the raw api. */
28
29 #include <ffi.h>
30 #include <ffi_common.h>
31
32 #ifdef GSTREAMER_LITE
33 #include <string.h>
34 #endif // GSTREAMER_LITE
35
36 #if !FFI_NO_RAW_API
37
38 size_t
39 ffi_raw_size (ffi_cif *cif)
40 {
41 size_t result = 0;
42 int i;
43
44 ffi_type **at = cif->arg_types;
45
46 for (i = cif->nargs-1; i >= 0; i--, at++)
47 {
48 #if !FFI_NO_STRUCTS
49 if ((*at)->type == FFI_TYPE_STRUCT)
50 result += FFI_ALIGN (sizeof (void*), FFI_SIZEOF_ARG);
51 else
52 #endif
53 result += FFI_ALIGN ((*at)->size, FFI_SIZEOF_ARG);
54 }
55
56 return result;
57 }
58
59
60 void
61 ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args)
62 {
63 unsigned i;
64 ffi_type **tp = cif->arg_types;
65
66 #if WORDS_BIGENDIAN
67
68 for (i = 0; i < cif->nargs; i++, tp++, args++)
69 {
70 switch ((*tp)->type)
71 {
72 case FFI_TYPE_UINT8:
73 case FFI_TYPE_SINT8:
85 *args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 4);
86 break;
87 #endif
88
89 #if !FFI_NO_STRUCTS
90 case FFI_TYPE_STRUCT:
91 *args = (raw++)->ptr;
92 break;
93 #endif
94
95 case FFI_TYPE_COMPLEX:
96 *args = (raw++)->ptr;
97 break;
98
99 case FFI_TYPE_POINTER:
100 *args = (void*) &(raw++)->ptr;
101 break;
102
103 default:
104 *args = raw;
105 raw += FFI_ALIGN ((*tp)->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
106 }
107 }
108
109 #else /* WORDS_BIGENDIAN */
110
111 #if !PDP
112
113 /* then assume little endian */
114 for (i = 0; i < cif->nargs; i++, tp++, args++)
115 {
116 #if !FFI_NO_STRUCTS
117 if ((*tp)->type == FFI_TYPE_STRUCT)
118 {
119 *args = (raw++)->ptr;
120 }
121 else
122 #endif
123 if ((*tp)->type == FFI_TYPE_COMPLEX)
124 {
125 *args = (raw++)->ptr;
126 }
127 else
128 {
129 *args = (void*) raw;
130 raw += FFI_ALIGN ((*tp)->size, sizeof (void*)) / sizeof (void*);
131 }
132 }
133
134 #else
135 #error "pdp endian not supported"
136 #endif /* ! PDP */
137
138 #endif /* WORDS_BIGENDIAN */
139 }
140
141 void
142 ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw)
143 {
144 unsigned i;
145 ffi_type **tp = cif->arg_types;
146
147 for (i = 0; i < cif->nargs; i++, tp++, args++)
148 {
149 switch ((*tp)->type)
150 {
173 (raw++)->sint = *(SINT32*) (*args);
174 break;
175 #endif
176
177 #if !FFI_NO_STRUCTS
178 case FFI_TYPE_STRUCT:
179 (raw++)->ptr = *args;
180 break;
181 #endif
182
183 case FFI_TYPE_COMPLEX:
184 (raw++)->ptr = *args;
185 break;
186
187 case FFI_TYPE_POINTER:
188 (raw++)->ptr = **(void***) args;
189 break;
190
191 default:
192 memcpy ((void*) raw->data, (void*)*args, (*tp)->size);
193 raw += FFI_ALIGN ((*tp)->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
194 }
195 }
196 }
197
198 #if !FFI_NATIVE_RAW_API
199
200
201 /* This is a generic definition of ffi_raw_call, to be used if the
202 * native system does not provide a machine-specific implementation.
203 * Having this, allows code to be written for the raw API, without
204 * the need for system-specific code to handle input in that format;
205 * these following couple of functions will handle the translation forth
206 * and back automatically. */
207
208 void ffi_raw_call (ffi_cif *cif, void (*fn)(void), void *rvalue, ffi_raw *raw)
209 {
210 void **avalue = (void**) alloca (cif->nargs * sizeof (void*));
211 ffi_raw_to_ptrarray (cif, raw, avalue);
212 ffi_call (cif, fn, rvalue, avalue);
213 }
|