mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 08:33:30 +00:00
progressing chapter 4...
This commit is contained in:
parent
b1e26a7eb4
commit
0b9063510a
1 changed files with 395 additions and 1 deletions
|
|
@ -2600,4 +2600,398 @@ By now, you’ve seen how a function prototype, its implementation, and its real
|
|||
|
||||
Before we go further into writing complex driver logic, we need to understand one of the most fundamental concepts in C programming: variable scope. Scope determines where a variable can be accessed in your code, how long it stays alive in memory, and what parts of the program can modify it. In FreeBSD driver development, misunderstanding scope can lead to elusive bugs from uninitialised values corrupting hardware state to variables mysteriously changing between function calls. By mastering scope rules, you’ll gain fine-grained control over your driver’s data, ensuring that values are only visible where they should be, and that critical state is preserved or isolated as needed. In the next section, we’ll break down scope into clear, practical categories and show you how to apply them effectively in kernel code.
|
||||
|
||||
*continue soon...*
|
||||
## Variable Scope in Functions
|
||||
|
||||
In programming, **scope** defines the boundaries within which a variable can be seen and used. In other words, it tells us where in the code a variable is visible and who is allowed to read or change its value.
|
||||
|
||||
When a variable is declared inside a function, we say it has **local scope**. Such a variable comes into existence when the function starts running and disappears as soon as the function finishes. No other function can see it, and even within the same function, it may be invisible if declared inside a more restricted block, such as inside a loop or an `if` statement.
|
||||
|
||||
This form of isolation is a powerful safeguard. It prevents accidental interference from other parts of the program, ensures that one function cannot inadvertently change the internal workings of another, and makes the program’s behaviour more predictable. By keeping variables confined to the places they are needed, you make your code easier to reason about, maintain, and debug.
|
||||
|
||||
To make this idea more concrete, let’s look at a short example in C. We’ll create a function with a variable that lives entirely inside it. You’ll see how the variable works perfectly within its own function, but becomes completely invisible the moment we step outside that function’s boundaries.
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
void print_number(void) {
|
||||
int x = 42; // x has local scope: only visible inside print_number()
|
||||
printf("%d\n", x);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
print_number();
|
||||
// printf("%d\n", x); // ERROR: 'x' is not in scope here
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
Here, the variable x is declared inside `print_number()`, which means it is created when the function starts and destroyed when the function ends. If we try to use `x` in `main()`, the compiler complains because `main()` has no knowledge of `x`—it lives in a separate, private workspace. This “one workspace per function” rule is one of the foundations of reliable programming: it keeps code modular, avoids accidental changes from unrelated parts of the program, and helps you reason about the behaviour of each function independently.
|
||||
|
||||
**Why Local Scope Is Good**
|
||||
|
||||
Local scope brings three key benefits to your code:
|
||||
|
||||
* Prevents bugs — a variable inside one function cannot accidentally overwrite or be overwritten by another function’s variable, even if they share the same name.
|
||||
* Keeps code predictable — you always know exactly where a variable can be read or modified, making it easier to follow and reason about the program’s flow.
|
||||
* Improves efficiency — the compiler can often keep local variables in CPU registers, and any stack space they use is automatically freed when the function returns.
|
||||
|
||||
By keeping variables confined to the smallest area where they’re needed, you reduce the chances of interference, make debugging easier, and help the compiler optimise performance.
|
||||
|
||||
**Why scope matters in driver development**
|
||||
|
||||
In FreeBSD device drivers, you’ll often manipulate temporary values—buffer sizes, indices, error codes, flags that are relevant only within a specific operation (e.g., probing a device, initialising a queue, handling an interrupt). Keeping these values local prevents cross-talk between concurrent paths and avoids subtle race conditions. In kernel space, small mistakes propagate fast; tight, local scope is your first line of defence.
|
||||
|
||||
**From Simple Scope to Real Kernel Code**
|
||||
|
||||
You’ve just seen how a local variable inside a small C program lives and dies within its function. Now, let’s step into a real FreeBSD driver and see exactly the same principle at work, but this time in code that interacts with actual hardware.
|
||||
|
||||
We’ll look at part of the VirtIO subsystem, which is used for virtual devices in environments like QEMU or bhyve. This example comes from the function `virtqueue_init_indirect()` that is located between the lines 230 and 271 in the file `sys/dev/virtio/virtqueue.c` in FreeBSD 14.3 source code, which sets up “indirect descriptors” for a virtual queue. Watch how variables are declared, used, and limited to the function’s own scope, just like in our earlier `print_number()` example.
|
||||
|
||||
Note: I’ve added some extra comments to highlight what’s happening at each step.
|
||||
|
||||
```c
|
||||
static int
|
||||
virtqueue_init_indirect(struct virtqueue *vq, int indirect_size)
|
||||
{
|
||||
// Local variable: holds the device reference for easy access
|
||||
// Only exists inside this function
|
||||
device_t dev;
|
||||
|
||||
// Local variable: temporary pointer to a descriptor structure
|
||||
// Used during loop iterations to point to the right element
|
||||
struct vq_desc_extra *dxp;
|
||||
|
||||
// Local variables: integer values used for temporary calculations
|
||||
// 'i' will be our loop counter, 'size' will hold the calculated memory size
|
||||
int i, size;
|
||||
|
||||
// Initialise 'dev' with the device associated with this virtqueue
|
||||
// 'dev' is local, so it's only valid here — other functions cannot touch it
|
||||
dev = vq->vq_dev;
|
||||
|
||||
// Check if the device supports the INDIRECT_DESC feature
|
||||
// This is done through a bus-level feature negotiation
|
||||
if (VIRTIO_BUS_WITH_FEATURE(dev, VIRTIO_RING_F_INDIRECT_DESC) == 0) {
|
||||
/*
|
||||
* If the driver requested indirect descriptors, but they were not
|
||||
* negotiated, we print a message (only if bootverbose is on).
|
||||
* Then we return 0 to indicate initialisation continues without them.
|
||||
*/
|
||||
if (bootverbose)
|
||||
device_printf(dev, "virtqueue %d (%s) requested "
|
||||
"indirect descriptors but not negotiated\n",
|
||||
vq->vq_queue_index, vq->vq_name);
|
||||
return (0); // At this point, all locals are destroyed
|
||||
}
|
||||
|
||||
// Calculate the memory size needed for the indirect descriptors
|
||||
size = indirect_size * sizeof(struct vring_desc);
|
||||
|
||||
// Store these values in the virtqueue structure for later use
|
||||
vq->vq_max_indirect_size = indirect_size;
|
||||
vq->vq_indirect_mem_size = size;
|
||||
|
||||
// Mark this virtqueue as using indirect descriptors
|
||||
vq->vq_flags |= VIRTQUEUE_FLAG_INDIRECT;
|
||||
|
||||
// Loop through all entries in the virtqueue
|
||||
for (i = 0; i < vq->vq_nentries; i++) {
|
||||
// Point 'dxp' to the i-th descriptor entry in the queue
|
||||
dxp = &vq->vq_descx[i];
|
||||
|
||||
// Allocate memory for the indirect descriptor list
|
||||
dxp->indirect = malloc(size, M_DEVBUF, M_NOWAIT);
|
||||
|
||||
// If allocation fails, log an error and stop initialisation
|
||||
if (dxp->indirect == NULL) {
|
||||
device_printf(dev, "cannot allocate indirect list\n");
|
||||
return (ENOMEM); // Locals are still destroyed upon return
|
||||
}
|
||||
|
||||
// Get the physical address of the allocated memory
|
||||
dxp->indirect_paddr = vtophys(dxp->indirect);
|
||||
|
||||
// Initialise the allocated descriptor list
|
||||
virtqueue_init_indirect_list(vq, dxp->indirect);
|
||||
}
|
||||
|
||||
// Successfully initialised indirect descriptors — locals end their life here
|
||||
return (0);
|
||||
}
|
||||
```
|
||||
|
||||
**Understanding the Scope in This Code**
|
||||
|
||||
Even though this is production-level kernel code, the principle is the same as in the tiny example we just saw. The variables `dev`, `dxp`, `i`, and `size` are all declared inside `virtqueue_init_indirect()` and exist only while this function is running. Once the function returns, whether it’s at the end or early via a return statement, those variables vanish, freeing their stack space for other uses.
|
||||
|
||||
Notice that this keeps things safe: the loop counter `i` can’t be accidentally reused in another part of the driver, and the `dxp` pointer is re-initialised for each call to the function. In driver development, this is a critical local scope that ensures that temporary work variables won’t collide with names or data in other parts of the kernel. The isolation you learned about in the simple `print_number()` example applies here in exactly the same way, just at a higher level of complexity and with real hardware resources involved.
|
||||
|
||||
**Common Beginner Mistakes (and How to Avoid Them)**
|
||||
|
||||
One of the quickest ways to get into trouble is to store the address of a local variable in a structure that outlives the function. Once the function returns, that memory is reclaimed and can be overwritten at any time, leading to mysterious crashes. Another issue is “over-sharing”, using too many global variables for convenience, which can cause unpredictable results if multiple execution paths modify them at the same time. And finally, be careful not to shadow variables (reusing a name inside an inner block), which can lead to confusion and hard-to-spot bugs.
|
||||
|
||||
**Wrapping Up and Moving Forward**
|
||||
|
||||
The lesson here is simple but powerful: local scope makes your code safer, easier to test, and more maintainable. In FreeBSD device drivers, it is the right tool for per-call, temporary data. Long-lived information should be stored in properly designed per-device structures, keeping your driver organised and avoiding accidental data sharing.
|
||||
|
||||
Now that you understand **where** a variable can be used, it is time to look at **how long** it exists. This is called **variable storage duration**, and it affects whether your data lives on the stack, in static storage, or on the heap. Knowing the difference is key to writing robust, efficient drivers, and that’s precisely where we are headed next.
|
||||
|
||||
## Variable Storage Duration
|
||||
|
||||
So far, you’ve learned where a variable can be used in your program, as well as its scope. But there’s another equally important property: how long the variable actually exists in memory. This is called its storage duration.
|
||||
|
||||
While scope is about visibility in the code, storage duration is about lifetime in memory. A variable’s storage duration determines:
|
||||
|
||||
* **When** the variable is created.
|
||||
* **When** it is destroyed.
|
||||
* **Where** it lives (stack, static storage, heap).
|
||||
|
||||
Understanding storage duration is critical in FreeBSD driver development because we often handle resources that must persist across function calls (like device state) alongside temporary values that must vanish quickly (like loop counters or temporary buffers).
|
||||
|
||||
### The Three Main Storage Durations in C
|
||||
|
||||
When you create a variable in C, you’re not just giving it a name and a value, you’re also deciding **how long that value will live in memory**. This “lifetime” is what we call the **storage** duration. Even two variables that look similar in the code can behave very differently depending on how long they stick around.
|
||||
|
||||
Let’s break down the three main types you’ll encounter, starting with the most common in day-to-day programming.
|
||||
|
||||
**Automatic Storage Duration (stack variables)**
|
||||
|
||||
Think of these as short-term helpers. They are born the moment a function starts running and disappear the instant the function finishes. You don’t have to create or destroy them manually; C takes care of that for you.
|
||||
|
||||
Automatic variables:
|
||||
|
||||
* Are declared inside functions without the `static` keyword.
|
||||
* Are created when the function is called and destroyed when it returns.
|
||||
* Live on the **stack**, a section of memory that’s automatically managed by the program.
|
||||
* Are perfect for quick, temporary jobs like loop counters, temporary pointers, or small scratch buffers.
|
||||
|
||||
Because they vanish when the function ends, you can’t keep their address for later use; doing so leads to one of the most common beginner mistakes in C.
|
||||
|
||||
Small Example:
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
void greet_user(void) {
|
||||
char name[] = "FreeBSD"; // automatic storage, stack memory
|
||||
printf("Hello, %s!\n", name);
|
||||
} // 'name' is destroyed here
|
||||
|
||||
int main(void) {
|
||||
greet_user();
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
Here, `name` lives only while `greet_user()` runs. When the function exits, the stack space is freed automatically.
|
||||
|
||||
**Static Storage Duration (globals and `static` variables)**
|
||||
|
||||
Now imagine a variable that doesn’t come and go with a function call, instead, it’s **always there** from the moment your program (or in kernel space, your driver module) loads until it ends. This is **static storage**.
|
||||
|
||||
Static variables:
|
||||
|
||||
* Are declared outside functions or inside functions with the `static` keyword.
|
||||
* Are created **once** when the program/module starts.
|
||||
* Remain in memory until the program/module ends.
|
||||
* Live in a dedicated **static memory** area.
|
||||
* Are great for things like per-device state structures or lookup tables that are needed throughout the program’s lifetime.
|
||||
|
||||
However, since they stick around, you must be careful in driver code shared, long-lived data can be accessed by multiple execution paths, so you may need locks or other synchronization to avoid conflicts.
|
||||
|
||||
Small Example:
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
static int counter = 0; // static storage, exists for the entire program
|
||||
|
||||
void increment(void) {
|
||||
counter++;
|
||||
printf("Counter = %d\n", counter);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
increment();
|
||||
increment();
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
`counter` keeps its value between calls to `increment()` because it never leaves memory until the program ends.
|
||||
|
||||
**Dynamic Storage Duration (heap allocation)**
|
||||
|
||||
Sometimes you don’t know in advance how much memory you’ll need, or you need to keep something around even after the function that created it has finished. That’s where dynamic storage comes in: you request memory at runtime, and you decide when it goes away.
|
||||
|
||||
Dynamic variables:
|
||||
|
||||
* Are created explicitly at runtime with `malloc()`/`free()` in user space, or `malloc(9)`/`free(9)` in the FreeBSD kernel.
|
||||
* Exist until you explicitly free them.
|
||||
* Live in the **heap**, a pool of memory managed by the operating system or kernel.
|
||||
* Are perfect for things like buffers whose size depends on hardware parameters or user input.
|
||||
|
||||
The flexibility comes with responsibility: forget to free them, and you’ll have a memory leak. Free them too soon, and you might crash the system by accessing invalid memory.
|
||||
|
||||
Small Example:
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void) {
|
||||
char *msg = malloc(32); // dynamic storage
|
||||
if (!msg) return 1;
|
||||
strcpy(msg, "Hello from dynamic memory!");
|
||||
printf("%s\n", msg);
|
||||
free(msg); // must free to avoid leaks
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
Here, the program decides at runtime to allocate 32 bytes. The memory is under your control, so you must free it when done.
|
||||
|
||||
### Bridging Theory and Practice
|
||||
|
||||
So far, we’ve looked at these storage durations in an abstract way. But concepts really sink in when you see them in the wild, inside a real FreeBSD driver or subsystem function. Kernel code often mixes these durations: a few automatic locals for temporary values, some static structures for persistent state, and carefully managed dynamic memory for resources that come and go during runtime.
|
||||
|
||||
To make this clearer, let’s walk through a real function from the FreeBSD 14.3 source tree. By following each variable and seeing how it’s declared, used, and eventually discarded or freed, you’ll gain an intuitive feel for how lifetime and scope interact in real-world kernel work.
|
||||
|
||||
|
||||
| Duration | Created | Destroyed | Memory area | Typical declarations | Good driver use cases | Common pitfalls | FreeBSD APIs to know |
|
||||
| --------- | ----------------------- | ------------------- | -------------- | --------------------------------------------- | --------------------------------------------------- | ----------------------------------------------- | ----------------------------------- |
|
||||
| Automatic | On function entry | On function return | Stack | Local variables without `static` | Scratch values in fast paths and interrupt handlers | Returning addresses of locals. Oversized locals | N/A |
|
||||
| Static | When module loads | When module unloads | Static storage | File scope variables or `static` inside funcs | Persistent device state. Constant tables. Tunables | Hidden shared state. Missing locks on SMP | `sysctl(9)` patterns for tunables |
|
||||
| Dynamic | When you call allocator | When you free it | Heap | Pointers returned by allocators | Buffers sized at probe time. Lifetime spans calls | Leaks. Use after free. Double free | `malloc(9)`, `free(9)`, `M_*` types |
|
||||
|
||||
|
||||
### Real Example from FreeBSD 14.3
|
||||
|
||||
Before we move on, let’s look at how these storage duration concepts appear in production-quality FreeBSD code. Our example comes from the network interface subsystem, specifically from the `_if_delgroup_locked()` function in `sys/net/if.c` (lines 1474 to 1512 in FreeBSD 14.3). This function removes an interface from a named interface group, updates reference counts, and frees memory when the group becomes empty.
|
||||
|
||||
As in our earlier, simpler examples, you’ll see **automatic** variables created and destroyed entirely within the function, **dynamic** memory being released explicitly with `free(9)`, and, elsewhere in the same file, **static** variables that persist for the module’s entire lifetime. By walking through this function, you’ll see lifetime and scope management in action not just in an isolated snippet, but in the complex, interconnected world of the FreeBSD kernel.
|
||||
|
||||
Note: I’ve added some extra comments to highlight what’s happening at each step.
|
||||
|
||||
```c
|
||||
/*
|
||||
* Helper function to remove a group out of an interface. Expects the global
|
||||
* ifnet lock to be write-locked, and drops it before returning.
|
||||
*/
|
||||
static void
|
||||
_if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
|
||||
const char *groupname)
|
||||
{
|
||||
struct ifg_member *ifgm; // [Automatic] (stack) pointer: used only in this call
|
||||
bool freeifgl; // [Automatic] (stack) flag: should we free the group?
|
||||
|
||||
IFNET_WLOCK_ASSERT(); // sanity: we entered with the write lock held
|
||||
|
||||
/* Remove the (interface,group) link from the interface's local list. */
|
||||
IF_ADDR_WLOCK(ifp);
|
||||
CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
|
||||
IF_ADDR_WUNLOCK(ifp);
|
||||
|
||||
/*
|
||||
* Find and remove this interface from the group's member list.
|
||||
* 'ifgm' is a LOCAL cursor; it does not escape this function
|
||||
* (classic automatic storage).
|
||||
*/
|
||||
CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
|
||||
/* [Automatic] 'ifgm' is a local iterator only */
|
||||
if (ifgm->ifgm_ifp == ifp) {
|
||||
CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
|
||||
ifg_member, ifgm_next);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Decrement the group's reference count. If we just removed the
|
||||
* last member, mark the group for freeing after we drop locks.
|
||||
*/
|
||||
if (--ifgl->ifgl_group->ifg_refcnt == 0) {
|
||||
CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
|
||||
ifg_next);
|
||||
freeifgl = true;
|
||||
} else {
|
||||
freeifgl = false;
|
||||
}
|
||||
IFNET_WUNLOCK(); // we promised to drop the global lock before return
|
||||
|
||||
/*
|
||||
* Wait for readers in the current epoch to finish before freeing memory
|
||||
* (RCU-style safety in the networking stack).
|
||||
*/
|
||||
NET_EPOCH_WAIT();
|
||||
|
||||
/* Notify listeners that the group membership changed. */
|
||||
EVENTHANDLER_INVOKE(group_change_event, groupname);
|
||||
|
||||
if (freeifgl) {
|
||||
/* Group became empty: fire detach event and free the group object. */
|
||||
EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
|
||||
free(ifgl->ifgl_group, M_TEMP); // [Dynamic] (heap) storage being returned
|
||||
}
|
||||
|
||||
/* Free the (interface,group) membership nodes allocated earlier. */
|
||||
free(ifgm, M_TEMP); // [Dynamic] the 'member' record
|
||||
free(ifgl, M_TEMP); // [Dynamic] the (ifnet, group) link record
|
||||
}
|
||||
```
|
||||
|
||||
What to notice
|
||||
|
||||
* `[Automatic]` `ifgm` and `freeifgl` live only for this call. They cannot outlive the function.
|
||||
* `[Dynamic]` frees return heap objects that were allocated earlier in the driver life cycle. The lifetime crosses function boundaries and must be released on the exact success path shown here.
|
||||
* `[Static]` is not used in this function. In the same file you will find persistent configuration and counters that exist from load to unload. Those are `[Static]`.
|
||||
|
||||
|
||||
**Understanding the Storage Durations in This Function**
|
||||
|
||||
If you follow `_if_delgroup_locked()` from start to finish, you can watch all three storage durations in C play their part. The variables `ifgm` and `freeifgl` are automatic, which means they are born when the function is called, live entirely on the stack, and disappear the moment the function returns. They are private to this call, so nothing outside can accidentally change them, and they cannot change anything outside either.
|
||||
|
||||
A little further down, the calls to `free(...)` deal with dynamic storage. The pointers passed to `free()` were created earlier in the driver’s life, often with `malloc()` during initialisation routines like `if_addgroup()`. Unlike stack variables, this memory stays around until the driver deliberately lets it go. Freeing it here tells the kernel, *“I’m done with this; you can reuse it for something else.”*
|
||||
|
||||
This function doesn’t use static variables directly, but in the same file (`if.c`), you will find examples like debugging flags declared with `YSCTL_INT` that live for as long as the kernel module is loaded. These variables keep their values across function calls and are a reliable place to store configuration or diagnostics that need to persist.
|
||||
|
||||
Each choice here is intentional.
|
||||
|
||||
* Automatic variables keep temporary state safe inside the function.
|
||||
* Dynamic memory gives flexibility at runtime, allowing the driver to adjust and then clean up when done.
|
||||
* Static storage, found elsewhere in the same codebase, supports persistent, shared information.
|
||||
|
||||
Put together, this is a clear, real-world example of how lifetime and visibility work hand in hand in FreeBSD driver code. It is not just theory from a C textbook, it is the day-to-day reality of writing drivers that are reliable, efficient, and safe to run in the kernel.
|
||||
|
||||
### Why Storage Duration Matters in FreeBSD Drivers
|
||||
|
||||
In kernel development, storage duration is not just an academic detail; it’s directly tied to system stability, performance, and even security. A wrong choice here can take down the entire operating system.
|
||||
|
||||
In FreeBSD drivers, the right storage duration ensures that data lives exactly as long as needed, no more and no less:
|
||||
|
||||
* **Automatic variables** are ideal for short-lived, private state, such as temporary values in an interrupt handler. They vanish automatically when the function ends, avoiding long-term clutter in memory.
|
||||
* **Static variables** can safely store hardware state or configuration that must persist across calls, but they introduce shared state that may require locking in SMP systems to avoid race conditions.
|
||||
* **Dynamic allocations** give you flexibility when buffer sizes depend on runtime conditions like device probing results, but they must be explicitly freed to avoid leaks and freeing too soon risks accessing invalid memory.
|
||||
|
||||
Mistakes with storage duration can be catastrophic in the kernel. Keeping a pointer to a stack variable beyond the function’s life is almost guaranteed to cause corruption. Forgetting to free dynamic memory ties up resources until a reboot. Overusing static variables can turn shared state into a performance bottleneck.
|
||||
|
||||
Understanding these trade-offs is not optional. In driver code, often triggered by hardware events in unpredictable contexts, correct lifetime management is a foundation for writing code that is safe, efficient, and maintainable.
|
||||
|
||||
### Common Beginner Mistakes
|
||||
|
||||
When you are new to C and especially to kernel programming, it is surprisingly easy to misuse storage duration without even realising it. One classic trap with automatic variables is returning the address of a local variable from a function. At first, it might seem harmless after all, the variable was right there a moment ago, but the moment the function returns, that memory is reclaimed for other uses. Accessing it later is like reading a letter you already burned; the result is undefined behaviour, and in the kernel, that can mean an instant crash.
|
||||
|
||||
Static variables can cause trouble differently. Because they persist across function calls, a value left over from a previous run of the function might influence the next run in unexpected ways. This is particularly dangerous if you assume that every call starts with a “clean slate.” In reality, static variables remember everything, even when you wish they wouldn’t.
|
||||
|
||||
Dynamic memory has its own set of hazards. Forgetting to `free()` something you allocated means the memory will be tied up until the system is restarted, a problem known as a memory leak. In kernel space, where resources are precious, a leak can slowly degrade the system. Freeing the same pointer twice is even worse, it can corrupt kernel memory structures and bring down the whole machine.
|
||||
|
||||
Being aware of these patterns early on helps you avoid them when working on real driver code, where the cost of a mistake is often far greater than in user-space programming.
|
||||
|
||||
### Wrapping Up
|
||||
|
||||
We have explored the three main storage durations in C: automatic, static, and dynamic. Each one has its place, and the right choice depends on how long you need the data to live and who should be able to see it. The safest general rule is to choose the smallest necessary lifetime for your variables. This limits their exposure, reduces the risk of unintended interactions, and often makes the compiler’s job easier.
|
||||
|
||||
In FreeBSD driver development, careful management of variable lifetimes is not optional; it is a fundamental skill. Done right, it helps you write code that is predictable, efficient, and resilient under load. With these principles in mind, you are ready to explore the next piece of the puzzle: understanding how variable linkage affects visibility across files and modules.
|
||||
|
||||
*Continue soon...*
|
||||
Loading…
Reference in a new issue