===================================================================
RCS file: /home/cvspublic/httpd-2.0/include/util_ldap.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- httpd-2.0/include/util_ldap.h	2003/02/14 16:04:00	1.11
+++ httpd-2.0/include/util_ldap.h	2003/11/06 20:45:56	1.12
@@ -75,6 +75,10 @@
 #include "http_protocol.h"
 #include "http_request.h"
 
+#if APR_HAS_SHARED_MEMORY
+#include "apr_rmm.h"
+#include "apr_shm.h"
+#endif
 
 /* Create a set of LDAP_DECLARE(type), LDLDAP_DECLARE(type) and 
  * LDAP_DECLARE_DATA with appropriate export and import tags for the platform
@@ -97,7 +101,6 @@
 #define LDAP_DECLARE_DATA             __declspec(dllimport)
 #endif
 
-
 /*
  * LDAP Connections
  */
@@ -138,9 +141,11 @@
     apr_pool_t *pool;           /* pool from which this state is allocated */
 #if APR_HAS_THREADS
     apr_thread_mutex_t *mutex;          /* mutex lock for the connection list */
+    apr_thread_rwlock_t *util_ldap_cache_lock;
 #endif
 
     apr_size_t cache_bytes;     /* Size (in bytes) of shared memory cache */
+    char *cache_file;           /* filename for shm */
     long search_cache_ttl;      /* TTL for search cache */
     long search_cache_size;     /* Size (in entries) of search cache */
     long compare_cache_ttl;     /* TTL for compare cache */
@@ -150,6 +155,15 @@
     char *cert_auth_file; 
     int   cert_file_type;
     int   ssl_support;
+
+#if APR_HAS_SHARED_MEMORY
+    apr_shm_t *cache_shm;
+    apr_rmm_t *cache_rmm;
+#endif
+
+    /* cache ald */
+    void *util_ldap_cache;
+
 } util_ldap_state_t;
 
 
@@ -286,21 +300,21 @@
  * @param reqsize The size of the shared memory segement to request. A size
  *                of zero requests the max size possible from
  *                apr_shmem_init()
- * @deffunc void util_ldap_cache_init(apr_pool_t *p)
+ * @deffunc void util_ldap_cache_init(apr_pool_t *p, util_ldap_state_t *st)
  * @return The status code returned is the status code of the
  *         apr_smmem_init() call. Regardless of the status, the cache
  *         will be set up at least for in-process or in-thread operation.
  */
-apr_status_t util_ldap_cache_init(apr_pool_t *pool, apr_size_t reqsize);
+apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st);
 
 /**
  * Display formatted stats for cache
  * @param The pool to allocate the returned string from
  * @tip This function returns a string allocated from the provided pool that describes
  *      various stats about the cache.
- * @deffunc char *util_ald_cache_display(apr_pool_t *pool)
+ * @deffunc char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
  */
-char *util_ald_cache_display(apr_pool_t *pool);
+char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st);
 
 
 /* from apr_ldap_cache_mgr.c */
@@ -310,9 +324,9 @@
  * @param The pool to allocate the returned string from
  * @tip This function returns a string allocated from the provided pool that describes
  *      various stats about the cache.
- * @deffunc char *util_ald_cache_display(apr_pool_t *pool)
+ * @deffunc char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
  */
-char *util_ald_cache_display(apr_pool_t *pool);
+char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st);
 
 #endif /* APU_HAS_LDAP */
 #endif /* UTIL_LDAP_H */

===================================================================
RCS file: /home/cvspublic/httpd-2.0/modules/experimental/util_ldap.c,v
retrieving revision 1.14
retrieving revision 1.16
diff -u -r1.14 -r1.16
--- httpd-2.0/modules/experimental/util_ldap.c	2003/04/04 13:47:13	1.14
+++ httpd-2.0/modules/experimental/util_ldap.c	2003/11/11 19:10:56	1.16
@@ -141,6 +141,7 @@
  */
 int util_ldap_handler(request_rec *r)
 {
+    util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
 
     r->allowed |= (1 << M_GET);
     if (r->method_number != M_GET)
@@ -171,7 +172,7 @@
              "</tr>\n", r
             );
 
-    ap_rputs(util_ald_cache_display(r->pool), r);
+    ap_rputs(util_ald_cache_display(r->pool, st), r);
 
     ap_rputs("</table>\n</p>\n", r);
 
@@ -506,9 +507,7 @@
     LDAPMessage *res, *entry;
     char *searchdn;
 
-    util_ldap_state_t *st = 
-        (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
-        &ldap_module);
+    util_ldap_state_t *st =  (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
 
     /* read lock this function */
     LDAP_CACHE_LOCK_CREATE(st->pool);
@@ -517,7 +516,7 @@
     LDAP_CACHE_WRLOCK();
 
     curnode.url = url;
-    curl = util_ald_cache_fetch(util_ldap_cache, &curnode);
+    curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
     if (curl == NULL) {
         curl = util_ald_create_caches(st, url);
     }
@@ -637,7 +636,7 @@
     /* get cache entry (or create one) */
     LDAP_CACHE_WRLOCK();
     curnode.url = url;
-    curl = util_ald_cache_fetch(util_ldap_cache, &curnode);
+    curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
     if (curl == NULL) {
         curl = util_ald_create_caches(st, url);
     }
@@ -760,7 +759,7 @@
     /* Get the cache node for this url */
     LDAP_CACHE_WRLOCK();
     curnode.url = url;
-    curl = (util_url_node_t *)util_ald_cache_fetch(util_ldap_cache, &curnode);
+    curl = (util_url_node_t *)util_ald_cache_fetch(st->util_ldap_cache, &curnode);
     if (curl == NULL) {
         curl = util_ald_create_caches(st, url);
     }
@@ -962,6 +961,26 @@
     return NULL;
 }
 
+static const char *util_ldap_set_cache_file(cmd_parms *cmd, void *dummy, const char *file)
+{
+    util_ldap_state_t *st = 
+        (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
+                                                  &ldap_module);
+
+    if (file) {
+        st->cache_file = ap_server_root_relative(st->pool, file);
+    }
+    else {
+        st->cache_file = NULL;
+    }
+
+    ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
+                 "LDAP cache: Setting shared memory cache file to %s bytes.", 
+                 st->cache_file);
+
+    return NULL;
+}
+
 static const char *util_ldap_set_cache_ttl(cmd_parms *cmd, void *dummy, const char *ttl)
 {
     util_ldap_state_t *st = 
@@ -1091,22 +1110,6 @@
     return st;
 }
 
-static void util_ldap_init_module(apr_pool_t *pool, server_rec *s)
-{
-    util_ldap_state_t *st = 
-        (util_ldap_state_t *)ap_get_module_config(s->module_config, 
-						  &ldap_module);
-
-    apr_status_t result = util_ldap_cache_init(pool, st->cache_bytes);
-    char buf[MAX_STRING_LEN];
-
-    apr_strerror(result, buf, sizeof(buf));
-    ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, result, s, 
-                      "[%d] ldap cache init: %s", 
-                      getpid(), buf);
-}
-
-
 static apr_status_t util_ldap_cleanup_module(void *data)
 {
     server_rec *s = data;
@@ -1128,13 +1131,46 @@
                                  apr_pool_t *ptemp, server_rec *s)
 {
     int rc = LDAP_SUCCESS;
+    apr_status_t result;
+    char buf[MAX_STRING_LEN];
 
-    util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
-                                                s->module_config, 
-                                                &ldap_module);
+    util_ldap_state_t *st =
+        (util_ldap_state_t *)ap_get_module_config(s->module_config, &ldap_module);
 
-        /* log the LDAP SDK used 
-        */
+#if APR_HAS_SHARED_MEMORY
+    server_rec *s_vhost;
+    util_ldap_state_t *st_vhost;
+    
+    /* initializing cache if file is here and we already don't have shm addr*/
+    if (st->cache_file && !st->cache_shm) {
+#endif
+        result = util_ldap_cache_init(p, st);
+        apr_strerror(result, buf, sizeof(buf));
+        ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, result, s,
+                     "LDAP cache init: %s", buf);
+
+#if APR_HAS_SHARED_MEMORY
+        /* merge config in all vhost */
+        s_vhost = s->next;
+        while (s_vhost) {
+            ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, result, s, 
+                         "LDAP merging Shared Cache conf: shm=0x%x rmm=0x%x for VHOST: %s",
+                         st->cache_shm, st->cache_rmm, s_vhost->server_hostname);
+
+            st_vhost = (util_ldap_state_t *)ap_get_module_config(s_vhost->module_config, &ldap_module);
+            st_vhost->cache_shm = st->cache_shm;
+            st_vhost->cache_rmm = st->cache_rmm;
+            st_vhost->cache_file = st->cache_file;
+            s_vhost = s_vhost->next;
+        }
+    }
+    else {
+        ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0 , s, "LDAP cache: Unable to init Shared Cache: no file");
+    }
+#endif
+    
+    /* log the LDAP SDK used 
+     */
     #if APR_HAS_NETSCAPE_LDAPSDK 
     
         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
@@ -1297,6 +1333,10 @@
                   "Sets the size of the shared memory cache in bytes. "
                   "Zero means disable the shared memory cache. Defaults to 100KB."),
 
+    AP_INIT_TAKE1("LDAPSharedCacheFile", util_ldap_set_cache_file, NULL, RSRC_CONF,
+                  "Sets the file of the shared memory cache."
+                  "Nothing means disable the shared memory cache."),
+
     AP_INIT_TAKE1("LDAPCacheEntries", util_ldap_set_cache_entries, NULL, RSRC_CONF,
                   "Sets the maximum number of entries that are possible in the LDAP "
                   "search cache. "
@@ -1331,7 +1371,6 @@
 static void util_ldap_register_hooks(apr_pool_t *p)
 {
     ap_hook_post_config(util_ldap_post_config,NULL,NULL,APR_HOOK_MIDDLE);
-    ap_hook_child_init(util_ldap_init_module, NULL, NULL, APR_HOOK_MIDDLE);
     ap_hook_handler(util_ldap_handler, NULL, NULL, APR_HOOK_MIDDLE);
 }
 
===================================================================
RCS file: /home/cvspublic/httpd-2.0/modules/experimental/util_ldap_cache.c,v
retrieving revision 1.8
retrieving revision 1.10
diff -u -r1.8 -r1.10
--- httpd-2.0/modules/experimental/util_ldap_cache.c	2003/01/21 10:53:09	1.8
+++ httpd-2.0/modules/experimental/util_ldap_cache.c	2003/11/11 19:10:56	1.10
@@ -66,8 +66,9 @@
 
 #ifdef APU_HAS_LDAP
 
+#if APR_HAS_SHARED_MEMORY
 #define MODLDAP_SHMEM_CACHE "/tmp/mod_ldap_cache"
-
+#endif
 
 /* ------------------------------------------------------------------ */
 
@@ -85,14 +86,14 @@
     return(strcmp(na->url, nb->url) == 0);
 }
 
-void *util_ldap_url_node_copy(void *c)
+void *util_ldap_url_node_copy(util_ald_cache_t *cache, void *c)
 {
     util_url_node_t *n = (util_url_node_t *)c;
-    util_url_node_t *node = (util_url_node_t *)util_ald_alloc(sizeof(util_url_node_t));
+    util_url_node_t *node = (util_url_node_t *)util_ald_alloc(cache, sizeof(util_url_node_t));
 
     if (node) {
-        if (!(node->url = util_ald_strdup(n->url))) {
-            util_ald_free(node->url);
+        if (!(node->url = util_ald_strdup(cache, n->url))) {
+            util_ald_free(cache, node->url);
             return NULL;
         }
         node->search_cache = n->search_cache;
@@ -105,15 +106,15 @@
     }
 }
 
-void util_ldap_url_node_free(void *n)
+void util_ldap_url_node_free(util_ald_cache_t *cache, void *n)
 {
     util_url_node_t *node = (util_url_node_t *)n;
 
-    util_ald_free(node->url);
+    util_ald_free(cache, node->url);
     util_ald_destroy_cache(node->search_cache);
     util_ald_destroy_cache(node->compare_cache);
     util_ald_destroy_cache(node->dn_compare_cache);
-    util_ald_free(node);
+    util_ald_free(cache, node);
 }
 
 /* ------------------------------------------------------------------ */
@@ -131,10 +132,10 @@
 		  ((util_search_node_t *)b)->username) == 0);
 }
 
-void *util_ldap_search_node_copy(void *c)
+void *util_ldap_search_node_copy(util_ald_cache_t *cache, void *c)
 {
     util_search_node_t *node = (util_search_node_t *)c;
-    util_search_node_t *newnode = util_ald_alloc(sizeof(util_search_node_t));
+    util_search_node_t *newnode = util_ald_alloc(cache, sizeof(util_search_node_t));
 
     /* safety check */
     if (newnode) {
@@ -144,13 +145,13 @@
             int k = 0;
             int i = 0;
             while (node->vals[k++]);
-            if (!(newnode->vals = util_ald_alloc(sizeof(char *) * (k+1)))) {
-                util_ldap_search_node_free(newnode);
+            if (!(newnode->vals = util_ald_alloc(cache, sizeof(char *) * (k+1)))) {
+                util_ldap_search_node_free(cache, newnode);
                 return NULL;
             }
             while (node->vals[i]) {
-                if (!(newnode->vals[i] = util_ald_strdup(node->vals[i]))) {
-                    util_ldap_search_node_free(newnode);
+                if (!(newnode->vals[i] = util_ald_strdup(cache, node->vals[i]))) {
+                    util_ldap_search_node_free(cache, newnode);
                     return NULL;
                 }
                 i++;
@@ -159,10 +160,10 @@
         else {
             newnode->vals = NULL;
         }
-        if (!(newnode->username = util_ald_strdup(node->username)) ||
-            !(newnode->dn = util_ald_strdup(node->dn)) ||
-            !(newnode->bindpw = util_ald_strdup(node->bindpw)) ) {
-            util_ldap_search_node_free(newnode);
+        if (!(newnode->username = util_ald_strdup(cache, node->username)) ||
+            !(newnode->dn = util_ald_strdup(cache, node->dn)) ||
+            !(newnode->bindpw = util_ald_strdup(cache, node->bindpw)) ) {
+            util_ldap_search_node_free(cache, newnode);
             return NULL;
         }
         newnode->lastbind = node->lastbind;
@@ -171,20 +172,20 @@
     return (void *)newnode;
 }
 
-void util_ldap_search_node_free(void *n)
+void util_ldap_search_node_free(util_ald_cache_t *cache, void *n)
 {
     int i = 0;
     util_search_node_t *node = (util_search_node_t *)n;
     if (node->vals) {
         while (node->vals[i]) {
-            util_ald_free(node->vals[i++]);
+            util_ald_free(cache, node->vals[i++]);
         }
-        util_ald_free(node->vals);
+        util_ald_free(cache, node->vals);
     }
-    util_ald_free(node->username);
-    util_ald_free(node->dn);
-    util_ald_free(node->bindpw);
-    util_ald_free(node);
+    util_ald_free(cache, node->username);
+    util_ald_free(cache, node->dn);
+    util_ald_free(cache, node->bindpw);
+    util_ald_free(cache, node);
 }
 
 /* ------------------------------------------------------------------ */
@@ -204,16 +205,16 @@
 	    strcmp(na->value, nb->value) == 0);
 }
 
-void *util_ldap_compare_node_copy(void *c)
+void *util_ldap_compare_node_copy(util_ald_cache_t *cache, void *c)
 {
     util_compare_node_t *n = (util_compare_node_t *)c;
-    util_compare_node_t *node = (util_compare_node_t *)util_ald_alloc(sizeof(util_compare_node_t));
+    util_compare_node_t *node = (util_compare_node_t *)util_ald_alloc(cache, sizeof(util_compare_node_t));
 
     if (node) {
-        if (!(node->dn = util_ald_strdup(n->dn)) ||
-            !(node->attrib = util_ald_strdup(n->attrib)) ||
-            !(node->value = util_ald_strdup(n->value))) {
-            util_ldap_compare_node_free(node);
+        if (!(node->dn = util_ald_strdup(cache, n->dn)) ||
+            !(node->attrib = util_ald_strdup(cache, n->attrib)) ||
+            !(node->value = util_ald_strdup(cache, n->value))) {
+            util_ldap_compare_node_free(cache, node);
             return NULL;
         }
         node->lastcompare = n->lastcompare;
@@ -225,13 +226,13 @@
     }
 }
 
-void util_ldap_compare_node_free(void *n)
+void util_ldap_compare_node_free(util_ald_cache_t *cache, void *n)
 {
     util_compare_node_t *node = (util_compare_node_t *)n;
-    util_ald_free(node->dn);
-    util_ald_free(node->attrib);
-    util_ald_free(node->value);
-    util_ald_free(node);
+    util_ald_free(cache, node->dn);
+    util_ald_free(cache, node->attrib);
+    util_ald_free(cache, node->value);
+    util_ald_free(cache, node);
 }
 
 /* ------------------------------------------------------------------ */
@@ -247,14 +248,14 @@
 		   ((util_dn_compare_node_t *)b)->reqdn) == 0);
 }
 
-void *util_ldap_dn_compare_node_copy(void *c)
+void *util_ldap_dn_compare_node_copy(util_ald_cache_t *cache, void *c)
 {
     util_dn_compare_node_t *n = (util_dn_compare_node_t *)c;
-    util_dn_compare_node_t *node = (util_dn_compare_node_t *)util_ald_alloc(sizeof(util_dn_compare_node_t));
+    util_dn_compare_node_t *node = (util_dn_compare_node_t *)util_ald_alloc(cache, sizeof(util_dn_compare_node_t));
     if (node) {
-        if (!(node->reqdn = util_ald_strdup(n->reqdn)) ||
-            !(node->dn = util_ald_strdup(n->dn))) {
-            util_ldap_dn_compare_node_free(node);
+        if (!(node->reqdn = util_ald_strdup(cache, n->reqdn)) ||
+            !(node->dn = util_ald_strdup(cache, n->dn))) {
+            util_ldap_dn_compare_node_free(cache, node);
             return NULL;
         }
         return node;
@@ -264,12 +265,12 @@
     }
 }
 
-void util_ldap_dn_compare_node_free(void *n)
+void util_ldap_dn_compare_node_free(util_ald_cache_t *cache, void *n)
 {
     util_dn_compare_node_t *node = (util_dn_compare_node_t *)n;
-    util_ald_free(node->reqdn);
-    util_ald_free(node->dn);
-    util_ald_free(node);
+    util_ald_free(cache, node->reqdn);
+    util_ald_free(cache, node->dn);
+    util_ald_free(cache, node);
 }
 
 
@@ -279,46 +280,53 @@
 
 apr_status_t util_ldap_cache_module_kill(void *data)
 {
+    util_ldap_state_t *st = (util_ldap_state_t *)data;
+
 #if APR_HAS_SHARED_MEMORY
-    if (util_ldap_shm != NULL) {
-        apr_status_t result = apr_shm_destroy(util_ldap_shm);
-        util_ldap_shm = NULL;
+    if (st->cache_shm != NULL) {
+        apr_status_t result = apr_shm_destroy(st->cache_shm);
+        st->cache_shm = NULL;
+        apr_file_remove(st->cache_file, st->pool);
         return result;
     }
 #endif
-    util_ald_destroy_cache(util_ldap_cache);
+    util_ald_destroy_cache(st->util_ldap_cache);
     return APR_SUCCESS;
 }
 
-apr_status_t util_ldap_cache_init(apr_pool_t *pool, apr_size_t reqsize)
+apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st)
 {
 #if APR_HAS_SHARED_MEMORY
     apr_status_t result;
 
-    result = apr_shm_create(&util_ldap_shm, reqsize, MODLDAP_SHMEM_CACHE, pool);
+    if (!st->cache_file) {
+    	return -1;
+    }
+
+    result = apr_shm_create(&st->cache_shm, st->cache_bytes, st->cache_file, st->pool);
     if (result == EEXIST) {
         /*
          * The cache could have already been created (i.e. we may be a child process).  See
          * if we can attach to the existing shared memory
          */
-        result = apr_shm_attach(&util_ldap_shm, MODLDAP_SHMEM_CACHE, pool);
+        result = apr_shm_attach(&st->cache_shm, st->cache_file, st->pool);
     } 
     if (result != APR_SUCCESS) {
         return result;
     }
 
     /* This will create a rmm "handler" to get into the shared memory area */
-    apr_rmm_init(&util_ldap_rmm, NULL,
-			(void *)apr_shm_baseaddr_get(util_ldap_shm), reqsize, pool);
+    apr_rmm_init(&st->cache_rmm, NULL, (void *)apr_shm_baseaddr_get(st->cache_shm), st->cache_bytes, st->pool);
 #endif
 
-    apr_pool_cleanup_register(pool, NULL, util_ldap_cache_module_kill, apr_pool_cleanup_null);
+    apr_pool_cleanup_register(st->pool, st , util_ldap_cache_module_kill, apr_pool_cleanup_null);
 
-    util_ldap_cache = util_ald_create_cache(50,
-				     util_ldap_url_node_hash,
-				     util_ldap_url_node_compare,
-				     util_ldap_url_node_copy,
-				     util_ldap_url_node_free);
+    st->util_ldap_cache =
+        util_ald_create_cache(st,
+                              util_ldap_url_node_hash,
+                              util_ldap_url_node_compare,
+                              util_ldap_url_node_copy,
+                              util_ldap_url_node_free);
     return APR_SUCCESS;
 }
 
===================================================================
RCS file: /home/cvspublic/httpd-2.0/modules/experimental/util_ldap_cache.h,v
retrieving revision 1.6
retrieving revision 1.8
diff -u -r1.6 -r1.8
--- httpd-2.0/modules/experimental/util_ldap_cache.h	2003/01/21 10:53:09	1.6
+++ httpd-2.0/modules/experimental/util_ldap_cache.h	2003/11/11 19:10:56	1.8
@@ -68,8 +68,10 @@
  * LDAP Cache Manager
  */
 
+#if APR_HAS_SHARED_MEMORY
 #include <apr_shm.h>
 #include <apr_rmm.h> /* EDD */
+#endif
 
 typedef struct util_cache_node_t {
     void *payload;		/* Pointer to the payload */
@@ -77,47 +79,48 @@
     struct util_cache_node_t *next;
 } util_cache_node_t;
 
-typedef struct util_ald_cache_t {
-    unsigned long size;		/* Size of cache array */
-    unsigned long maxentries;	/* Maximum number of cache entries */
-    unsigned long numentries;	/* Current number of cache entries */
-    unsigned long fullmark;	/* Used to keep track of when cache becomes 3/4 full */
-    apr_time_t marktime;	/* Time that the cache became 3/4 full */
-    unsigned long (*hash)(void *);  /* Func to hash the payload */
-    int (*compare)(void *, void *); /* Func to compare two payloads */
-    void * (*copy)(void *);	/* Func to alloc mem and copy payload to new mem */
-    void (*free)(void *);	/* Func to free mem used by the payload */
+typedef struct util_ald_cache util_ald_cache_t;
+
+struct util_ald_cache {
+    unsigned long size;	                /* Size of cache array */
+    unsigned long maxentries;           /* Maximum number of cache entries */
+    unsigned long numentries;           /* Current number of cache entries */
+    unsigned long fullmark;             /* Used to keep track of when cache becomes 3/4 full */
+    apr_time_t marktime;                /* Time that the cache became 3/4 full */
+    unsigned long (*hash)(void *);      /* Func to hash the payload */
+    int (*compare)(void *, void *);     /* Func to compare two payloads */
+    void * (*copy)(util_ald_cache_t *cache, void *); /* Func to alloc mem and copy payload to new mem */
+    void (*free)(util_ald_cache_t *cache, void *); /* Func to free mem used by the payload */
     util_cache_node_t **nodes;
 
-    unsigned long numpurges;	/* No. of times the cache has been purged */
-    double avg_purgetime;	/* Average time to purge the cache */
-    apr_time_t last_purge;	/* Time of the last purge */
-    unsigned long npurged;	/* Number of elements purged in last purge. This is not
-				   obvious: it won't be 3/4 the size of the cache if 
-				   there were a lot of expired entries. */
-
-    unsigned long fetches;	/* Number of fetches */
-    unsigned long hits;		/* Number of cache hits */
-    unsigned long inserts;	/* Number of inserts */
-    unsigned long removes;	/* Number of removes */
-} util_ald_cache_t;
+    unsigned long numpurges;    /* No. of times the cache has been purged */
+    double avg_purgetime;       /* Average time to purge the cache */
+    apr_time_t last_purge;      /* Time of the last purge */
+    unsigned long npurged;      /* Number of elements purged in last purge. This is not
+                                   obvious: it won't be 3/4 the size of the cache if 
+                                   there were a lot of expired entries. */
+
+    unsigned long fetches;      /* Number of fetches */
+    unsigned long hits;         /* Number of cache hits */
+    unsigned long inserts;      /* Number of inserts */
+    unsigned long removes;      /* Number of removes */
 
 #if APR_HAS_SHARED_MEMORY
-apr_shm_t *util_ldap_shm;
-apr_rmm_t *util_ldap_rmm;
+    apr_shm_t *shm_addr;
+    apr_rmm_t *rmm_addr;
 #endif
-util_ald_cache_t *util_ldap_cache;
+
+};
 
 #if APR_HAS_THREADS
-apr_thread_rwlock_t *util_ldap_cache_lock;
 #define LDAP_CACHE_LOCK_CREATE(p) \
-    if (!util_ldap_cache_lock) apr_thread_rwlock_create(&util_ldap_cache_lock, p)
+    if (!st->util_ldap_cache_lock) apr_thread_rwlock_create(&st->util_ldap_cache_lock, st->pool)
 #define LDAP_CACHE_WRLOCK() \
-    apr_thread_rwlock_wrlock(util_ldap_cache_lock)
+    apr_thread_rwlock_wrlock(st->util_ldap_cache_lock)
 #define LDAP_CACHE_UNLOCK() \
-    apr_thread_rwlock_unlock(util_ldap_cache_lock)
+    apr_thread_rwlock_unlock(st->util_ldap_cache_lock)
 #define LDAP_CACHE_RDLOCK() \
-    apr_thread_rwlock_rdlock(util_ldap_cache_lock)
+    apr_thread_rwlock_rdlock(st->util_ldap_cache_lock)
 #else
 #define LDAP_CACHE_LOCK_CREATE(p)
 #define LDAP_CACHE_WRLOCK()
@@ -192,35 +195,39 @@
 /* util_ldap_cache.c */
 unsigned long util_ldap_url_node_hash(void *n);
 int util_ldap_url_node_compare(void *a, void *b);
-void *util_ldap_url_node_copy(void *c);
-void util_ldap_url_node_free(void *n);
+void *util_ldap_url_node_copy(util_ald_cache_t *cache, void *c);
+void util_ldap_url_node_free(util_ald_cache_t *cache, void *n);
 unsigned long util_ldap_search_node_hash(void *n);
 int util_ldap_search_node_compare(void *a, void *b);
-void *util_ldap_search_node_copy(void *c);
-void util_ldap_search_node_free(void *n);
+void *util_ldap_search_node_copy(util_ald_cache_t *cache, void *c);
+void util_ldap_search_node_free(util_ald_cache_t *cache, void *n);
 unsigned long util_ldap_compare_node_hash(void *n);
 int util_ldap_compare_node_compare(void *a, void *b);
-void *util_ldap_compare_node_copy(void *c);
-void util_ldap_compare_node_free(void *n);
+void *util_ldap_compare_node_copy(util_ald_cache_t *cache, void *c);
+void util_ldap_compare_node_free(util_ald_cache_t *cache, void *n);
 unsigned long util_ldap_dn_compare_node_hash(void *n);
 int util_ldap_dn_compare_node_compare(void *a, void *b);
-void *util_ldap_dn_compare_node_copy(void *c);
-void util_ldap_dn_compare_node_free(void *n);
+void *util_ldap_dn_compare_node_copy(util_ald_cache_t *cache, void *c);
+void util_ldap_dn_compare_node_free(util_ald_cache_t *cache, void *n);
 
 
 /* util_ldap_cache_mgr.c */
 
-void util_ald_free(const void *ptr);
-void *util_ald_alloc(unsigned long size);
-const char *util_ald_strdup(const char *s);
+/* Cache alloc and free function, dealing or not with shm */
+void util_ald_free(util_ald_cache_t *cache, const void *ptr);
+void *util_ald_alloc(util_ald_cache_t *cache, unsigned long size);
+const char *util_ald_strdup(util_ald_cache_t *cache, const char *s);
+
+/* Cache managing function */
 unsigned long util_ald_hash_string(int nstr, ...);
 void util_ald_cache_purge(util_ald_cache_t *cache);
 util_url_node_t *util_ald_create_caches(util_ldap_state_t *s, const char *url);
-util_ald_cache_t *util_ald_create_cache(unsigned long maxentries,
+util_ald_cache_t *util_ald_create_cache(util_ldap_state_t *st,
                                 unsigned long (*hashfunc)(void *), 
                                 int (*comparefunc)(void *, void *),
-                                void * (*copyfunc)(void *),
-                                void (*freefunc)(void *));
+                                void * (*copyfunc)(util_ald_cache_t *cache, void *),
+                                void (*freefunc)(util_ald_cache_t *cache, void *));
+                                
 void util_ald_destroy_cache(util_ald_cache_t *cache);
 void *util_ald_cache_fetch(util_ald_cache_t *cache, void *payload);
 void util_ald_cache_insert(util_ald_cache_t *cache, void *payload);
===================================================================
RCS file: /home/cvspublic/httpd-2.0/modules/experimental/util_ldap_cache_mgr.c,v
retrieving revision 1.5
retrieving revision 1.10
diff -u -r1.5 -r1.10
--- httpd-2.0/modules/experimental/util_ldap_cache_mgr.c	2003/01/21 10:53:09	1.5
+++ httpd-2.0/modules/experimental/util_ldap_cache_mgr.c	2003/12/16 20:20:09	1.10
@@ -65,7 +65,7 @@
 #include "util_ldap_cache.h"
 #include <apr_strings.h>
 
-#ifdef APU_HAS_LDAP
+#if APR_HAS_LDAP
 
 /* only here until strdup is gone */
 #include <string.h>
@@ -112,14 +112,17 @@
   0
 };
 
-void util_ald_free(const void *ptr)
+void util_ald_free(util_ald_cache_t *cache, const void *ptr)
 {
 #if APR_HAS_SHARED_MEMORY
-    if (util_ldap_shm) {
+    if (cache->rmm_addr) {
         if (ptr)
-            apr_rmm_free(util_ldap_rmm, apr_rmm_offset_get(util_ldap_rmm, (void *)ptr));
-    } else {
+            /* Free in shared memory */
+            apr_rmm_free(cache->rmm_addr, apr_rmm_offset_get(cache->rmm_addr, (void *)ptr));
+    }
+    else {
         if (ptr)
+            /* Cache shm is not used */
             free((void *)ptr);
     }
 #else
@@ -128,14 +131,17 @@
 #endif
 }
 
-void *util_ald_alloc(unsigned long size)
+void *util_ald_alloc(util_ald_cache_t *cache, unsigned long size)
 {
     if (0 == size)
         return NULL;
 #if APR_HAS_SHARED_MEMORY
-    if (util_ldap_shm) {
-        return (void *)apr_rmm_addr_get(util_ldap_rmm, apr_rmm_calloc(util_ldap_rmm, size));
-    } else {
+    if (cache->rmm_addr) {
+        /* allocate from shared memory */
+        return (void *)apr_rmm_addr_get(cache->rmm_addr, apr_rmm_calloc(cache->rmm_addr, size));
+    }
+    else {
+        /* Cache shm is not used */
         return (void *)calloc(sizeof(char), size);
     }
 #else
@@ -143,11 +149,12 @@
 #endif
 }
 
-const char *util_ald_strdup(const char *s)
+const char *util_ald_strdup(util_ald_cache_t *cache, const char *s)
 {
 #if APR_HAS_SHARED_MEMORY
-    if (util_ldap_shm) {
-        char *buf = (char *)apr_rmm_addr_get(util_ldap_rmm, apr_rmm_calloc(util_ldap_rmm, strlen(s)+1));
+    if (cache->rmm_addr) {
+        /* allocate from shared memory */
+        char *buf = (char *)apr_rmm_addr_get(cache->rmm_addr, apr_rmm_calloc(cache->rmm_addr, strlen(s)+1));
         if (buf) {
             strcpy(buf, s);
             return buf;
@@ -156,6 +163,7 @@
             return NULL;
         }
     } else {
+        /* Cache shm is not used */
         return strdup(s);
     }
 #else
@@ -217,8 +225,8 @@
         while (p != NULL) {
             if (p->add_time < cache->marktime) {
 	        q = p->next;
-	        (*cache->free)(p->payload);
-	        util_ald_free(p);
+	        (*cache->free)(cache, p->payload);
+	        util_ald_free(cache, p);
 	        cache->numentries--;
 	        cache->npurged++;
 	        p = q;
@@ -247,17 +255,17 @@
     util_ald_cache_t *dn_compare_cache;
 
     /* create the three caches */
-    search_cache = util_ald_create_cache(st->search_cache_size,
+    search_cache = util_ald_create_cache(st,
 					  util_ldap_search_node_hash,
 					  util_ldap_search_node_compare,
 					  util_ldap_search_node_copy,
 					  util_ldap_search_node_free);
-    compare_cache = util_ald_create_cache(st->compare_cache_size,
+    compare_cache = util_ald_create_cache(st,
 					   util_ldap_compare_node_hash,
 					   util_ldap_compare_node_compare,
 					   util_ldap_compare_node_copy,
 					   util_ldap_compare_node_free);
-    dn_compare_cache = util_ald_create_cache(st->compare_cache_size,
+    dn_compare_cache = util_ald_create_cache(st,
 					      util_ldap_dn_compare_node_hash,
 					      util_ldap_dn_compare_node_compare,
 					      util_ldap_dn_compare_node_copy,
@@ -272,7 +280,7 @@
         curl->compare_cache = compare_cache;
         curl->dn_compare_cache = dn_compare_cache;
 
-        util_ald_cache_insert(util_ldap_cache, curl);
+        util_ald_cache_insert(st->util_ldap_cache, curl);
 
     }
 
@@ -280,32 +288,36 @@
 }
 
 
-util_ald_cache_t *util_ald_create_cache(unsigned long maxentries,
+util_ald_cache_t *util_ald_create_cache(util_ldap_state_t *st,
                                 unsigned long (*hashfunc)(void *), 
                                 int (*comparefunc)(void *, void *),
-                                void * (*copyfunc)(void *),
-                                void (*freefunc)(void *))
+                                void * (*copyfunc)(util_ald_cache_t *cache, void *),
+                                void (*freefunc)(util_ald_cache_t *cache, void *))
 {
     util_ald_cache_t *cache;
     unsigned long i;
 
-    if (maxentries <= 0)
+    if (st->search_cache_size <= 0)
         return NULL;
 
-    cache = (util_ald_cache_t *)util_ald_alloc(sizeof(util_ald_cache_t));
+#if APR_HAS_SHARED_MEMORY
+    cache = (util_ald_cache_t *)apr_rmm_addr_get(st->cache_rmm, apr_rmm_calloc(st->cache_rmm, sizeof(util_ald_cache_t)));
+#else
+    cache = (util_ald_cache_t *)calloc(sizeof(util_ald_cache_t), 1);
+#endif
     if (!cache)
         return NULL;
 
-    cache->maxentries = maxentries;
+    cache->maxentries = st->search_cache_size;
     cache->numentries = 0;
-    cache->size = maxentries / 3;
+    cache->size = st->search_cache_size / 3;
     if (cache->size < 64) cache->size = 64;
         for (i = 0; primes[i] && primes[i] < cache->size; ++i) ;
             cache->size = primes[i]? primes[i] : primes[i-1];
 
-    cache->nodes = (util_cache_node_t **)util_ald_alloc(cache->size * sizeof(util_cache_node_t *));
+    cache->nodes = (util_cache_node_t **)util_ald_alloc(cache, cache->size * sizeof(util_cache_node_t *));
     if (!cache->nodes) {
-        util_ald_free(cache);
+        util_ald_free(cache, cache);
         return NULL;
     }
 
@@ -345,13 +357,13 @@
         q = NULL;
         while (p != NULL) {
             q = p->next;
-           (*cache->free)(p->payload);
-           util_ald_free(p);
+           (*cache->free)(cache, p->payload);
+           util_ald_free(cache, p);
            p = q;
         }
     }
-    util_ald_free(cache->nodes);
-    util_ald_free(cache);
+    util_ald_free(cache, cache->nodes);
+    util_ald_free(cache, cache);
 }
 
 void *util_ald_cache_fetch(util_ald_cache_t *cache, void *payload)
@@ -390,11 +402,13 @@
     if (cache == NULL || payload == NULL)
         return;
 
+    if ((node = (util_cache_node_t *)util_ald_alloc(cache, sizeof(util_cache_node_t))) == NULL)
+        return;
+
     cache->inserts++;
     hashval = (*cache->hash)(payload) % cache->size;
-    node = (util_cache_node_t *)util_ald_alloc(sizeof(util_cache_node_t));
     node->add_time = apr_time_now();
-    node->payload = (*cache->copy)(payload);
+    node->payload = (*cache->copy)(cache, payload);
     node->next = cache->nodes[hashval];
     cache->nodes[hashval] = node;
     if (++cache->numentries == cache->fullmark) 
@@ -431,8 +445,8 @@
         /* We found the node and it's not the first in the list */
         q->next = p->next;
     }
-    (*cache->free)(p->payload);
-    util_ald_free(p);
+    (*cache->free)(cache, p->payload);
+    util_ald_free(cache, p);
     cache->numentries--;
 }
 
@@ -499,16 +513,19 @@
     return buf;
 }
 
-char *util_ald_cache_display(apr_pool_t *pool)
+char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
 {
     unsigned long i;
     char *buf, *t1, *t2, *t3;
 
+    util_ald_cache_t *util_ldap_cache = st->util_ldap_cache;
+
+
     if (!util_ldap_cache) {
         return "<tr valign='top'><td nowrap colspan=7>Cache has not been enabled/initialised.</td></tr>";
     }
 
-    buf = util_ald_cache_display_stats(pool, util_ldap_cache, "LDAP URL Cache");
+    buf = util_ald_cache_display_stats(pool, st->util_ldap_cache, "LDAP URL Cache");
 
     for (i=0; i < util_ldap_cache->size; ++i) {
         util_cache_node_t *p;
@@ -535,4 +552,4 @@
     return buf;
 }
 
-#endif /* APU_HAS_LDAP */
+#endif /* APR_HAS_LDAP */

