apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "PERKES, Graeme" <graeme.per...@baesystems.com>
Subject Using apr_allocator_max_free_set to constrain the total size of a memory pool
Date Wed, 04 Oct 2006 06:52:09 GMT
Hi,

I am trying to create memory pools that are constrained to a specific
size (e.g. 32MB). i.e. free list + used memory never exceeds 32MB.
I am not sure if the operation of apr_allocator_max_free_set matches
this.

Does this apr_allocator_max_free_set:
a) Restrict the total size of the memory pool controlled by the
   allocator to the value in the size parameter, or
b) Bound the size of the free list such that when the pool needs to
   grab more memory (pool fills) it simply allocates another freelist
   to the specified list and thus grows the pool.

I have written a small test program below to test this on Linux
(2.6.9 kernel). This program just keeps allocating and will eventually
fill memory since apr_calloc ignores the 32MB limit I set.

#include <stdio.h>
#include <apr-1/apr.h>
#include <apr-1/apr_general.h>

#define YOUR_POOL_MAX_FREE_SIZE (32*1024*1024)

void failed_to_allocate(int retcode) {
	printf("\n\n*** failed to allocate sufficient memory : retcode
%d *** \n\n", retcode);
}

int main( int argc, char **argv) {

	apr_pool_t *system_pool = NULL;
	apr_pool_t *mp = NULL;
	apr_allocator_t	*my_allocator_ptr = NULL;

	int chunk_size, i;
	void *ptr = NULL;
		
	/* Setup any Apache Portbale Runtime (APR) internal data
structures.
           This MUST be the first function called for any APR library.
*/
	apr_initialize();

	atexit( apr_terminate );

	/* create an allocator with free size list restricted to 32MB */
	if ( apr_allocator_create( &my_allocator_ptr ) == APR_SUCCESS )
{
		printf("Created my_allocator_ptr\n");
		apr_allocator_max_free_set(my_allocator_ptr,
YOUR_POOL_MAX_FREE_SIZE);
		printf("Set max free size for my_allocator_ptr to %d\n",
YOUR_POOL_MAX_FREE_SIZE);
	}

	/* create system pool with no restriction */
	apr_pool_create(&system_pool, NULL);

	/* create a subpool using the size-restricted allocator */
	apr_pool_create_ex(&mp, system_pool, (apr_abortfunc_t)
failed_to_allocate, my_allocator_ptr);


	apr_allocator_t* pa = apr_pool_allocator_get(mp);

	/* debug - would expect these two pointers to be equal */
	printf("pa %x my_allocator_ptr %x\n\n", pa, my_allocator_ptr );

	/* consume memory in 1 megabyte chunks */	
	chunk_size = 1024 * 1024;
	ptr = apr_pcalloc( mp, chunk_size );
	for ( i = 1; ptr != NULL; i++ ) {
		ptr = apr_pcalloc( mp, chunk_size );

		if ( ptr != NULL ) {
			printf("pass %3.3d: allocated %d bytes\n", i,
chunk_size );
		} else {
			printf("pass %3.3d: *** failed to allocate %d
bytes\n", i, chunk_size );
		}
		usleep( 100000 );
	}

	apr_pool_destroy( mp );	
}

Is my understanding of apr_allocator_max_free_set totally wrong?

Thanks,

Graeme

Mime
View raw message