Skip to content

Standard Library Overview

The SafeC standard library (std/) covers core utilities, collections, allocators, networking, filesystems, DSP, debugging, and security. Each module is a .h (declarations) + .sc (implementation) pair. Include prelude.h to pull in every module at once.

c
#include "prelude.h"

int main() {
    println("Hello from SafeC stdlib!");
    return 0;
}

Module Categories

Core

ModuleHeaderDescription
memmem.hAllocation, deallocation, safe memcpy/memmove/memset/memcmp; cache-line helpers, alignment utilities
ioio.hFormatted output (stdout/stderr), stdin input, buffer formatting
strstr.hString length, comparison, copy, search, tokenisation, duplication
mathmath.hConstants (PI, E, …), float/double math, classification
threadthread.hThreads, mutexes, condition variables, read-write locks
atomicatomic.hLock-free atomic operations (C11 <stdatomic.h> wrappers)

Collections

ModuleHeaderDescription
slicecollections/slice.hBounds-checked fat pointer + generic array functions
veccollections/vec.hDynamic array with push/pop/sort/filter/map
stringcollections/string.hMutable, heap-allocated growable string (30+ methods)
stackcollections/stack.hLIFO stack backed by growable array
queuecollections/queue.hFIFO circular buffer queue
listcollections/list.hDoubly linked list
mapcollections/map.hHash map (open addressing, linear probing)
bstcollections/bst.hUnbalanced binary search tree
btreecollections/btree.hB-tree ordered map (order-4, 256-node pool)
ringbuffercollections/ringbuffer.hSPSC lock-free power-of-two ring buffer
static_collectionscollections/static_vec.hHeader-only zero-heap vec + map macros

Allocators

ModuleHeaderDescription
bumpalloc/bump.hLinear bump-pointer arena; O(1) alloc, reset-only free
slaballoc/slab.hFreelist slab for fixed-size objects; O(1) alloc/dealloc
poolalloc/pool.hFixed-block pool for mixed content; O(1) alloc/free
tlsfalloc/tlsf.hTwo-Level Segregated Fit; O(1) worst-case general heap

Synchronization

ModuleHeaderDescription
spinlocksync/spinlock.hBusy-wait mutual exclusion (__sync_lock_test_and_set)
lockfreesync/lockfree.hWait-free SPSC ring buffer with compiler barriers
tasksync/task.hCooperative round-robin task scheduler
thread_baresync/thread_bare.hPriority-ordered freestanding threads (no OS)

Networking

ModuleHeaderDescription
net-corenet/net_core.hPacketBuf, NetIf, byte-order utilities, IP/MAC helpers
ethernetnet/ethernet.hEthernetHdr, eth_parse, eth_build
arpnet/arp.hArpTable (16-entry FIFO), arp_build_packet, arp_parse_packet
ipv4net/ipv4.hIpv4Hdr, Internet checksum, ipv4_parse, ipv4_build
ipv6net/ipv6.hIpv6Addr/Ipv6Hdr, link-local/loopback predicates, ipv6_frame
udpnet/udp.hUdpHdr, udp_parse, udp_build, udp_frame
tcpnet/tcp.hTcpConn 10-state machine, pseudo-header checksum
dnsnet/dns.hA-record query builder + reply parser (label compression)
dhcpnet/dhcp.hDhcpClient DORA handshake

Filesystems

ModuleHeaderDescription
blockfs/block.hBlockDevice driver interface (function pointer–based)
partitionfs/partition.hMBR partition table parser (4 primary entries)
vfsfs/vfs.hVFS with longest-prefix mount routing; VfsNode forwarding
fatfs/fat.hFAT32 read-only driver; 8.3 path walk, cluster chain
extfs/ext.hext2 read-only driver; inode walk, direct-block reads
tmpfsfs/tmpfs.hIn-memory FS; 32 inodes, 64 KiB data pool; full CRUD

DSP & Real-Time

ModuleHeaderDescription
fixeddsp/fixed.hQ16.16 fixed-point arithmetic (newtype Fixed = int)
dspdsp/dsp.hdsp_dot, dsp_scale, dsp_moving_avg, dsp_iir_lp, dsp_rms
audio_bufferdsp/audio_buffer.hMulti-channel SPSC audio ring buffer (interleaved Fixed frames)
timer_wheeldsp/timer_wheel.h256-slot O(1) timer wheel; one-shot + periodic

Security & Cryptography

ModuleHeaderDescription
aescrypto/aes.hAES-128/256 ECB + CBC; full S-box + key expansion
sha256crypto/sha256.hSHA-256/224; streaming and one-shot API
rngcrypto/rng.hChaCha20 CSPRNG; rdrand//dev/urandom seeding
secure_alloccrypto/secure_alloc.hSlab allocator with zeroing-on-free
x509crypto/x509.hX.509 DER/ASN.1 parser; SAN, wildcard hostname, validity
tlscrypto/tls.hTLS 1.3 record layer; AES-CBC + PKCS#7 + nonce XOR seq

Debugging & Profiling

ModuleHeaderDescription
perfdebug/perf.hArch-dispatched cycle counter (RDTSC/cntvct_el0/CSR); ns calibration
coveragedebug/coverage.h1024-site coverage tracker; COV_SITE() macro; report()
jtagdebug/jtag.hdebug_break per arch; ARM/AArch64 semihosting; ITM ports

C Compatibility Headers

HeaderDescription
assert.hRuntime assertions (runtime_assert, assert_true); NDEBUG support
ctype.hCharacter classification (char_is_alpha, char_is_digit, ...) and conversion
stdckdint.hChecked integer arithmetic (C23-style ckd_add/ckd_sub/ckd_mul)
stdint.hFixed-width integer types
stddef.hsize_t, NULL, offsetof
stdbool.hBoolean constants
limits.hInteger limits
float.hFloating-point limits
inttypes.hFormat macros for fixed-width types

Generic Pattern

The SafeC compiler does not support generic structs. Collections use void* structs for the underlying data structure, with generic<T> wrapper functions for type-safe access. T is inferred from T* arguments at the call site via monomorphization.

c
#include "collections/vec.h"

int main() {
    struct Vec v = vec_new(sizeof(int));

    // Type-erased API
    int x = 42;
    vec_push(&v, &x);

    // Generic typed wrapper — T inferred from int* argument
    vec_push_t(&v, 100);
    int* p = vec_at(&v, 0);  // returns int*

    vec_free(&v);
    return 0;
}

Including the Standard Library

Individual modules:

c
#include "mem.h"
#include "io.h"
#include "collections/vec.h"

All modules at once:

c
#include "prelude.h"

When building with the safeguard package manager, the standard library is automatically compiled to build/deps/libsafec_std.a and linked.

When building manually, pass the std directory with -I:

bash
./build/safec myfile.sc -I /path/to/SafeC/std --emit-llvm -o myfile.ll
clang myfile.ll -o myfile

Released under the MIT License.