Skip to content

Commit a19e05c

Browse files
committed
add modmod exercise_ref
1 parent 07b9848 commit a19e05c

File tree

1 file changed

+5
-5
lines changed
  • content/mods/E-rust-for-systems/topics/ffi/exercises/qoi-bindgen

1 file changed

+5
-5
lines changed

Diff for: content/mods/E-rust-for-systems/topics/ffi/exercises/qoi-bindgen/description.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ In this exercise, we test if the image crate produces the same results when deco
88

99
The QOI C library is a header-only library, which means the function implementations are included within the header file instead of in a separate C file. We've added a separate C file which includes the header to make it easier to compile and include the library in our Rust program.
1010

11-
### Generating bindings
11+
### #[modmod:exercise_ref] Generating bindings
1212
Prerequisites:
1313

1414
- A C compiler is installed on the system
@@ -33,7 +33,7 @@ Steps:
3333
3. Create `src/lib.rs` with the contents `pub mod bindings;`. This will make the `bindings` module available in `main.rs`.
3434
4. Run `cargo check` to verify everything is compiling correctly.
3535

36-
### Inspecting our bindings
36+
### #[modmod:exercise_ref] Inspecting our bindings
3737

3838
In the generated `bindings.rs` file we find this signature for the `qoi_read` C function from QOI:
3939

@@ -70,7 +70,7 @@ Some observations:
7070

7171
We will deal with the last point by writing a nice Rust wrapper *around* the generated bindings.
7272

73-
### Writing our wrapper
73+
### #[modmod:exercise_ref] Writing our wrapper
7474
To make the `qoi_read` function easier to use, we would like to write a wrapper that takes a path and returns an image buffer:
7575

7676
```rust
@@ -123,7 +123,7 @@ running 1 test
123123
test tests::test_qoi_read ... ok
124124
```
125125

126-
### Freeing the pixel data
126+
### #[modmod:exercise_ref] Freeing the pixel data
127127
When working with data from C, we are responsible for deallocating the memory once we are done using it. Some C libraries might provide a separate function to clean up data structures. For QOI, we instead have to call `libc::free` to free the memory, as indicated by the documentation of the `qoi_read` function:
128128
> The returned pixel data should be free()d after use.
129129
@@ -149,7 +149,7 @@ To make sure someone using our wrapper does not forget to free the memory, we ca
149149
```
150150
- Now update the `read_qoi_image` function to return an instance of `MyImage`.
151151

152-
### Uninitialized memory
152+
### #[modmod:exercise_ref] Uninitialized memory
153153
There is one more trick: our current function initializes the `qoi_desc` struct with zeros (or whatever values you put there while creating an instance of the struct). This is wasteful because the extern function will overwrite these values. Because the extern function is linked in, the compiler likely does not have enough information to optimize this.
154154

155155
For a relatively small struct such as `qoi_desc`, this is not much of a problem. However, for larger structures or big arrays, this can make a serious impact on performance.

0 commit comments

Comments
 (0)