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 let err_msg = isl_rs_ctx.last_error_msg();
51 isl_rs_ctx.reset_error();
52 return Err(LibISLError::new(err, err_msg));
53 }
54 Ok(isl_rs_result)
55 }
56
57 pub fn dump(&self) -> Result<(), LibISLError> {
59 let box_ = self;
60 let isl_rs_ctx = box_.get_ctx();
61 let box_ = box_.ptr;
62 let isl_rs_result = unsafe { isl_fixed_box_dump(box_) };
63 let err = isl_rs_ctx.last_error();
64 if err != Error::None_ {
65 let err_msg = isl_rs_ctx.last_error_msg();
66 isl_rs_ctx.reset_error();
67 return Err(LibISLError::new(err, err_msg));
68 }
69 Ok(isl_rs_result)
70 }
71
72 pub fn free(self) -> Result<FixedBox, LibISLError> {
74 let box_ = self;
75 let isl_rs_ctx = box_.get_ctx();
76 let mut box_ = box_;
77 box_.do_not_free_on_drop();
78 let box_ = box_.ptr;
79 let isl_rs_result = unsafe { isl_fixed_box_free(box_) };
80 let isl_rs_result = FixedBox { ptr: isl_rs_result,
81 should_free_on_drop: true };
82 let err = isl_rs_ctx.last_error();
83 if err != Error::None_ {
84 let err_msg = isl_rs_ctx.last_error_msg();
85 isl_rs_ctx.reset_error();
86 return Err(LibISLError::new(err, err_msg));
87 }
88 Ok(isl_rs_result)
89 }
90
91 pub fn get_ctx(&self) -> Context {
93 let box_ = self;
94 let box_ = box_.ptr;
95 let isl_rs_result = unsafe { isl_fixed_box_get_ctx(box_) };
96 let isl_rs_result = Context { ptr: isl_rs_result,
97 should_free_on_drop: false };
98 isl_rs_result
99 }
100
101 pub fn get_offset(&self) -> Result<MultiAff, LibISLError> {
103 let box_ = self;
104 let isl_rs_ctx = box_.get_ctx();
105 let box_ = box_.ptr;
106 let isl_rs_result = unsafe { isl_fixed_box_get_offset(box_) };
107 let isl_rs_result = MultiAff { ptr: isl_rs_result,
108 should_free_on_drop: true };
109 let err = isl_rs_ctx.last_error();
110 if err != Error::None_ {
111 let err_msg = isl_rs_ctx.last_error_msg();
112 isl_rs_ctx.reset_error();
113 return Err(LibISLError::new(err, err_msg));
114 }
115 Ok(isl_rs_result)
116 }
117
118 pub fn get_size(&self) -> Result<MultiVal, LibISLError> {
120 let box_ = self;
121 let isl_rs_ctx = box_.get_ctx();
122 let box_ = box_.ptr;
123 let isl_rs_result = unsafe { isl_fixed_box_get_size(box_) };
124 let isl_rs_result = MultiVal { ptr: isl_rs_result,
125 should_free_on_drop: true };
126 let err = isl_rs_ctx.last_error();
127 if err != Error::None_ {
128 let err_msg = isl_rs_ctx.last_error_msg();
129 isl_rs_ctx.reset_error();
130 return Err(LibISLError::new(err, err_msg));
131 }
132 Ok(isl_rs_result)
133 }
134
135 pub fn get_space(&self) -> Result<Space, LibISLError> {
137 let box_ = self;
138 let isl_rs_ctx = box_.get_ctx();
139 let box_ = box_.ptr;
140 let isl_rs_result = unsafe { isl_fixed_box_get_space(box_) };
141 let isl_rs_result = Space { ptr: isl_rs_result,
142 should_free_on_drop: true };
143 let err = isl_rs_ctx.last_error();
144 if err != Error::None_ {
145 let err_msg = isl_rs_ctx.last_error_msg();
146 isl_rs_ctx.reset_error();
147 return Err(LibISLError::new(err, err_msg));
148 }
149 Ok(isl_rs_result)
150 }
151
152 pub fn is_valid(&self) -> Result<bool, LibISLError> {
154 let box_ = self;
155 let isl_rs_ctx = box_.get_ctx();
156 let box_ = box_.ptr;
157 let isl_rs_result = unsafe { isl_fixed_box_is_valid(box_) };
158 let isl_rs_result = match isl_rs_result {
159 0 => false,
160 1 => true,
161 _ => {
162 return Err(LibISLError::new(Error::Unknown, "Got isl_bool = -1"));
163 }
164 };
165 let err = isl_rs_ctx.last_error();
166 if err != Error::None_ {
167 let err_msg = isl_rs_ctx.last_error_msg();
168 isl_rs_ctx.reset_error();
169 return Err(LibISLError::new(err, err_msg));
170 }
171 Ok(isl_rs_result)
172 }
173
174 pub fn read_from_str(ctx: &Context, str_: &str) -> Result<FixedBox, LibISLError> {
176 let isl_rs_ctx = Context { ptr: ctx.ptr,
177 should_free_on_drop: false };
178 let ctx = ctx.ptr;
179 let str_ = CString::new(str_).unwrap();
180 let str_ = str_.as_ptr();
181 let isl_rs_result = unsafe { isl_fixed_box_read_from_str(ctx, str_) };
182 let isl_rs_result = FixedBox { ptr: isl_rs_result,
183 should_free_on_drop: true };
184 let err = isl_rs_ctx.last_error();
185 if err != Error::None_ {
186 let err_msg = isl_rs_ctx.last_error_msg();
187 isl_rs_ctx.reset_error();
188 return Err(LibISLError::new(err, err_msg));
189 }
190 Ok(isl_rs_result)
191 }
192
193 pub fn to_str(&self) -> Result<&str, LibISLError> {
195 let box_ = self;
196 let isl_rs_ctx = box_.get_ctx();
197 let box_ = box_.ptr;
198 let isl_rs_result = unsafe { isl_fixed_box_to_str(box_) };
199 let isl_rs_result = unsafe { CStr::from_ptr(isl_rs_result) };
200 let isl_rs_result = isl_rs_result.to_str().unwrap();
201 let err = isl_rs_ctx.last_error();
202 if err != Error::None_ {
203 let err_msg = isl_rs_ctx.last_error_msg();
204 isl_rs_ctx.reset_error();
205 return Err(LibISLError::new(err, err_msg));
206 }
207 Ok(isl_rs_result)
208 }
209
210 pub fn do_not_free_on_drop(&mut self) {
213 self.should_free_on_drop = false;
214 }
215}
216
217impl Drop for FixedBox {
218 fn drop(&mut self) {
219 if self.should_free_on_drop {
220 unsafe {
221 isl_fixed_box_free(self.ptr);
222 }
223 }
224 }
225}