1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::{
    alloc,
    ops::{Deref, DerefMut},
    ptr,
};

use crate::ibase;

pub struct XSqlDa {
    ptr: ptr::NonNull<ibase::XSQLDA>,
    len: i16,
}

unsafe impl Send for XSqlDa {}

impl XSqlDa {
    /// Allocates a new XSQLDA of length `len`
    pub fn new(len: i16) -> Self {
        #[allow(clippy::cast_ptr_alignment)]
        let ptr = unsafe { alloc::alloc_zeroed(xsqlda_layout(len)) } as *mut ibase::XSQLDA;

        let ptr = if let Some(ptr) = ptr::NonNull::new(ptr) {
            ptr
        } else {
            alloc::handle_alloc_error(xsqlda_layout(len))
        };

        let mut xsqlda = Self { ptr, len };

        xsqlda.version = ibase::SQLDA_VERSION1 as i16;
        xsqlda.sqln = len;

        xsqlda
    }

    /// Returns a mutable reference to a XSQLVAR
    pub fn get_xsqlvar_mut(&mut self, col: usize) -> Option<&mut ibase::XSQLVAR> {
        if col < self.len as usize {
            let xsqlvar = unsafe { self.ptr.as_mut().sqlvar.get_unchecked_mut(col as usize) };

            Some(xsqlvar)
        } else {
            None
        }
    }
}

impl Deref for XSqlDa {
    type Target = ibase::XSQLDA;

    fn deref(&self) -> &Self::Target {
        unsafe { self.ptr.as_ref() }
    }
}

impl DerefMut for XSqlDa {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { self.ptr.as_mut() }
    }
}

impl Drop for XSqlDa {
    fn drop(&mut self) {
        unsafe { alloc::dealloc(self.ptr.as_ptr() as *mut u8, xsqlda_layout(self.len)) }
    }
}

/// Calculates the memory layout (size and alignment) for a xsqlda
fn xsqlda_layout(len: i16) -> alloc::Layout {
    let (xsqlda_layout, _) = alloc::Layout::new::<ibase::XSQLDA>()
        .extend(alloc::Layout::array::<ibase::XSQLVAR>((len - 1).max(0) as usize).unwrap())
        .unwrap();

    xsqlda_layout
}