Tying Everything Together

Example 2-1 is a revision of Example 1-2 that uses its own malloc_type structure instead of the kernel-defined M_TEMP.[2] Example 2-1 should clarify any misunderstandings you may have about MALLOC_DEFINE and MALLOC_DECLARE.

Note

To save space, the functions echo_open, echo_close, echo_write, and echo_read aren’t listed here, as they haven’t been changed.

Example 2-1. echo-2.0.c

#include <sys/param.h>
  #include <sys/module.h>
  #include <sys/kernel.h>
  #include <sys/systm.h>

  #include <sys/conf.h>
  #include <sys/uio.h>
  #include <sys/malloc.h>

  #define BUFFER_SIZE     256

 MALLOC_DECLARE(M_ECHO);
 MALLOC_DEFINE(M_ECHO, "echo_buffer", "buffer ...

Get FreeBSD Device Drivers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.