M*LIB: Generic type-safe Container Library for C language
- Overview
- Components
- Build & Installation
- How to use
- Performance
- OPLIST
- Memory Allocation
- Emplace construction
- Errors & compilers
- External Reference
- API Documentation
- Common Interface
- Sequence containers
- Array
- List
- Intrusive list
- Double end queue
- Fixed size queue / stack
- Associative containers
- Dictionary
- Red/Black Tree
- B+ Tree
- Misc containers
- Priority queue
- Generic Tree
- Tuple
- Variant
- Thread containers
- Shared Fixed size queue
- Atomic Shared Register
- Shared pointers
- Worker threads
- Dataset
- String
- Byte String
- Bitset
- Algorithms
- Generic algorithms
- Function objects
- Serialization
- JSON Serialization
- Binary Serialization
- Uniform interface
- Core preprocessing
- C11 compatibility headers
- Thread
- Atomic
- Global User Customization
- Exception handling
- Memory context Customization
- License
Overview
M*LIB (M star lib) is a C library enabling to define and to use generic and
type safe container in C, aka handling generic
containers in pure C language.
The encapsulated objects can have their own constructor / copy / move / destructor operators
or can be basic C type like the C type 'int': both are fully supported.
This makes it possible to construct fully
recursive container objects (container-of[...]-container-of-type-T)
while keeping compile time type checking.
This is an equivalent of the C++
Standard Library,
providing vector, deque, forward_list, set, map, multiset, multimap,
unordered_set, unordered_map, stack, queue, shared_ptr, string, variant, option
to standard ISO C99 / C11.
There is not a strict mapping as both the STL and M*LIB have their exclusive containers:
See here for details.
M*LIB provides also additional concurrent containers to design properly
multi-threaded programs: shared register, communication queue, ...
M*LIB is portable to any systems that support ISO C99.
Some optional features need at least ISO C11.
M*LIB is only composed of a set of headers.
There is no C file, and as such, the installation is quite simple:
you just have to put the header in the search path of your compiler,
and it will work.
There is no dependency (except some other headers of M*LIB and the LIBC).
One of M*LIB design key is to ensure safety. This is done by multiple means:
- in debug mode, defensive programming is extensively used:
the contracts of the function are checked, ensuring
that the data are not corrupted. For example, strict
Buffer overflow are checked in this mode
through bound checking
or the intrinsic properties of a Red-Black tree (for example) are verified.
Buffer overflow checks can still be kept in release mode if needed.
- as few cast as possible are used within the library (casts are the evil of safety).
Still the library can be used with the greatest level of warnings by a C compiler without
any aliasing warning.
- the genericity is not done directly by macro (which usually prevent type safety), but indirectly by making them
define inline functions with the proper prototypes: this enables
the user calls to have proper error and warning checks.
- extensive testing: the library is tested on the main targets using Continuous Integration with a coverage of the test suite of more than 99%.
The test suite itself is run through the multiple sanitizers defined by GCC/CLANG (Address, undefined, leak, thread).
The test suite also includes a comparison of equivalent behaviors of M*LIB with the C++ STL using random testing or fuzzer testing.
- static analysis: multiple static analyzer (like scan-build or GCC fanalyzer or CodeQL) are run on the generated code, and the results analyzed.
Other key designs are:
- do not rewrite the C library and just wrap around it (for example don't rewrite sort but stable sort),
- do not make users pay the cost of what they don't need.
Due to the unfortunate weak nature of the C language for pointers,
type safe means that at least a warning
is generated by the compiler in case of wrong type passed as
container arguments to the functions.
The containers have exclusive ownership of the objects they encapsulate. The objects can be pushed in the container:
- by creating a copy of the data (default behavior -
INIT_SET),
- by stealing as much from the given source (move semantics -
INIT_MOVE),
- by constructing the object directly in the container using its own constructor (emplace methods -
EMPLACE)
M*LIB is still quite-efficient:
there is no overhead in using this library rather than using
direct C low-level access as the compiler is able to fully optimize
the library usage
and the library is carefully designed.
In fact, M*LIB
is one of the fastest generic C/C++ library you can find.
By default, M*LIB uses the malloc, realloc and free functions to handle
the memory pool.
This behavior can be customized to your heart content (fully supporting arena allocator).
Its default policy is to abort the program if there is a memory error: this behavior can also be customized globally.
M*LIB supports also the exception error model by providing its own implementation of the try / catch mechanism.
This mechanism is compatible with RAII programming:
when an exception is thrown, the destructors of the constructed objects are called (See m-try for more details).
M*LIB may use a lot of assertions and contract checking in its implementation to ensure robustness:
it is highly recommended to properly define NDEBUG for released programs.
M*LIB provides automatically several serialization methods for each containers.
You can read or write your full and complex data structure into JSON format in a few lines.
M*LIB is distributed under BSD-2 simplified license.
You can look at the commented examples to get along with how to use the library. You'll see by yourself how easy is to write code using it.
Even if writing the library itself is hard (as the code is quite complex and uses a lot of tricks - pretty much like the STL itself),
I can ensure you that using it is very natural and simple, and the learning curve low.
In this documentation,
- shall will be used to indicate a user constraint that is
mandatory to follow under penalty of undefined behavior.
- should will be used to indicate a recommendation to the user.
All pointers expected by the functions of the library shall expect non-null argument except if indicated.
Key points
- Genericity Mechanism through code generation by macro-base approaches while keeping code size reasonable,
- Minimum supported C Standard is C99,
- Header only library (easier to integrate),
- Performance: M*LIB is highly optimized with Minimal Runtime Overhead in Release mode,
- Type Safety: Compiler catches type mismatches.
- Runtime errors: Assertion are enabled in Debug mode and check for contract violation and runtime errors are enabled for robustness in debug mode,
- High focus on homogeneous API design and ease of use,
- Near 100% coverage in instruction and decision by the test suite,
- Actively maintained library,
- Can be used fully without any macro usage outside of its code generation section,
- Provide all the standard containers (vectors, lists, hash maps, sets, queues, etc.) and algorithms (sort, search) and even more,
- Fully customizable memory allocator,
- Can use _Generic keyword for uniform usage of the operators on any registered type.
- Implementation of a trait system for C through oplists.
Components
The following headers define containers that don't require the user structure to be modified:
- m-array.h: header for creating dynamic array of generic type,
- m-list.h: header for creating singly-linked list of generic type,
- m-deque.h: header for creating dynamic double-ended queue of generic type,
- m-queue.h: header for creating static queue or stack of generic type,
- m-prioqueue.h: header for creating dynamic priority queue of generic type.
- m-dict.h: header for creating unordered associative array (through hashmap) or unordered set of generic type,
- m-rbtree.h: header for creating ordered set (through Red/Black binary sorted tree) of generic type,
- m-bptree.h: header for creating ordered map/set/multimap/multiset (through sorted B+TREE) of generic type,
- m-tree.h: header for creating arbitrary tree of generic type,
- m-tuple.h: header for creating arbitrary tuple of generic types,
- m-variant.h: header for creating arbitrary variant of generic type,
The available containers of M*LIB for thread synchronization are in the following headers:
- m-buffer.h: header for creating fixed-size queue (or stack) of generic type (multiple producer / multiple consumer) used for transferring data from a thread to another,
- m-snapshot: header for creating 'atomic buffer' (through triple buffer) for sharing synchronously big data (thread safe),
- m-shared-ptr.h: header for creating shared pointer of generic type,
The following containers are intrusive (You need to modify your structure to add fields needed by the container) and are defined in:
- m-i-list.h: header for creating doubly-linked intrusive list of generic type,
Other headers offering other functionality are:
- m-string.h: header for creating dynamic string of characters (UTF-8 support),
- m-bstring.h: header for creating dynamic string of BYTE,
- m-bitset.h: header for creating dynamic bitset (or "packed array of bool"),
- m-algo.h: header for providing various generic algorithms to the previous containers,
- m-funcobj.h: header for creating function object (used by algorithm generation),
- m-try.h: header for handling errors by throwing exceptions,
- m-worker.h: header for providing an easy pool of workers on separated threads to handle work orders (used for parallel tasks),
- m-serial-json.h: header for importing / exporting the containers