summaryrefslogtreecommitdiffstats
path: root/ffi.c
Commit message (Collapse)AuthorAgeFilesLines
...
* ffi: use chk_calloc for txr_ffi_type.Kaz Kylheku2017-04-281-4/+2
| | | | | | | | | Let's defend against uninitialized struct member bugs over this type which has grown somewhat complicated. * ffi.c (make_ffi_type_struct): Use chk_calloc for tft. (make_ffi_type_array): Likewise, and remove a couple of explicit zero initializations.
* ffi: buf type doesn't need fill function.Kaz Kylheku2017-04-281-11/+2
| | | | | | | | | | | | | The fill function is useless, because the object is already filled directly. It's just performing an exactly overlapping memcpy. * ffi.c (ffi_buf_alloc): Function removed. (ffi_type_compile): We can't use the presence or absence of the fill function as the test whether a type can be passed by ptr-in-out or ptr-out, since buf can be passed that way and has no fill. A better criterion is "has no fill and use the fixed size allocation". Removing the assignment statements which set up the fill function for buffers.
* ffi: properly recurse the handling of pointers.Kaz Kylheku2017-04-271-18/+64
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | The FFI type system needs to handle out and in-out pointers at nested levels. We are only doing the tft->in(...) calls on the top-level parameters so this doesn't happen. What's worse, because the tft->put(...) calls recurse, they prepare buffers which are not being freed because freeing is the responsibility of tft->in(...) calls. Because the type descriptor structures also store run-time state, this change requires us to change how arrays are treated. The array elements cannot share the same type descriptor since each could hold a different buffer. * ffi.c (ffi_struct_in): New static function. (ffi_struct_put): Determine whether any of the members need their in function called. If so, set this struct type's in to point to ffi_struct_in. (ffi_array_in): New static function. (ffi_array_put): Determine whether any of the array elements need their in function called. If so, set this struct type's in to point to ffi_array_in. Treat tft->mtypes as a type descriptor list, rather than a single type. (ffi_array_get, ffi_array_fill, make_ffi_type_array): Treat tft->mtypes as a type descriptor list, rather than a single type. (ffi_type_compile): Compile the type expression of an array as many times as the number of dimensions and gather into a list, then pass that list to make_ffi_type_array.
* ffi: fix problems caught by g++.Kaz Kylheku2017-04-271-7/+4
| | | | | | | | | | | | | | * ffi.c (float_s): Variable removed. This is a duplicate definition; we already have this symbol in lib.c. (ffi_type_s): Duplicate definition removed; it is repeated two lines below. (ffi_str_put): Remove pointless const qualifier on u8s variable. (ffi_call_wrap): Cast return value of alloca. Also, rc pointer needs to be cast to mem_t *. (ffi_init): Remove initialization of float_s. * ffi.h (float_s): Declaration removed.
* ffi: sane in/out protocol; buffers work directly.Kaz Kylheku2017-04-271-6/+46
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fix incorrect memory allocation in (ptr-in buf) and (ptr-in-out buf) passing. Buffer arguments passed to a function as (ptr-in buf) or (ptr-in-out buf) now pass the buffer directly without allocating another copy, as in the case of arrays or structs. * ffi.c (struct txr_ffi_type): New members alloc, free. The pointer types use these functions, together with fill, for the management of the buffering of their target type. (ffi_fixed_alloc, ffi_noop_free, ffi_buf_alloc, ffi_ptr_in_in): New static functions. (ffi_ptr_in_put): Use target type's alloc function, rather than chk_malloc. A struct or array will actually allocate the buffer needed for their C version. A buffer will not; it will just return its internal pointer, just like what the wstr type does with strings. This function now sets up ffi_ptr_in_in as the in handler for this pointer type, rather than ffi_freeing_in, because the freeing has to go through the target type interface, and not directly to free. Buffers use the no-op free function. (ffi_ptr_out_in): Use the target type's free function rathr than free. (ffi_ptr_out_put, ffi_ptr_in_out_put): Use the target type's allocator instead of chk_malloc. (make_ffi_type_pointer, make_ffi_type_struct, make_ffi_type_array): Initialize the alloc and free function pointer members of the txr_ffi_type struct. (ffi_type_compile): Set up alloc and free for buffers.
* ffi: array support.Kaz Kylheku2017-04-261-0/+96
| | | | | | | | | | | * ffi.c (array_s): New symbol variable. (ffi_array_put, ffi_array_get, ffi_array_fill, make_ffi_type_array): New static functions. (ffi_type_compile): Support (array <dim> <type>) syntax. (ffi_init): Initialize array_s. * ffi.h (array_s): Declared.
* ffi: incorrect handling of struct element array.Kaz Kylheku2017-04-261-6/+11
| | | | | | | | | | | | | | | * ffi.c (ffi_type_struct_destroy_op): Fix silly code. The size field most certainly doesn't indicate the number of elements, but rather the byte size. The array is documented by libffi as null-pointer terminated, so let's take advantage of that. (make_ffi_type_struct): Speaking of the array being null-terminated, it is we who are required to ensure this representation and we are not. Let's fix it. Also, we are here wrongly storing the number of elements into the struct type's size field, which is the basis for misusing that in the destroy op. The documentation says that the size field should be initialized to zero.
* ffi: no memcpy for string pointers.Kaz Kylheku2017-04-261-7/+6
| | | | | | ffi.c (ffi_str_put, ffi_str_get, ffi_wstr_put, ffi_wstr_get): Use assignment instead of memcpy to move the string pointer to and from the buffer.
* ffi: support buf objects.Kaz Kylheku2017-04-261-0/+39
| | | | | | | | | | | | | | | | * buf.c (make_duplicate_buf, buf_get, buf_fill): New functions. * buf.h (make_duplicate_buf, buf_get, buf_fill): Declared. * ffi.c (struct txr_ffi_type): New member, nelem. Keeps track of number of elements, for types that are FFI pointers. This lets us support the get method so that a buf can be a C function return value, if its size is declared in our FFI type system. (ffi_buf_put, ffi_buf_get, ffi_buf_fill): New functions. (ffi_type_compile): Handle two new cases of syntax for buffers: (buf <size>) and buf.
* Start of FFI implementation based on libffi.Kaz Kylheku2017-04-241-0/+1133
* Makefile (OBJS): Add ffi.o. * configure (have_libffi): New variable. (gen_config_make): Generate have_libffi make variable. New check for availability of libffi. * ffi.c, ffi.h: New files. * lib.c (init): Call ffi_init.