]> git.kaiwu.me - nginx.git/commitdiff
Moved the code for coalescing file buffers to a separate function.
authorValentin Bartenev <vbart@nginx.com>
Wed, 13 Aug 2014 11:11:45 +0000 (15:11 +0400)
committerValentin Bartenev <vbart@nginx.com>
Wed, 13 Aug 2014 11:11:45 +0000 (15:11 +0400)
src/core/ngx_buf.c
src/core/ngx_buf.h
src/os/unix/ngx_darwin_sendfile_chain.c
src/os/unix/ngx_freebsd_sendfile_chain.c
src/os/unix/ngx_linux_sendfile_chain.c

index 764b34bf97e12b272f73e6ddf3948264cb42fc7a..00b6644585ab6a975a155f24a877f7168026db65 100644 (file)
@@ -220,6 +220,48 @@ ngx_chain_update_chains(ngx_pool_t *p, ngx_chain_t **free, ngx_chain_t **busy,
 }
 
 
+off_t
+ngx_chain_coalesce_file(ngx_chain_t **in, off_t limit)
+{
+    off_t         total, size, aligned, fprev;
+    ngx_fd_t      fd;
+    ngx_chain_t  *cl;
+
+    total = 0;
+
+    cl = *in;
+    fd = cl->buf->file->fd;
+
+    do {
+        size = cl->buf->file_last - cl->buf->file_pos;
+
+        if (size > limit - total) {
+            size = limit - total;
+
+            aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
+                       & ~((off_t) ngx_pagesize - 1);
+
+            if (aligned <= cl->buf->file_last) {
+                size = aligned - cl->buf->file_pos;
+            }
+        }
+
+        total += size;
+        fprev = cl->buf->file_pos + size;
+        cl = cl->next;
+
+    } while (cl
+             && cl->buf->in_file
+             && total < limit
+             && fd == cl->buf->file->fd
+             && fprev == cl->buf->file_pos);
+
+    *in = cl;
+
+    return total;
+}
+
+
 ngx_chain_t *
 ngx_chain_update_sent(ngx_chain_t *in, off_t sent)
 {
index f6b2dba86d9e54cb9b9de48316dd3428cc5f1ef1..13536a69a4c6e3ea9cba5d3b001aa6f8d8f93e49 100644 (file)
@@ -158,6 +158,8 @@ ngx_chain_t *ngx_chain_get_free_buf(ngx_pool_t *p, ngx_chain_t **free);
 void ngx_chain_update_chains(ngx_pool_t *p, ngx_chain_t **free,
     ngx_chain_t **busy, ngx_chain_t **out, ngx_buf_tag_t tag);
 
+off_t ngx_chain_coalesce_file(ngx_chain_t **in, off_t limit);
+
 ngx_chain_t *ngx_chain_update_sent(ngx_chain_t *in, off_t sent);
 
 #endif /* _NGX_BUF_H_INCLUDED_ */
index d98fd74b08a375e72da03ca2ebdefd63d6e123bf..a85269dcd44cca4705d0503e1dc75fc5d594cfcf 100644 (file)
@@ -31,7 +31,7 @@ ngx_chain_t *
 ngx_darwin_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
 {
     int              rc;
-    off_t            size, send, prev_send, aligned, sent, fprev;
+    off_t            send, prev_send, sent;
     off_t            file_size;
     ngx_uint_t       eintr;
     ngx_err_t        err;
@@ -95,30 +95,9 @@ ngx_darwin_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
 
             /* coalesce the neighbouring file bufs */
 
-            do {
-                size = cl->buf->file_last - cl->buf->file_pos;
+            file_size = ngx_chain_coalesce_file(&cl, limit - send);
 
-                if (send + size > limit) {
-                    size = limit - send;
-
-                    aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
-                               & ~((off_t) ngx_pagesize - 1);
-
-                    if (aligned <= cl->buf->file_last) {
-                        size = aligned - cl->buf->file_pos;
-                    }
-                }
-
-                file_size += size;
-                send += size;
-                fprev = cl->buf->file_pos + size;
-                cl = cl->next;
-
-            } while (cl
-                     && cl->buf->in_file
-                     && send < limit
-                     && file->file->fd == cl->buf->file->fd
-                     && fprev == cl->buf->file_pos);
+            send += file_size;
         }
 
         if (file && header.count == 0) {
index 83e0f680cc67c0845ca48b51382edba13cc0f4f4..96d783e277e06c3f83b65a3ef4ead5eaa4ed7fa3 100644 (file)
@@ -33,7 +33,7 @@ ngx_chain_t *
 ngx_freebsd_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
 {
     int              rc, flags;
-    off_t            size, send, prev_send, aligned, sent, fprev;
+    off_t            send, prev_send, sent;
     size_t           file_size;
     ngx_uint_t       eintr, eagain;
     ngx_err_t        err;
@@ -99,30 +99,9 @@ ngx_freebsd_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
 
             /* coalesce the neighbouring file bufs */
 
-            do {
-                size = cl->buf->file_last - cl->buf->file_pos;
+            file_size = (size_t) ngx_chain_coalesce_file(&cl, limit - send);
 
-                if (send + size > limit) {
-                    size = limit - send;
-
-                    aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
-                               & ~((off_t) ngx_pagesize - 1);
-
-                    if (aligned <= cl->buf->file_last) {
-                        size = aligned - cl->buf->file_pos;
-                    }
-                }
-
-                file_size += (size_t) size;
-                send += size;
-                fprev = cl->buf->file_pos + size;
-                cl = cl->next;
-
-            } while (cl
-                     && cl->buf->in_file
-                     && send < limit
-                     && file->file->fd == cl->buf->file->fd
-                     && fprev == cl->buf->file_pos);
+            send += file_size;
         }
 
 
index a84a1346f00df7118681549074ac9890fb7e7660..417c19b02ef789d46e93cb17d188791afa494ac9 100644 (file)
@@ -31,7 +31,7 @@ ngx_chain_t *
 ngx_linux_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
 {
     int            rc, tcp_nodelay;
-    off_t          size, send, prev_send, aligned, sent, fprev;
+    off_t          send, prev_send, sent;
     size_t         file_size;
     ngx_err_t      err;
     ngx_buf_t     *file;
@@ -153,30 +153,9 @@ ngx_linux_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
 
             /* coalesce the neighbouring file bufs */
 
-            do {
-                size = cl->buf->file_last - cl->buf->file_pos;
+            file_size = (size_t) ngx_chain_coalesce_file(&cl, limit - send);
 
-                if (send + size > limit) {
-                    size = limit - send;
-
-                    aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
-                               & ~((off_t) ngx_pagesize - 1);
-
-                    if (aligned <= cl->buf->file_last) {
-                        size = aligned - cl->buf->file_pos;
-                    }
-                }
-
-                file_size += (size_t) size;
-                send += size;
-                fprev = cl->buf->file_pos + size;
-                cl = cl->next;
-
-            } while (cl
-                     && cl->buf->in_file
-                     && send < limit
-                     && file->file->fd == cl->buf->file->fd
-                     && fprev == cl->buf->file_pos);
+            send += file_size;
         }
 
         if (file) {