#Dynamic string allocation in C callback

1 messages · Page 1 of 1 (latest)

latent wharf
#

I was wondering what the best way way to handle dynamic string allocation is. I'm doing the below

char_callback :: proc "c" (window: glfw.WindowHandle, code: rune) {
        term: ^Terminal = glfw.GetWindowUserPointer(window)
        txt := term.lines[term.start].txt
        term.lines[term.start].txt = strings.concatenate({txt[:term.cursor], code, txt[term.cursor:]})
        // term.view_height = 0;
        term.window = term.start
        term.cursor += 1
}

But this doesn't work since there is no context allocator in a C calling convention. This is super easy to do in C++. I'm wondering why there is no stack allocated concatenation in Odin. (or maybe there is an I'm missing the point?)

raw hound
#

What do you mean by "stack allocated concatenation".
For the context, you must decide what context you want in this function.
You can then assign that to context and thereafter use contextful procs.
One can get the same context as main gets via runtime.default_context().
However you could just set the glfw user pointer to a struct that contains a runtime.Context value and your ^Terminal.

#

Doing that would allow you to get it from there