C has no native classes, inheritance, or virtual methods. This project provides a manual abstraction layer built from C primitives to model object-oriented runtime behavior. It is designed for systems programming education and for understanding kernel-style object design and control flow.
An object is defined as struct data + vtable pointer + capability mask.
Dynamic dispatch is implemented with function pointers stored in a vtable.
Inheritance is simulated with struct embedding, where derived structs place the base object as the first member.
The runtime centers on a base Object type and an ObjectVTable.
All polymorphic behavior is dispatched through vtable entries.
typedef struct ObjectVTable ObjectVTable;
typedef struct Object {
const ObjectVTable *vtable;
uint32_t caps;
uint32_t ref_count; /* optional */
} Object;
struct ObjectVTable {
void (*destroy)(Object *self);
Object *(*clone)(const Object *self);
int (*read)(const Object *self, void *out, size_t n);
int (*write)(Object *self, const void *in, size_t n);
};
typedef struct MemoryObject {
Object base; /* embedded base for inheritance simulation */
uint8_t *buf;
size_t len;
} MemoryObject;Strict rule: all operations on Object* and derived objects must go through the vtable. Direct type-specific calls bypassing the vtable are not part of the core runtime contract.
The runtime defines capability bits:
#define CAP_READ (1u << 0)
#define CAP_WRITE (1u << 1)
#define CAP_CLONE (1u << 2)
#define CAP_EXEC (1u << 3)Every operation validates required capabilities before execution.
On capability failure, the runtime returns an error code or NULL; it does not proceed with undefined behavior.
MemoryObject represents a heap-backed byte buffer object.
clone performs a deep copy of metadata and buffer content.
destroy frees owned memory exactly once and clears internal state to prevent reuse.
ref_count can be enabled for shared ownership, with destruction triggered at zero.
The runtime can expose an object registry for lookup, diagnostics, and controlled teardown.
strict_mode acts as sandbox mode and enforces capability checks for all dispatch paths.
Lifecycle tracking records allocation, clone, retain/release (if enabled), and destroy transitions.
makemake testtest result: ok. 100% passed; 0 failed
#include <stdint.h>
#include <stdlib.h>
int main(void) {
MemoryObject *m = memory_object_new(256, CAP_READ | CAP_WRITE | CAP_CLONE);
if (!m) return 1;
const char msg[] = "kernel";
if (m->base.vtable->write((Object *)m, msg, sizeof(msg)) < 0) {
m->base.vtable->destroy((Object *)m);
return 1;
}
Object *copy = m->base.vtable->clone((const Object *)m);
if (!copy) {
m->base.vtable->destroy((Object *)m);
return 1;
}
copy->vtable->destroy(copy);
m->base.vtable->destroy((Object *)m);
return 0;
}- pure C only
- no C++
- no external dependencies
- no undefined behavior in core runtime
- all operations go through vtables
- no full GC
- no thread safety
- no kernel integration
- minimal security model only
MIT License
Copyright (c) 2026
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.