apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dr...@apache.org
Subject cvs commit: apr/memory/unix apr_sms.c apr_sms_std.c apr_sms_tracking.c
Date Fri, 08 Jun 2001 00:44:52 GMT
dreid       01/06/07 17:44:52

  Modified:    include  apr_sms.h
               memory/unix apr_sms.c apr_sms_std.c apr_sms_tracking.c
  Log:
  Some tidying up of the locking code.
  Move where we allocate the pool from and clean it up.
  Change the pre_destroy to be an apr_status_t
  Move the lock to the individual structures and add a lock_destroy in
    the pre_destroy function.
  Some changing of parameter names to make them shorter.
  
  Submitted by:  Sander Striker <striker@samba-tng.org>
                 David Reid <dreid@apache.org>
  
  Revision  Changes    Path
  1.12      +10 -11    apr/include/apr_sms.h
  
  Index: apr_sms.h
  ===================================================================
  RCS file: /home/cvs/apr/include/apr_sms.h,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- apr_sms.h	2001/06/06 18:11:05	1.11
  +++ apr_sms.h	2001/06/08 00:44:51	1.12
  @@ -97,20 +97,19 @@
       const char *identity; /* a string identifying the module */
   
       apr_pool_t *pool;
  -    apr_lock_t *lock;
   
       struct apr_sms_cleanup *cleanups;
   
  -    void * (*malloc_fn)       (apr_sms_t *mem_sys, apr_size_t size);
  -    void * (*calloc_fn)       (apr_sms_t *mem_sys, apr_size_t size);
  -    void * (*realloc_fn)      (apr_sms_t *mem_sys, void *memory, 
  -                               apr_size_t size);
  -    apr_status_t (*free_fn)   (apr_sms_t *mem_sys, void *memory);
  -    apr_status_t (*reset_fn)  (apr_sms_t *mem_sys);
  -    void (*pre_destroy_fn)    (apr_sms_t *mem_sys);
  -    apr_status_t (*destroy_fn)(apr_sms_t *mem_sys);
  -    apr_status_t (*lock_fn)   (apr_sms_t *mem_sys);
  -    apr_status_t (*unlock_fn) (apr_sms_t *mem_sys);
  +    void * (*malloc_fn)            (apr_sms_t *mem_sys, apr_size_t size);
  +    void * (*calloc_fn)            (apr_sms_t *mem_sys, apr_size_t size);
  +    void * (*realloc_fn)           (apr_sms_t *mem_sys, void *memory, 
  +                                    apr_size_t size);
  +    apr_status_t (*free_fn)        (apr_sms_t *mem_sys, void *memory);
  +    apr_status_t (*reset_fn)       (apr_sms_t *mem_sys);
  +    apr_status_t (*pre_destroy_fn) (apr_sms_t *mem_sys);
  +    apr_status_t (*destroy_fn)     (apr_sms_t *mem_sys);
  +    apr_status_t (*lock_fn)        (apr_sms_t *mem_sys);
  +    apr_status_t (*unlock_fn)      (apr_sms_t *mem_sys);
   };
   
   /*
  
  
  
  1.15      +12 -6     apr/memory/unix/apr_sms.c
  
  Index: apr_sms.c
  ===================================================================
  RCS file: /home/cvs/apr/memory/unix/apr_sms.c,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- apr_sms.c	2001/06/06 21:49:00	1.14
  +++ apr_sms.c	2001/06/08 00:44:51	1.15
  @@ -153,7 +153,7 @@
   }
   
   APR_DECLARE(apr_status_t) apr_sms_init(apr_sms_t *mem_sys, 
  -                                       apr_sms_t *parent_mem_sys)
  +                                       apr_sms_t *pms)
   {
       /* XXX - I've assumed that memory passed in will be zeroed,
        * i.e. calloc'd instead of malloc'd...
  @@ -163,7 +163,7 @@
        * an assumption to make as it sounds :)
        */
   
  -    mem_sys->parent_mem_sys = parent_mem_sys;
  +    mem_sys->parent_mem_sys = pms;
       mem_sys->accounting_mem_sys = mem_sys;
       mem_sys->child_mem_sys = NULL;
   
  @@ -185,14 +185,17 @@
        *      sibling->ref = ref;
        */
        
  -    if (parent_mem_sys) {
  -        if ((mem_sys->sibling_mem_sys = parent_mem_sys->child_mem_sys) != NULL)
  +    if (pms) {
  +        if ((mem_sys->sibling_mem_sys = pms->child_mem_sys) != NULL)
               mem_sys->sibling_mem_sys->ref_mem_sys = &mem_sys->sibling_mem_sys;
   
  -        mem_sys->ref_mem_sys = &parent_mem_sys->child_mem_sys;
  -        parent_mem_sys->child_mem_sys = mem_sys;
  +        mem_sys->ref_mem_sys = &pms->child_mem_sys;
  +        pms->child_mem_sys = mem_sys;
       }
   
  +    /* XXX - This should eventually be removed */
  +    apr_pool_create(&mem_sys->pool, pms ? pms->pool : NULL);
  +    
       return APR_SUCCESS;
   }
   
  @@ -423,6 +426,9 @@
       if (mem_sys->pre_destroy_fn)
           mem_sys->pre_destroy_fn(mem_sys);
   
  +    /* XXX - This should eventually be removed */
  +    apr_pool_destroy(mem_sys->pool);
  +    
       /* 1 - If we have a self destruct, use it */
       if (mem_sys->destroy_fn)
           return mem_sys->destroy_fn(mem_sys);
  
  
  
  1.8       +0 -3      apr/memory/unix/apr_sms_std.c
  
  Index: apr_sms_std.c
  ===================================================================
  RCS file: /home/cvs/apr/memory/unix/apr_sms_std.c,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- apr_sms_std.c	2001/06/06 18:11:39	1.7
  +++ apr_sms_std.c	2001/06/08 00:44:52	1.8
  @@ -127,9 +127,6 @@
       new_mem_sys->free_fn    = apr_sms_std_free;
       new_mem_sys->identity   = module_identity;
   
  -    /* If this fails, what should we do??? */
  -    apr_pool_create(&(new_mem_sys->pool), NULL);
  -     
       /* as we're not a tracking memory module, i.e. we don't keep
        * track of our allocations, we don't have apr_sms_reset or
        * apr_sms_destroy functions.
  
  
  
  1.8       +88 -56    apr/memory/unix/apr_sms_tracking.c
  
  Index: apr_sms_tracking.c
  ===================================================================
  RCS file: /home/cvs/apr/memory/unix/apr_sms_tracking.c,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- apr_sms_tracking.c	2001/06/06 18:11:41	1.7
  +++ apr_sms_tracking.c	2001/06/08 00:44:52	1.8
  @@ -65,6 +65,7 @@
   #include "apr_private.h"
   #include "apr_sms.h"
   #include "apr_sms_tracking.h"
  +#include "apr_lock.h"
   #include <stdlib.h>
   
   static const char *module_identity = "TRACKING";
  @@ -84,41 +85,32 @@
   {
       apr_sms_t            header;
       apr_track_node_t    *nodes;
  +    apr_lock_t          *lock;
   } apr_sms_tracking_t;
   
  -static apr_status_t apr_sms_tracking_lock(apr_sms_t *mem_sys)
  -{
  -    return apr_lock_acquire(mem_sys->lock);
  -}
  -
  -static apr_status_t apr_sms_tracking_unlock(apr_sms_t *mem_sys)
  -{
  -    return apr_lock_release(mem_sys->lock);
  -}
  -
   static void *apr_sms_tracking_malloc(apr_sms_t *mem_sys,
                                        apr_size_t size)
   {
       apr_sms_tracking_t *tms;
       apr_track_node_t *node;
   
  -    apr_sms_tracking_lock(mem_sys);
  -  
  -    tms = (apr_sms_tracking_t *)mem_sys;
       node = apr_sms_malloc(mem_sys->parent_mem_sys,
                             size + sizeof(apr_track_node_t));
       if (!node)
           return NULL;
   
  +    tms = (apr_sms_tracking_t *)mem_sys;
  +    apr_lock_acquire(tms->lock);
  +
       node->next = tms->nodes;
       tms->nodes = node;
       node->ref = &tms->nodes;
       if (node->next)
           node->next->ref = &node->next;
   
  -    node++;
  +    apr_lock_release(tms->lock);
   
  -    apr_sms_tracking_unlock(mem_sys);
  +    node++;
   
       return (void *)node;
   }
  @@ -129,23 +121,23 @@
       apr_sms_tracking_t *tms;
       apr_track_node_t *node;
     
  -    apr_sms_tracking_lock(mem_sys);
  -
  -    tms = (apr_sms_tracking_t *)mem_sys;
       node = apr_sms_calloc(mem_sys->parent_mem_sys,
                             size + sizeof(apr_track_node_t));
       if (!node)
           return NULL;
   
  +    tms = (apr_sms_tracking_t *)mem_sys;
  +    apr_lock_acquire(tms->lock);
  +
       node->next = tms->nodes;
       tms->nodes = node;
       node->ref = &tms->nodes;
       if (node->next)
           node->next->ref = &node->next;
   
  -    node++;
  +    apr_lock_release(tms->lock);
   
  -    apr_sms_tracking_unlock(mem_sys);
  +    node++;
   
       return (void *)node;
   }
  @@ -156,14 +148,20 @@
       apr_sms_tracking_t *tms;
       apr_track_node_t *node;
   
  -    apr_sms_tracking_lock(mem_sys);
  -
       tms = (apr_sms_tracking_t *)mem_sys;
  +
  +
       node = (apr_track_node_t *)mem;
   
       if (node) {
           node--;
  +        apr_lock_acquire(tms->lock);
  +        
           *(node->ref) = node->next;
  +        if (node->next)
  +            node->next->ref = node->ref;
  +        
  +        apr_lock_release(tms->lock);
       }
   
       node = apr_sms_realloc(mem_sys->parent_mem_sys,
  @@ -171,16 +169,18 @@
       if (!node)
           return NULL;
   
  +    apr_lock_acquire(tms->lock);
  +    
       node->next = tms->nodes;
       tms->nodes = node;
       node->ref = &tms->nodes;
       if (node->next)
           node->next->ref = &node->next;
   
  +    apr_lock_release(tms->lock);
  +    
       node++;
   
  -    apr_sms_tracking_unlock(mem_sys);
  -
       return (void *)node;
   }
   
  @@ -188,17 +188,20 @@
                                             void *mem)
   {
       apr_track_node_t *node;
  - 
  -    apr_sms_tracking_lock(mem_sys);
  +    apr_sms_tracking_t *tms;
      
       node = (apr_track_node_t *)mem;
       node--;
   
  +    tms = (apr_sms_tracking_t *)mem_sys;
  + 
  +    apr_lock_acquire(tms->lock);
  +
       *(node->ref) = node->next;
       if (node->next)
           node->next->ref = node->ref;
    
  -    apr_sms_tracking_unlock(mem_sys);
  +    apr_lock_release(tms->lock);
            
       return apr_sms_free(mem_sys->parent_mem_sys, node);
   }
  @@ -208,12 +211,11 @@
       apr_sms_tracking_t *tms;
       apr_track_node_t *node;
       apr_status_t rv;
  -
  -    if ((rv = apr_sms_tracking_lock(mem_sys)) != APR_SUCCESS)
  -        return rv;
    
       tms = (apr_sms_tracking_t *)mem_sys;
   
  +    apr_lock_acquire(tms->lock);
  +    
       while (tms->nodes) {
           node = tms->nodes;
           *(node->ref) = node->next;
  @@ -221,31 +223,64 @@
               node->next->ref = node->ref;
           if ((rv = apr_sms_free(mem_sys->parent_mem_sys, 
                                  node)) != APR_SUCCESS) {
  -            apr_sms_tracking_unlock(mem_sys);
  +            apr_lock_release(tms->lock);
               return rv;
           }
       }
       
  -    if ((rv = apr_sms_tracking_unlock(mem_sys)) != APR_SUCCESS)
  -        return rv;
  +    apr_lock_release(tms->lock);
   
       return APR_SUCCESS;
   }
   
  +static apr_status_t apr_sms_tracking_pre_destroy(apr_sms_t *mem_sys)
  +{
  +    /* This function WILL alwways be called.  However, be aware that the
  +     * main sms destroy function knows that it's not wise to try and destroy
  +     * the same piece of memory twice, so the destroy function in a child won't
  +     * neccesarily be called.  To guarantee we destroy the lock it's therefore
  +     * destroyed here.
  +     */
  +    apr_sms_tracking_t *tms;
  + 
  +    tms = (apr_sms_tracking_t *)mem_sys;
  +    apr_lock_acquire(tms->lock);
  +    apr_lock_destroy(tms->lock);
  +    tms->lock = NULL;
  +    
  +    return APR_SUCCESS;    
  +}
  +
   static apr_status_t apr_sms_tracking_destroy(apr_sms_t *mem_sys)
   {
       apr_status_t rv;
  +    apr_sms_tracking_t *tms;
  +    apr_track_node_t *node;
       
  -    if ((rv = apr_sms_tracking_reset(mem_sys)) != APR_SUCCESS)
  -        return rv;
  +    tms = (apr_sms_tracking_t *)mem_sys;
  + 
  +    /* XXX - As we've already had the lock we've been using destroyed
  +     * in the pre_destroy function we can't use it.  However, if we
  +     * have threads trying to use the sms while another is trying to
  +     * destroy it, then we have serious problems anyway.
  +     */
  +    while (tms->nodes) {
  +        node = tms->nodes;
  +        *(node->ref) = node->next;
  +        if (node->next)
  +            node->next->ref = node->ref;
  +        if ((rv = apr_sms_free(mem_sys->parent_mem_sys, node)) != APR_SUCCESS)
  +            return rv;
  +    }
       
       return apr_sms_free(mem_sys->parent_mem_sys, mem_sys);
   }
   
  +
   APR_DECLARE(apr_status_t) apr_sms_tracking_create(apr_sms_t **mem_sys, 
                                                     apr_sms_t *pms)
   {
  -    apr_sms_t *new_mem_sys;
  +    apr_sms_t *new_sms;
       apr_sms_tracking_t *tms;
       apr_status_t rv;
   
  @@ -254,34 +289,31 @@
        * we allocate the memory for the structure from our parent.
        * This is safe as we shouldn't outlive our parent...
        */
  -    new_mem_sys = apr_sms_calloc(pms, sizeof(apr_sms_tracking_t));
  +    new_sms = apr_sms_calloc(pms, sizeof(apr_sms_tracking_t));
   
  -    if (!new_mem_sys)
  +    if (!new_sms)
           return APR_ENOMEM;
   
  -    if ((rv = apr_sms_init(new_mem_sys, pms)) != APR_SUCCESS)
  +    if ((rv = apr_sms_init(new_sms, pms)) != APR_SUCCESS)
           return rv;
  -
  -    new_mem_sys->malloc_fn  = apr_sms_tracking_malloc;
  -    new_mem_sys->calloc_fn  = apr_sms_tracking_calloc;
  -    new_mem_sys->realloc_fn = apr_sms_tracking_realloc;
  -    new_mem_sys->free_fn    = apr_sms_tracking_free;
  -    new_mem_sys->reset_fn   = apr_sms_tracking_reset;
  -    new_mem_sys->lock_fn    = apr_sms_tracking_lock;
  -    new_mem_sys->unlock_fn  = apr_sms_tracking_unlock;
  -    new_mem_sys->destroy_fn = apr_sms_tracking_destroy;
  -    new_mem_sys->identity   = module_identity;
  -
  -    apr_pool_create(&(new_mem_sys->pool), pms->pool);
  -    apr_lock_create(&(new_mem_sys->lock), APR_MUTEX, APR_LOCKALL, NULL,
  -                    new_mem_sys->pool);
   
  -    tms = (apr_sms_tracking_t *)new_mem_sys;
  +    new_sms->malloc_fn      = apr_sms_tracking_malloc;
  +    new_sms->calloc_fn      = apr_sms_tracking_calloc;
  +    new_sms->realloc_fn     = apr_sms_tracking_realloc;
  +    new_sms->free_fn        = apr_sms_tracking_free;
  +    new_sms->reset_fn       = apr_sms_tracking_reset;
  +    new_sms->destroy_fn     = apr_sms_tracking_destroy;
  +    new_sms->identity       = module_identity;
  +    new_sms->pre_destroy_fn = apr_sms_tracking_pre_destroy;
  +    
  +    tms = (apr_sms_tracking_t *)new_sms;
       tms->nodes = NULL;
  +    apr_lock_create(&tms->lock, APR_MUTEX, APR_LOCKALL, NULL,
  +                    new_sms->pool);
   
  -    apr_sms_assert(new_mem_sys);
  +    apr_sms_assert(new_sms);
   
  -    *mem_sys = new_mem_sys;
  +    *mem_sys = new_sms;
       return APR_SUCCESS;
   }
   
  
  
  

Mime
View raw message