Skip to content

Add centrallix-lib Utilities#130

Open
Lightning11wins wants to merge 16 commits into
masterfrom
add-utils
Open

Add centrallix-lib Utilities#130
Lightning11wins wants to merge 16 commits into
masterfrom
add-utils

Conversation

@Lightning11wins

Copy link
Copy Markdown
Contributor

This PR adds a more organized version of the utilities I created while working on #77, which have since been used in several other projects (which are now blocked by this PR).

@Lightning11wins Lightning11wins self-assigned this Jun 26, 2026
@Lightning11wins Lightning11wins added ai-review Request AI review for PRs. size: trivial Easy to review, probably ~100 lines or fewer. labels Jun 26, 2026
@greptile-apps

greptile-apps Bot commented Jun 26, 2026

Copy link
Copy Markdown

Greptile Summary

This PR adds four new utility modules to centrallix-lib — a diagnostic check macro set (check.h/check.c), a monotonic wall-clock timer (timer.h/timer.c), numerical range helpers (range.h/range.c), and byte/comma formatting functions in util.c — along with the necessary Makefile changes to include them in both the static and shared library builds.

  • check.h: GNU statement-expression macros (check, check_neg, check_double, check_ptr) that capture and print diagnostics on failure; previously flagged issues (missing includes, multi-statement macro guards) are resolved.
  • timer.c: Accumulating monotonic timer using NAN as a sentinel for the unstarted state; previous bugs (accumulation vs. assignment, clock_gettime unchecked return, NAN poisoning, NULL guard) are addressed.
  • util.c: Adds snprint_bytes (human-readable byte sizes), snprint_commas_llu (comma-formatted integers), and fprint_mem (RSS memory report via /proc/self/statm).

Confidence Score: 5/5

Safe to merge; all changes are additive utility code with no modifications to existing logic paths.

The new modules are self-contained and only add code; no existing functionality is altered. The two findings are style-level issues (a missing do-while(0) on a debug-only macro, and duplicated cleanup in fprint_mem) that do not affect correctness.

centrallix-lib/src/util.c and centrallix-lib/include/timer.h are the two files with style findings worth a second look before merge.

Important Files Changed

Filename Overview
centrallix-lib/src/util.c Adds snprint_bytes, snprint_commas_llu, and fprint_mem utilities; logic is correct but fprint_mem duplicates fclose cleanup across two branches instead of using goto error as required by the team's style rule.
centrallix-lib/include/timer.h Declares Timer struct and API; timer_benchmark macro uses bare braces instead of do-while(0), making it unsafe in unbraced if-else contexts.
centrallix-lib/src/timer.c Timer implementation with correct start/stop/accumulation logic, null guards, clock_gettime error handling, and NAN sentinel for unstarted state; no issues found.
centrallix-lib/include/check.h Diagnostic check macros with correct includes (errno.h, math.h, string.h) and GNU statement expressions for safe result capture; no new issues.
centrallix-lib/include/range.h min/max/clamp macros guarded with #ifndef to prevent redefinition collisions; uses typeof for type-safe expansion.
centrallix-lib/src/check.c Implements print_err_internal; correctly dispatches between perror, error-code, and generic stderr output paths.
centrallix-lib/src/range.c Stub translation unit that only includes range.h (all logic is macro-based in the header); no issues.
centrallix-lib/include/util.h Adds declarations for snprint_bytes, snprint_commas_llu, and fprint_mem with correct types; includes stdio.h for FILE*.
centrallix-lib/Makefile.in Adds timer.o/lo, check.o/lo, range.o/lo to both XSTATICFILES and XDYNAMICFILES; correct and symmetric.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Caller] -->|timer_new| B[nmMalloc + timer_init]
    A -->|timer_start| C{isnan start?}
    C -->|yes| D[start = get_time]
    C -->|no| E[noop - already running]
    A -->|timer_stop| F{isnan start?}
    F -->|yes| G[noop - not running]
    F -->|no| H[total += get_time - start\nstart = NAN]
    A -->|timer_get| I{isnan start?}
    I -->|yes| J[return total]
    I -->|no| K[return get_time - start + total]
    A -->|timer_free| L[NULL guard → timer_de_init → nmFree]

    subgraph get_time
        M[clock_gettime CLOCK_MONOTONIC] -->|failure| N[return NAN]
        M -->|success| O[return tv_sec + tv_nsec / 1e9]
    end

    D --> get_time
    H --> get_time
    K --> get_time
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A[Caller] -->|timer_new| B[nmMalloc + timer_init]
    A -->|timer_start| C{isnan start?}
    C -->|yes| D[start = get_time]
    C -->|no| E[noop - already running]
    A -->|timer_stop| F{isnan start?}
    F -->|yes| G[noop - not running]
    F -->|no| H[total += get_time - start\nstart = NAN]
    A -->|timer_get| I{isnan start?}
    I -->|yes| J[return total]
    I -->|no| K[return get_time - start + total]
    A -->|timer_free| L[NULL guard → timer_de_init → nmFree]

    subgraph get_time
        M[clock_gettime CLOCK_MONOTONIC] -->|failure| N[return NAN]
        M -->|success| O[return tv_sec + tv_nsec / 1e9]
    end

    D --> get_time
    H --> get_time
    K --> get_time
Loading

Reviews (5): Last reviewed commit: "Fix incorrect type format specifier." | Re-trigger Greptile

Comment thread centrallix-lib/include/check.h
Comment thread centrallix-lib/include/check.h
Comment thread centrallix-lib/src/timer.c
Comment thread centrallix-lib/src/timer.c
Comment thread centrallix-lib/src/timer.c
Comment thread centrallix-lib/src/util.c Outdated
Comment thread centrallix-lib/include/range.h
Comment thread centrallix-lib/src/timer.c Outdated
Comment thread centrallix-lib/include/check.h
Comment thread centrallix-lib/src/util.c Outdated
@Lightning11wins

Copy link
Copy Markdown
Contributor Author

This PR is ready for human review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-review Request AI review for PRs. size: trivial Easy to review, probably ~100 lines or fewer.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant