简体   繁体   中英

How to do an OCaml binding to a C function with an array of void pointers as a parameter

I am currently doing a binding for a C library and I had some trouble doing a binding of a function with an array of void pointers as a parameter.

I wrote a simple example binding to show you how I did it.

Here is the OCaml code:

type void

external function_c : string -> void array -> int -> void ref -> unit = "function_c"

let _ = 
    let int_arguments = [| ((Obj.magic 4) : void); ((Obj.magic 10) : void) |] in
    let int_sum = ref 0 in
    function_c "int" int_arguments (Array.length int_arguments) ((Obj.magic int_sum) : void ref);
    print_int !int_sum;
    print_endline ""

And here is the C code:

#include <stdio.h>
#include <string.h>

#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>

void* function(char* type, void** numbers, int size) {
    int i;
    if(strcmp(type, "float") == 0) {
        /* ... */
    }   
    else if(strcmp(type, "int") == 0) {
        int* result = malloc(sizeof(int));
        int sum = 0;
        for(i = 0 ; i < size ; i++) {
            int* number = (int*) numbers[i];
            sum += *number;
        }   
        *result = sum;
        return (void*) result;
    }   
    return NULL;
}

CAMLprim value function_c(value type, value numbers, value size, value result) {
    CAMLparam4(type, numbers, size, result);

    int i;
    char* c_type = String_val(type);
    int c_size = Int_val(size);
    if(strcmp(c_type, "float") == 0) {
        /* ... */
    }
    else if(strcmp(c_type, "int") == 0) {
        void** c_numbers = malloc(sizeof(void*) * size);
        int* int_numbers = malloc(sizeof(int) * size);
        int* int_result;
        for(i = 0 ; i < c_size ; i++) {
            int_numbers[i] = Int_val(Field(numbers, i));
            c_numbers[i] = &int_numbers[i];
        }
        int_result = function(c_type, c_numbers, c_size);

        Store_field(result, 0, Val_int(*int_result));

        free(int_result);
        free(c_numbers);
        free(int_numbers);
    }

    CAMLreturn(Val_unit);
}

I looks at lot like a hack (because of the use of Obj.magic) and is likely to be much slower because of the malloc and conditions.

Is there a better way to write a binding to ac function with an array of void pointers as a parameter?

The void** parameter of the function I want to bind can have parameters of different types which are only known at runtime.

Thanks.

最终我按照Leo White的建议切换到了Ctypes,并且我不再遇到这个问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM