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).
append(T)- Add element to endprepend(T)- Add element to beginningensure_capacity(size_t)- Pre-allocate spacesize()- Get current sizeis_empty()- Check if emptyclear()- Remove all elementsat(index)- Bounds-checked accessoperator[]- Direct access
HashMap
Hash table mapping keys to values with optional ordering.set(K key, V value)- Insert or updatetry_set(K, V)- ErrorOr versionget(K key)- Returns Optional<V>contains(K key)- Check existenceremove(K key)- Remove entrysize()- Number of entriesclear()- 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.from_utf8(StringView)- Create from UTF-8 bytes (validates encoding)from_utf8_without_validation()- Skip validation for trusted inputfrom_code_point(u32)- Create from single Unicode code pointrepeated(String, size_t)- Repeat string N timesto_lowercase()- Convert to lowercase (requires LibUnicode)to_uppercase()- Convert to uppercase (requires LibUnicode)to_ascii_lowercase()- ASCII-only lowercaseto_ascii_uppercase()- ASCII-only uppercasebytes_as_string_view()- View as StringView
StringView
Non-owning view into a string (likestd::string_view).
StringBuilder
Efficient string building without repeated allocations.Smart Pointers
RefPtr & NonnullRefPtr
Reference-counted smart pointers for heap-allocated objects.RefPtr vs NonnullRefPtr
RefPtr vs NonnullRefPtr
- 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 implementref()andunref()
OwnPtr & NonnullOwnPtr
Unique ownership smart pointers (likestd::unique_ptr).
Error Handling
Error & ErrorOr
Modern error handling without exceptions.Optional
Type-safe nullable values.Utilities
Span
Non-owning view over contiguous memory.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 (likestd::function).
Algorithms
Functional-style algorithms.Memory & Performance
Traits
Customize type behavior (hashing, comparison, etc.).Atomic
Atomic operations for lock-free programming.Best Practices
When to use each string type
When to use each string type
- 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.
Smart pointer selection
Smart pointer selection
- NonnullRefPtr: Shared ownership, cannot be null
- RefPtr: Shared ownership, can be null
- NonnullOwnPtr: Exclusive ownership, cannot be null
- OwnPtr: Exclusive ownership, can be null
Error handling patterns
Error handling patterns
Related APIs
- LibC - POSIX C library integration
- LibCore - Core utilities built on AK
- Kernel API - Kernel-safe AK subset
