Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

mlib

> 编程语言
Open source

M*LIB is a library of generic and type safe containers / data structures in pure C language (C99 / C11) for a wide collection of container (comparable to the C+

1.1K stars0 likes1 views
WebsiteGitHub

About

M*LIB is a library of generic and type safe containers / data structures in pure C language (C99 / C11) for a wide collection of container (comparable to the C+

M*LIB: Generic type-safe Container Library for C language

  1. Overview
  2. Components
  3. Build & Installation
  4. How to use
  5. Performance
  6. OPLIST
  7. Memory Allocation
  8. Emplace construction
  9. Errors & compilers
  10. External Reference
  11. API Documentation
    1. Common Interface
    2. Sequence containers
      1. Array
      2. List
      3. Intrusive list
      4. Double end queue
      5. Fixed size queue / stack
    3. Associative containers
      1. Dictionary
      2. Red/Black Tree
      3. B+ Tree
    4. Misc containers
      1. Priority queue
      2. Generic Tree
      3. Tuple
      4. Variant
    5. Thread containers
      1. Shared Fixed size queue
      2. Atomic Shared Register
      3. Shared pointers
      4. Worker threads
    6. Dataset
      1. String
      2. Byte String
      3. Bitset
    7. Algorithms
      1. Generic algorithms
      2. Function objects
    8. Serialization
      1. JSON Serialization
      2. Binary Serialization
    9. Uniform interface
    10. Core preprocessing
    11. C11 compatibility headers
      1. Thread
      2. Atomic
  12. Global User Customization
    1. Exception handling
    2. Memory context Customization
  13. 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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Calgorithmsarraybitsetc

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言