Menu

Infrastructure and Utility Libraries

Relevant source files

This page documents the foundational infrastructure and utility libraries that underpin the fastfetch codebase. These libraries provide the core data structures and cross-platform abstractions that are used throughout all major systems, including detection, configuration, and output.

For details on how these utilities are used in platform abstraction, see Cross-Platform Abstraction Patterns. For their role in the format string system, see Format String System.

Overview

The infrastructure layer consists of:

  • FFstrbuf: A dynamic string buffer supporting both static and dynamic memory models, with a comprehensive API for string manipulation.
  • FFlist: A generic, type-agnostic dynamic array with type-safe macros for element access and iteration.
  • Platform Utilities: Cross-platform abstractions for file I/O, process management, and environment access, primarily in src/util/platform/FFPlatform.c and related files.
  • Memory Management Patterns: Consistent allocation, growth, and cleanup strategies, including support for automatic cleanup via GCC's __attribute__((__cleanup__)).

These utilities are designed for performance, safety, and portability, and are used by all major subsystems in fastfetch.

Sources:
src/util/FFstrbuf.h src/util/FFstrbuf.c src/util/FFlist.h src/util/FFlist.c src/util/platform/FFPlatform.c src/fastfetch.h src/common/init.c

FFstrbuf: Dynamic String Buffer

Structure and Memory Model

FFstrbuf is a dynamic string buffer that supports both static (non-owning) and dynamic (owning) memory models. It is used for all string manipulation in fastfetch, including module output, configuration parsing, and format string processing.

Diagram: "FFstrbuf Structure and Modes"

Sources:
src/util/FFstrbuf.h27-32 src/util/FFstrbuf.c7 src/util/FFstrbuf.c31-56

The allocated == 0 convention is used to indicate a static string, which allows zero-copy operations on string literals until a modification is required.

Initialization and Lifecycle

Diagram: "FFstrbuf Initialization and Lifecycle"

Sources:
src/util/FFstrbuf.h34-36 src/util/FFstrbuf.h269-275 src/util/FFstrbuf.h284-308 src/util/FFstrbuf.h190-205

The FF_STRBUF_AUTO_DESTROY macro enables automatic cleanup of buffers at scope exit using GCC's __cleanup__ attribute.

String Operations

FFstrbuf provides a comprehensive set of string manipulation functions, including append, prepend, insert, substring, search, trim, replace, comparison, and transformation.

CategoryKey FunctionsDescription
AppendffStrbufAppendC, ffStrbufAppendS, ffStrbufAppendNS, ffStrbufAppendFAdd to end
PrependffStrbufPrependC, ffStrbufPrependNSAdd to beginning
InsertffStrbufInsertNCInsert at position
SubstringffStrbufSubstrBefore, ffStrbufSubstrAfter, ffStrbufSubstrExtract portions
SearchffStrbufFirstIndexC, ffStrbufNextIndexS, ffStrbufCountCFind characters/strings
TrimffStrbufTrimLeft, ffStrbufTrimRight, ffStrbufTrimLeftSpace, ffStrbufTrimRightSpaceRemove whitespace/chars
ReplaceffStrbufReplaceAllC, ffStrbufRemoveS, ffStrbufRemoveSubstrModify content
ComparisonffStrbufEqual, ffStrbufStartsWith, ffStrbufEndsWith, ffStrbufIgnCaseCompCompare strings
TransformffStrbufUpperCase, ffStrbufLowerCase, ffStrbufAppendTransformSChange case

Sources:
src/util/FFstrbuf.h43-87 src/util/FFstrbuf.c95-268 src/util/FFstrbuf.c317-503

Static String Handling

Static string optimization is a key feature of FFstrbuf. When initialized with a string literal, no memory is allocated until a write operation occurs.

Diagram: "FFstrbuf Static to Dynamic Transition"

Sources:
src/util/FFstrbuf.c235-246 src/util/FFstrbuf.c260-268 src/util/FFstrbuf.c282-293 tests/strbuf.c260-372

Numeric Conversions and yyjson Integration

FFstrbuf integrates with yyjson for efficient numeric formatting and parsing, avoiding temporary allocations.

Diagram: "FFstrbuf Numeric Formatting and Parsing"

Sources:
src/util/FFstrbuf.c546-625 src/util/FFstrbuf.c525-544 src/util/FFstrbuf.h13-17

Advanced Features

Separated String Matching

FFstrbuf provides functions for working with separator-delimited strings, such as those found in environment variables or module lists.

Diagram: "FFstrbuf Separated String Matching"

Sources:
src/util/FFstrbuf.c733-785 src/util/FFstrbuf.c824-856 tests/strbuf.c654-708

UTF-32 Codepoint Support

  • int ffStrbufAppendUtf32CodePoint(FFstrbuf* strbuf, uint32_t codepoint) appends a UTF-32 codepoint as UTF-8, supporting internationalization and logo rendering.

Sources:
src/util/FFstrbuf.c787-817 tests/strbuf.c757-763

Line-by-Line Iteration

  • ffStrbufGetline() and ffStrbufGetlineRestore() allow efficient iteration over multi-line buffers without copying.

Sources:
src/util/FFstrbuf.c663-709 tests/strbuf.c478-582

FFlist: Generic Dynamic Array

Structure and Memory Model

FFlist is a generic, type-agnostic dynamic array used for storing collections of arbitrary structs (e.g., GPU results, module lists).

Diagram: "FFlist Structure and Growth"

Sources:
src/util/FFlist.h12-18 src/util/FFlist.h10 src/util/FFlist.c6-17

Type-Safe Macros

FFlist uses macro wrappers to provide type safety for generic storage and iteration.

Diagram: "FFlist Type-Safe Macro Usage"

Sources:
src/util/FFlist.h109-128 src/util/FFlist.h50-54 src/util/FFlist.c6-17

Example usage is found throughout detection and module code, e.g., for storing GPU results or module configurations.

Operations Reference

OperationFunction/MacroDescriptionTime Complexity
InitializeffListInit(list, elementSize)Create empty listO(1)
AddffListAdd(list)Append element, return pointerO(1) amortized
GetffListGet(list, index)Access element by indexO(1)
ShiftffListShift(list, result)Remove and return first elementO(n)
PopffListPop(list, result)Remove and return last elementO(1)
SortffListSort(list, compar)Sort using qsortO(n log n)
FindffListFirstIndexComp(list, elem, cmp)Linear searchO(n)
ContainsffListContains(list, elem, cmp)Check membershipO(n)
ClearffListClear(list)Reset length to 0O(1)
DestroyffListDestroy(list)Free memoryO(1)

Sources:
src/util/FFlist.h20-108 src/util/FFlist.c19-38

Memory Management Patterns

Automatic Cleanup with GCC Attributes

Both FFstrbuf and FFlist support automatic cleanup using GCC's __attribute__((__cleanup__)), preventing memory leaks even in early returns or exceptions.

Diagram: "Automatic Resource Management"

Sources:
src/util/FFstrbuf.h602 src/util/FFlist.h115 tests/strbuf.c255-257 tests/list.c118-122

Growth and Reallocation Strategies

Both data structures use exponential growth to amortize allocation costs.

Diagram: "FFstrbuf and FFlist Growth"

Sources:
src/util/FFstrbuf.c31-56 src/util/FFstrbuf.c59-81 src/util/FFlist.c6-17 tests/strbuf.c390-450

Move Semantics

Both structures support efficient transfer of ownership:

  • ffStrbufInitMove(&dest, &src) moves buffer contents, leaving src empty.
  • ffListInitMove(&destList, &srcList) moves list contents, leaving srcList empty.

Sources:
src/util/FFstrbuf.h141-152 src/util/FFlist.h78-92

Integration with External Libraries

yyjson for Number Formatting

FFstrbuf uses yyjson's optimized number formatting for appending integers and doubles.

Diagram: "FFstrbuf Number Append Flow"

Sources:
src/util/FFstrbuf.c546-625

Key advantages:

  • No temporary string allocation
  • Direct buffer writing
  • Configurable precision for doubles

JSON Value Conversion

ffStrbufSetJsonVal(&strbuf, jsonVal) reads a JSON string value directly into FFstrbuf with zero-copy, when possible.

Sources:
src/util/FFstrbuf.h228-236 src/util/FFstrbuf.h263-267

Usage Patterns in Fastfetch

Detection Module Pattern

Detection modules use FFstrbuf and FFlist for result collection and aggregation.

Diagram: "Detection Module Data Flow"

Sources:
Pattern observed throughout detection modules

Format String Processing

The format string system uses FFstrbuf for template parsing and output construction.

Diagram: "Format String Processing"

Sources:
Pattern from format string system usage

Configuration Parsing

The configuration system uses FFlist for module lists and FFstrbuf for string fields in options.

Diagram: "Configuration Parsing Data Flow"

Sources:
Configuration parsing patterns throughout codebase

Performance Characteristics

Space Complexity

Data StructureEmpty (struct only)Small (default alloc)Large (expanded)
FFstrbuf12 bytes12 + 32 bytes12 + allocated
FFlist20 bytes20 + 16*elementSize20 + capacity*elementSize

Assumes 64-bit pointers.

Time Complexity

OperationFFstrbufFFlist
Append/AddO(1) amortizedO(1) amortized
PrependO(n)N/A
Get/IndexO(1)O(1)
SearchO(n)O(n)
SubstringO(1) static, O(n) dynamicN/A
ClearO(1)O(1)
DestroyO(1)O(1)

Optimization Strategies

  • Static string optimization: delay allocation for read-only operations.
  • Exponential growth: minimize reallocation overhead.
  • In-place operations: many operations modify without copying.
  • yyjson integration: zero-copy numeric formatting.
  • Type-safe macros: no runtime overhead for type safety.

Sources:
Implementation analysis, src/util/FFstrbuf.h src/util/FFlist.h

Testing

Both data structures have comprehensive test suites:

  • tests/strbuf.c: 864 lines testing all FFstrbuf operations
  • tests/list.c: 127 lines testing all FFlist operations

Test coverage includes:

  • Basic operations (append, prepend, insert, remove)
  • Edge cases (empty strings, static strings, zero-length operations)
  • Memory management (allocation, reallocation, cleanup)
  • Static string transitions (static → dynamic conversions)
  • Numeric conversions (parsing and formatting)
  • Advanced features (UTF-32, separated strings, line iteration)

Sources: tests/strbuf.c1-864 tests/list.c1-127