Passing array of pointers cfunc

Hello,
I have a function which as an argument takes:

  • pointer to a list of pointers
  • types of this pointers
  • number of pointers

I would like to do sth like:

@cfunc(types.void(types.CPointer(types.int64), types.CPointer(types.int64), types.int64), nopython=True)
def foo(l, types_ptr, num):
    l_arr = carray(l, num)
    types = carray(types_ptr, num)
    for ptr, type in zip(l_arr, types):
        x = carray(ptr, SIZE, dtype=type) # can't be done because ptr is actually an int.

I would like to cast this int64 ptr to c_void_p or sth like that.

Hi @majra20

The “cast int to pointer” issue can be resolved with a function like:

from numba.extending import intrinsic
from llvmlite import ir
voidptr_t = ir.IntType(8).as_pointer()
@intrinsic
def asvoidp(tyctx, thing):
    sig = types.voidptr(thing)
    def codegen(cgctx, builder, sig, args):
        return builder.inttoptr(args[0], voidptr_t)
    return sig, codegen

and call it like

true_ptr = asvoidp(some_int)

Even with this added I don’t think the above will be something that can be compiled as the type of x cannot be known at compile time from the information currently provided.

Hope this helps?

1 Like