Using mbuf routines in userland

To not depend exclusively on testing in the kernel (which can be inconvenient), I did an implementation of the mbuf routines (functions and macros in the kern/uipc_mbuf.c, kern/uipc_mbuf2.c and sys/mbuf.h files) in userland. The differences are fortunately minimal and are controlled by a __UNIT_TEST macro (the name is chosen according to the primary planned purpose of this - unit testing), so those files can be shared between the userland implementation and the kernel. To do this, I had to copy some of the kernel headers and provide stub implementation of some kernel routines that those three files expect. For example, the pool allocator has been implemented by malloc(3) library function. The result can be found in the directory userland under the mbuf module. The subdirectory mbuf_impl contains the three files, in kernel there are the unmodified supporting kernel sources, stubs contains those that were heavily simplified, and opts contains replacement for the autogenerated opt_*.h files (which can be mostly empty).

"make" (which must be GNU Make) in the userland directory will create a kernel.a static library containing the mbuf routines, which can be used to create testing code in userland. To facilitate that, in the directory tests there is a Makefile.inc file, which can be included in the Makefile of any tests. The including Makefile is supposed to set the S variable to the userland directory. The test itself is supposed to be divided in two sets of files. The first are compiled like kernel sources with all the -D_KERNEL -ffreestanding flags, and the inclusion of Makefile.inc takes care of compiling them and setting the right CFLAGS. The second set are files compiled like normal userland files. One of those should contain the main function. A separate Makefile should be created for them and be named Makefile.std. As the GNU Make has sufficient built-in rules, this file can be usually left empty. In the including Makefile, the names of the first set of files should be assigned to a variable KERNSRCS ant of the second set to the variable STDSRCS. THe PROG variable should be set to the name of the resulting program. The Makefile.inc itself can be included after setting those variables. That's all. After that, gmake in this directory creates the program from the sources named in KERNSRCS and STDSRCS. Makefile.inc takes care of rebuilding the kernel.a library. A sample program is in the directory tests/example1. It contains a deliberate bug, where the mbuf is not ensured to be long enough. This is to demonstrate how the Valgrind memory debugger can catch such errors (because the subsequent printf then references unitialized data).