isl_rs/bindings/
fixed_box.rs1use super::{Context, Error, LibISLError, MultiAff, MultiVal, Space};
5use libc::uintptr_t;
6use std::ffi::{CStr, CString};
7use std::os::raw::c_char;
8
9pub struct FixedBox {
11 pub ptr: uintptr_t,
12 pub should_free_on_drop: bool,
13}
14
15extern "C" {
16
17 fn isl_fixed_box_copy(box_: uintptr_t) -> uintptr_t;
18
19 fn isl_fixed_box_dump(box_: uintptr_t) -> ();
20
21 fn isl_fixed_box_free(box_: uintptr_t) -> uintptr_t;
22
23 fn isl_fixed_box_get_ctx(box_: uintptr_t) -> uintptr_t;
24
25 fn isl_fixed_box_get_offset(box_: uintptr_t) -> uintptr_t;
26
27 fn isl_fixed_box_get_size(box_: uintptr_t) -> uintptr_t;
28
29 fn isl_fixed_box_get_space(box_: uintptr_t) -> uintptr_t;
30
31 fn isl_fixed_box_is_valid(box_: uintptr_t) -> i32;
32
33 fn isl_fixed_box_read_from_str(ctx: uintptr_t, str_: *const c_char) -> uintptr_t;
34
35 fn isl_fixed_box_to_str(box_: uintptr_t) -> *const c_char;
36
37}
38
39impl FixedBox {
40 pub fn copy(&self) -> Result<FixedBox, LibISLError> {
42 let box_ = self;
43 let isl_rs_ctx = box_.get_ctx();
44 let box_ = box_.ptr;
45 let isl_rs_result = unsafe { isl_fixed_box_copy(box_) };
46 let isl_rs_result = FixedBox { ptr: isl_rs_result,
47 should_free_on_drop: true };
48 let err = isl_rs_ctx.last_error();
49 if err != Error::None_ {
50 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
51 }
52 Ok(isl_rs_result)
53 }
54
55 pub fn dump(&self) -> Result<(), LibISLError> {
57 let box_ = self;
58 let isl_rs_ctx = box_.get_ctx();
59 let box_ = box_.ptr;
60 let isl_rs_result = unsafe { isl_fixed_box_dump(box_) };
61 let err = isl_rs_ctx.last_error();
62 if err != Error::None_ {
63 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
64 }
65 Ok(isl_rs_result)
66 }
67
68 pub fn free(self) -> Result<FixedBox, LibISLError> {
70 let box_ = self;
71 let isl_rs_ctx = box_.get_ctx();
72 let mut box_ = box_;
73 box_.do_not_free_on_drop();
74 let box_ = box_.ptr;
75 let isl_rs_result = unsafe { isl_fixed_box_free(box_) };
76 let isl_rs_result = FixedBox { ptr: isl_rs_result,
77 should_free_on_drop: true };
78 let err = isl_rs_ctx.last_error();
79 if err != Error::None_ {
80 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
81 }
82 Ok(isl_rs_result)
83 }
84
85 pub fn get_ctx(&self) -> Context {
87 let box_ = self;
88 let box_ = box_.ptr;
89 let isl_rs_result = unsafe { isl_fixed_box_get_ctx(box_) };
90 let isl_rs_result = Context { ptr: isl_rs_result,
91 should_free_on_drop: false };
92 isl_rs_result
93 }
94
95 pub fn get_offset(&self) -> Result<MultiAff, LibISLError> {
97 let box_ = self;
98 let isl_rs_ctx = box_.get_ctx();
99 let box_ = box_.ptr;
100 let isl_rs_result = unsafe { isl_fixed_box_get_offset(box_) };
101 let isl_rs_result = MultiAff { ptr: isl_rs_result,
102 should_free_on_drop: true };
103 let err = isl_rs_ctx.last_error();
104 if err != Error::None_ {
105 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
106 }
107 Ok(isl_rs_result)
108 }
109
110 pub fn get_size(&self) -> Result<MultiVal, LibISLError> {
112 let box_ = self;
113 let isl_rs_ctx = box_.get_ctx();
114 let box_ = box_.ptr;
115 let isl_rs_result = unsafe { isl_fixed_box_get_size(box_) };
116 let isl_rs_result = MultiVal { ptr: isl_rs_result,
117 should_free_on_drop: true };
118 let err = isl_rs_ctx.last_error();
119 if err != Error::None_ {
120 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
121 }
122 Ok(isl_rs_result)
123 }
124
125 pub fn get_space(&self) -> Result<Space, LibISLError> {
127 let box_ = self;
128 let isl_rs_ctx = box_.get_ctx();
129 let box_ = box_.ptr;
130 let isl_rs_result = unsafe { isl_fixed_box_get_space(box_) };
131 let isl_rs_result = Space { ptr: isl_rs_result,
132 should_free_on_drop: true };
133 let err = isl_rs_ctx.last_error();
134 if err != Error::None_ {
135 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
136 }
137 Ok(isl_rs_result)
138 }
139
140 pub fn is_valid(&self) -> Result<bool, LibISLError> {
142 let box_ = self;
143 let isl_rs_ctx = box_.get_ctx();
144 let box_ = box_.ptr;
145 let isl_rs_result = unsafe { isl_fixed_box_is_valid(box_) };
146 let isl_rs_result = match isl_rs_result {
147 0 => false,
148 1 => true,
149 _ => panic!("Got isl_bool = -1"),
150 };
151 let err = isl_rs_ctx.last_error();
152 if err != Error::None_ {
153 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
154 }
155 Ok(isl_rs_result)
156 }
157
158 pub fn read_from_str(ctx: &Context, str_: &str) -> Result<FixedBox, LibISLError> {
160 let isl_rs_ctx = Context { ptr: ctx.ptr,
161 should_free_on_drop: false };
162 let ctx = ctx.ptr;
163 let str_ = CString::new(str_).unwrap();
164 let str_ = str_.as_ptr();
165 let isl_rs_result = unsafe { isl_fixed_box_read_from_str(ctx, str_) };
166 let isl_rs_result = FixedBox { ptr: isl_rs_result,
167 should_free_on_drop: true };
168 let err = isl_rs_ctx.last_error();
169 if err != Error::None_ {
170 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
171 }
172 Ok(isl_rs_result)
173 }
174
175 pub fn to_str(&self) -> Result<&str, LibISLError> {
177 let box_ = self;
178 let isl_rs_ctx = box_.get_ctx();
179 let box_ = box_.ptr;
180 let isl_rs_result = unsafe { isl_fixed_box_to_str(box_) };
181 let isl_rs_result = unsafe { CStr::from_ptr(isl_rs_result) };
182 let isl_rs_result = isl_rs_result.to_str().unwrap();
183 let err = isl_rs_ctx.last_error();
184 if err != Error::None_ {
185 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
186 }
187 Ok(isl_rs_result)
188 }
189
190 pub fn do_not_free_on_drop(&mut self) {
193 self.should_free_on_drop = false;
194 }
195}
196
197impl Drop for FixedBox {
198 fn drop(&mut self) {
199 if self.should_free_on_drop {
200 unsafe {
201 isl_fixed_box_free(self.ptr);
202 }
203 }
204 }
205}