微小的C库,利用未定义的行为来实现泛型编程

网友投稿 591 2022-10-25

微小的C库,利用未定义的行为来实现泛型编程

微小的C库,利用未定义的行为来实现泛型编程

TinyGenericC

A problematic C99 project that exploit undefined behavior to achieve generic programming.

Inspired by rxi/map and rxi/vec and attractivechaos/klib

WARNING

You are summoning the horrific nasal demons from the abyss of computer hell. Multiple nasal demons. You should never recall them unless you are crystal-clear of knowing what you are doing.

Compile-time type checking is practically disabled here by the side effect of pointer aliasing.

Documents and comments are not completed.

PLEASE DO NOT USE IT IN PRODUCTION. YOU WILL REGRET IT.

Index

All libraries are embedded in single-file header form using macro and C99 inline, older C compilers might not support all of them.

Currently undergoing development:

vector(T): Dynamic array. Header-only.vector_view(T): Like vector but using a static memory location.linked_list(T) and singly_linked_list(T): (Type-unsafe) Linked List.hash_table(K, V): Hash table using FNV-1a and separate chaining with list head cells. Custom hash has to be done by user.binary_tree(T): Binary tree (WIP)ref_count(T): Manual reference counting (WIP).mempool: Memory pool (WIP).tagged_mem: Linear memory with a pre-defined size (just like sds) (WIP)

A toy JSON library is also included here to demonstrate TGC. However the deserializer part is not started (I'm still learning parser sorry).

Version history

0.1.0: Major restructure (Files separated into tgc/ folder, TinyLang project started)0.0.5: Added event (emitter)0.0.4a: Added new tests for vector_view0.0.4: Incorporated static_assert to accommodate compile-time type matching hint0.0.3a: Added a loop detection algorithm for singly linked list (Floyd's cycle-finding)0.0.3: Reworked on the algorithm of hash_table (Now using a self-invented and flawed twin-hash implementation)0.0.2: Rewriting all the external linkage functions into C99-inline form0.0.1: Init

Note

Unfortunately, this is not true generic programming. Instead, it's more like Java-ish type erasure that defines a certain bound on object/struct size beforehand, and the program pretends not to write beyond the memory by asserting the size at run-time and partially, compile-time.

The indicator objects (e.g. iterator, index) however, are user-defined, and so highly dangerous since type-aliasings are easily or unintentionally violated.

Therefore, types are not reified and validated. You are responsible for correcting the types in case you shoot yourself in the foot.

TODO

Implement mempool first to support a memory allocator (useful for MCUs)Rewrite all containers to utilize mempool

3. Make vector_view able to use local buffer

Fix the goddamn documentation!

Footnote

Contributions are very welcomed as I have a very, very limited skill in Computer Science. This would get better as my ages went higher.

Wouldn't mind someone scold me for misunderstanding basic CS concepts, but if you are a computer science student/lecturer/professor, no, don't kill me. Please don't send me no death threats. I know you are so mad to me for exploiting undefined behaviors, but come on, it's an interesting project isn't it?

This set of programs is licensed under The Unlicense, i.e., Public Domain. Feel free to use it on your own, but this is simply cancer for production code.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:会话技术 cookie 和 Session(1)
下一篇:Ghidra是由美国国家安全局研究理事会创建和维护的软件逆向工程(SRE)框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~