apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sander Striker" <s.stri...@striker.nl>
Subject Re: Opaque apr_pool_t structure
Date Wed, 04 Jun 2008 13:48:02 GMT
On Wed, Jun 4, 2008 at 3:20 PM, Yann <yl@bee-ware.net> wrote:
> Hello,
> I'm making a piece of software that recycles apr_sockets in sockets-pools.

Have you looked at apr_reslist in apr-util?

> Each socket is allocated and created on its own pool (not a subpool, or
> exactly a subpool of the internal global pool only).
> I've read on svn programming pages that it is not recommanded to do this,
> but I find it very usefull, that's the power of pools, IMHO.

The problem is the memory footprint that this brings with it.  Each pool
will pre-allocate 8k of memory.

> Anyway, to make the things clean, I need a way to attach/detach pools
> to/from others, whether the socket belongs to the socket-pool's pool or to
> the user's one.
> As the apr_pool_join() function is a noop (and I don't knwow if that's its
> purpose anyway), I used the apr_pool_cleanup_register()ing /
> apr_pool_cleanup_kill()ing mechanism to attach / detach pools between them.
> I create a small new API with my pools based on the APR ones, where I can
> attach/detach pools, as I said, but also malloc() and realloc() memories
> attached to a pool (with the same registering mechanism).
> My pool struct is like :
> struct my_pool_t {
>  apr_pool_t pool;
>  apr_pool_t *owner;
> };
> And the functions :
> apr_status_t my_pool_create(my_pool_t **pool, void *owner);
> apr_status_t my_pool_attach(my_pool_t *pool, void *owner);
> apr_status_t my_pool_detach(my_pool_t *pool);
> apr_status_t my_pmalloc(void *pool, apr_size_t size);
> apr_status_t my_pzalloc(void *pool, apr_size_t size);
> apr_status_t my_prealloc(void *pool, void *ptr, apr_size_t size);
> void my_pfree(void *pool, void *ptr);
> As you can see with the first element of the struct which is an APR pool and
> 'void*' types used in functions, I expect the functions to be "compatible"
> with the APR ones, that is I can use my_pool_t and apr_pool_t pools
> indistinctly with them (where the type is void*) ...

That's not the case, see below.

> My problem is that I can't initialize my_pool because I haven't got the
> sizeof(apr_pool_t), an opaque structure ...
> I understand the advantages of opaque types, but is there a way an
> apr_pool_sizeof() function be added (and exported) in the APR, simply like :
> APR_DECLARE(apr_size_t) apr_pool_sizeof(void)
> {
>  /* maybe the aligned SIZEOF_POOL_T should be used */
>  return sizeof(apr_pool_t);
> }
> With it, I could at least do :
>  apr_pool_create(&p, NULL);
>  memcpy(my_pool->pool, p, apr_pool_sizeof())

This would be a bad idea, as you'd be copying pointers and the like, which
would be off as soon as copied.  Even if that were not the case, the apr_pool_t
struct instance lives in an allocated block of memory from which the pool
will hand out memory.  This block in turn is managed by an allocator.

> Or should I consider using my_p*() functions strictly with my_pools ?
> Thanks for your advices, answers.



View raw message