Overview

The FreeWorld OS C++ Standard Library provides a comprehensive implementation of the C++ standard library, including core runtime support, STL containers, I/O streams, smart pointers, and utility functions. The library is designed to be compatible with C++17 standards and provides both assembly-level core runtime and C++ template-based implementations.

Note: The C++ standard library builds upon the C library (glibc equivalent) for low-level operations, while providing high-level C++ abstractions for modern C++ development.

Core Runtime (Assembly)

The core runtime components are implemented in assembly for maximum performance and direct kernel integration:

Memory Management

  • operator new/delete - Single object allocation/deallocation
  • operator new[]/delete[] - Array allocation/deallocation
  • nothrow variants - Exception-safe allocation

Exception Handling

  • __cxa_allocate_exception - Allocate exception object
  • __cxa_throw - Throw exception
  • __cxa_begin_catch - Begin exception handling
  • __cxa_end_catch - End exception handling
  • __cxa_rethrow - Rethrow exception
  • std::terminate - Terminate handler

Runtime Type Information (RTTI)

  • __dynamic_cast - Dynamic type casting
  • type_info - Type information
  • Type comparison - Type equality checking

STL Containers

Standard Template Library containers provide efficient data structures:

std::vector

Dynamic array with automatic resizing:

  • Random access iterators
  • Automatic capacity management
  • Element insertion/erasure
  • Move semantics support
  • Initializer list support
#include <vector>
#include <iostream>

std::vector<int> vec = {1, 2, 3, 4, 5};
vec.push_back(6);
for (int val : vec) {
    std::cout << val << " ";
}

std::string

String class with comprehensive functionality:

  • Dynamic string management
  • Substring operations
  • String concatenation
  • Search and replace
  • C-string compatibility
#include <string>

std::string str = "Hello";
str += " World";
std::string substr = str.substr(0, 5);  // "Hello"

I/O Streams

Input/output stream library for formatted I/O:

std::ostream / std::istream

  • Formatted output with << operator
  • Formatted input with >> operator
  • Type-safe I/O operations
  • Stream manipulators (endl, flush)

Standard Streams

  • std::cout - Standard output
  • std::cerr - Standard error
  • std::cin - Standard input
#include <iostream>

std::cout << "Hello, World!" << std::endl;
int value;
std::cin >> value;

Smart Pointers

Automatic memory management with RAII:

std::unique_ptr

  • Exclusive ownership
  • Automatic deletion
  • Move semantics
  • Array specialization

std::shared_ptr

  • Shared ownership
  • Reference counting
  • Thread-safe reference counting
  • Automatic cleanup when last reference is destroyed

std::weak_ptr

  • Non-owning reference
  • Break circular references
  • Lock to shared_ptr
#include <memory>

// unique_ptr
auto ptr = std::make_unique<int>(42);

// shared_ptr
auto shared = std::make_shared<int>(42);
auto weak = std::weak_ptr<int>(shared);

Utility Functions

General-purpose utility functions and classes:

std::pair

  • Two-element tuple
  • Comparison operators
  • make_pair helper

std::move / std::forward

  • std::move - Convert to rvalue reference
  • std::forward - Perfect forwarding
  • Enable move semantics

std::swap

  • Generic swap function
  • Specialized for types with swap member
  • Efficient value exchange
#include <utility>

auto p = std::make_pair(1, "hello");
int a = 10, b = 20;
std::swap(a, b);  // a=20, b=10

Type Traits

Compile-time type information and transformations:

Type Modifications

  • remove_const - Remove const qualifier
  • remove_volatile - Remove volatile qualifier
  • remove_reference - Remove reference
  • remove_pointer - Remove pointer
  • decay - Array/function to pointer

Type Properties

  • is_integral - Check if integral type
  • is_floating_point - Check if floating point
  • is_pointer - Check if pointer
  • is_reference - Check if reference
  • is_class - Check if class type
  • is_same - Check if types are same

Conditional Compilation

  • enable_if - SFINAE helper
  • conditional - Conditional type selection
#include <type_traits>

static_assert(std::is_integral_v<int>);
static_assert(std::is_same_v<std::remove_const_t<const int>, int>);

Build System

The C++ standard library is built using the Makefile in libc++/:

cd libc++
make

This produces lib64/libc++.a, a static library containing:

  • Core runtime functions (assembly)
  • Exception handling infrastructure
  • RTTI support

Header files are located in:

  • libc++/stl/containers/ - STL containers
  • libc++/string/ - String class
  • libc++/io/ - I/O streams
  • libc++/memory/ - Smart pointers
  • libc++/utility/ - Utility functions
  • libc++/type_traits/ - Type traits

Usage Example

Complete example demonstrating C++ standard library usage:

#include <vector>
#include <string>
#include <memory>
#include <iostream>
#include <utility>

int main() {
    // Vector
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    numbers.push_back(6);
    
    // String
    std::string message = "Hello";
    message += " World!";
    
    // Smart pointer
    auto ptr = std::make_unique<std::string>("Test");
    
    // Output
    std::cout << message << std::endl;
    std::cout << "Vector size: " << numbers.size() << std::endl;
    
    return 0;
}

Integration with C Library

The C++ standard library integrates with the C library (glibc equivalent) for:

  • Memory allocation (malloc/free)
  • I/O operations (stdio functions)
  • String operations (C string functions)
  • Mathematical functions

This ensures compatibility and efficient resource sharing between C and C++ code.

Future Enhancements

Additional components planned for future implementation:

  • Additional STL containers (list, map, set, deque, etc.)
  • STL algorithms (sort, find, transform, etc.)
  • File streams (fstream, ifstream, ofstream)
  • String streams (sstream)
  • Threading support (thread, mutex, condition_variable)
  • Filesystem support (C++17 filesystem)
  • Regular expressions (regex)
  • Chrono library (time utilities)