How to use PointerBuffer for array of String?

Started by devent, September 30, 2021, 12:13:17

Previous topic - Next topic

devent

Hello. I need to use CL12.clCompileProgram and I need to pass additional headers as String[]. How to do this with the PointerBuffer? Is this correct what I have?
I'm not sure if the size is correct because of the null-termination. Or is there a simpler way?

    private static PointerBuffer createBuffer(String[] list) {
        var size = 0;
        for (int i = 0; i < list.length; i++) {
            size += list[i].length();
        }
        var p = PointerBuffer.allocateDirect(size);
        p.rewind();
        for (String s : list) {
            var buffer = ByteBuffer.wrap(s.getBytes());
            p.put(buffer);
        }
        p.flip();
        return p;
    }

devent

I tried like this bellow. But the clCompileProgram still only sees the very first header name. Is this correct to pass a String[] to a char** parameter?

    private static PointerBuffer createBuffer(String[] list) {
        var p = PointerBuffer.allocateDirect(list.length);
        p.rewind();
        for (String s : list) {
            var bytes = s.getBytes();
            var buffer = BufferUtils.createByteBuffer(s.length());
            buffer.rewind();
            buffer.put(bytes);
            buffer.flip();
            p.put(MemoryUtil.memAddress(buffer));
        }
        p.flip();
        return p;
    }

ignaciafelix

Sorry if I'm being annoying but could I please get an answer?

jpavel

It looks like:

1. You're not null-terminating the bytes from s.getBytes()

2. You're not retaining references to the ByteBuffers allocated via BufferUtils.createByteBuffer(s.length()), and so the GC might reclaim them and free the associated native memory.