Skip to content

System variable strings #3

@tgross35

Description

@tgross35

To my best understanding, it looks like the SQL side only looks for a pointer at the address of this struct. So, I think we could do something like this:

use std::sync::{atomic::AtomicPtr, Mutex};
use std::ffi::{CString, c_char};
use std::marker::PhantomPinned;

#[repr(C)]
struct SysVarString {
    ptr: AtomicPtr<c_char>,
    cs: Mutex<Option<CString>>,
    pin: PhantomPinned
}

impl SysVarString {
    fn update(&self, save: *const c_char) {
        // lock mutex
        if ptr.is_none() { self.cs = None} 
        else { self.cs = CString::new(CStr::from_ptr(save).expect("non UTF-8")).unwrap() }
        self.ptr = cs.as_ref().map_or(std::ptr::null(), |cs| cs.as_ptr());
    }
}

A safety caveat is that we need to be sure SQL doesn't write to this buffer. I think this would actually be completely thread safe as well (well, the C side wouldn't atomically read the pointer, but I'm not sure what we could do there)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions