estdlib: A Lightweight and Efficient C++ Standard Library Designed for Resource-Constrained Embedded Systems

C++ plays an increasingly important role in embedded system development, but the large size of the Standard Library (STL) and its reliance on dynamic memory often become bottlenecks. What to do? Don’t worry, today we proudly introduce a powerful tool: estdlib! It is a C++ standard library tailored for embedded environments, balancing functionality and efficiency, which will greatly enhance your embedded development experience.

What is estdlib?

estdlib (Embedded Standard Library) is a lightweight and efficient C++ standard library specifically designed for resource-constrained embedded systems. It is inspired by the ETL library, but its design philosophy and implementation differ. The core goal of estdlib is to provide an interface similar to that of the standard library while avoiding the use of virtual functions and dynamic memory allocation, thereby minimizing memory usage and runtime overhead.

Design Philosophy of estdlib: Layered Memory Management

estdlib cleverly adopts a layered memory management strategy, dividing data structures into three layers:

1. Layer 1: Inline Fixed Buffer This layer’s data structures are allocated directly on the stack, avoiding dynamic memory and pointers, resulting in minimal memory usage and maximum efficiency.

2. Layer 2: Fixed Buffer or Pointer + Fixed Size This layer’s data structures use a fixed-size buffer or a pointer pointing to a fixed-size buffer. It also avoids dynamic memory allocation, with moderate memory usage and efficiency.

3. Layer 3: Pointer + Runtime Size This layer’s data structures use pointers and runtime size variables to manage memory, used when dynamic-sized data structures are needed, but still avoids dynamic memory allocation, with slightly lower efficiency than the first two layers.

This layered design allows developers to choose the most suitable memory management scheme based on specific application scenarios, achieving a balance between performance and memory usage.

Core Features of estdlib

estdlib offers a rich set of features, covering many important components of the standard library, all while avoiding virtual functions and dynamic memory allocation:

Character Conversion (charconv)

estdlib provides <span>from_chars</span> and <span>to_chars</span> functions for fast and efficient conversion between integers and strings.

Time Library (Chrono)

estdlib provides time handling and comparison functions similar to <span>std::chrono</span>, and even includes a <span>freertos_clock</span> implementation specifically designed for FreeRTOS.

Function Objects (Functional)

estdlib provides a low-level function object implementation, with initialization and memory management differing from the standard library, but the usage remains consistent.

Locale

estdlib implements locales through templates, supporting multiple languages and character sets, avoiding the overhead of runtime configuration and improving performance. Notably, the underlying implementation of <span>num_get</span> has been modified to a non-blocking mode.

Optional Values (Optional)

estdlib provides an <span>optional</span> class, along with some specialized <span>layer1::optional</span> implementations to further reduce memory usage.

IO Streams

estdlib offers a streamlined version of <span>ostream</span>/<span>istream</span> support, which, while less robust, remains very powerful and has a much lower memory footprint than the standard library. Its underlying structure is based on the <span>streambuf</span> class and has been redesigned for non-blocking mode.

Queues

estdlib provides an efficient circular queue <span>layer1::deque</span>, as well as a complete implementation of a priority queue <span>priority_queue</span>.

Vectors

estdlib provides vectors that support three-layer memory management, allowing for the selection of the appropriate memory allocation method as needed.

Span

estdlib provides <span>span</span> for buffer views, and also includes some <span>const_buffer</span> and <span>mutable_buffer</span> code inspired by Boost.

Tuples

estdlib provides tuples with functionality equivalent to <span>std::tuples</span>, utilizing Empty Base Optimization (EBO) to further reduce memory usage.

Strings

estdlib offers various types of strings, supporting null-terminated and explicit size tracking modes, with different implementations based on memory management layers.

Variadic Templates

estdlib provides a set of variadic template tools, similar to the C# LINQ namespace, which are very useful in scenarios where fold expressions are not supported.

Variant

estdlib provides a complete <span>variant</span> implementation, compatible with C++11.

Conclusion

estdlib brings the convenience of the standard library to embedded C++ development while avoiding many of the drawbacks of the standard library, making it an ideal choice for embedded development. It is lightweight, efficient, and flexible, helping you write more concise and faster code. If you are looking for a C++ standard library suitable for embedded systems, estdlib will be your best choice!

Project Address: https://github.com/malachi-iot/estdlib

Leave a Comment