Skip to content

Commit 71efd83

Browse files
committed
adjust title level
1 parent 206b731 commit 71efd83

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

Chapters/5-valueholders.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Pointer arithmetic and value holders
1+
## Pointer arithmetic and value holders
22

33
One of the reasons for the power of C is that, despite the appearance of having a type system, it effectively lacks one: every value is ultimately reduced to a memory position.
44

@@ -29,7 +29,7 @@ void main() {
2929

3030
Here, `store_value` receives not the value of `x`, but its address. By dereferencing that address, the function modifies the memory where `x` is stored.
3131

32-
## What does this mean in uFFI?
32+
### What does this mean in uFFI?
3333

3434
Historically, this has been handled in uFFI by passing a `ByteArray` to the C function. A `ByteArray` is essentially a reification of a chunk of memory, which allows C to operate on it directly.
3535

@@ -49,7 +49,7 @@ In short, we pass a `ByteArray` to a C function that expects a pointer to an int
4949
**This is straightforward, but it is low-level and error-prone.**
5050
It requires detailed knowledge of memory layout, sizes, and access primitives.
5151

52-
## ... enter value holders
52+
### ... enter value holders
5353

5454
To simplify this complexity and make code easier to write and understand, we introduce **value holders**.
5555

@@ -97,11 +97,11 @@ This mechanism works with all basic C types defined in uFFI, including:
9797
`FFIUInt8`, `FFIUInt16`, `FFIUInt32`, `FFIUInt64`,
9898
`FFIInt8`, `FFIInt16`, `FFIInt32`, `FFIInt64`, `FFILong`, `FFIULong`.
9999

100-
## What happens with structures, unions, and external objects?
100+
### What happens with structures, unions, and external objects?
101101

102102
Value holders work naturally for basic C types, but what about more complex ones?
103103

104-
### Structures (and unions)
104+
#### Structures (and unions)
105105

106106
When you pass a structure *by value* in C, it is always copied. This means the function receives a copy of the structure's contents and can only read them.
107107

@@ -146,7 +146,7 @@ void fill_values(mystructtype *t) {
146146

147147
This example is intentionally simplified and not realistic, but it captures the essence: modifying a structure through a pointer.
148148

149-
### Passing structure value holders
149+
#### Passing structure value holders
150150

151151
Using value holders, this is straightforward:
152152

@@ -162,7 +162,7 @@ Transcript show: ('{1} + {2} = {3}' format: {
162162

163163
This ensures uniform access to structures and unions, just like any other type.
164164

165-
### Passing a reference to a structure
165+
#### Passing a reference to a structure
166166

167167
Sometimes you already have a structure instance and need to pass it by reference. This is common when a structure must be initialized first and then modified by a C function.
168168

@@ -185,7 +185,8 @@ This behavior is subtle and relies on internal implementation details. Now that
185185

186186
In C, it is common --- especially when dealing with lists --- to encounter arguments with more than one level of indirection.
187187
Of this cases, we will focus on arrays, since other cases follow the same pattern.
188-
### Passing arrays
188+
189+
#### Passing arrays
189190
In C, arrays are just pointer arithmetic. A function declared with `int *` or `char **` often expects a list of values.
190191

191192
uFFI provides an abstraction for this through the `FFIArray` class. `FFIArray` can be used both to define array types and to create instances that manage storing and retrieving data through C pointers.

0 commit comments

Comments
 (0)