On Thu, Jan 11, 2018 at 3:05 PM, Eric Covener <[email protected]> wrote:
> On Thu, Jan 11, 2018 at 3:55 AM, Simon Walter <[email protected]> wrote:
>>
>> I suppose that the pool is keeping track of all it's allocations and if
>> something is still referenced, it will not free it.
>
> No the only tracking is done by whoever manages the lifecycle of the
> pool itself -- no magic.
>
> apr_pool_destroy will call free() or munmap or any underlying
> allocation on the way out, returning it to the OS.
Actually the memory is returned to the (apr_)allocator, which itself
may cache for further reuse.
One can use apr_allocator_max_free_set() to limit the number of pages
cached (no limit by default), e.g. something like the following code
based on Simon's (not even compile tested...):
int main(int ArgCount, char * Arg[])
{
char * String;
apr_pool_t * Pool = NULL;
apr_allocator_t * Alloc = NULL;
apr_initialize();
/* New allocator (not the default/unlimited one) */
apr_allocator_create(&Alloc);
/* Cache one page only (may be 4K pages, not system's),
* zero is unlimited, so the cache is always 1 page min... */
apr_allocator_max_free_set(Alloc, 1/*page*/);
/* Use this allocator for the pool */
apr_pool_create_ex(&Pool, NULL, NULL, Alloc);
/* Destroy Alloc when destroying Pool */
apr_allocator_owner_set(Alloc, Pool);
/* Won't crash (possibly), don't do that for real... */
String = apr_pstrdup(Pool, "small alloc");
apr_pool_clear(Pool);
printf("String: %s\n", String);
/* Should crash */
(void)apr_palloc(Pool, 4100);
(void)apr_palloc(Pool, 4100);
(void)apr_palloc(Pool, 4100);
String = apr_pstrdup(Pool, "small alloc");
apr_pool_clear(Pool);
printf("String: %s\n", String);
/* Should also crash */
String = apr_pstrdup(Pool, "small alloc");
apr_pool_destroy(Pool); /* + Alloc */
printf("String: %s\n", String);
apr_terminate();
return 0;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]