Skip to content

Commit 88c238a

Browse files
committed
Helpful comments
1 parent 19fd492 commit 88c238a

File tree

2 files changed

+70
-40
lines changed

2 files changed

+70
-40
lines changed

src/rust/src/decoder/mod.rs

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,41 @@ pub struct Dtvcc<'a> {
4545
impl<'a> Dtvcc<'a> {
4646
/// Create a new dtvcc context
4747
pub fn new<'b>(opts: &'b mut ccx_decoder_dtvcc_settings) -> Self {
48-
let report = unsafe { &mut *opts.report };
48+
// closely follows `dtvcc_init` at `src/lib_ccx/ccx_dtvcc.c:76`
49+
50+
let report = unsafe { opts.report.as_mut().unwrap() };
4951
report.reset_count = 0;
5052

53+
let is_active = false;
54+
let no_rollup = opts.no_rollup;
55+
let active_services_count = opts.active_services_count;
56+
let no_rollup = is_true(opts.no_rollup);
57+
let active_services_count = opts.active_services_count as u8;
58+
let services_active = opts.services_enabled.clone();
59+
60+
// `dtvcc_clear_packet` does the following
61+
let packet_length = 0;
62+
let is_header_parsed = false;
63+
let packet = [0; CCX_DTVCC_MAX_SERVICES]; // unlike C, packet is allocated on the stack
64+
65+
let last_sequence = CCX_DTVCC_NO_LAST_SEQUENCE;
66+
67+
let report_enabled = is_true(opts.print_file_reports);
68+
let timing = unsafe { opts.timing.as_mut() }.unwrap();
69+
70+
// unlike C, here the decoders are allocated on the stack as an array.
5171
let decoders = {
5272
const INIT: Option<dtvcc_service_decoder> = None;
5373
let val = [INIT; CCX_DTVCC_MAX_SERVICES];
5474

5575
for i in 0..CCX_DTVCC_MAX_SERVICES {
56-
if !is_true(opts.services_enabled[i]) {
76+
if is_false(opts.services_enabled[i]) {
5777
continue;
5878
}
5979

6080
let decoder = dtvcc_service_decoder {
81+
// we cannot allocate this on the stack as `dtvcc_service_decoder` is a C
82+
// struct cannot be changed trivially
6183
tv: Box::into_raw(Box::new(dtvcc_tv_screen {
6284
cc_count: 0,
6385
service_number: i as i32 + 1,
@@ -78,28 +100,22 @@ impl<'a> Dtvcc<'a> {
78100
val
79101
};
80102

81-
for i in 0..CCX_DTVCC_MAX_SERVICES {
82-
if !is_true(opts.services_enabled[i]) {
83-
continue;
84-
}
85-
}
86-
87-
// dtvcc_clear_packet sets current_packet_length, is_current_packet_header_parsed, current_packet to 0
103+
let encoder = None; // Unlike C, does not mention `encoder` and is initialised to `null` by default
88104

89105
Self {
90106
report,
107+
is_active,
108+
no_rollup,
109+
active_services_count,
110+
services_active,
111+
packet_length,
112+
is_header_parsed,
113+
packet,
114+
last_sequence,
115+
report_enabled,
116+
timing,
91117
decoders,
92-
is_active: false,
93-
no_rollup: is_true(opts.no_rollup),
94-
active_services_count: opts.active_services_count as u8,
95-
services_active: opts.services_enabled.clone(),
96-
packet_length: 0,
97-
is_header_parsed: false,
98-
packet: [0; CCX_DTVCC_MAX_SERVICES],
99-
last_sequence: CCX_DTVCC_NO_LAST_SEQUENCE,
100-
report_enabled: is_true(opts.print_file_reports),
101-
timing: unsafe { &mut *opts.timing },
102-
encoder: None,
118+
encoder,
103119
}
104120
}
105121

@@ -238,30 +254,28 @@ impl<'a> Dtvcc<'a> {
238254

239255
impl<'a> Drop for Dtvcc<'a> {
240256
fn drop(&mut self) {
257+
// closely follows `dtvcc_free` at `src/lib_ccx/ccx_dtvcc.c:126`
241258
for i in 0..CCX_DTVCC_MAX_SERVICES {
242-
match self.decoders[i] {
243-
Some(decoder) => {
244-
if !is_true(self.services_active[i]) {
245-
continue;
246-
}
247-
248-
decoder.windows.iter().for_each(|window| {
249-
if is_false(window.memory_reserved) {
250-
return;
251-
}
259+
if let Some(decoder) = self.decoders[i] {
260+
if !is_true(self.services_active[i]) {
261+
continue;
262+
}
252263

253-
window.rows.iter().for_each(|symbol_ptr| unsafe {
254-
symbol_ptr.drop_in_place();
255-
});
264+
decoder.windows.iter().for_each(|window| {
265+
if is_false(window.memory_reserved) {
266+
return;
267+
}
256268

257-
window.memory_reserved = 0;
269+
window.rows.iter().for_each(|symbol_ptr| unsafe {
270+
symbol_ptr.drop_in_place();
258271
});
259272

260-
unsafe {
261-
decoder.tv.drop_in_place();
262-
}
273+
window.memory_reserved = 0;
274+
});
275+
276+
unsafe {
277+
decoder.tv.drop_in_place();
263278
}
264-
None => {}
265279
}
266280
}
267281
}

src/rust/src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,30 @@ pub extern "C" fn ccxr_init_logger() {
4444
.init();
4545
}
4646

47+
/// Create a `dtvcc_rust`
48+
///
49+
/// SAFETY:
50+
/// The following should not be `NULL`:
51+
/// - opts
52+
/// - opts.report
53+
/// - opts.timing
4754
#[no_mangle]
4855
extern "C" fn ccxr_dtvcc_init<'a>(opts: *mut ccx_decoder_dtvcc_settings) -> *mut Dtvcc<'a> {
4956
Box::into_raw(Box::new(Dtvcc::new(unsafe { opts.as_mut() }.unwrap())))
5057
}
5158

59+
/// Frees `dtvcc_rust`
60+
///
61+
/// SAFETY:
62+
/// The following should not be `NULL`:
63+
/// - dtvcc_rust
64+
/// - dtvcc_rust.decoders[i] if dtvcc_rust.services_active[i] is true
65+
/// - dtvcc_rust.decoders[i].windows[j].rows[k] if
66+
/// dtvcc_rust.decoders[i].windows[j].memory_reserved is true
67+
/// - dtvcc_rust.decoders[i].tv
5268
#[no_mangle]
53-
extern "C" fn ccxr_dtvcc_free(dtvcc: *mut Dtvcc) {
54-
unsafe { dtvcc.drop_in_place() };
69+
extern "C" fn ccxr_dtvcc_free(dtvcc_rust: *mut Dtvcc) {
70+
unsafe { dtvcc_rust.drop_in_place() };
5571
}
5672

5773
/// Process cc_data

0 commit comments

Comments
 (0)