Skip to main content

Overview

AK (short for Andreas Kling) is SerenityOS’s custom C++ standard library implementation. It provides a comprehensive set of modern, memory-safe data structures and utilities that replace the C++ STL throughout the codebase. AK contains 186+ header files offering containers, smart pointers, string handling, error handling, and more.
AK is designed to be used both in userspace and the kernel (with appropriate restrictions). Headers that cannot be used in the kernel are explicitly marked.

Core Containers

Vector

Dynamic array container with inline capacity optimization.
size_t
default:"0"
Number of elements to store inline before allocating on heap. Default is 0 (always use heap).
Key Methods:
  • append(T) - Add element to end
  • prepend(T) - Add element to beginning
  • ensure_capacity(size_t) - Pre-allocate space
  • size() - Get current size
  • is_empty() - Check if empty
  • clear() - Remove all elements
  • at(index) - Bounds-checked access
  • operator[] - Direct access

HashMap

Hash table mapping keys to values with optional ordering.
Key Methods:
  • set(K key, V value) - Insert or update
  • try_set(K, V) - ErrorOr version
  • get(K key) - Returns Optional<V>
  • contains(K key) - Check existence
  • remove(K key) - Remove entry
  • size() - Number of entries
  • clear() - Remove all entries

HashTable

Hash set for storing unique values.

String Types

String

Modern, UTF-8 encoded, immutable string type (reference counted).
Short String Optimization: Strings up to MAX_SHORT_STRING_BYTE_COUNT bytes are stored inline without heap allocation.
Key Methods:
  • from_utf8(StringView) - Create from UTF-8 bytes (validates encoding)
  • from_utf8_without_validation() - Skip validation for trusted input
  • from_code_point(u32) - Create from single Unicode code point
  • repeated(String, size_t) - Repeat string N times
  • to_lowercase() - Convert to lowercase (requires LibUnicode)
  • to_uppercase() - Convert to uppercase (requires LibUnicode)
  • to_ascii_lowercase() - ASCII-only lowercase
  • to_ascii_uppercase() - ASCII-only uppercase
  • bytes_as_string_view() - View as StringView

StringView

Non-owning view into a string (like std::string_view).

StringBuilder

Efficient string building without repeated allocations.

Smart Pointers

RefPtr & NonnullRefPtr

Reference-counted smart pointers for heap-allocated objects.
  • NonnullRefPtr: Guaranteed non-null, no overhead checking
  • RefPtr: Can be null, use when optional ownership needed
  • Both use reference counting for automatic memory management
  • Objects must inherit from RefCounted<T> or implement ref() and unref()

OwnPtr & NonnullOwnPtr

Unique ownership smart pointers (like std::unique_ptr).

Error Handling

Error & ErrorOr

Modern error handling without exceptions.

Optional

Type-safe nullable values.

Utilities

Span

Non-owning view over contiguous memory.
Type Aliases:
  • Bytes = Span<u8>
  • ReadonlyBytes = Span<u8 const>

Format

Type-safe string formatting.

ByteBuffer

Dynamic byte array.

Bitmap & BitmapView

Efficient bit manipulation.

Functional Programming

Function

Type-safe function wrapper (like std::function).

Algorithms

Functional-style algorithms.

Memory & Performance

Traits

Customize type behavior (hashing, comparison, etc.).

Atomic

Atomic operations for lock-free programming.

Best Practices

  • String: Owned, immutable strings. Use for storage and return values.
  • StringView: Non-owning views. Use for function parameters.
  • StringBuilder: Building strings incrementally. Use when concatenating.
  • ByteString: Deprecated, avoid in new code.
  • NonnullRefPtr: Shared ownership, cannot be null
  • RefPtr: Shared ownership, can be null
  • NonnullOwnPtr: Exclusive ownership, cannot be null
  • OwnPtr: Exclusive ownership, can be null
  • LibC - POSIX C library integration
  • LibCore - Core utilities built on AK
  • Kernel API - Kernel-safe AK subset