khklau / mod_tile_rs

A prototype Rust implementation of the tile module for Apache 2 webserver
GNU Affero General Public License v3.0
0 stars 0 forks source link

Adapt the APR memory pool for use by Rust code #4

Open khklau opened 3 years ago

khklau commented 3 years ago

In Apache2 modules allocating from the heap via any malloc implementation is forbidden. Instead modules are expected to allocate via the supplied APR memory pools.

To allow the std Rust containers to be used the APR memory pool must be adapted to fit one of the allocator traits and it must be set as the default global allocator.

khklau commented 3 years ago

Apparently Apache 2.4 provides access to a memory pool per thread.

khklau commented 3 years ago

After more research the pool is designed as a per request heap.

For allocations within a hook callback or per thread lifetime a non-pool allocator is allowed, albeit it must be the same system allocator used by Apache, which would be the Glibc allocator.

khklau commented 3 years ago

The pattern for Apache modules to store request metadata is: 1) Use the pool in request_rec to allocate space for your metadata structure 2) Save the pointer the metadata structure in the request_config field of request_rec via ap_set_module_config

khklau commented 3 years ago

There are a number of memory pools available for use:

khklau commented 3 years ago

Now that issue#7 is resolved we don't need the memory pools for convenient allocation since nothing is more convenient than the global allocator.

Having values with lifetimes bound to a configuration, connection or request however is still useful.

Some initial idea for how this could be implemented are:

khklau commented 3 years ago

The plan is to now:

  1. use the system allocator as the global allocator so that std containers continue to work like normal
  2. create a new PoolBox type that backed by a memory pool, which is instantiated via Box::new_in
  3. to tie the lifetime of values allocated on the heap via the global allocator to a particular scope (process, connection, request etc), create a PoolBox context struct that drops via apr_pool_cleanup_register. Then the regular Box and container values are stored in the context.
khklau commented 3 years ago

As an alternative PoolBox might not even be needed. The context structs can be constructed on the heap via the global allocator and the context can be forcefully dropped via the cleanup function that is registered to the pool.