httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <...@covalent.net>
Subject MMAP and File setaside functions
Date Thu, 14 Jun 2001 17:36:22 GMT

This is a simple patch that adds the new MMAP and file setaside functions
this works in my testing, but I had to hack on the server to make it fail,
so I can't garauntee that this will work for people who can make it fail
out of the box.  Can somebody test this and make sure it works?

Ryan

Index: server/core.c
===================================================================
RCS file: /home/cvs/httpd-2.0/server/core.c,v
retrieving revision 1.19
diff -u -d -b -w -u -r1.19 core.c
--- server/core.c	2001/06/13 13:44:39	1.19
+++ server/core.c	2001/06/14 17:04:59
@@ -2920,7 +2920,6 @@
      *     when the charset is translated).
      */
     int bld_content_md5;
-    apr_pool_t *main_pool;

     /*
      * The old way of doing handlers meant that this handler would
@@ -2965,9 +2964,8 @@
     if (r->method_number != M_GET && r->method_number != M_POST) {
         return HTTP_METHOD_NOT_ALLOWED;
     }
-    main_pool = (r->main) ? (r->main->pool) : (r->pool);

-    if ((status = apr_file_open(&fd, r->filename, APR_READ | APR_BINARY, 0, main_pool))
!= APR_SUCCESS) {
+    if ((status = apr_file_open(&fd, r->filename, APR_READ | APR_BINARY, 0, r->pool))
!= APR_SUCCESS) {
         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
 		     "file permissions deny server access: %s", r->filename);
         return HTTP_FORBIDDEN;
Index: server/protocol.c
===================================================================
RCS file: /home/cvs/httpd-2.0/server/protocol.c,v
retrieving revision 1.26
diff -u -d -b -w -u -r1.26 protocol.c
--- server/protocol.c	2001/06/13 13:44:39	1.26
+++ server/protocol.c	2001/06/14 17:05:00
@@ -881,7 +881,7 @@
     }

     if ((ctx->curr_len < AP_MIN_BYTES_TO_WRITE) && !send_it) {
-        return ap_save_brigade(f, &ctx->saved, &b, r->pool);
+        return ap_save_brigade(f, &ctx->saved, &b, (r->main) ? r->main->pool
: r->pool);
     }

     /* We will compute a content length if:
Index: srclib/apr-util/buckets/apr_buckets_file.c
===================================================================
RCS file: /home/cvs/apr-util/buckets/apr_buckets_file.c,v
retrieving revision 1.42
diff -u -d -b -w -u -r1.42 apr_buckets_file.c
--- srclib/apr-util/buckets/apr_buckets_file.c	2001/06/04 21:20:33	1.42
+++ srclib/apr-util/buckets/apr_buckets_file.c	2001/06/14 17:07:43
@@ -83,7 +83,6 @@

 #endif /* APR_HAS_MMAP */

-
 static void file_destroy(void *data)
 {
     if (apr_bucket_shared_destroy(data)) {
@@ -93,27 +92,18 @@
     }
 }

-static apr_status_t file_read(apr_bucket *e, const char **str,
-			      apr_size_t *len, apr_read_type_e block)
+static int file_make_mmap(apr_bucket *e, apr_off_t filelength,
+                           apr_off_t fileoffset, apr_pool_t *p)
 {
     apr_bucket_file *a = e->data;
     apr_file_t *f = a->fd;
-    apr_bucket *b = NULL;
-    char *buf;
-    apr_status_t rv;
-    apr_off_t filelength = e->length;  /* bytes remaining in file past offset */
-    apr_off_t fileoffset = e->start;
-#if APR_HAS_MMAP
-    apr_mmap_t *mm = NULL;
-#endif
+    apr_mmap_t *mm;

-#if APR_HAS_MMAP
     if ((filelength >= MMAP_THRESHOLD)
         && (filelength < MMAP_LIMIT)) {
         /* we need to protect ourselves in case we die while we've got the
          * file mmapped */
         apr_status_t status;
-        apr_pool_t *p = apr_file_pool_get(f);
         if ((status = apr_mmap_create(&mm, f, fileoffset, filelength,
                                       APR_MMAP_READ, p)) != APR_SUCCESS) {
             mm = NULL;
@@ -125,6 +115,24 @@
     if (mm) {
         apr_bucket_mmap_make(e, mm, 0, filelength); /*XXX: check for failure? */
         file_destroy(a);
+        return 1;
+    }
+    return 0;
+}
+
+static apr_status_t file_read(apr_bucket *e, const char **str,
+			      apr_size_t *len, apr_read_type_e block)
+{
+    apr_bucket_file *a = e->data;
+    apr_file_t *f = a->fd;
+    apr_bucket *b = NULL;
+    char *buf;
+    apr_status_t rv;
+    apr_off_t filelength = e->length;  /* bytes remaining in file past offset */
+    apr_off_t fileoffset = e->start;
+
+#if APR_HAS_MMAP
+    if (file_make_mmap(e, filelength, fileoffset, apr_file_pool_get(f))) {
         return apr_bucket_read(e, str, len, block);
     }
 #endif
@@ -200,11 +208,33 @@
     return apr_bucket_file_make(b, fd, offset, len);
 }

+APU_DECLARE_NONSTD(apr_status_t) file_setaside(apr_bucket *data, apr_pool_t *pool)
+{
+    apr_bucket_file *a = data->data;
+    apr_file_t *fd;
+    apr_file_t *f = a->fd;
+    apr_pool_t *p = apr_file_pool_get(f);
+    apr_off_t filelength = data->length;  /* bytes remaining in file past offset */
+    apr_off_t fileoffset = data->start;
+
+    if (apr_pool_is_ancestor(p, pool)) {
+        return APR_SUCCESS;
+    }
+
+#if APR_HAS_MMAP
+    if (file_make_mmap(data, filelength, fileoffset, p)) {
+        return APR_SUCCESS;
+    }
+#endif
+    apr_file_dup(&fd, f, p);
+    a->fd = fd;
+}
+
 APU_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_file = {
     "FILE", 5,
     file_destroy,
     file_read,
-    apr_bucket_setaside_notimpl,
+    file_setaside,
     apr_bucket_shared_split,
     apr_bucket_shared_copy
 };
Index: srclib/apr-util/buckets/apr_buckets_mmap.c
===================================================================
RCS file: /home/cvs/apr-util/buckets/apr_buckets_mmap.c,v
retrieving revision 1.35
diff -u -d -b -w -u -r1.35 apr_buckets_mmap.c
--- srclib/apr-util/buckets/apr_buckets_mmap.c	2001/04/30 04:11:20	1.35
+++ srclib/apr-util/buckets/apr_buckets_mmap.c	2001/06/14 17:07:43
@@ -115,11 +115,35 @@
     return apr_bucket_mmap_make(b, mm, start, length);
 }

+APU_DECLARE(apr_status_t) mmap_setaside(apr_bucket *data, apr_pool_t *p)
+{
+    apr_bucket_mmap *m;
+    apr_mmap_t *mm;
+    char *base;
+    void *addr;
+    apr_status_t ok;
+
+    m = data->data;
+    mm = m->mmap;
+    if (apr_pool_is_ancestor(mm->cntxt, p)) {
+        return APR_SUCCESS;
+    }
+
+    base = apr_pcalloc(p, data->length);
+    ok = apr_mmap_offset(&addr, m->mmap, data->start);
+    if (ok != APR_SUCCESS) {
+        return ok;
+    }
+    memcpy(base, addr, data->length);
+    data = apr_bucket_pool_make(data, base, data->length, p);
+    return APR_SUCCESS;
+}
+
 APU_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_mmap = {
     "MMAP", 5,
     mmap_destroy,
     mmap_read,
-    apr_bucket_setaside_notimpl,
+    mmap_setaside,
     apr_bucket_shared_split,
     apr_bucket_shared_copy
 };


_______________________________________________________________________________
Ryan Bloom                        	rbb@apache.org
406 29th St.
San Francisco, CA 94131
-------------------------------------------------------------------------------




Mime
View raw message