xv6, line by line
kernel/types.h

kernel/types.h

C header · 10 lines · annotated 100% · kernel · upstream

Included by 47 fileskernel/bio.c, kernel/console.c, kernel/exec.c, kernel/file.c, kernel/fs.c, kernel/kalloc.c, kernel/log.c, kernel/main.c, kernel/pipe.c, kernel/plic.c, kernel/printk.c, kernel/proc.c, kernel/sleeplock.c, kernel/spinlock.c, kernel/start.c, kernel/string.c, kernel/syscall.c, kernel/sysfile.c, kernel/sysproc.c, kernel/trap.c, kernel/uart.c, kernel/virtio_disk.c, kernel/vm.c, mkfs/mkfs.c, user/cat.c, user/dorphan.c, user/echo.c, user/forktest.c, user/forphan.c, user/grep.c, user/grind.c, user/init.c, user/kill.c, user/ln.c, user/logstress.c, user/ls.c, user/mkdir.c, user/printf.c, user/rm.c, user/sh.c, user/stressfs.c, user/sync.c, user/ulib.c, user/umalloc.c, user/usertests.c, user/wc.c, user/zombie.c
About this file

Short names for C’s integer types, used everywhere in the kernel, in user programs and in mkfs. xv6 defines its own short names instead of using <stdint.h>. That header would in fact be available (it is one of the few headers a freestanding (vs. hosted) C compiler supplies), but xv6 uses no standard headers at all, and the same file is shared with mkfs, a normal host program.

The names that end in a number say exactly how many bits the type has. That matters in an operating system: a CSR (control and status register), a register and a page-table entry are all 64 bits, and on-disk structures must have the same layout every time. On 64-bit RISC-V (the “LP64” model GCC uses here) char is 8 bits, short 16, int 32 and long 64, which is what these typedefs rely on.

The file has no include guard. Standard C99 does not allow repeating a typedef (GCC accepts it unless -pedantic is given); xv6 keeps it simple by including this file once, first, in every .c file, before headers that use these names.

Read next: kernel/param.h, kernel/riscv.h.

1typedef unsigned int uint;
2typedef unsigned short ushort;
3typedef unsigned char uchar;
5typedef unsigned char uint8;
6typedef unsigned short uint16;
7typedef unsigned int uint32;
8typedef unsigned long uint64;
10typedef uint64 pde_t;