1
/* -----------------------------------------------------------------------
2
raw_api.c - Copyright (c) 1999, 2008 Red Hat, Inc.
4
Author: Kresten Krab Thorup <krab@gnu.org>
6
Permission is hereby granted, free of charge, to any person obtaining
7
a copy of this software and associated documentation files (the
8
``Software''), to deal in the Software without restriction, including
9
without limitation the rights to use, copy, modify, merge, publish,
10
distribute, sublicense, and/or sell copies of the Software, and to
11
permit persons to whom the Software is furnished to do so, subject to
12
the following conditions:
14
The above copyright notice and this permission notice shall be included
15
in all copies or substantial portions of the Software.
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
----------------------------------------------------------------------- */
27
/* This file defines generic functions for use with the raw api. */
30
#include <ffi_common.h>
35
ffi_raw_size (ffi_cif *cif)
40
ffi_type **at = cif->arg_types;
42
for (i = cif->nargs-1; i >= 0; i--, at++)
45
if ((*at)->type == FFI_TYPE_STRUCT)
46
result += ALIGN (sizeof (void*), FFI_SIZEOF_ARG);
49
result += ALIGN ((*at)->size, FFI_SIZEOF_ARG);
57
ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args)
60
ffi_type **tp = cif->arg_types;
64
for (i = 0; i < cif->nargs; i++, tp++, args++)
70
*args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 1);
75
*args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 2);
78
#if FFI_SIZEOF_ARG >= 4
81
*args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 4);
91
case FFI_TYPE_POINTER:
92
*args = (void*) &(raw++)->ptr;
97
raw += ALIGN ((*tp)->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
101
#else /* WORDS_BIGENDIAN */
105
/* then assume little endian */
106
for (i = 0; i < cif->nargs; i++, tp++, args++)
109
if ((*tp)->type == FFI_TYPE_STRUCT)
111
*args = (raw++)->ptr;
117
raw += ALIGN ((*tp)->size, sizeof (void*)) / sizeof (void*);
122
#error "pdp endian not supported"
125
#endif /* WORDS_BIGENDIAN */
129
ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw)
132
ffi_type **tp = cif->arg_types;
134
for (i = 0; i < cif->nargs; i++, tp++, args++)
139
(raw++)->uint = *(UINT8*) (*args);
143
(raw++)->sint = *(SINT8*) (*args);
146
case FFI_TYPE_UINT16:
147
(raw++)->uint = *(UINT16*) (*args);
150
case FFI_TYPE_SINT16:
151
(raw++)->sint = *(SINT16*) (*args);
154
#if FFI_SIZEOF_ARG >= 4
155
case FFI_TYPE_UINT32:
156
(raw++)->uint = *(UINT32*) (*args);
159
case FFI_TYPE_SINT32:
160
(raw++)->sint = *(SINT32*) (*args);
165
case FFI_TYPE_STRUCT:
166
(raw++)->ptr = *args;
170
case FFI_TYPE_POINTER:
171
(raw++)->ptr = **(void***) args;
175
memcpy ((void*) raw->data, (void*)*args, (*tp)->size);
176
raw += ALIGN ((*tp)->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
181
#if !FFI_NATIVE_RAW_API
184
/* This is a generic definition of ffi_raw_call, to be used if the
185
* native system does not provide a machine-specific implementation.
186
* Having this, allows code to be written for the raw API, without
187
* the need for system-specific code to handle input in that format;
188
* these following couple of functions will handle the translation forth
189
* and back automatically. */
191
void ffi_raw_call (ffi_cif *cif, void (*fn)(void), void *rvalue, ffi_raw *raw)
193
void **avalue = (void**) alloca (cif->nargs * sizeof (void*));
194
ffi_raw_to_ptrarray (cif, raw, avalue);
195
ffi_call (cif, fn, rvalue, avalue);
198
#if FFI_CLOSURES /* base system provides closures */
201
ffi_translate_args (ffi_cif *cif, void *rvalue,
202
void **avalue, void *user_data)
204
ffi_raw *raw = (ffi_raw*)alloca (ffi_raw_size (cif));
205
ffi_raw_closure *cl = (ffi_raw_closure*)user_data;
207
ffi_ptrarray_to_raw (cif, avalue, raw);
208
(*cl->fun) (cif, rvalue, raw, cl->user_data);
212
ffi_prep_raw_closure_loc (ffi_raw_closure* cl,
214
void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
220
status = ffi_prep_closure_loc ((ffi_closure*) cl,
225
if (status == FFI_OK)
228
cl->user_data = user_data;
234
#endif /* FFI_CLOSURES */
235
#endif /* !FFI_NATIVE_RAW_API */
239
/* Again, here is the generic version of ffi_prep_raw_closure, which
240
* will install an intermediate "hub" for translation of arguments from
241
* the pointer-array format, to the raw format */
244
ffi_prep_raw_closure (ffi_raw_closure* cl,
246
void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
249
return ffi_prep_raw_closure_loc (cl, cif, fun, user_data, cl);
252
#endif /* FFI_CLOSURES */
254
#endif /* !FFI_NO_RAW_API */