You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Ben Hyde <bh...@pobox.com> on 1999/09/21 18:22:41 UTC

Re: malloc/free ...

I think I'm happy.  I've been using subpools to fill in for
malloc/free.  It's very sweet and addictive.  I rarely build data
structures that are just contiguous memory, in fact I'm embaressed 
when I do.

It might be better to be fixated on enhancing the performance of or
creating variants of these two operations in place of adding another
memory abstraction.

I have accumulated a large amount of code where when ever I want to
create a "object" or an "abstract data type" or a "data structure"
I give each instance it's own pool.  This tends to look like this,

   #define MAKE_INSTANCE(pool, type) ((type*)ap_palloc(pool,sizeof(type)))

   typedef struct foo_s foo;

   struct foo_s { ap_pool *pool;... };
  
   void destroy_foo(foo *)
   {
      ap_destroy_pool(foo->pool);
   }

   foo *create_foo(ap_pool p, ...p1 ...pn)
   {
       ap_pool foo_pool = ap_make_sub_pool(p);
       foo *result = MAKE_INSTANCE(p, foo_pool);

       result->pool = foo_pool;
       result->p1 = p1;
       ...
       result->pn = pn;
       ... further initialize foo ...
       ... cleanup for foo in foo_pool ...
   }

   foo operationX_foo(foo *f ...)
   {
       foo_element *e = MAKE_INSTANCE(f->pool, foo_element);
       e->next = f->whatever;
       f->whatever = z;
       ... more what ever ...
   }

I particularly like the transparent semantics of the destroy 
operation, and that the enclosing pool/object folds my cleaning
into his destruction.  That is _so_ much better than the 
ad hoc cleanup strategies so commonly found in systems.

This example glosses over some details.  For example my create_foo
rarely takes a pool explicitly, but instead takes an "activity"
object and a "parent" object and then use the activity's pool to
create the child and then it will stow the child into the parent
object.  Then cleaning is a little subtle since the termination
of the parent child or activity can triger it.  Getting the
various unhooking and ordering right reminds one of why a
free operation is such an incredible thorn the foot.

  - ben