Tracking these kind of allocations is what I was wondering if the effect system
could be used for. It'd be one way to tag a function to ensure no
hidden/accidental allocations (or at least count them). Custom allocators might
better handle the use case though.
Well actually, being able to specify allocators for different segments of code.
For example, embedded devices sometimes have special RAM for interrupts and
it'd be nice to be able to use that ram for regular standard library objects. I
think it overlaps with other systems programming though (e.g. if you wanted a
serious Nim kernel ;) ).
I'm not sure this would be too easy to do at compile time? An example of my
ideal use case might look like:
switch(nimOverridableAllocator, true)
var fifoBuffer: seq[byte]
proc time_sensitive_function() {.cdecl, exportc.} =
withAllocator(none):
...
# Some code that shouldn't allocate
# produces compile error if it does
# temporary copies on the stack is fine usually
var item = fifoBuffer[idx]
...
proc setup_time_sensitive_function() =
withAllocator(isrMalloc):
...
allocate a small FIFO buffer say
fifoBuffer = initSeq(1000)
...
# Or maybe things like this:
proc fast_algorithm_function() =
var simpleFixPool = initSimpleFixPool(1000)
withAllocator(simplePool):
...
some inner loop processing
...
... some regular code
Run
Whether it would require tagging proc's, or other stuff the case would be to be
able to set some proc's and all the inner calls to use that allocator.
I think switching allocators in a dynamic scope at runtime would be feasible
(at least per thread?), but it'd be really nice to do it at compile time. Then
you could ensure no allocations at compile time right? Still being able to do a
runtime test would be helpful.