Skip to content

Commit 8b22ee8

Browse files
committed
freebsd code path optimisation attempt
from regular file to socket cases.
1 parent 1b97d61 commit 8b22ee8

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,7 @@ PHP_ADD_SOURCES_X([main],
16871687
PHP_ADD_SOURCES([main/io], m4_normalize([
16881688
php_io.c
16891689
php_io_copy_linux.c
1690+
php_io_copy_freebsd.c
16901691
]),
16911692
[-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1])
16921693

main/io/php_io_copy_freebsd.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifdef __FreeBSD__
2+
3+
#include "php_io_internal.h"
4+
#include <sys/types.h>
5+
#include <sys/socket.h>
6+
#include <sys/uio.h>
7+
#include <unistd.h>
8+
#include <errno.h>
9+
10+
static ssize_t php_io_freebsd_sendfile(int src_fd, int dest_fd, size_t maxlen)
11+
{
12+
off_t total_sent = 0;
13+
size_t remaining = (maxlen == PHP_IO_COPY_ALL) ? 0 : maxlen;
14+
15+
while (maxlen == PHP_IO_COPY_ALL || remaining > 0) {
16+
off_t sent_in_this_call = 0;
17+
int result = sendfile(src_fd, dest_fd, total_sent, remaining, NULL, &sent_in_this_call, 0);
18+
19+
if (sent_in_this_call > 0) {
20+
total_sent += sent_in_this_call;
21+
if (maxlen != PHP_IO_COPY_ALL) {
22+
remaining -= (size_t)sent_in_this_call;
23+
}
24+
}
25+
26+
if (result == 0) {
27+
if (sent_in_this_call == 0 || remaining == 0) {
28+
break;
29+
}
30+
} else {
31+
if (errno == EAGAIN || errno == EINTR) {
32+
if (sent_in_this_call > 0) {
33+
continue;
34+
}
35+
break;
36+
}
37+
if (total_sent == 0) {
38+
return php_io_generic_copy_fallback(src_fd, dest_fd, maxlen);
39+
}
40+
break;
41+
}
42+
}
43+
44+
return (total_sent > 0) ? (ssize_t)total_sent : -1;
45+
}
46+
47+
ssize_t php_io_freebsd_copy(php_io_fd *src, php_io_fd *dest, size_t maxlen)
48+
{
49+
/* unlike linux, sendfile on freebsd works only under those conditions */
50+
if (src->fd_type == PHP_IO_FD_FILE && dest->fd_type == PHP_IO_FD_SOCKET) {
51+
return php_io_freebsd_sendfile(src->fd, dest->fd, maxlen);
52+
}
53+
54+
return php_io_generic_copy_fallback(src->fd, dest->fd, maxlen);
55+
}
56+
57+
#endif
58+

main/io/php_io_freebsd.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| license@php.net so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: Jakub Zelenka <bukka@php.net> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef PHP_IO_FREEBSD_H
18+
#define PHP_IO_FREEBSD_H
19+
20+
#define PHP_IO_PLATFORM_COPY php_io_freebsd_copy
21+
#define PHP_IO_PLATFORM_NAME "freebsd"
22+
23+
ssize_t php_io_freebsd_copy(php_io_fd *src, php_io_fd *dest, size_t maxlen);
24+
25+
#endif /* PHP_IO_FREEBSD_H */

main/io/php_io_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ ssize_t php_io_generic_copy_fallback(int src_fd, int dest_fd, size_t maxlen);
2626
#include "php_io_linux.h"
2727
#elif defined(PHP_WIN32)
2828
#include "php_io_windows.h"
29+
#elif defined(__FreeBSD__)
30+
#include "php_io_freebsd.h"
2931
#else
3032
#include "php_io_generic.h"
3133
#endif

0 commit comments

Comments
 (0)