nanovms / nanos

A kernel designed to run one and only one application in a virtualized environment
https://nanos.org
Apache License 2.0
2.58k stars 133 forks source link

Closures: add closure struct definition to closure_type() macro #2006

Closed francescolavra closed 6 months ago

francescolavra commented 6 months ago

With this change, the closure_type() macro includes both a typedef statement and a struct definition; the struct can be used by any basic closure (i.e. a closure without any closure-specific fields in its struct) that uses the type. This change makes it unnecessary to define a closure struct for each basic closure, and by reusing basic closure structs allows removing several declare_closure_struct() macro invocations. In order to be able to include both a typedef and a struct in the closure_type() macro, the different macros in the closure templates have been changed so that each function parameter in the signature of closure functions is written as <type> <name> (i.e. as a single macro argument) instead of <type>, <name> (i.e. as 2 separate macro arguments). In order to support the different types of closure instantiations when using a basic closure, 2 new macros have been added: closure_func (for allocating basic closures from a heap) and stack_closure_func (for allocating basic closures on the stack). Various non-basic closures have been replaced with basic closures (embedded in other structs) in different parts of the kernel, which simplifies the code and minimizes the number of allocations and deallocations required when setting up and tearing down the larger structs.