Skip to content

Instantly share code, notes, and snippets.

@serhei
Last active October 30, 2019 19:54
Show Gist options
  • Select an option

  • Save serhei/711260a7351dd91cd4c807bda69733c7 to your computer and use it in GitHub Desktop.

Select an option

Save serhei/711260a7351dd91cd4c807bda69733c7 to your computer and use it in GitHub Desktop.
WIP interesting examples of how C type system is used

(1) Hash table supporting different key types (using a union type)

(2) "Subclassing" a struct by declaring a struct with the same fields and additional data at the end:

In memory:

struct hlist_node |#####|
struct mountpoint |#####|############|

A function that works on hlist_nodes can then also be called on a list of mountpoint.
e.g. hlist_for_each_entry() used on mountpoint: https://github.com/torvalds/linux/blob/cbafe18c71028d5e0ee1626b4776fea5d5824a78/fs/namespace.c#L686

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment