| 1 | /* -----------------------------------------------------------------------
|
|---|
| 2 | raw_api.c - Copyright (c) 1999 Cygnus Solutions
|
|---|
| 3 |
|
|---|
| 4 | Author: Kresten Krab Thorup <[email protected]>
|
|---|
| 5 |
|
|---|
| 6 | $Id $
|
|---|
| 7 |
|
|---|
| 8 | Permission is hereby granted, free of charge, to any person obtaining
|
|---|
| 9 | a copy of this software and associated documentation files (the
|
|---|
| 10 | ``Software''), to deal in the Software without restriction, including
|
|---|
| 11 | without limitation the rights to use, copy, modify, merge, publish,
|
|---|
| 12 | distribute, sublicense, and/or sell copies of the Software, and to
|
|---|
| 13 | permit persons to whom the Software is furnished to do so, subject to
|
|---|
| 14 | the following conditions:
|
|---|
| 15 |
|
|---|
| 16 | The above copyright notice and this permission notice shall be included
|
|---|
| 17 | in all copies or substantial portions of the Software.
|
|---|
| 18 |
|
|---|
| 19 | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|---|
| 20 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|---|
| 21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|---|
| 22 | IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|---|
| 23 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|---|
| 24 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|---|
| 25 | OTHER DEALINGS IN THE SOFTWARE.
|
|---|
| 26 | ----------------------------------------------------------------------- */
|
|---|
| 27 |
|
|---|
| 28 | /* This file defines generic functions for use with the raw api. */
|
|---|
| 29 |
|
|---|
| 30 | #include <ffi.h>
|
|---|
| 31 | #include <ffi_common.h>
|
|---|
| 32 |
|
|---|
| 33 | #if !FFI_NO_RAW_API
|
|---|
| 34 |
|
|---|
| 35 | size_t
|
|---|
| 36 | ffi_raw_size (ffi_cif *cif)
|
|---|
| 37 | {
|
|---|
| 38 | size_t result = 0;
|
|---|
| 39 | int i;
|
|---|
| 40 |
|
|---|
| 41 | ffi_type **at = cif->arg_types;
|
|---|
| 42 |
|
|---|
| 43 | for (i = cif->nargs-1; i >= 0; i--, at++)
|
|---|
| 44 | {
|
|---|
| 45 | #if !FFI_NO_STRUCTS
|
|---|
| 46 | if ((*at)->type == FFI_TYPE_STRUCT)
|
|---|
| 47 | result += ALIGN (sizeof (void*), SIZEOF_ARG);
|
|---|
| 48 | else
|
|---|
| 49 | #endif
|
|---|
| 50 | result += ALIGN ((*at)->size, SIZEOF_ARG);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | return result;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | void
|
|---|
| 58 | ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args)
|
|---|
| 59 | {
|
|---|
| 60 | unsigned i;
|
|---|
| 61 | ffi_type **tp = cif->arg_types;
|
|---|
| 62 |
|
|---|
| 63 | #if WORDS_BIGENDIAN
|
|---|
| 64 |
|
|---|
| 65 | for (i = 0; i < cif->nargs; i++, tp++, args++)
|
|---|
| 66 | {
|
|---|
| 67 | switch ((*tp)->type)
|
|---|
| 68 | {
|
|---|
| 69 | case FFI_TYPE_UINT8:
|
|---|
| 70 | case FFI_TYPE_SINT8:
|
|---|
| 71 | *args = (void*) ((char*)(raw++) + SIZEOF_ARG - 1);
|
|---|
| 72 | break;
|
|---|
| 73 |
|
|---|
| 74 | case FFI_TYPE_UINT16:
|
|---|
| 75 | case FFI_TYPE_SINT16:
|
|---|
| 76 | *args = (void*) ((char*)(raw++) + SIZEOF_ARG - 2);
|
|---|
| 77 | break;
|
|---|
| 78 |
|
|---|
| 79 | #if SIZEOF_ARG >= 4
|
|---|
| 80 | case FFI_TYPE_UINT32:
|
|---|
| 81 | case FFI_TYPE_SINT32:
|
|---|
| 82 | *args = (void*) ((char*)(raw++) + SIZEOF_ARG - 4);
|
|---|
| 83 | break;
|
|---|
| 84 | #endif
|
|---|
| 85 |
|
|---|
| 86 | #if !FFI_NO_STRUCTS
|
|---|
| 87 | case FFI_TYPE_STRUCT:
|
|---|
| 88 | *args = (raw++)->ptr;
|
|---|
| 89 | break;
|
|---|
| 90 | #endif
|
|---|
| 91 |
|
|---|
| 92 | case FFI_TYPE_POINTER:
|
|---|
| 93 | *args = (void*) &(raw++)->ptr;
|
|---|
| 94 | break;
|
|---|
| 95 |
|
|---|
| 96 | default:
|
|---|
| 97 | *args = raw;
|
|---|
| 98 | raw += ALIGN ((*tp)->size, SIZEOF_ARG) / SIZEOF_ARG;
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | #else /* WORDS_BIGENDIAN */
|
|---|
| 103 |
|
|---|
| 104 | #if !PDP
|
|---|
| 105 |
|
|---|
| 106 | /* then assume little endian */
|
|---|
| 107 | for (i = 0; i < cif->nargs; i++, tp++, args++)
|
|---|
| 108 | {
|
|---|
| 109 | #if !FFI_NO_STRUCTS
|
|---|
| 110 | if ((*tp)->type == FFI_TYPE_STRUCT)
|
|---|
| 111 | {
|
|---|
| 112 | *args = (raw++)->ptr;
|
|---|
| 113 | }
|
|---|
| 114 | else
|
|---|
| 115 | #endif
|
|---|
| 116 | {
|
|---|
| 117 | *args = (void*) raw;
|
|---|
| 118 | raw += ALIGN ((*tp)->size, sizeof (void*)) / sizeof (void*);
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | #else
|
|---|
| 123 | #error "pdp endian not supported"
|
|---|
| 124 | #endif /* ! PDP */
|
|---|
| 125 |
|
|---|
| 126 | #endif /* WORDS_BIGENDIAN */
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | void
|
|---|
| 130 | ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw)
|
|---|
| 131 | {
|
|---|
| 132 | unsigned i;
|
|---|
| 133 | ffi_type **tp = cif->arg_types;
|
|---|
| 134 |
|
|---|
| 135 | for (i = 0; i < cif->nargs; i++, tp++, args++)
|
|---|
| 136 | {
|
|---|
| 137 | switch ((*tp)->type)
|
|---|
| 138 | {
|
|---|
| 139 | case FFI_TYPE_UINT8:
|
|---|
| 140 | (raw++)->uint = *(UINT8*) (*args);
|
|---|
| 141 | break;
|
|---|
| 142 |
|
|---|
| 143 | case FFI_TYPE_SINT8:
|
|---|
| 144 | (raw++)->sint = *(SINT8*) (*args);
|
|---|
| 145 | break;
|
|---|
| 146 |
|
|---|
| 147 | case FFI_TYPE_UINT16:
|
|---|
| 148 | (raw++)->uint = *(UINT16*) (*args);
|
|---|
| 149 | break;
|
|---|
| 150 |
|
|---|
|
|---|