Skip to content

Commit 17adf2e

Browse files
authored
Period (#12)
* change periodicity to period * fix memory leaks
1 parent ac13c0c commit 17adf2e

File tree

4 files changed

+25
-40
lines changed

4 files changed

+25
-40
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The purpose of this changelog is to document new features and breaking changes t
77
- Add JSON <-> Scheme interface
88
- Separate crypto primitives into separate namespace
99
- Add unix time primitive
10+
- Use explicit period instead of periodicity
1011

1112
--- v1.0.4 (2025.09.14) ---
1213

src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ pub struct Config {
3030
#[arg(
3131
short = 'c',
3232
long,
33-
default_value_t = 0,
34-
help = "Power of two determining the period of the step query"
33+
default_value_t = 1.0,
34+
help = "Number of seconds between each step inquiry"
3535
)]
36-
pub periodicity: i32,
36+
pub period: f64,
3737
}
3838

3939
impl Config {

src/evaluator.rs

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,13 @@ impl Type {
7676

7777
pub fn obj2str(sc: *mut s7_scheme, obj: *mut s7_cell) -> String {
7878
unsafe {
79-
unsafe {
80-
let expr = s7_object_to_c_string(sc, obj);
81-
let cstr = CStr::from_ptr(expr);
82-
let result = match cstr.to_str() {
83-
Ok(expr) => expr.to_owned(),
84-
Err(_) => format!("(error 'encoding-error \"Failed to encode string\")"),
85-
};
86-
free(expr as *mut libc::c_void);
87-
result
88-
}
79+
let expr = s7_string(s7_object_to_string(sc, obj, false));
80+
let cstr = CStr::from_ptr(expr);
81+
let result = match cstr.to_str() {
82+
Ok(expr) => expr.to_owned(),
83+
Err(_) => format!("(error 'encoding-error \"Failed to encode string\")"),
84+
};
85+
result
8986
}
9087
}
9188

@@ -108,24 +105,21 @@ pub fn scheme2json(expression: &str) -> Result<Value, String> {
108105
// - @hash-table: {"*type/hash-table*": [["a", 6], [53, 199]]}
109106

110107
unsafe {
111-
unsafe {
112-
let sc: *mut s7_scheme = s7_init();
108+
let sc: *mut s7_scheme = s7_init();
113109

114-
// Parse the expression without evaluating it
115-
let c_expr = CString::new(expression).unwrap_or_else(|_| CString::new("()").unwrap());
116-
let input_port = s7_open_input_string(sc, c_expr.as_ptr());
117-
let s7_obj = s7_read(sc, input_port);
118-
s7_close_input_port(sc, input_port);
110+
// Parse the expression without evaluating it
111+
let c_expr = CString::new(expression).unwrap_or_else(|_| CString::new("()").unwrap());
112+
let input_port = s7_open_input_string(sc, c_expr.as_ptr());
113+
let s7_obj = s7_read(sc, input_port);
114+
s7_close_input_port(sc, input_port);
119115

120-
let result = s7_obj_to_json(sc, s7_obj);
121-
s7_free(sc);
122-
result
123-
}
116+
let result = s7_obj_to_json(sc, s7_obj);
117+
s7_free(sc);
118+
result
124119
}
125120
}
126121

127122
pub fn json2scheme(expression: Value) -> Result<String, String> {
128-
unsafe {
129123
unsafe {
130124
let sc: *mut s7_scheme = s7_init();
131125
match json_to_s7_obj(sc, &expression) {
@@ -139,7 +133,6 @@ pub fn json2scheme(expression: Value) -> Result<String, String> {
139133
Err(err)
140134
}
141135
}
142-
}
143136
}
144137
}
145138

@@ -162,7 +155,6 @@ impl Evaluator {
162155

163156
primitives_.extend(primitives);
164157

165-
unsafe {
166158
unsafe {
167159
let sc: *mut s7_scheme = s7_init();
168160

@@ -209,7 +201,6 @@ impl Evaluator {
209201
sc,
210202
primitives: primitives_,
211203
}
212-
}
213204
}
214205
}
215206

@@ -232,19 +223,17 @@ impl Evaluator {
232223

233224
impl Drop for Evaluator {
234225
fn drop(&mut self) {
235-
unsafe {
236226
unsafe {
237227
s7_free(self.sc);
238228
}
239-
}
240229
}
241230
}
242231

243232
fn primitive_expression_to_byte_vector() -> Primitive {
244233
unsafe extern "C" fn code(sc: *mut s7_scheme, args: s7_pointer) -> s7_pointer {
245234
let arg = s7_car(args);
246235

247-
let s7_c_str = s7_object_to_c_string(sc, arg);
236+
let s7_c_str = s7_string(s7_object_to_string(sc, arg, false));
248237
let c_string = CStr::from_ptr(s7_c_str);
249238

250239
let bv = s7_make_byte_vector(
@@ -256,7 +245,6 @@ fn primitive_expression_to_byte_vector() -> Primitive {
256245
for (i, b) in c_string.to_bytes().iter().enumerate() {
257246
s7_byte_vector_set(bv, i as i64, *b);
258247
}
259-
free(s7_c_str as *mut libc::c_void);
260248
bv
261249
}
262250

@@ -328,7 +316,7 @@ fn primitive_hex_string_to_byte_vector() -> Primitive {
328316
);
329317
}
330318

331-
let s7_c_str = s7_object_to_c_string(sc, arg);
319+
let s7_c_str = s7_string(s7_object_to_string(sc, arg, false));
332320
let hex_string = CStr::from_ptr(s7_c_str)
333321
.to_str()
334322
.expect("Failed to convert C string to hex string");
@@ -338,8 +326,6 @@ fn primitive_hex_string_to_byte_vector() -> Primitive {
338326
.map(|i| u8::from_str_radix(&hex_string[i..i + 2], 16))
339327
.collect();
340328

341-
free(s7_c_str as *mut libc::c_void);
342-
343329
match result {
344330
Ok(result) => {
345331
let bv =

src/main.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,19 @@ async fn main() {
117117
rocket_config.address = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
118118
rocket_config.limits = Limits::new().limit("string", 1_i32.mebibytes());
119119

120-
let period = 2_f64.powi(config.periodicity);
121-
122120
if config.step != "" {
123121
tokio::spawn(async move {
124122
let mut step = 0;
125123
let start = ((SystemTime::now()
126124
.duration_since(UNIX_EPOCH)
127125
.expect("Failed to get system time")
128126
.as_micros() as f64
129-
/ (period * MICRO))
127+
/ (config.period * MICRO))
130128
.ceil()
131-
* (period * MICRO)) as u128;
129+
* (config.period * MICRO)) as u128;
132130

133131
loop {
134-
let until = start + step * (period * MICRO) as u128;
132+
let until = start + step * (config.period * MICRO) as u128;
135133
let now = SystemTime::now()
136134
.duration_since(UNIX_EPOCH)
137135
.expect("Failed to get system time")

0 commit comments

Comments
 (0)