docs::a...R::Pool(3)
okay bestie, welcome to the User Contributed Perl Documentation page for docs::a...R::Pool(3) β the manual page for APR::Pool, and no cap, this one is all about pools: APR pools, memory management, and the scoping lore you actually need. π
πΈ NAME πΈ
APR::Pool β Perl API for APR pools. That's the whole name vibe, periodt: it gives you Perl access to APR pools, which are the little memory-management main characters here. π
β¨ Synopsis β¨
Here's the synopsis in all its glory β copy-paste exact because code is sacred: β¨
use APR::Pool ();
my $sp = $r->pool->new;
my $sp2 = APR::Pool->new;
# $sp3 is a subpool of $sp,
# which in turn is a subpool of $r->pool
$sp3 = $sp->new;
print '$r->pool is an ancestor of $sp3'
if $r->pool->is_ancestor($sp3);
# but sp2 is not a sub-pool of $r->pool
print '$r->pool is not an ancestor of $sp2'
unless $r->pool->is_ancestor($sp2);
# $sp4 and $sp are the same pool (though you can't
# compare the handle as variables)
my $sp4 = $sp3->parent_get;
# register a dummy cleanup function
# that just prints the passed args
$sp->cleanup_register(sub { print @{ $_[0] || [] } }, [1..3]);
# tag the pool
$sp->tag("My very best pool");
# clear the pool
$sp->clear();
# destroy sub pool
$sp2->destroy;
π Description π
okay so APR::Pool gives you access to APR pools, and APR pools are used for easy memory management. Different pools have different life scopes, so you don't have to explicitly free allocated memory β instead it gets freed when the pool's life comes to an end. For example, a request pool is created at the beginning of a request and destroyed at the end of it, and all the memory allocated during request processing with the request pool is freed all at once at the end of the request. π«Ά
Most of the time you'll just pass various pool objects to the methods that require them. But bestie, vibe check β you must understand the scoping of the pools: if you pass a long lived server pool to a method that needs the memory only for a short scoped request, you are going to leak memory. A request pool should be used in that case. And vice versa: if you need to allocate some memory for a scope longer than a single request, then a request pool is inappropriate, because when the request is over, the memory will be freed and bad things may happen. π
If you need to create a new pool, you can always do that via the new() method. β¨
π«Ά API π«Ά
APR::Pool provides the following functions and/or methods:
π§Ό cleanup_register π§Ό
Register cleanup callback to run. This is how you tell the pool, βbestie, when you're done, run this little callback.β π
$pool->cleanup_register($callback);
$pool->cleanup_register($callback, $arg);
- obj:
$pool(APR::Pool object) β the pool object to register the cleanup callback for. π«Ά - arg1:
$callback(CODE ref or sub name) β a cleanup callbackCODEreference or just a name of the subroutine (fully qualified unless defined in the current package). β¨ - opt arg2:
$arg(SCALAR) β if this optional argument is passed, the$callbackfunction will receive it as the first and only argument when executed. To pass more than one argument, use anARRAYor aHASHreference. π - ret: no return value.
- excpt: if a registered callback dies or throws an exception,
$@is stringified and passed towarn(). Usually, this results in printing it to theerror_log. However, a$SIG{__WARN__}handler can be used to catch them. π«Ά
$pool->cleanup_register(sub {die "message1\n"});
$pool->cleanup_register(sub {die "message2\n"});
my @warnings;
{
local $SIG{__WARN__}=sub {push @warnings, @_};
$pool->destroy; # or simply undef $pool
}
Both of the cleanups above are executed at the time $pool->destroy is called. @warnings contains message2\n and message1\n afterwards. $pool->destroy itself does not throw an exception. Any value of $@ is preserved. π
- since:
2.0.00
If there is more than one callback registered (when cleanup_register is called more than once on the same pool object), the last registered callback will be executed first (LIFO). π
π Examples π
No arguments, using anon sub as a cleanup callback:
$r->pool->cleanup_register(sub { warn "running cleanup" });
One or more arguments using a cleanup code reference:
$r->pool->cleanup_register(\&cleanup, $r);
$r->pool->cleanup_register(\&cleanup, [$r, $foo]);
sub cleanup {
my @args = (@_ && ref $_[0] eq ARRAY) ? @{ +shift } : shift;
my $r = shift @args;
warn "cleaning up";
}
No arguments, using a function name as a cleanup callback:
$r->pool->cleanup_register('foo');
π§Ό clear π§Ό
Clear all memory in the pool and run all the registered cleanups. This also destroys all sub-pools. It's giving iconic full reset energy. π
$pool->clear();
- obj:
$pool(APR::Pool object) β the pool to clear. - ret: no return value.
- since:
2.0.00
This method differs from destroy() in that it is not freeing the previously allocated, but allows the pool to re-use it for the future memory allocations. So it's like, clear the vibes but keep the space reusable. π«Ά
π DESTROY π
DESTROY is an alias to destroy. It's there so that custom APR::Pool objects will get properly cleaned up when the pool object goes out of scope. If you ever want to destroy an APR::Pool object before it goes out of scope, use destroy. π
- since:
2.0.00
𧨠destroy π§¨
Destroy the pool. No cap, this is the full send and it ate. π₯
$pool->destroy();
- obj:
$pool(APR::Pool object) β the pool to destroy. - ret: no return value.
- since:
2.0.00
This method takes a similar action to clear() and then frees all the memory. π
π³ is_ancestor π³
Determine if pool a is an ancestor of pool b. It's giving family tree check. π
$ret = $pool_a->is_ancestor($pool_b);
- obj:
$pool_a(APR::Pool object) β the pool to search. - arg1:
$pool_b(APR::Pool object) β the pool to search for. - ret:
$ret(integer) β true if$pool_ais an ancestor of$pool_b. - since:
2.0.00
For example create a sub-pool of a given pool and check that the pool is an ancestor of that sub-pool: π
use APR::Pool ();
my $pp = $r->pool;
my $sp = $pp->new();
$pp->is_ancestor($sp) or die "Don't mess with genes!";
π± new π±
Create a new sub-pool. New pool, new me. π
my $pool_child = $pool_parent->new;
my $pool_child = APR::Pool->new;
- obj:
$pool_parent(APR::Pool object) β the parent pool. If you don't have a parent pool to create the sub-pool from, you can use this object method as a class method, in which case the sub-pool will be created from the global pool βmy $pool_child = APR::Pool->new;. π«Ά - ret:
$pool_child(APR::Pool object) β the child sub-pool. - since:
2.0.00
π¨βπ©βπ§ parent_get π¨βπ©βπ§
Get the parent pool. It's giving ancestry dot com. π
$parent_pool = $child_pool->parent_get();
- obj:
$child_pool(APR::Pool object) β the child pool. - ret:
$parent_pool(APR::Pool object) β the parent pool.undefif there is no parent pool (which is the case for the top-most global pool). π«Ά - since:
2.0.00
Example: Calculate how big is the pool's ancestry: π³
use APR::Pool ();
sub ancestry_count {
my $child = shift;
my $gen = 0;
while (my $parent = $child->parent_get) {
$gen++;
$child = $parent;
}
return $gen;
}
π·οΈ tag π·οΈ
Tag a pool (give it a name). Main character gets a label. π
$pool->tag($tag);
- obj:
$pool(APR::Pool object) β the pool to tag. - arg1:
$tag(string) β the tag (some unique string). - ret: no return value.
- since:
2.0.00
Each pool can be tagged with a unique label. This can prove useful when doing low level apr_pool C tracing (when apr is compiled with -DAPR_POOL_DEBUG). It allows you to grep(1) for the tag you have set, to single out the traces relevant to you. π
Though there is no way to get read the tag value, since APR doesn't provide such an accessor method. π
π Unsupported API π
APR::Pool also provides auto-generated Perl interface for a few other methods which aren't tested at the moment and therefore their API is a subject to change. These methods will be finalized later as a need arises. If you want to rely on any of the following methods please contact the the mod_perl development mailing list so we can help each other take the steps necessary to shift the method to an officially supported API. π
π§ cleanup_for_exec π§
META: Autogenerated - needs to be reviewed/completed. π§
Preparing for exec() --- close files, etc., but don't flush I/O buffers, don't wait for subprocesses, and don't free any memory. Run all of the child_cleanups, so that any unnecessary files are closed because we are about to exec a new program. π
- ret: no return value.
- since: subject to change.
π¦ See Also π¦
mod_perl 2.0 documentation. That's the lore. π
π Copyright π
mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0. β¨
π Authors π
The mod_perl development team and numerous contributors. π
okay bye bestie, this was docs::a...R::Pool(3) β signing off with perl v5.42.3, dated 2023-10-21. mwah ππ
π¦ where this page comes from
- nixos 26.05: mod_perl 2.0.13
- nixpkgs unstable: mod_perl 2.0.13
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms