| 1 | n/a | /* ----------------------------------------------------------------------- |
|---|
| 2 | n/a | raw_api.c - Copyright (c) 1999, 2008 Red Hat, Inc. |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | Author: Kresten Krab Thorup <krab@gnu.org> |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | Permission is hereby granted, free of charge, to any person obtaining |
|---|
| 7 | n/a | a copy of this software and associated documentation files (the |
|---|
| 8 | n/a | ``Software''), to deal in the Software without restriction, including |
|---|
| 9 | n/a | without limitation the rights to use, copy, modify, merge, publish, |
|---|
| 10 | n/a | distribute, sublicense, and/or sell copies of the Software, and to |
|---|
| 11 | n/a | permit persons to whom the Software is furnished to do so, subject to |
|---|
| 12 | n/a | the following conditions: |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | The above copyright notice and this permission notice shall be included |
|---|
| 15 | n/a | in all copies or substantial portions of the Software. |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, |
|---|
| 18 | n/a | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|---|
| 19 | n/a | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 20 | n/a | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|---|
| 21 | n/a | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|---|
| 22 | n/a | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 23 | n/a | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|---|
| 24 | n/a | DEALINGS IN THE SOFTWARE. |
|---|
| 25 | n/a | ----------------------------------------------------------------------- */ |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | /* This file defines generic functions for use with the raw api. */ |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | #include <ffi.h> |
|---|
| 30 | n/a | #include <ffi_common.h> |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | #if !FFI_NO_RAW_API |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | size_t |
|---|
| 35 | n/a | ffi_raw_size (ffi_cif *cif) |
|---|
| 36 | n/a | { |
|---|
| 37 | n/a | size_t result = 0; |
|---|
| 38 | n/a | int i; |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | ffi_type **at = cif->arg_types; |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | for (i = cif->nargs-1; i >= 0; i--, at++) |
|---|
| 43 | n/a | { |
|---|
| 44 | n/a | #if !FFI_NO_STRUCTS |
|---|
| 45 | n/a | if ((*at)->type == FFI_TYPE_STRUCT) |
|---|
| 46 | n/a | result += ALIGN (sizeof (void*), FFI_SIZEOF_ARG); |
|---|
| 47 | n/a | else |
|---|
| 48 | n/a | #endif |
|---|
| 49 | n/a | result += ALIGN ((*at)->size, FFI_SIZEOF_ARG); |
|---|
| 50 | n/a | } |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | return result; |
|---|
| 53 | n/a | } |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | void |
|---|
| 57 | n/a | ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args) |
|---|
| 58 | n/a | { |
|---|
| 59 | n/a | unsigned i; |
|---|
| 60 | n/a | ffi_type **tp = cif->arg_types; |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | #if WORDS_BIGENDIAN |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | for (i = 0; i < cif->nargs; i++, tp++, args++) |
|---|
| 65 | n/a | { |
|---|
| 66 | n/a | switch ((*tp)->type) |
|---|
| 67 | n/a | { |
|---|
| 68 | n/a | case FFI_TYPE_UINT8: |
|---|
| 69 | n/a | case FFI_TYPE_SINT8: |
|---|
| 70 | n/a | *args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 1); |
|---|
| 71 | n/a | break; |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | case FFI_TYPE_UINT16: |
|---|
| 74 | n/a | case FFI_TYPE_SINT16: |
|---|
| 75 | n/a | *args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 2); |
|---|
| 76 | n/a | break; |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | #if FFI_SIZEOF_ARG >= 4 |
|---|
| 79 | n/a | case FFI_TYPE_UINT32: |
|---|
| 80 | n/a | case FFI_TYPE_SINT32: |
|---|
| 81 | n/a | *args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 4); |
|---|
| 82 | n/a | break; |
|---|
| 83 | n/a | #endif |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | #if !FFI_NO_STRUCTS |
|---|
| 86 | n/a | case FFI_TYPE_STRUCT: |
|---|
| 87 | n/a | *args = (raw++)->ptr; |
|---|
| 88 | n/a | break; |
|---|
| 89 | n/a | #endif |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | case FFI_TYPE_POINTER: |
|---|
| 92 | n/a | *args = (void*) &(raw++)->ptr; |
|---|
| 93 | n/a | break; |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | default: |
|---|
| 96 | n/a | *args = raw; |
|---|
| 97 | n/a | raw += ALIGN ((*tp)->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG; |
|---|
| 98 | n/a | } |
|---|
| 99 | n/a | } |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | #else /* WORDS_BIGENDIAN */ |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | #if !PDP |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | /* then assume little endian */ |
|---|
| 106 | n/a | for (i = 0; i < cif->nargs; i++, tp++, args++) |
|---|
| 107 | n/a | { |
|---|
| 108 | n/a | #if !FFI_NO_STRUCTS |
|---|
| 109 | n/a | if ((*tp)->type == FFI_TYPE_STRUCT) |
|---|
| 110 | n/a | { |
|---|
| 111 | n/a | *args = (raw++)->ptr; |
|---|
| 112 | n/a | } |
|---|
| 113 | n/a | else |
|---|
| 114 | n/a | #endif |
|---|
| 115 | n/a | { |
|---|
| 116 | n/a | *args = (void*) raw; |
|---|
| 117 | n/a | raw += ALIGN ((*tp)->size, sizeof (void*)) / sizeof (void*); |
|---|
| 118 | n/a | } |
|---|
| 119 | n/a | } |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | #else |
|---|
| 122 | n/a | #error "pdp endian not supported" |
|---|
| 123 | n/a | #endif /* ! PDP */ |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | #endif /* WORDS_BIGENDIAN */ |
|---|
| 126 | n/a | } |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | void |
|---|
| 129 | n/a | ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw) |
|---|
| 130 | n/a | { |
|---|
| 131 | n/a | unsigned i; |
|---|
| 132 | n/a | ffi_type **tp = cif->arg_types; |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | for (i = 0; i < cif->nargs; i++, tp++, args++) |
|---|
| 135 | n/a | { |
|---|
| 136 | n/a | switch ((*tp)->type) |
|---|
| 137 | n/a | { |
|---|
| 138 | n/a | case FFI_TYPE_UINT8: |
|---|
| 139 | n/a | (raw++)->uint = *(UINT8*) (*args); |
|---|
| 140 | n/a | break; |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | case FFI_TYPE_SINT8: |
|---|
| 143 | n/a | (raw++)->sint = *(SINT8*) (*args); |
|---|
| 144 | n/a | break; |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | case FFI_TYPE_UINT16: |
|---|
| 147 | n/a | (raw++)->uint = *(UINT16*) (*args); |
|---|
| 148 | n/a | break; |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | case FFI_TYPE_SINT16: |
|---|
| 151 | n/a | (raw++)->sint = *(SINT16*) (*args); |
|---|
| 152 | n/a | break; |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | #if FFI_SIZEOF_ARG >= 4 |
|---|
| 155 | n/a | case FFI_TYPE_UINT32: |
|---|
| 156 | n/a | (raw++)->uint = *(UINT32*) (*args); |
|---|
| 157 | n/a | break; |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | case FFI_TYPE_SINT32: |
|---|
| 160 | n/a | (raw++)->sint = *(SINT32*) (*args); |
|---|
| 161 | n/a | break; |
|---|
| 162 | n/a | #endif |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | #if !FFI_NO_STRUCTS |
|---|
| 165 | n/a | case FFI_TYPE_STRUCT: |
|---|
| 166 | n/a | (raw++)->ptr = *args; |
|---|
| 167 | n/a | break; |
|---|
| 168 | n/a | #endif |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | case FFI_TYPE_POINTER: |
|---|
| 171 | n/a | (raw++)->ptr = **(void***) args; |
|---|
| 172 | n/a | break; |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | default: |
|---|
| 175 | n/a | memcpy ((void*) raw->data, (void*)*args, (*tp)->size); |
|---|
| 176 | n/a | raw += ALIGN ((*tp)->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG; |
|---|
| 177 | n/a | } |
|---|
| 178 | n/a | } |
|---|
| 179 | n/a | } |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | #if !FFI_NATIVE_RAW_API |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | /* This is a generic definition of ffi_raw_call, to be used if the |
|---|
| 185 | n/a | * native system does not provide a machine-specific implementation. |
|---|
| 186 | n/a | * Having this, allows code to be written for the raw API, without |
|---|
| 187 | n/a | * the need for system-specific code to handle input in that format; |
|---|
| 188 | n/a | * these following couple of functions will handle the translation forth |
|---|
| 189 | n/a | * and back automatically. */ |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | void ffi_raw_call (ffi_cif *cif, void (*fn)(void), void *rvalue, ffi_raw *raw) |
|---|
| 192 | n/a | { |
|---|
| 193 | n/a | void **avalue = (void**) alloca (cif->nargs * sizeof (void*)); |
|---|
| 194 | n/a | ffi_raw_to_ptrarray (cif, raw, avalue); |
|---|
| 195 | n/a | ffi_call (cif, fn, rvalue, avalue); |
|---|
| 196 | n/a | } |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | #if FFI_CLOSURES /* base system provides closures */ |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | static void |
|---|
| 201 | n/a | ffi_translate_args (ffi_cif *cif, void *rvalue, |
|---|
| 202 | n/a | void **avalue, void *user_data) |
|---|
| 203 | n/a | { |
|---|
| 204 | n/a | ffi_raw *raw = (ffi_raw*)alloca (ffi_raw_size (cif)); |
|---|
| 205 | n/a | ffi_raw_closure *cl = (ffi_raw_closure*)user_data; |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | ffi_ptrarray_to_raw (cif, avalue, raw); |
|---|
| 208 | n/a | (*cl->fun) (cif, rvalue, raw, cl->user_data); |
|---|
| 209 | n/a | } |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | ffi_status |
|---|
| 212 | n/a | ffi_prep_raw_closure_loc (ffi_raw_closure* cl, |
|---|
| 213 | n/a | ffi_cif *cif, |
|---|
| 214 | n/a | void (*fun)(ffi_cif*,void*,ffi_raw*,void*), |
|---|
| 215 | n/a | void *user_data, |
|---|
| 216 | n/a | void *codeloc) |
|---|
| 217 | n/a | { |
|---|
| 218 | n/a | ffi_status status; |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | status = ffi_prep_closure_loc ((ffi_closure*) cl, |
|---|
| 221 | n/a | cif, |
|---|
| 222 | n/a | &ffi_translate_args, |
|---|
| 223 | n/a | codeloc, |
|---|
| 224 | n/a | codeloc); |
|---|
| 225 | n/a | if (status == FFI_OK) |
|---|
| 226 | n/a | { |
|---|
| 227 | n/a | cl->fun = fun; |
|---|
| 228 | n/a | cl->user_data = user_data; |
|---|
| 229 | n/a | } |
|---|
| 230 | n/a | |
|---|
| 231 | n/a | return status; |
|---|
| 232 | n/a | } |
|---|
| 233 | n/a | |
|---|
| 234 | n/a | #endif /* FFI_CLOSURES */ |
|---|
| 235 | n/a | #endif /* !FFI_NATIVE_RAW_API */ |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | #if FFI_CLOSURES |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | /* Again, here is the generic version of ffi_prep_raw_closure, which |
|---|
| 240 | n/a | * will install an intermediate "hub" for translation of arguments from |
|---|
| 241 | n/a | * the pointer-array format, to the raw format */ |
|---|
| 242 | n/a | |
|---|
| 243 | n/a | ffi_status |
|---|
| 244 | n/a | ffi_prep_raw_closure (ffi_raw_closure* cl, |
|---|
| 245 | n/a | ffi_cif *cif, |
|---|
| 246 | n/a | void (*fun)(ffi_cif*,void*,ffi_raw*,void*), |
|---|
| 247 | n/a | void *user_data) |
|---|
| 248 | n/a | { |
|---|
| 249 | n/a | return ffi_prep_raw_closure_loc (cl, cif, fun, user_data, cl); |
|---|
| 250 | n/a | } |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | #endif /* FFI_CLOSURES */ |
|---|
| 253 | n/a | |
|---|
| 254 | n/a | #endif /* !FFI_NO_RAW_API */ |
|---|