mod_annot editor

Annotate Section

Basic Memory Management

The most basic usage of pools is for memory management. Instead of

	mytype* myvar = malloc(sizeof(mytype)) ;
	/* make sure it gets freed later in every possible execution path */

we use

	mytype* myvar = apr_palloc(pool, sizeof(mytype)) ;

and the pool automatically takes responsibility for freeing it, regardless of what may happen in the meantime.

This takes many forms in APR and Apache, where memory is allocated within another function. Examples are string manipulation functions and logging, where we gain the immediate benefit of being able to use constructs like the APR version of sprintf() without having to know the size of a string in advance:

	char* result = apr_psprintf(pool, fmt, ...) ;

APR also provides higher-level abstractions of pool memory, for example in the buckets used to pass data down the filter chain. But we'll keep those for another article.