add function fc_safe_writev

pull/47/head V1.0.67
YuQing 2023-05-29 18:15:59 +08:00
parent 0c588d965e
commit 4df1107fa3
5 changed files with 109 additions and 8 deletions

View File

@ -1,5 +1,5 @@
Version 1.67 2023-05-28 Version 1.67 2023-05-29
* lc_pair in struct fc_queue change to lcp * lc_pair in struct fc_queue change to lcp
* sorted queue use double link chain for quick push * sorted queue use double link chain for quick push
* add function uniq_skiplist_clear * add function uniq_skiplist_clear
@ -7,6 +7,7 @@ Version 1.67 2023-05-28
* fast_mblock_init_ex2 add parameter prealloc_trunk_count * fast_mblock_init_ex2 add parameter prealloc_trunk_count
* sorted_queue.[hc] support pop_compare_func * sorted_queue.[hc] support pop_compare_func
* bugfixed: fast_mblock_batch_alloc correct return value * bugfixed: fast_mblock_batch_alloc correct return value
* add function fc_safe_writev
Version 1.66 2023-02-12 Version 1.66 2023-02-12
* struct fast_task_info add field: notify_next for nio notify queue * struct fast_task_info add field: notify_next for nio notify queue

View File

@ -109,6 +109,12 @@ extern int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind);
#define SYNC_LOG_BUFF_DEF_INTERVAL 10 #define SYNC_LOG_BUFF_DEF_INTERVAL 10
#define TIME_NONE -1 #define TIME_NONE -1
#if defined(IOV_MAX) && IOV_MAX > 256
#define FC_IOV_BATCH_SIZE 256
#else
#define FC_IOV_BATCH_SIZE IOV_MAX
#endif
#define IP_ADDRESS_SIZE 16 #define IP_ADDRESS_SIZE 16
#define INFINITE_FILE_SIZE (256 * 1024LL * 1024 * 1024 * 1024 * 1024LL) #define INFINITE_FILE_SIZE (256 * 1024LL * 1024 * 1024 * 1024 * 1024LL)

View File

@ -2986,6 +2986,96 @@ ssize_t fc_safe_write(int fd, const char *buf, const size_t nbyte)
return nbyte; return nbyte;
} }
ssize_t fc_safe_writev(int fd, const struct iovec *iov, int iovcnt)
{
int write_bytes;
int bytes;
ssize_t total_written;
int remain_count;
int current_count;
int current_done;
int remain_len;
struct iovec *iob;
struct iovec iov_array[FC_IOV_BATCH_SIZE];
struct iovec *iovp;
iovp = (struct iovec *)iov;
remain_count = current_count = iovcnt;
total_written = 0;
while (remain_count > 0)
{
write_bytes = writev(fd, iovp, current_count);
if (write_bytes < 0)
{
if (errno == EINTR)
{
continue;
}
return total_written > 0 ? total_written : -1;
} else if (write_bytes == 0) {
return total_written;
}
total_written += write_bytes;
iob = iovp;
bytes = iob->iov_len;
while (bytes < write_bytes)
{
++iob;
bytes += iob->iov_len;
}
if (bytes == write_bytes)
{
++iob;
if (iob < iovp + current_count) {
bytes += iob->iov_len;
}
}
current_done = iob - iovp;
remain_count -= current_done;
if (remain_count == 0) {
break;
}
if (current_done == current_count) //full done
{
current_count = ((remain_count <= FC_IOV_BATCH_SIZE) ?
remain_count : FC_IOV_BATCH_SIZE);
memcpy(iov_array, iov + (iovcnt - remain_count),
current_count * sizeof(struct iovec));
iovp = iov_array;
}
else //partial done
{
if (iovp == (struct iovec *)iov)
{
current_count = ((remain_count <= FC_IOV_BATCH_SIZE) ?
remain_count : FC_IOV_BATCH_SIZE);
memcpy(iov_array, iob, current_count *
sizeof(struct iovec));
iovp = iov_array;
}
else
{
current_count -= current_done;
iovp = iob;
}
/* adjust the first element */
remain_len = bytes - write_bytes;
if (remain_len < iovp->iov_len)
{
iovp->iov_base = (char *)iovp->iov_base +
(iovp->iov_len - remain_len);
iovp->iov_len = remain_len;
}
}
}
return total_written;
}
ssize_t fc_lock_write(int fd, const char *buf, const size_t nbyte) ssize_t fc_lock_write(int fd, const char *buf, const size_t nbyte)
{ {
int lock_result; int lock_result;

View File

@ -24,6 +24,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/uio.h>
#include "common_define.h" #include "common_define.h"
#ifdef OS_LINUX #ifdef OS_LINUX
#include <sys/syscall.h> #include <sys/syscall.h>
@ -886,7 +887,7 @@ bool isLeadingSpacesLine(const char *content, const char *current);
*/ */
bool isTrailingSpacesLine(const char *tail, const char *end); bool isTrailingSpacesLine(const char *tail, const char *end);
/** write to file /** safe write wrapper
* parameters: * parameters:
* fd: the fd to write * fd: the fd to write
* buf: the buffer * buf: the buffer
@ -895,6 +896,15 @@ bool isTrailingSpacesLine(const char *tail, const char *end);
*/ */
ssize_t fc_safe_write(int fd, const char *buf, const size_t nbyte); ssize_t fc_safe_write(int fd, const char *buf, const size_t nbyte);
/** safe writev wrapper
* parameters:
* fd: the fd to write
* iov: the iov array
* iovcnt: the iov count
* return: written bytes for success, -1 when fail
*/
ssize_t fc_safe_writev(int fd, const struct iovec *iov, int iovcnt);
/** lock and write to file /** lock and write to file
* parameters: * parameters:
* fd: the fd to write * fd: the fd to write

View File

@ -33,12 +33,6 @@
#define SUB_NET_TYPE_INNER_172_STR3 "inner172" #define SUB_NET_TYPE_INNER_172_STR3 "inner172"
#define SUB_NET_TYPE_INNER_192_STR3 "inner192" #define SUB_NET_TYPE_INNER_192_STR3 "inner192"
#if defined(IOV_MAX) && IOV_MAX > 256
#define FC_IOV_BATCH_SIZE 256
#else
#define FC_IOV_BATCH_SIZE IOV_MAX
#endif
#if defined(OS_LINUX) || defined(OS_FREEBSD) #if defined(OS_LINUX) || defined(OS_FREEBSD)
#include <ifaddrs.h> #include <ifaddrs.h>
#endif #endif