Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,765 changes: 2,918 additions & 2,847 deletions src/cli/cli.c

Large diffs are not rendered by default.

585 changes: 367 additions & 218 deletions src/foundation/platform.c

Large diffs are not rendered by default.

186 changes: 108 additions & 78 deletions src/foundation/platform.h
Original file line number Diff line number Diff line change
@@ -1,78 +1,108 @@
/*
* platform.h — OS abstractions.
*
* Provides cross-platform wrappers for:
* - Memory-mapped files (mmap / VirtualAlloc)
* - High-resolution monotonic clock
* - CPU core count
* - File existence check
*/
#ifndef CBM_PLATFORM_H
#define CBM_PLATFORM_H

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>

/* ── Safe memory ──────────────────────────────────────────────── */

/* Safe realloc: frees old pointer on failure instead of leaking it.
* Returns NULL on allocation failure (old memory is freed). */
static inline void *safe_realloc(void *ptr, size_t size) {
void *tmp = realloc(ptr, size); // NOLINT(clang-analyzer-optin.portability.UnixAPI)
if (!tmp) {
free(ptr);
}
return tmp;
}

/* ── Memory mapping ────────────────────────────────────────────── */

/* Map a file read-only into memory. Returns NULL on error.
* *out_size is set to the file size. */
void *cbm_mmap_read(const char *path, size_t *out_size);

/* Unmap a previously mapped region. */
void cbm_munmap(void *addr, size_t size);

/* ── Timing ────────────────────────────────────────────────────── */

/* Monotonic nanosecond timestamp (for elapsed time measurement). */
uint64_t cbm_now_ns(void);

/* Monotonic millisecond timestamp. */
uint64_t cbm_now_ms(void);

/* ── System info ───────────────────────────────────────────────── */

/* Number of available CPU cores. */
int cbm_nprocs(void);

/* System topology: core types and RAM (only fields with production consumers). */
typedef struct {
int total_cores; /* hw.ncpu (all cores) */
int perf_cores; /* P-cores (Apple) or total_cores (others) */
size_t total_ram; /* total physical RAM in bytes */
} cbm_system_info_t;

/* Query system information. Results are cached after first call. */
cbm_system_info_t cbm_system_info(void);

/* Recommended worker count for parallel indexing.
* initial=true: all cores (user is waiting for initial index)
* initial=false: max(1, perf_cores-1) (leave headroom for user apps) */
int cbm_default_worker_count(bool initial);

/* ── File system ───────────────────────────────────────────────── */

/* Check if a path exists. */
bool cbm_file_exists(const char *path);

/* Check if path is a directory. */
bool cbm_is_dir(const char *path);

/* Get file size. Returns -1 on error. */
int64_t cbm_file_size(const char *path);

#endif /* CBM_PLATFORM_H */
/*
* platform.h — OS abstractions.
*
* Provides cross-platform wrappers for:
* - Memory-mapped files (mmap / VirtualAlloc)
* - High-resolution monotonic clock
* - CPU core count
* - File existence check
*/
#ifndef CBM_PLATFORM_H
#define CBM_PLATFORM_H

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>

/* ── Safe memory ──────────────────────────────────────────────── */

/* Safe realloc: frees old pointer on failure instead of leaking it.
* Returns NULL on allocation failure (old memory is freed). */
static inline void *safe_realloc(void *ptr, size_t size) {
void *tmp = realloc(ptr, size); // NOLINT(clang-analyzer-optin.portability.UnixAPI)
if (!tmp) {
free(ptr);
}
return tmp;
}

/* ── Memory mapping ────────────────────────────────────────────── */

/* Map a file read-only into memory. Returns NULL on error.
* *out_size is set to the file size. */
void *cbm_mmap_read(const char *path, size_t *out_size);

/* Unmap a previously mapped region. */
void cbm_munmap(void *addr, size_t size);

/* ── Timing ────────────────────────────────────────────────────── */

/* Monotonic nanosecond timestamp (for elapsed time measurement). */
uint64_t cbm_now_ns(void);

/* Monotonic millisecond timestamp. */
uint64_t cbm_now_ms(void);

/* ── System info ───────────────────────────────────────────────── */

/* Number of available CPU cores. */
int cbm_nprocs(void);

/* System topology: core types and RAM (only fields with production consumers). */
typedef struct {
int total_cores; /* hw.ncpu (all cores) */
int perf_cores; /* P-cores (Apple) or total_cores (others) */
size_t total_ram; /* total physical RAM in bytes */
} cbm_system_info_t;

/* Query system information. Results are cached after first call. */
cbm_system_info_t cbm_system_info(void);

/* Recommended worker count for parallel indexing.
* initial=true: all cores (user is waiting for initial index)
* initial=false: max(1, perf_cores-1) (leave headroom for user apps) */
int cbm_default_worker_count(bool initial);

/* ── File system ───────────────────────────────────────────────── */

/* Check if a path exists. */
bool cbm_file_exists(const char *path);

/* Check if path is a directory. */
bool cbm_is_dir(const char *path);

/* Get the user's home directory.
* POSIX: returns HOME. Windows: returns HOME, then USERPROFILE.
* Path is normalized to forward slashes.
* Returns static buffer — do NOT free. Returns NULL on failure. */
const char *cbm_home_dir(void);

/* Get the application config directory.
* macOS: returns HOME (caller appends "Library/Application Support/App/...").
* Linux: returns XDG_CONFIG_HOME or HOME/.config (caller appends "app/...").
* Windows: returns APPDATA, e.g. "C:/Users/x/AppData/Roaming" (caller appends "App/...").
* Path is normalized to forward slashes.
* Returns static buffer — do NOT free. Returns NULL on failure. */
const char *cbm_app_config_dir(void);

/* Get the application local data directory.
* macOS/Linux: same as cbm_app_config_dir().
* Windows: returns LOCALAPPDATA, e.g. "C:/Users/x/AppData/Local" (caller appends "App/...").
* Path is normalized to forward slashes.
* Returns static buffer — do NOT free. Returns NULL on failure. */
const char *cbm_app_local_dir(void);

/* Get the absolute path of the currently running executable.
* Returns static buffer — do NOT free. Returns "" on failure. */
const char *cbm_self_exe_path(void);

/* Normalize path separators to forward slashes (in-place).
* On Windows, converts backslashes to forward slashes.
* On POSIX, this is a no-op. Returns the input pointer. */
char *cbm_normalize_path_sep(char *path);

/* Get file size. Returns -1 on error. */
int64_t cbm_file_size(const char *path);

#endif /* CBM_PLATFORM_H */
Loading