C Collections

chttpclient · Section 3

chttpcli_set_engine_mem_mgmt_procs

chttpcli_set_engine_mem_mgmt_procs - set custom memory management for the allocations of the async engine of chttpclient

C Collections (libccollections, -lccollections)

#include <ccollections/chttpclient.h>
 
ccol_retval chttpcli_set_engine_mem_mgmt_procs(ccol_memmgmt_procs *mp);

All chttpcli instances of a process share one ceventloop reactor
for their Tier 2 and Tier 3 work. This function routes every
allocation of that reactor (the registration table, the per-connection
dispatch state, the worker pool for DNS and connect, and more) to the
malloc, free, calloc and realloc in mp, the same way the memory
management procs of every other module of this library work. An mp of
NULL brings the default allocator back.
 
Each chttpcli instance also has its own allocator for its requests and
connections, which you set with chttpcli_create_mp by the usual _mp
convention. That allocator is separate from this one, which only
affects how the single shared reactor is built.
 
You can call this function at only two points: Before the reactor
first starts in this process (before the first call to
chttpcli_do_async or chttpcli_do_async_streaming anywhere), or
after the engine has stopped completely. The engine stops once every
async user of a chttpcli has dropped its reference and the automatic
teardown has finished, and chttpcli_engine_wait blocks until that
teardown is over. The reactor behind chttpsvr_set_engine_mem_mgmt_procs
usually lives as long as the process, but this engine starts and stops
along with the Tier 2 and Tier 3 traffic, so the window for this
call opens again every time the engine stops and is not a one-time
chance at process start.
 
The engine allocates and frees through mp until its teardown ends, and
the teardown runs on its own thread after the last client lets go of
the engine. So mp and the functions it points to must stay valid until
chttpcli_engine_wait returns after the last client is destroyed. The
default client of chttp_default_client can keep the engine alive
through its idle Tier 2 and Tier 3 connections until the library's
exit handler destroys it, so a process that uses the default client
for those tiers must keep mp valid until it exits.
 
If mp is not NULL, all four function pointers in it (malloc, free,
calloc and realloc) must be set.

ccol_success, if the call succeeds.
ccol_invalid_args, if mp is not NULL but holds a function pointer that
    is NULL.
ccol_not_permitted, while the engine runs. Wait for the engine to stop
    completely first.

chttpcli_engine_wait(3), chttpcli_set_engine_logger(3), chttpcli_set_engine_num_reactor_threads(3), chttpcli_do_async(3), chttpcli_do_pooled(3), chttpsvr_set_engine_mem_mgmt_procs(3)