Before we can dive into writing FreeBSD device drivers, we need to learn the language they’re written in. That language is C, short, powerful, and, admittedly, a little quirky. But don’t worry, you don’t need to be a programming expert to get started.
In this chapter, I’ll walk you through the basics of the C programming language, assuming absolutely no prior experience. If you’ve never written a line of code in your life, you’re in the right place. If you’ve done some programming in other languages like Python or JavaScript, that’s fine too; C might feel a little more manual, but we’ll tackle it together.
Our goal here isn’t to become master C programmers in one chapter. Instead, I want to introduce you to the language gently, showing you its syntax, its building blocks, and how it works in the context of UNIX systems like FreeBSD. Along the way, I’ll point out real-world examples taken directly from the FreeBSD source code to help ground the theory in actual practice.
By the time we’re done, you’ll be able to read and write basic C programs, understand the core syntax, and feel confident enough to take the next steps toward kernel development. But that part will come later, for now, let’s focus on learning the essentials.
## 4.1 Introduction
Let’s start at the beginning: what is C, and why is it important to us?
### What is C?
C is a programming language created in the early 1970s by Dennis Ritchie at Bell Labs. It was designed to write operating systems, and that’s still one of its biggest strengths today. In fact, most modern operating systems, including FreeBSD, Linux, and even parts of Windows and macOS, are written mainly in C.
C is fast, compact, and close to the hardware, but unlike assembly language, it’s still readable and expressive. You can write efficient, powerful code with C, but it also expects you to be careful. There’s no safety net: no automatic memory management, no runtime error messages, and not even built-in strings like in Python or JavaScript.
This might sound scary, but it’s actually a feature. When writing drivers or working inside the kernel, you want control, and C gives you that control.
### Why Should I Learn C for FreeBSD?
FreeBSD is written almost entirely in C, and that includes the kernel, device drivers, userland tools, and system libraries. If you want to write code that interacts with the operating system, whether it’s a new device driver or a custom kernel module, C is your entry point.
More specifically:
* The FreeBSD kernel APIs are written in C.
* All device drivers are implemented in C.
* Even debugging tools like dtrace and kgdb understand and expose C-level information.
So, to work with FreeBSD’s internals, you’ll need to understand how C code is written, structured, compiled, and used in the system.
### What If I’ve Never Programmed Before?
No problem! I’m writing this chapter with you in mind. We’ll take it one step at a time, starting with the simplest possible program and slowly building our way up. You’ll learn about:
* Variables and data types
* Functions and flow control
* Pointers, arrays, and structures
* How to read real code from the FreeBSD kernel
And don’t worry if any of those terms are unfamiliar right now, they’ll all make sense soon. I’ll provide plenty of examples, explain every step in plain language, and help you build your confidence as we go.
### How Is This Chapter Organised?
Here’s a quick preview of what’s coming up:
* We’ll begin by setting up your development environment on FreeBSD.
* Then, we’ll walk through your first C program, the classic “Hello, World!”.
* From there, we’ll cover the syntax and semantics of C: variables, loops, functions, and more.
* We’ll show you real examples from the FreeBSD source tree, so you can start learning how the system works under the hood.
* Finally, we’ll wrap up with some good practices and a look at what’s coming in the next chapter, where we begin applying C to the kernel world.
Are you ready? Let’s jump into the next section and get your environment set up so you can run your first C program on FreeBSD.
Before we can start writing C code, we need to set up a working development environment. The good news? If you’re running FreeBSD, **you already have most of what you need**.
Attention: Every command line that will be executed by the shell within a Makefile rule must begin with a tab character, not spaces. If you use spaces, the make execution will fail."
To use it:
Save this in a file called Makefile (note the capital "M")
Run make in the same directory:
% make
cc -o hello hello.c
This is especially helpful when your project grows to include multiple files.
**Important Note:** One of the most common mistakes when writing your first Makefile is forgetting to use a TAB character at the beginning of each command line in a rule. In Makefiles, every line that should be executed by the shell must start with a TAB, not spaces. If you accidentally use spaces instead, `make` will produce an error and fail to run. This detail often trips up beginners, so be sure to check your indentation carefully!
This error will appear as shown below:
% make
make: "/home/ebrandi/hello/Makefile" line 4: Invalid line type
make: Fatal errors encountered -- cannot continue
make: stopped in /home/ebrandi/hello
### Installing the FreeBSD Source Code
As we move forward, we’ll look at examples from the actual FreeBSD kernel source. To follow along, it’s useful to have the FreeBSD source tree installed locally.
To store a complete local copy of the FreeBSD source code, you will need approximately 3.6 GB of free disk space. You can install it using Git by running the following command:
This will give you access to all source code, which we’ll reference frequently throughout this book.
### Summary
You now have a working development setup on FreeBSD! It was simple, wasn't it?
Here’s what you’ve accomplished:
* Verified the C compiler is installed
* Wrote and compiled your first C program
* Learned how to use Makefiles
* Cloned the FreeBSD source tree for future reference
These tools are all you need to start learning C, and later, to build your own kernel modules and drivers. In the next section, we’ll look at what makes up a typical C program and how it’s structured.
## 4.3 Anatomy of a C Program
Now that you've compiled your first "Hello, World!" program, let’s take a closer look at what’s actually going on inside that code. In this section, we’ll break down the basic structure of a C program and explain what each part does, step by step.
We’ll also introduce how this structure appears in the FreeBSD kernel code, so you can begin recognising familiar patterns in real-world systems programming.
This line is handled by the **C preprocessor** before the program is compiled. It tells the compiler to include the contents of a system header file.
*`<stdio.h>` is a standard header file that provides I/O functions like printf.
* Anything you include this way is pulled into your program at compile time.
In FreeBSD source code, you'll often see many `#include` directives at the top of a file. Here’s an example from the FreeBSD kernel file `sys/kern/kern_shutdown.c`:
These headers define macros, constants, and function prototypes used in the kernel. For now, just remember: `#include` brings in definitions you want to use.
* This is the **entry point of your program**. When your program runs, it starts here.
* The `int` means the function returns an integer to the operating system.
* void means it takes no arguments.
In user programs, `main()` is where you write your logic. In the kernel, however, there’s **no**`main()` function like this; the kernel has its own bootstrapping process. But FreeBSD kernel modules and subsystems still define **entry points** that act in similar ways.
This is a **statement**, a single instruction that performs some action.
*`printf()` is a function provided by `<stdio.h>` that prints formatted output.
*`"Hello, World!\n"` is a string literal, with `\n` meaning "new line".
**Important Note:** In kernel code, you don’t use the `printf()` function from the Standard C Library (libc). Instead, the FreeBSD kernel provides its own internal version of `printf()` tailored for kernel-space output, a distinction we'll explore in more detail later in the book.
Returned when the executable file's vnode (`imgp->vp`) has no associated virtual memory object (`v_object`). Without this object, the kernel cannot map the file into memory. This is treated as a **permission/access error**, using the standard `EACCES` error code (`"Permission Denied"`).
*`return (EIO);`
Returned when the kernel fails to retrieve a valid memory page from the file via `vm_page_grab_valid_unlocked()`. This may happen due to an I/O failure, memory issue, or file corruption. The `EIO` code (`"Input/Output Error"`) signals a **low-level failure** in reading or allocating memory for the file.
*`return (0);`
Returned on successful completion of the function. This indicates that the kernel has successfully grabbed the first page of the executable file, mapped it into memory, and stored the address of the header in `imgp->image_header`. A return value of `0` is the standard kernel convention for indicating success.
The use of `errno`-style error codes like `EIO` and `EACCES` ensures consistent error handling throughout the kernel, making it easier for driver developers and kernel programmers to propagate errors reliably and interpret failure conditions in a familiar, standardised way.
The FreeBSD kernel makes extensive use of `errno`-style error codes to represent different failure conditions consistently. Don’t worry if they seem unfamiliar at first, as we move forward, you’ll naturally encounter many of them, and I’ll help you understand how they work and when to use them.
For a complete list of standard error codes and their meanings, you can refer to the FreeBSD manual page:
% man 2 intro
### Putting It All Together
Let’s revisit our Hello World program, now with full comments:
* How similar structures appear in FreeBSD’s kernel code
The more C code you read, both your own and from FreeBSD, the more these patterns will become second nature.
## Variables and Data Types
In any programming language, variables are how you store and manipulate data. In C, variables are a little more "manual" than in higher-level languages, but they give you the control you need to write fast, efficient programs, and that’s precisely what operating systems like FreeBSD require.
In this section, we’ll explore:
* How to declare and initialise variables
* The most common data types in C
* How FreeBSD uses them in kernel code
* Some tips to avoid common beginner mistakes
Let’s start with the basics.
### What Is a Variable?
A variable is like a labeled box in memory where you can store a value, such as a number, a character, or even a block of text.
In C, you must declare the type of every variable before using it. This is different from languages like Python, where the type is determined automatically.
Here’s how to declare different types of variables:
Always initialise your variables, not just because it’s good C practice, but because in kernel development, uninitialized values can lead to subtle and dangerous bugs, including kernel panics, unpredictable behaviour, and security vulnerabilities. In userland, mistakes might crash your program; in the kernel, they can compromise the stability of the entire system.
Unless you have a very specific and justified reason not to (such as performance-critical code paths where the value is immediately overwritten), make initialisation the rule, not the exception.
The `volatile` qualifier can be important in FreeBSD kernel development, but only in very specific contexts, such as accessing hardware registers or dealing with interrupt-driven updates. It tells the compiler not to optimise accesses to a variable, which is critical when values can change outside of normal program flow.
However, `volatile` is not a substitute for proper synchronisation and should not be used for coordinating access between threads or CPUs. For that, the FreeBSD kernel provides dedicated primitives like mutexes and atomic operations, which offer both compiler and CPU-level guarantees.
### Constant Values and #define
In C programming and especially in kernel development, it's very common to define constant values using the #define directive:
This line doesn't declare a variable. Instead, it's a **preprocessor macro**, which means the C preprocessor will **replace every occurrence of**`MAX_DEVICES`**with**`64` before the actual compilation begins. This replacement happens **textually**, and the compiler never even sees the name `MAX_DEVICES`.
### Why Use #define for Constants?
Using `#define` for constant values has several advantages in kernel code:
* **Improves readability**: Instead of seeing magic numbers (like 64) scattered throughout the code, you see meaningful names like MAX_DEVICES.
* **Makes code easier to maintain**: If the maximum number of devices ever needs to change, you update it in one place, and the change is reflected wherever it's used.
* **Keeps kernel code lightweight**: Kernel code often avoids runtime overhead, and #define constants don’t allocate memory or exist in the symbol table; they simply get replaced during preprocessing.
### Real Example From FreeBSD
You will find many `#define` lines in `sys/sys/param.h`, for example:
This defines the maximum number of characters allowed in a system hostname, and it's used throughout the kernel and system utilities to enforce a consistent limit. The value 256 is now standardised and can be reused wherever the hostname length is relevant.
### Watch Out: There Is No Type Checking
Because `#define` simply performs textual substitution, it does not respect types or scoping.
This works, but it can lead to problems in certain contexts (e.g., integer promotion, unintended precision loss). For more complex or type-sensitive constants, you may prefer using `const` variables or `enums` in userland, but in the kernel, especially in headers, `#define` is often chosen for efficiency and compatibility.
### Best Practices for #define Constants in Kernel Development
* Use **ALL CAPS** for macro names to distinguish them from variables.
* Add comments to explain what the constant represents.
* Avoid defining constants that depend on runtime values.
* Prefer `#define` over `const` in header files or when targeting C89 compatibility (which is still common in kernel code).
### Best Practices for Variables
Writing correct and robust kernel code starts with disciplined variable usage. The tips below will help you avoid subtle bugs, improve code readability, and align with FreeBSD kernel development conventions.
**Always initialise your variables**: Never assume a variable starts at zero or any default value, especially in kernel code, where behaviour must be deterministic. An uninitialized variable could hold random garbage from the stack, leading to unpredictable behaviour, memory corruption, or kernel panics. Even when the variable will be overwritten soon, it’s often safer and more transparent to initialise it explicitly unless performance measurements prove otherwise.
**Don't use variables before assigning a value**: This is one of the most common bugs in C, and compilers won’t always catch it. In the kernel, using an uninitialized variable can result in silent failures or catastrophic system crashes. Always trace your logic to ensure every variable is assigned a valid value before use, especially if it influences memory access or hardware operations.
**Use `const` whenever the value shouldn’t change**:
Using `const` is more than good style; it helps the compiler enforce read-only constraints and catch unintended modifications. This is particularly important when:
* Passing read-only pointers into functions
* Protecting configuration structures or table entries
* Marking driver data that must not change after initialisation
In kernel code, this can even lead to compiler optimisations and make the code easier to reason about for reviewers and maintainers.
**Use `unsigned` for values that can’t be negative (like sizes or counters)**: Variables that represent quantities like buffer sizes, loop counters, or device counts should be declared as `unsigned` types (`unsigned int`, `size_t`, or `uint32_t`, etc.). This improves clarity and prevents logic bugs, especially when comparing with other `unsigned` types, which can cause unexpected behaviour if signed values are mixed in.
**Prefer fixed-width types in kernel code (`uint32_t`, `int64_t`, etc.)**: Kernel code must behave predictably across architectures (e.g., 32-bit vs 64-bit systems). Types like `int`, `long`, or `short` can vary in size depending on the platform, which can lead to portability issues and alignment bugs. Instead, FreeBSD uses standard types from `<sys/types.h>` such as:
*`uint8_t`, `uint16_t`, `uint32_t`, `uint64_t`
*`int32_t`, `int64_t`, etc.
These types ensure your code has a known, fixed layout and avoids surprises when compiling or running on different hardware.
**Pro Tip**: When in doubt, look at existing FreeBSD kernel code, especially drivers and subsystems close to what you're working on. The variable types and initialisation patterns used there are often based on years of hard-earned lessons from real-world systems.
### Summary
In this section, you’ve learned:
* How to declare and initialise variables
* The most important data types in C
* What type qualifiers like const and volatile do
* How to spot and understand variable declarations in FreeBSD’s kernel code
You now have the tools to store and work with data in C, and you've already seen how FreeBSD uses the same concepts in production-quality kernel code.
## Operators and Expressions
So far, we've learned how to declare and initialise variables. Now it's time to make them do something! In this section, we’ll dive into operators and expressions, the mechanisms in C that allow you to compute values, compare them, and control program logic.
We’ll cover:
* Arithmetic operators
* Comparison operators
* Logical operators
* Bitwise operators (lightly)
* Assignment operators
* Real examples from FreeBSD kernel code
### What Is an Expression?
In C, an expression is anything that produces a value. For example:
**Note**: In C, division of two integers **discards the decimal part**. To get floating-point results, at least one operand must be a `float` or `double`.
### Comparison Operators
These are used to compare two values and return either `true (1)` or `false (0)`:
Uses the less-than operator to compare two pointers (`td` and `td2`). This is valid in C; pointer comparisons are common when choosing between resources.
Another real expression in that same file can be found at line 367:
So far, we’ve learned how to declare variables and write expressions. But programs need to do more than compute values; they need to **make decisions** and **repeat actions**. This is where **control flow comes** in.
Control flow statements allow you to:
* Choose between different paths (`if`, `else`, `switch`)
* Repeat operations using loops (`for`, `while`, `do...while`)
* Exit loops early (`break`, `continue`)
These are the **decision-making tools of C**, and they’re essential for writing meaningful programs, from small utilities to operating system kernels.
### Understanding the `if`, `else`, and `else if`
One of the most basic ways to control the flow of a C program is with the `if` statement. It lets your code make decisions based on whether a condition is true or false.
1.`else`– If none of the previous conditions are true, the code inside `else` runs.
**Important syntax rules:**
* Each condition must be inside **parentheses**`( )`.
* Each block of code is surrounded by **curly braces `{ }`**, even if it’s only one line (this prevents common mistakes).
You can see a real example of `if` , `if else` and `else` usage flow control in the function `ifhwioctl()` that starts at line 2407 of `sys/net/if.c` file, the fragment that we are interested in starts at line 2537:
This fragment handles a request from user space to set a description for a network interface, for example, giving `em0` a human-readable label like "Main uplink port". The code checks the length of the description provided and decides what to do next.
Let’s walk through the flow control step by step:
1. First `if`– Checks whether the description is too long to fit.
* If **true**, the function immediately stops and returns an error code (`ENAMETOOLONG`).
* If **false**, execution moves on to the next condition.
1.`else if`– Runs only if the first condition was **false**.
* If the length is exactly zero, it means the user didn’t provide a description, so the code sets `descrbuf` to `NULL`.
* If **false**, the program moves on to the final `else`.
1. Final `else`– Executes when neither of the previous conditions are true.
* Allocates memory for the description and copies the provided text into it.
* If copying fails, it frees the memory and exits the loop or function.
* This is a classic example of using `if / else if / else` to handle mutually exclusive conditions, reject invalid input, handle the empty case, or process a valid value.
In C, `if / else if / else` chains provide a straightforward way to handle several possible outcomes in a single structure. The program checks each condition in order, and as soon as one is true, that block runs and the rest are skipped. This simple rule keeps your logic predictable and easy to follow. In the FreeBSD kernel, you’ll see this pattern everywhere, from network stack functions to device drivers, because it ensures that only the correct code path runs for each situation, making the system’s decision-making both efficient and reliable.
### Understanding the `switch` and `case`
A switch statement is a decision-making structure that’s useful when you need to compare one variable against multiple possible values. Instead of writing a long chain of if and else if statements, you can list each possible value as a case.
* Each case is a possible value that `cmd` might have.
* The `break` statement tells the program to stop checking further cases once a match is found. Without `break`, execution will continue into the next case, a behaviour called **fall-through**.
* The `default` case runs if none of the listed cases match.
You can see a real use of switch in the FreeBSD kernel inside the function `thread_compare()` (starting at line 109 in `sys/kern/tty_info.c`). The fragment we’re interested in is from lines 134 to 141:
In short, `switch` statements provide a clean and efficient way to handle multiple possible outcomes from a single expression, avoiding the clutter of long `if / else if` chains. In the FreeBSD kernel, they are often used to react to different commands, flags, or states, as in our example, which decides between thread A, thread B, or both. Once you become comfortable reading switch structures, you’ll start to recognise them throughout kernel code as a go-to pattern for organising decision-making logic in a clear, maintainable way.
### Understanding the `for` Loops
A `for` loop in C is perfect when you know **how many times** you want to repeat something. It sets things up in a compact, easy-to-read style:
You can see a real for loop inside `sys/net/iflib.c`, in the function `netmap_fl_refill()`, which begins at line 859. The fragment we care about is the inner batching loop at lines 922–949:
* The loop stops when either the batch is full (`i` hits the max) or there’s nothing left to do (`n == 0`). The batch is then “published” to the NIC by the code right after the loop.
In essence, a `for` loop is the go-to choice when you have a clear limit on how many times something should run. It packages initialisation, condition checking, and iteration updates into a single, compact header, making the flow easy to follow.
In FreeBSD’s kernel code, this structure is everywhere from scanning arrays to walking network ring buffers, because it keeps repetitive work both predictable and efficient. Our example from `netmap_fl_refill()` shows precisely how this works in practice:
the loop counts through a fixed-size batch of buffers, stopping either when the batch is full or when there’s no more work left, then hands that batch off to the NIC. Once you get comfortable reading for loops like this, you’ll spot them throughout the kernel and understand how they keep complex systems running smoothly.
### Understanding the `while` Loop
In C, a while loop is a control structure that allows your program to repeat a block of code as long as a certain condition remains true.
Think of it like telling your program, "Keep doing this task while this rule is true. Stop as soon as the rule becomes false."
`i++;` - Increases `i` by 1 after each iteration. This step is crucial; without it, `i` would stay 0 forever, and the loop would never end, creating an infinite loop.
**Key Points to Remember**
* A while loop **may not run at all** if the condition is false from the start.
* Always ensure something inside the loop **changes the condition** over time, or you risk an infinite loop.
* In FreeBSD kernel code, `while` loops are common for:
* Polling hardware status registers until a device is ready.
* Waiting for a buffer to be filled.
* Implementing retry mechanisms.
You can see a real example of `while` loop usage in the function `netmap_fl_refill()` that starts at line 858 of `sys/net/iflib.c` file.
This time, I’ve decided to show you the complete source code for this FreeBSD kernel function because it offers an excellent opportunity to see several concepts from this chapter working together in a real-world context.
To make it easier to follow, I’ve added explanatory comments at key points so you can connect the theory to the actual implementation. Don’t worry if you don’t fully understand every detail right now; this is normal when first looking at kernel code.
For our discussion, pay special attention to the while loop that begins at line 915, as it’s the part we will explore in depth. Look for `while (n > 0) {` in the code below:
It comes from **iflib** (the Interface Library) in FreeBSD’s network stack, in a section of code that connects **netmap** with network drivers.
Netmap is a high-performance packet I/O framework designed for very fast packet processing. In this context, the kernel uses the loop to **refill receive buffers**, ensuring the network interface card (NIC) always has space ready to store incoming packets, keeping data flowing smoothly at high speed.
Here, `n` is simply the number of buffers that still need to be prepared. The loop works through them in **efficient batches**, processing a few at a time until all are ready. This batching approach reduces overhead and is a common technique in high-performance network drivers.
**What the `while (n > 0)` Really Does**
As we’ve just seen, `n` is the count of receive buffers still waiting to be prepared. This loop’s job is simple in concept:
*“Work through those buffers in batches until there are none left.”*
Each pass of the loop prepares a group of buffers and hands them off to the NIC. If there’s still work to do, the loop runs again, ensuring that by the end, all required buffers are ready for incoming packets.
**What Happens Inside the while (n > 0) Loop**
Each time the loop runs, it processes one batch of buffers. Here’s the breakdown:
1.**Debug Tracking**– If the driver is compiled with debugging enabled, it may update counters that track how often large batches of buffers are refilled. This is just for performance monitoring.
1.**Inner Batch Processing**– Inside the loop, there’s another for loop that refills up to a maximum number of buffers at a time (IFLIB_MAX_RX_REFRESH). For each buffer in this batch:
* Look up the buffer’s address and physical location in memory.
1.**Publishing the Batch to the NIC**– Once the batch is ready, the driver calls a function to tell the NIC:
“These new buffers are ready for use.”
By breaking the work into manageable batches and looping until every buffer is ready, this while loop ensures the NIC is always prepared to receive incoming data without interruption. It’s a small but crucial part of keeping packet flow continuous in a high-performance networking environment.
Even if some of the lower-level details—like DMA mapping or ring indices aren’t fully clear yet, the key takeaway is this:
Loops like this are the engine that quietly keeps the system running at full speed. As you progress through the book, these concepts will become second nature, and you’ll start to recognise similar patterns across many parts of the FreeBSD kernel.
### Understanding `do...while` Loops
A `do...while` loop is a variation of the while loop where the **loop body runs at least once**, and then repeats only **if the condition remains true**:
* The loop always executes the code inside at least once, even if the condition is false to begin with.
* Afterwards, it checks the condition (`i <10`)todecidewhethertorepeat.
In the FreeBSD kernel, you'll often see this pattern inside macros designed to behave like single statements. For example, in `sys/sys/timespec.h`, you'll find an example of his type of loop starting at line 44:
2.**Convert and Assign Microseconds**: Divides `ts->tv_nsec` by 1000 to convert nanoseconds to microseconds and stores that in `tv_usec`.
3.`do...while (0)`: Wraps the two statements so that when this macro is used, it behaves syntactically like a single statement, even if followed by a semicolon, preventing issues in constructs like:
While `do...while (0)` may look odd, it’s a solid C idiom used to make macro expansions safe and predictable in all contexts (like inside conditional statements). It ensures that the entire macro behaves like one statement and avoids accidentally creating half-executed code. Understanding this helps you read and avoid subtle bugs in kernel code that rely heavily on macros for clarity and safety.
### Understanding `break` and `continue`
When working with loops in C, sometimes you need to change the normal flow:
1.`break`– Immediately exits the loop, even if the loop condition could still be true.
1.`continue`– Skips the rest of the current iteration and jumps directly to the loop’s next iteration.
`if_purgeaddrs(ifp)` removes all non-link-layer addresses from a network interface. In plain words, it walks the list of addresses attached to the interface and deletes unicast or broadcast addresses that belong to IPv4 or IPv6. Some families are handled by calling helpers who update the lists for us. Anything not handled by a helper is explicitly removed and freed.
**How the loop is organised**
The outer `while (1)` repeats until there are no more removable addresses. Each pass:
1. Enters the network epoch (`NET_EPOCH_ENTER`) to safely walk the interface address list.
1. Scans the list with `CK_STAILQ_FOREACH` to find the **first address after the `AF_LINK entries**. Link-layer entries come first and are not purged here.
1. Leaves the epoch and then decides what to do with the address it found.
For IPv4, `in_control(SIOCDIFADDR)` removes the address and updates the list. Since that work is done, we skip the manual removal below and continue to the next outer-loop pass to look for the next address.
This explicitly removes the address from the list and frees it.
In loops, `break` and `continue` are precision tools for controlling execution flow. In the `if_purgeaddrs()` function from FreeBSD’s `sys/net/if.c`, `break` stops the search when there are no more addresses to remove, or halts the inner scan as soon as a target address is found. `continue` skips the generic removal step when a specialised IPv4 or IPv6 routine has already handled the work, jumping straight to the next pass through the outer loop. This design lets the function repeatedly find one removable address at a time, remove it using the most appropriate method, and keep going until no non-link-layer addresses remain.
The key takeaway is that well-placed break and continue statements keep loops efficient and focused, avoiding wasted work and making the code’s intent clear, a pattern you’ll encounter often in FreeBSD’s kernel for both clarity and performance.
### Pro Tip: Always Use Braces `{}`
In C, if you omit braces after an if, only one statement is actually controlled by the if. This can easily lead to mistakes:
In C, a **function** is like a dedicated workshop in a large factory; it is a self-contained area where a specific task is carried out, start to finish, without disturbing the rest of the production line. When you need that task done, you simply send the work there, and the function delivers the result.
Functions are one of the most important tools you have as a programmer because they let you:
* **Break down complexity**: Large programs become easier to understand when split into smaller, focused operations.
* **Reuse logic**: Once written, a function can be called anywhere, saving you from typing (and debugging) the same code repeatedly.
* **Improve clarity**: A descriptive function name turns a block of cryptic code into a clear statement of intent.
You’ve already seen functions at work:
*`main()` — the starting point of every C program.
*`printf()` — a library function that handles formatted output for you.
In the FreeBSD kernel, you’ll find functions everywhere, from low-level routines that copy data between memory regions to specialised ones that communicate with hardware. For example, when a network packet arrives, the kernel doesn’t put all the processing logic in one giant block of code. Instead, it calls a series of functions, each responsible for a clear, isolated step in the process.
In this section, you’ll learn how to create your own functions, giving you the power to write clean, modular code. This isn’t just good style in FreeBSD device driver development; it’s the foundation for stability, reusability, and long-term maintainability.
**How a Function Call Works in Memory**
When your program calls a function, something important happens behind the scenes:
1.**The caller pauses** - your program stops at the function call and saves the **return address** on the stack so it knows where to continue afterwards.
1.**Arguments are placed** - the values you pass to the function (parameters) are stored, either in registers or on the stack, depending on the platform.
1.**Local variables are created** — the function gets its own workspace in memory, separate from the caller’s variables.
1.**The function runs** — it executes its statements in order, possibly calling other functions along the way.
1.**A return value is sent back** — if the function produces a result, it is placed in a register (commonly eax on x86) for the caller to pick up.
1.**Cleanup and resume** — the function’s workspace is removed from the stack, and the program continues where it left off.
**Why do you need to understand that?**
In kernel programming, every function call has a cost in both time and memory. Understanding this process will help you write efficient driver code and avoid subtle bugs, especially when working with low-level routines where stack space is limited.
### Defining and Declaring Functions
Every function in C follows a simple recipe. To create one, you need to specify four things:
1.**Return type**– what kind of value the function gives back to the caller.
* Example: `int` means the function will return an integer.
* If it doesn’t return anything, we use the keyword `void`.
1.**Name**– a unique, descriptive label for your function, so you can call it later.
* Example: `read_temperature()` is much clearer than `rt()`.
1.**Parameters**– zero or more values the function needs to do its job.
* Each parameter has its own type and name.
* If there are no parameters, use void in the list to make it explicit.
1.**Body**– the block of code, enclosed in {} braces, that performs the task.
* This is where you write the actual instructions.
A lot of beginners get tripped up here, so let’s make it crystal clear:
* **Declaration** tells the compiler that a function exists, what it’s called, what parameters it takes, and what it returns, but it does not provide the code for it.
* **Definition** is where you actually write the body of the function, the full implementation that does the work.
Think of it like planning and building a workshop:
* **Declaration**: putting up a sign saying *“This workshop exists, here’s what it’s called, and here’s the kind of work it does.”*
* **Definition**: actually building the workshop, stocking it with tools, and hiring workers to do the job.
In small single-file programs, you can just put the definition before you call the function and be done. But in larger programs, especially in FreeBSD drivers, code is often split across many files.
For example:
* The function `mydevice_probe()` might be **defined** in `mydevice.c`.
* Its **declaration** will go into a header file `mydevice.h` so that other parts of the driver, or even the kernel, can call it without knowing the details of how it works.
When the compiler sees the declaration, it knows how to check that calls to `mydevice_probe()` use the right number and types of parameters, even before it sees the definition.
**FreeBSD driver perspective**
When writing a driver:
* Declarations often live in `.h` header files.
* Definitions live in `.c` source files.
* The kernel will call your driver’s functions (like `probe()`, `attach()`, `detach()`) based on the declarations it sees in your driver’s headers, without caring exactly how you implement them as long as the signatures match.
Understanding this difference will save you a lot of compiler errors, especially “implicit declaration of function” or “undefined reference” errors, which are among the most common mistakes beginners hit when starting with C.
**How Declarations and Definitions Work Together**
In small programs, you might write the function’s definition before `main()` and be done.
But in real projects, like a FreeBSD device driver, code is split into header files (`.h`) for declarations and source files (`.c`) for definitions.
1. Declaration in the header file tells the compiler: *“These functions exist somewhere, here’s what they look like.”*
1. Definition in the source file provides the actual code.
1. Any other `.c` file that includes `mydevice.h` can now call these functions, and the compiler will check the parameters and return types.
1. At link time, the function calls are connected to their definitions.
**In the context of FreeBSD drivers:**
* You might have `mydevice.c` containing the driver logic, and `mydevice.h` holding the function declarations shared across the driver.
* The kernel build system will compile your `.c` files and link them into a kernel module.
* If the declarations don’t match the definitions exactly, you’ll get compiler errors — which is why keeping them in sync is critical.
**Common mistakes with functions and how to fix them**
1) Calling a function before the compiler knows it exists
Symptom: “implicit declaration of function” warning or error.
Fix: Add a declaration in a header file and include it, or place the definition above its first use.
2) Declaration and definition do not match
Symptom: “conflicting types” or odd runtime bugs.
Fix: Make the signature identical in both places. Same return type, parameter types, and qualifiers in the same order.
3) Forgetting `void` for a function with no parameters
Symptom: The compiler may think the function takes unknown arguments.
Fix: Use int `my_fn(void)` instead of int `my_fn()`.
4) Returning a value from a `void` function or forgetting to return a value
Symptom: “void function cannot return a value” or “control reaches end of non-void function.”
Fix: For non-void functions, always return the right type. For `void`, do not return a value.
5) Returning pointers to local variables
Symptom: Random crashes or garbage data.
Fix: Do not return the address of a stack variable. Use dynamically allocated memory or pass a buffer in as a parameter.
6) Mismatched `const` or pointer levels between declaration and definition
Symptom: Type mismatch errors or subtle bugs.
Fix: Keep qualifiers consistent. If the declaration has `const char *`, the definition must match exactly.
7) Multiple definitions across files
Symptom: Linker error “multiple definition of …”.
Fix: Only one definition per function. If a helper should be private to a file, mark it `static` in that `.c` file.
8) Putting function definitions in headers by accident
Symptom: Multiple definition linker errors when the header is included by several `.c` files.
Fix: Headers should usually have declarations only. If you really need code in a header, make it `static inline` and keep it small.
9) Missing includes for functions you call
Symptom: Implicit declarations or wrong default types.
Fix: Include the correct system or project header that declares the function you are calling, for example `#include <stdio.h>` for `printf`.
10) Kernel specific: undefined symbols when building a module
Symptom: Linker error “undefined reference” while building your KMOD.
Fix: Ensure the function is actually defined in your module or exported by the kernel, that the declaration matches the definition, and that the right source files are part of the module build.
11) Kernel specific: using a helper that is meant to be file local
Symptom: “undefined reference” from other files or unexpected symbol visibility.
Fix: Mark internal helpers as `static` to restrict visibility. Expose only what other files must call through your header.
12) Choosing poor names
Symptom: Hard to read code and name collisions.
Fix: Use descriptive, project prefixed names, for example `mydev_read_reg`, not `readreg`.
**Hands-on exercise: Splitting Declarations and Definitions**
For this exercise, we will create 3 files.
`mydevice.h` - This header file declares the functions and makes them available to any .c file that includes it.
**Why this matters for FreeBSD driver development**
In a real FreeBSD kernel module,
*`mydevice.h` would hold your driver’s public API (function declarations).
*`mydevice.c` would have the full implementations of those functions.
* The kernel (or other parts of the driver) would include the header to know how to call into your code, without needing to see the actual implementation details.
This exact pattern is how `probe()`, `attach()`, and `detach()` routines are structured in actual device drivers. Learning it now will make those later chapters feel familiar.
Understanding the relationship between declarations and definitions is a cornerstone of C programming, and it becomes even more important when you step into the world of FreeBSD device drivers. In kernel development, functions are rarely defined and used in the same file; they are spread across multiple source and header files, compiled separately, and linked together into a single module. A clear separation between **what a function does** (its declaration) and **how it does it** (its definition) keeps code organized, reusable, and easier to maintain. Master this concept now, and you’ll be well-prepared for the more complex modular structures you’ll encounter when we begin building real kernel drivers.