Files
subhobhai943andClaude Opus 5 f9400f6ac4 feat(kernel): Add RB-tree/kfifo/hashtable, wait queues, futex, tiny RCU, page cache and KTest harness
Expands the kernel-and-TTY core with the primitives the higher layers have
been missing, plus an in-kernel harness that proves they work.

Data structures (lib/):
  - rbtree.c: Linux-compatible red-black tree with parent pointers, full
    insert/erase rebalancing, in-order iteration and an invariant validator.
  - kfifo.c: power-of-two byte ring with free-running indices, so the wrap
    path costs one mask instead of a branch per byte.
  - hashtable.c: separately-chained table with FNV-1a hashing, string or
    u64 keys, and rehash-on-growth at a 3/4 load factor.

Kernel subsystems:
  - kernel/wait.c: wait queues and one-shot completion barriers, backed by a
    static sleeper pool so a blocking path never re-enters the allocator.
  - kernel/futex.c: 32-bucket futex hash with value re-check, requeue, and an
    fmutex_t sleeping mutex built on the uncontended-fastpath protocol.
  - kernel/rcu.c: tiny RCU with reader nesting, deferred callbacks, kfree_rcu
    and rcu_assign_pointer/rcu_dereference publish barriers.
  - mm/page_cache.c: write-back block page cache, 4 KB pages indexed by
    (device, page), CLOCK second-chance eviction and dirty write-back.
  - kernel/ktest.c: KUnit-style harness with 6 built-in suites, 21 cases and
    735 assertions covering every subsystem above (all passing under QEMU).

LazyBox userland:
  - coreutils.c: sort, uniq, cut, tr, rev, tac, nl, seq, diff, xxd, du,
    factor, sum and truncate, reading through the VFS and handling the
    zero-length synthetic nodes that procfs and sysfs expose.
  - ktest, rcuinfo, futexinfo, waitinfo and pagecache diagnostics applets.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01L5yKJT4Wo2cV3rfPrwY5MP
2026-08-24 23:38:59 +05:30

82 lines
2.4 KiB
C

#ifndef _KERNEL_KTEST_H
#define _KERNEL_KTEST_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
// In-kernel self-test harness (KUnit-style). Suites register a list of cases;
// `ktest` from the LazyBox shell runs them and reports pass/fail per assertion.
#define KTEST_MAX_SUITES 16
#define KTEST_MAX_CASES 24
typedef struct ktest_context {
const char* suite;
const char* test;
uint32_t assertions;
uint32_t failures;
bool aborted;
} ktest_context_t;
typedef void (*ktest_fn_t)(ktest_context_t* ctx);
typedef struct {
const char* name;
ktest_fn_t func;
} ktest_case_t;
typedef struct {
const char* name;
const ktest_case_t* cases;
size_t case_count;
} ktest_suite_t;
typedef struct {
uint32_t suites_run;
uint32_t tests_run;
uint32_t tests_passed;
uint32_t tests_failed;
uint32_t assertions;
uint32_t assertion_failures;
uint64_t elapsed_ticks;
} ktest_result_t;
void ktest_init(void);
int ktest_register_suite(const ktest_suite_t* suite);
// Run every suite, or one by name. Returns the number of failed tests.
int ktest_run_all(ktest_result_t* result_out);
int ktest_run_suite(const char* name, ktest_result_t* result_out);
void ktest_list_suites(void);
// Assertion primitives used by the macros below.
void ktest_report(ktest_context_t* ctx, bool passed, const char* expr,
const char* file, int line, const char* detail);
#define KTEST_ASSERT(ctx, cond) \
ktest_report((ctx), (cond), #cond, __FILE__, __LINE__, NULL)
#define KTEST_ASSERT_EQ(ctx, a, b) \
do { \
long long _a = (long long)(a); \
long long _b = (long long)(b); \
ktest_expect_eq((ctx), _a, _b, #a " == " #b, __FILE__, __LINE__); \
} while (0)
#define KTEST_ASSERT_STREQ(ctx, a, b) \
ktest_expect_streq((ctx), (a), (b), #a " == " #b, __FILE__, __LINE__)
#define KTEST_ASSERT_NOT_NULL(ctx, p) \
ktest_report((ctx), (p) != NULL, #p " != NULL", __FILE__, __LINE__, NULL)
void ktest_expect_eq(ktest_context_t* ctx, long long a, long long b,
const char* expr, const char* file, int line);
void ktest_expect_streq(ktest_context_t* ctx, const char* a, const char* b,
const char* expr, const char* file, int line);
// Built-in suites covering the core data structures and subsystems.
void ktest_register_builtin_suites(void);
#endif // _KERNEL_KTEST_H