Pool memory allocator to automatically free memory of a code block

Simple usage of Pool memory allocator to automatically free memory of a code block:

#import "Basic";
Pool :: #import "Pool";

pool :: (code: Code) #expand {
    _pool: Pool.Pool;
    Pool.set_allocators(*_pool);
    
    {
        push_allocator(Pool.pool_allocator, *_pool);
        #insert code;
    }

    print("The pool contains % bytes.\n", _pool.memblock_size - _pool.bytes_left);
    print("Releasing the pool now.\n");
    Pool.release(*_pool);
}

Can be used via

x: string; // define variables that need to out-live the pool outside
pool(#code {
    // your code here
    x = "some allocated data";
});
print(x);

Could you explain, what exactly happenned here, pls?

  • x is usable after block with same address in mem but empty?
  • x is usable after block but nullpointed
  • x is unusable after block?

Maybe it will be better to add few more complicated and diversified types into example, so part of vars will remain ok-ish, other part will be dead-dead and remaining ones will be usable but empty.

I feel that it is quite good example for privacy/cipher-related variables, so managed pool could manage to securely destroy all data after block closes

1 Like