My scheduler operations implementation A benefit of working on your own operating system is that you’re free from the usual "restraints" of collaboration and real applications. That has always been a major factor in my interest in osdev. You don’t have to worry about releasing your program, or about critical security vulnerabilities, or about hundreds of people having to maintain your code.In the OSDev world you’re usually alone, and that solitude gives you the freedom to experiment with unusual programming patterns.While developing a kernel module for my master’s thesis I came across an article on LWN: “Object-oriented design patterns in the kernel.” The article describes how the Linux kernel, despite being written in C, embraces object-oriented principles by using function pointers in structures to achieve polymorphism. It was fascinating to see how something as low-level as the kernel can still borrow the benefits of object orientation "encapsulation", modularity, and extensibility. This lead me to experimenting with implementing all my kernels services with this approach.The basic idea is to have a "vtable" as a struct with function pointers. Describing the interface for the object./* "Interface" with function pointers */ struct device_ops { void (*start)(void); void (*stop)(void); };The device in this case will hold a reference to this vtable./* Device struct holding a pointer to its ops */ struct device { const char *name; const struct device_ops *ops; };Different type of devices can now utilize the same 'api', while calling very different functions.struct device netdev = { "eth0", &net_ops }; struct device disk = { "sda", &disk_ops }; netdev.ops->start(); // net: up disk.ops->start(); // disk: spinning netdev.ops->stop(); // net: down disk.ops->stop(); // disk: stoppedWhat makes vtables especially powerful is that they can be swapped out at runtime. The caller doesn’t need to change anything, once the vtable is updated, every call is automatically redirected ...
First seen: 2025-08-27 14:22
Last seen: 2025-08-28 03:26