Code style and design¶
Format¶
atlOS uses the WebKit code style,
enforced by clang-format (.clang-format, BasedOnStyle: WebKit):
- 4 spaces, no tabs, no column limit
- function braces on their own line, other braces on the same line
- no indentation inside the outermost namespace
T* p/T& r#pragma onceinstead of include guards
scripts/build.sh format # rewrite all kernel and libc sources
scripts/build.sh format-check # fail if a file is not formatted
Naming¶
| What | Style | Example |
|---|---|---|
| types, namespaces, enum values | PascalCase | PhysicalMemory, ThreadState::Running |
| functions, methods | camelCase | allocateBlocks, addTask |
| variables, parameters, struct fields | camelCase | pageIndex, lbaFirstSector |
| class data members | m_ + camelCase |
m_name |
| static and namespace scope variables | s_ + camelCase |
s_readyQueue |
constexpr constants |
camelCase | maxProcesses, pageSize2Mb |
| macros | UPPER_CASE | KERNEL_VIRTUAL_LOAD_OFFSET |
Acronyms in type names stay upper case (CPUInfo, VGAColor).
Namespaces¶
Which side of the system a name belongs to is part of the name:
| Namespace | Code | Example |
|---|---|---|
Kernel:: |
everything under kernel/ |
Kernel::Memory::AddressSpace, Kernel::Threading::Thread |
User:: |
the user program library (user/lib/) |
User::Threading::Thread, User::Process::exitProcess |
Common:: |
code built into the kernel and into user programs (common/heap/) |
Common::Heap |
Abi:: |
the contract between the two: system call numbers, errors, ids (common/abi/) |
Abi::SysStartThread, Abi::ThreadId |
Where the kernel and the user library offer the same thing, they offer it
with the same names and signatures, as separate classes with their own
data: Kernel::Threading::Thread holds a TCB and a kernel stack,
User::Threading::Thread a thread id, and code on either side writes
Threading::Thread t(entry, arg); t.wait();. (SerenityOS keeps its kernel
in Kernel:: the same way; Windows NT's Ps, Ke, Mm prefixes are the
idea in C.)
Inside Kernel:: the code names its neighbours without the prefix
(Memory::AddressSpace), and so do the docs. A backtrace or a symbol shows
the full name. Functions called from C or assembly (kernelMain, kmalloc,
kernelPanic, the interrupt stubs) keep C linkage, so their symbol has no
namespace.
Exceptions, all excluded from the naming check:
- names shared with assembly or the linker script (
kernelMain,switchTask,gdt_ptr,pml4,kernel_physical_end, ...) - reserved and ABI names (
__cxa_*,__ctors_start,_text_start, ...) - libc and the standard C names user programs keep (
read,write,open,close,malloc,free,strlen, theO_*flags andE*errors), the names the start code shares with crt0 and the linker script (atlos_start,__init_array_start), and the multiboot headers inkernel/arch/x86_64/boot(copied from the spec)
User programs are C++17 and follow the same naming as the kernel
(printHex, heapInfo, Threading::currentThread).
clang-tidy¶
.clang-tidy enables bugprone-*, clang-analyzer-*, performance-* and
readability-identifier-naming (the naming table above). Disabled on purpose:
bugprone-easily-swappable-parameters (noise for (addr, size) APIs),
bugprone-reserved-identifier (the C++ runtime has to define __cxa_*),
performance-no-int-to-ptr (page table code casts addresses by design);
modernize-* and cppcoreguidelines-* are not used because they fight the
raw pointers and casts a kernel needs.
scripts/build.sh tidy # kernel sources, uses build/default/compile_commands.json
The kernel is clean: new warnings should be fixed, not suppressed. Code inside
#ifdef ATLOS_SELFTEST is only seen when running tidy on build/selftest.
Third party code¶
libc/stdio/printf.cpp (mpaland/printf) is kept as upstream: no
reformatting, no renaming.
Subsystem READMEs¶
Each subsystem directory has a README.md with the same sections:
- What is it for? the problem, without assuming OS background
- How could it be designed? the usual approaches, with pros, cons and who uses them (Linux, xv6, ...)
- How atlOS does it and why; what is deliberately left out
- Flow diagrams (mermaid, rendered by GitHub and the site)
- Tests that cover it, and references (OSDev Wiki, Intel SDM, xv6, OSTEP)
A change to a subsystem updates its README in the same commit series. A new
README also gets a chapter in tutorial.md and an entry in the
nav of docs/mkdocs.yml. Keep them short: the code and the references
go deeper.
Design¶
Agreed direction, introduced subsystem by subsystem. Done: the interrupt
controllers and timers (Drivers::ApicInterruptController, Drivers::PIC,
Drivers::ApicTimer, Drivers::PIT behind InterruptController /
TickSource), the physical memory manager
(Memory::PhysicalMemoryManager), the kernel address space
(Memory::AddressSpace) and the scheduler (Threading::Scheduler; threads
reach it through the current task), all built by kernelMain in
Cxx::Uninitialized storage, and the console (Drivers::SerialPort and
Drivers::VGATextConsole behind CharacterSink, bound to printf with
Core::setConsole), and the kernel heap (Common::Heap on
Memory::AddressSpacePages, bound to kmalloc and new with
Memory::setKernelHeap). logMsg keeps no state and stays a function over
printf.
- No singletons. Each subsystem is a class that owns its state
(
PhysicalMemoryManager,AddressSpace,Heap,Scheduler, drivers); there are noinstance()accessors and no lazily created globals. - Dependencies are passed in, by constructor reference, so they are
visible in the signatures and the construction order is checked by the
compiler:
AddressSpace kernelSpace(pmm); AddressSpacePages pages(kernelSpace); Heap heap(pages); - Hardware behind interfaces:
InterruptController(APIC, PIC),TickSource(APIC timer, PIT),CharacterSink(serial, VGA),BlockDeviceandFileSystem. kernelMainis the composition root. It creates the objects in order, in static storage that is constructed explicitly (there is no heap yet and the boot stack is small), which also avoids static initialization order problems.- Globals only at C/assembly boundaries (interrupt dispatch, the user
program runtime behind
int 0x80and ring 3 faults, the kernel heap behindkmallocandoperator new, the panic/printf sink): one explicitly bound pointer per boundary, set by the composition root. And one fact about the CPU: the current task (Threading::currentTask(), set by every switch), which sync objects and threads use to find the running thread and its scheduler.
git blame ignores the mechanical style commits listed in
.git-blame-ignore-revs; enable it once with
git config blame.ignoreRevsFile .git-blame-ignore-revs.