FDD-book/examples/part-01/ch04-a-first-look-at-the-c-programming-language/lab-memlab-kernel/memlab.c
Edson Brandi 6e6dac94fe Add book companion files under examples/
Organize companion code and materials by book part:

  part-01/      Foundations
  part-02/      Kernel & driver fundamentals
  part-03/      Character & block devices
  part-04/      Networking & storage
  part-05/      Buses & advanced topics
  part-06/      Debugging & performance
  part-07/      Real-world drivers
  appendices/   Supplementary material
  README.md     Overview and build instructions
2026-04-25 16:51:00 +00:00

59 lines
1.3 KiB
C

/*
* memlab.c - simple kernel module demonstrating malloc(9) / free(9).
*
* Companion for "Mini-Lab 3: Memory Allocation in a Kernel Module"
* in Chapter 4. Load the module and then check `vmstat -m | grep
* memlab` to see the allocation recorded; unload it and check again
* to confirm the memory is returned.
*
* Compatible with FreeBSD 14.3.
*/
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/systm.h>
MALLOC_DEFINE(M_MEMLAB, "memlab", "Memory Lab Example");
static void *buffer = NULL;
static int
memlab_load(struct module *m, int event, void *arg)
{
int error = 0;
switch (event) {
case MOD_LOAD:
printf("memlab: Loading module\n");
buffer = malloc(128, M_MEMLAB, M_WAITOK | M_ZERO);
if (buffer == NULL) {
printf("memlab: malloc failed!\n");
error = ENOMEM;
} else {
printf("memlab: allocated 128 bytes\n");
}
break;
case MOD_UNLOAD:
printf("memlab: Unloading module\n");
if (buffer != NULL) {
free(buffer, M_MEMLAB);
printf("memlab: memory freed\n");
}
break;
default:
error = EOPNOTSUPP;
break;
}
return (error);
}
static moduledata_t memlab_mod = {
"memlab", memlab_load, NULL
};
DECLARE_MODULE(memlab, memlab_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
MODULE_VERSION(memlab, 1);