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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Partial port of `AffinityPanel.c` — htop's "Use CPUs:" affinity picker.
//!
//! C names are preserved verbatim (htop uses `CamelCase_snake`), so
//! `non_snake_case` is allowed for the whole module — matching the spec
//! name-for-name is the point of the port.
//!
//! # HAVE_LIBHWLOC
//!
//! `AffinityPanel.c` is written for two builds: the `HAVE_LIBHWLOC` build
//! (a topology tree backed by `hwloc_bitmap_t` cpusets) and the plain
//! build (a flat CPU list, `int cpu` per item). htoprs links no `hwloc`,
//! so this port follows the `#else` (non-hwloc) branch throughout: the
//! [`MaskItem`] struct carries the `int cpu` field, and every function
//! whose body lives entirely inside `#ifdef HAVE_LIBHWLOC` has no
//! non-hwloc counterpart and stays an honest stub.
//!
//! # Ported (faithful, non-hwloc branch)
//!
//! - [`MaskItem_newSingleton`] (`AffinityPanel.c:108`) — the flat-CPU
//! constructor: `text`, `indent = NULL`, `sub_tree = 0`, empty
//! `children`, `cpu = cpu`, `value = isSet ? 2 : 0`. The `#ifdef
//! HAVE_LIBHWLOC` arm (allocate + set a one-bit cpuset) is compiled out;
//! the `#else` arm (`this->cpu = cpu`) is what ports. Returns an owned
//! [`MaskItem`] (the `History_new`/`Affinity_new` idiom for a C fn that
//! heap-allocates and returns a pointer).
//! - [`MaskItem_display`] (`AffinityPanel.c:62`) — the always-run checkbox
//! glyph (`[x]`/`[o]`/`[ ]` in `CHECK_BOX`/`CHECK_MARK`) plus the trailing
//! `text` (in `CHECK_TEXT`), through the real [`RichString`]/
//! [`ColorElements`] substrate. The `if (this->indent)` tree-node branch
//! needs `CRT_treeStr[TREE_STR_OPEN/SHUT]`, which is not ported in
//! `crt.rs`; since only the hwloc-only [`MaskItem_newMask`] ever sets a
//! non-NULL `indent`, that branch is unreachable in this build and stays
//! a `todo!()` (the `ListItem_display` moving-glyph precedent).
//! - [`AffinityPanel_getAffinity`] (`AffinityPanel.c:444`) — the non-hwloc
//! branch: `Affinity_new(host)`, then for each `cpuids` item whose
//! `value` is set, `Affinity_add(affinity, item->cpu)`. Both
//! `Affinity_new`/`Affinity_add` are ported. Takes `this: &AffinityPanel`
//! (the C casts its `Panel* super` to `AffinityPanel*`).
//!
//! # Stubbed (cannot be ported faithfully yet)
//!
//! hwloc-only (no non-hwloc body exists — the whole function is inside
//! `#ifdef HAVE_LIBHWLOC`, and htoprs links no `hwloc`):
//! - [`MaskItem_newMask`] (`AffinityPanel.c:94`) — takes an
//! `hwloc_bitmap_t cpuset` and weighs it with `hwloc_bitmap_weight`.
//! - [`AffinityPanel_updateItem`] (`AffinityPanel.c:156`) — sets `value`
//! from `hwloc_bitmap_isincluded`/`intersects` against `workCpuset`.
//! - [`AffinityPanel_updateTopo`] (`AffinityPanel.c:165`) — recurses the
//! topology tree built from hwloc objects.
//! - [`AffinityPanel_addObject`] (`AffinityPanel.c:283`) — reads
//! `hwloc_obj_t` fields (`depth`, `logical_index`, `complete_cpuset`, …).
//! - [`AffinityPanel_buildTopology`] (`AffinityPanel.c:341`) — walks the
//! `hwloc` object children recursively.
//!
//! `Drop`-teardown (a C `free`/`Vector_delete`/`Panel_done` chain with no
//! safe-Rust algorithm — owned fields are released by `Drop`, the
//! `History_delete`/`Panel_delete` precedent):
//! - [`MaskItem_delete`] (`AffinityPanel.c:48`).
//! - [`AffinityPanel_delete`] (`AffinityPanel.c:141`).
//!
//! panel/cpuids aliasing + unported substrate:
//! - [`AffinityPanel_update`] (`AffinityPanel.c:177`) — the non-hwloc arm is
//! `Panel_splice(super, this->cpuids)`, and `Panel_splice` is itself
//! stubbed: htop's `AffinityPanel` uses a *non-owning* `Panel` that shares
//! the `MaskItem*` pointers held by the *owning* `cpuids` `Vector`, so a
//! toggle applied to a spliced item is seen through `cpuids`. htoprs's
//! `Panel` owns its items as `Vec<Box<dyn Object>>` and cannot alias
//! `cpuids`; reproducing the shared-pointer store needs either the
//! unported `Vector` (with its `owner` flag) or `Rc`/`RefCell` shared
//! ownership, neither of which the substrate provides.
//! - [`AffinityPanel_eventHandler`] (`AffinityPanel.c:203`) — toggles the
//! selected item's `value` in `super->items`, which must alias `cpuids`
//! (see [`AffinityPanel_update`]), and calls [`AffinityPanel_update`] on a
//! `HANDLED` result. `HandlerResult` is now modeled in `panel.rs`, so the
//! remaining block is the `cpuids` aliasing above plus the still-stubbed
//! [`AffinityPanel_update`] / `Panel_splice`.
//! - [`AffinityPanel_new`] (`AffinityPanel.c:379`) — builds `cpuids` while
//! the `Panel` splices the same pointers, and its last statement calls
//! [`AffinityPanel_update`]; blocked transitively on the same aliasing.
use crate;
use crate;
use crateMachine;
use crate;
use crate;
use crate;
/// Model of the C `MaskItem` struct (`AffinityPanel.c:33`), non-hwloc
/// variant. The C type embeds an `Object super` vtable as its first field;
/// that is expressed by the `impl Object for MaskItem` below instead. `text`
/// is the row label (C `char* text`); `indent` doubles as the tree-node flag
/// (C `char* indent`, NULL when the item is a flat CPU, so `Option<String>`);
/// `value` and `sub_tree` are the C tri-states; `children` is the C
/// `Vector* children` (empty in the non-hwloc build — only the hwloc topology
/// builder populates it); `cpu` is the C `#else`-branch `int cpu`.
/// Port of `static const ObjectClass MaskItem_class` (`AffinityPanel.c:87`).
/// The C initializer sets `.display`/`.delete` but no `.extends`, so
/// `extends` is `NULL` — ported verbatim as `None`. Declared `static` so its
/// address (the type's identity, per `Object_isA`) is stable.
static MaskItem_class: ObjectClass = ObjectClass ;
/// Port of `static void MaskItem_delete(Object* cast)` from
/// `AffinityPanel.c:48`: `free(text); free(indent);
/// Vector_delete(children); free(this);` (the `hwloc_bitmap_free` block is
/// `#ifdef HAVE_LIBHWLOC`, not built here). Taking `this` by value consumes
/// the item; the owned `text` `String`, `indent` `Option<String>`, and the
/// `children` `Vec<MaskItem>` (whose drop recursively runs each child's
/// teardown — the C's owner-`Vector_delete` recursion) all drop with the
/// struct free.
/// Port of `static void MaskItem_display(const Object* cast, RichString* out)`
/// from `AffinityPanel.c:62`.
///
/// Appends the checkbox (`[x]` for a fully-set item, `[o]` for a partial
/// one, `[ ]` otherwise) using `CRT_colors[CHECK_BOX]`/`CHECK_MARK`, a
/// `CHECK_TEXT` space, then the item `text` in `CHECK_TEXT`. The
/// `if (this->indent)` tree-node branch draws the indent guides and the
/// open/shut glyph via `CRT_treeStr[TREE_STR_OPEN/SHUT]`, which is not
/// ported in `crt.rs` (`crt.rs` is off-limits to this module); since only
/// the hwloc-only [`MaskItem_newMask`] sets a non-NULL `indent`, that branch
/// is unreachable in this non-hwloc build and stays a `todo!()` — the
/// `ListItem_display` moving-glyph precedent.
/// TODO: port of `static MaskItem* MaskItem_newMask(const char* text,
/// const char* indent, hwloc_bitmap_t cpuset, bool owner)` from
/// `AffinityPanel.c:94`. Entirely `#ifdef HAVE_LIBHWLOC`: it takes an
/// `hwloc_bitmap_t` and sets `sub_tree` from `hwloc_bitmap_weight(cpuset)`.
/// htoprs links no `hwloc`, so there is no body to port.
/// Port of `static MaskItem* MaskItem_newSingleton(const char* text, int cpu,
/// bool isSet)` from `AffinityPanel.c:108`, non-hwloc branch.
///
/// Builds a flat-CPU item: `text` (C `xStrdup`), `indent = NULL` (not a tree
/// node), `sub_tree = 0`, an empty `children` vector, `cpu = cpu` (the
/// `#else` arm; the `#ifdef HAVE_LIBHWLOC` arm that allocates a one-bit
/// cpuset is compiled out), and `value = isSet ? 2 : 0`. Returns an owned
/// [`MaskItem`] (the C fn heap-allocates and returns a pointer).
/// Model of the C `AffinityPanel` struct (`AffinityPanel.c:127`), non-hwloc
/// variant. `super_` is the embedded `Panel super` (`super` is a Rust
/// keyword); `host` is the borrowed `Machine*` (raw pointer — the `Affinity`
/// `host` precedent, never dereferenced by ported code); `topoView` mirrors
/// the C flag (always `false` without hwloc); `cpuids` is the C
/// `Vector* cpuids` of flat-CPU items; `width` is the computed panel width.
/// The hwloc-only fields (`topoRoot`, `allCpuset`, `workCpuset`) live inside
/// `#ifdef HAVE_LIBHWLOC` and are omitted.
/// Port of `const PanelClass AffinityPanel_class` (`AffinityPanel.c:358`): sets
/// only `.eventHandler = AffinityPanel_eventHandler`; `.drawFunctionBar` /
/// `.printHeader` are NULL and inherit the `Panel` defaults. The ported
/// [`AffinityPanel_eventHandler`] is a `todo!()` stub whose signature is `()`
/// (not `(&mut AffinityPanel, i32) -> HandlerResult` — it awaits the
/// panel/cpuids `Panel_splice` aliasing), so the `event_handler` slot cannot be
/// wired without a signature mismatch and inherits the default here.
/// Port of `static void AffinityPanel_delete(Object* cast)` from
/// `AffinityPanel.c:141`: `Vector_delete(this->cpuids); Panel_done(&this->super);
/// free(this);` (the `hwloc_bitmap_free`/`MaskItem_delete(topoRoot)` block is
/// `#ifdef HAVE_LIBHWLOC`, not built here). Taking `this` by value consumes
/// the panel; the owned `cpuids` `Vec<MaskItem>` (C's `Vector_delete`, its
/// drop recursively running each `MaskItem` teardown) and the non-owning
/// `host`/`topoView`/`width` fields drop, and the embedded `super_` [`Panel`]
/// is handed to [`Panel_done`] (mirroring the C call graph).
/// TODO: port of `static void AffinityPanel_updateItem(AffinityPanel* this,
/// MaskItem* item)` from `AffinityPanel.c:156`. Entirely `#ifdef
/// HAVE_LIBHWLOC`: it computes `item->value` from
/// `hwloc_bitmap_isincluded`/`hwloc_bitmap_intersects` against `workCpuset`.
/// htoprs links no `hwloc`, so there is no body to port.
/// TODO: port of `static void AffinityPanel_updateTopo(AffinityPanel* this,
/// MaskItem* item)` from `AffinityPanel.c:165`. Entirely `#ifdef
/// HAVE_LIBHWLOC`: it recurses the hwloc topology tree. htoprs links no
/// `hwloc`, so there is no body to port.
/// TODO: port of `static void AffinityPanel_update(AffinityPanel* this,
/// bool keepSelected)` from `AffinityPanel.c:177`. The non-hwloc arm is
/// `Panel_splice(super, this->cpuids)`, and `Panel_splice` (`Panel.c:222`)
/// is itself stubbed: htop's `AffinityPanel` uses a non-owning `Panel`
/// that shares the `MaskItem*` pointers held by the owning `cpuids`
/// `Vector`, so a toggle on a spliced item is seen through `cpuids`.
/// htoprs's `Panel` owns its items as `Vec<Box<dyn Object>>` and cannot
/// alias `cpuids`; reproducing the shared store needs the unported `Vector`
/// (with its `owner` flag) or `Rc`/`RefCell`, neither in the substrate.
/// TODO: port of `static HandlerResult AffinityPanel_eventHandler(Panel* super,
/// int ch)` from `AffinityPanel.c:203`. `HandlerResult`
/// (`IGNORED`/`HANDLED`/`BREAK_LOOP`) is now modeled in `panel.rs`, but the
/// body toggles the selected item's `value` in `super->items` — which must
/// alias `cpuids` (see [`AffinityPanel_update`]) — and calls
/// `AffinityPanel_update` on a `HANDLED` result. Both still depend on the
/// panel/cpuids shared-pointer aliasing, i.e. the stubbed `Panel_splice`.
/// TODO: port of `static MaskItem* AffinityPanel_addObject(AffinityPanel* this,
/// hwloc_obj_t obj, unsigned indent, MaskItem* parent)` from
/// `AffinityPanel.c:283`. Entirely `#ifdef HAVE_LIBHWLOC`: it reads
/// `hwloc_obj_t` fields (`depth`, `logical_index`, `complete_cpuset`, …) and
/// builds an indent string. htoprs links no `hwloc`, so there is no body to
/// port.
/// TODO: port of `static MaskItem* AffinityPanel_buildTopology(AffinityPanel* this,
/// hwloc_obj_t obj, unsigned indent, MaskItem* parent)` from
/// `AffinityPanel.c:341`. Entirely `#ifdef HAVE_LIBHWLOC`: it walks the
/// `hwloc` object children recursively. htoprs links no `hwloc`, so there is
/// no body to port.
/// TODO: port of `Panel* AffinityPanel_new(Machine* host, const Affinity* affinity,
/// int* width)` from `AffinityPanel.c:379`. Builds `cpuids` (one
/// [`MaskItem_newSingleton`] per online CPU) while the `Panel` is meant to
/// splice the same `MaskItem*` pointers, and its final statement calls
/// [`AffinityPanel_update`]. Blocked on the same panel/cpuids shared-pointer
/// aliasing as [`AffinityPanel_update`] (htoprs's `Panel` owns its items and
/// cannot alias `cpuids`).
/// Port of `Affinity* AffinityPanel_getAffinity(Panel* super, Machine* host)`
/// from `AffinityPanel.c:444`, non-hwloc branch.
///
/// Allocates a fresh [`Affinity`] for `host`, then for every `cpuids` item
/// whose `value` is set (non-zero), adds that item's `cpu` to the affinity
/// set. The C casts `Panel* super` to `AffinityPanel*`; the faithful analog
/// takes `this: &AffinityPanel` directly. `item->cpu` is a non-negative CPU
/// index; the C passes it to `Affinity_add(unsigned int)`, so it is widened
/// to `u32`. Returns an owned [`Affinity`] (the C fn returns a pointer).