未验证 提交 dd0adf17 编写于 作者: O openharmony_ci 提交者: Gitee

!902 Optimize musl I/O function fread

Merge pull request !902 from Wang Yaofeng/master
......@@ -2165,6 +2165,9 @@ musl_src_porting_file = [
"src/ldso/riscv64/dlvsym.s",
"src/ldso/x86_64/dlvsym.s",
"src/stdio/__fdopen.c",
"src/stdio/__stdio_read.c",
"src/stdio/fread.c",
"src/internal/stdio_impl.h",
"src/internal/vdso.c",
"src/time/clock_gettime.c",
"src/time/clock_getres.c",
......
#ifndef _STDIO_IMPL_H
#define _STDIO_IMPL_H
#include <stdio.h>
#include "syscall.h"
#define UNGET 8
#define FFINALLOCK(f) ((f)->lock>=0 ? __lockfile((f)) : 0)
#define FLOCK(f) int __need_unlock = ((f)->lock>=0 ? __lockfile((f)) : 0)
#define FUNLOCK(f) do { if (__need_unlock) __unlockfile((f)); } while (0)
#define F_PERM 1
#define F_NORD 4
#define F_NOWR 8
#define F_EOF 16
#define F_ERR 32
#define F_SVB 64
#define F_APP 128
struct _IO_FILE {
unsigned flags;
unsigned char *rpos, *rend;
int (*close)(FILE *);
unsigned char *wend, *wpos;
unsigned char *mustbezero_1;
unsigned char *wbase;
size_t (*read)(FILE *, unsigned char *, size_t);
size_t (*readx)(FILE *, unsigned char *, size_t);
size_t (*write)(FILE *, const unsigned char *, size_t);
off_t (*seek)(FILE *, off_t, int);
unsigned char *buf;
size_t buf_size;
FILE *prev, *next;
int fd;
int pipe_pid;
long lockcount;
int mode;
volatile int lock;
int lbf;
void *cookie;
off_t off;
char *getln_buf;
void *mustbezero_2;
unsigned char *shend;
off_t shlim, shcnt;
FILE *prev_locked, *next_locked;
struct __locale_struct *locale;
};
extern hidden FILE *volatile __stdin_used;
extern hidden FILE *volatile __stdout_used;
extern hidden FILE *volatile __stderr_used;
hidden int __lockfile(FILE *);
hidden void __unlockfile(FILE *);
hidden size_t __stdio_read(FILE *, unsigned char *, size_t);
hidden size_t __stdio_readx(FILE *, unsigned char *, size_t);
hidden size_t __stdio_write(FILE *, const unsigned char *, size_t);
hidden size_t __stdout_write(FILE *, const unsigned char *, size_t);
hidden off_t __stdio_seek(FILE *, off_t, int);
hidden int __stdio_close(FILE *);
hidden int __fill_buffer(FILE *f);
hidden int __toread(FILE *);
hidden int __towrite(FILE *);
hidden void __stdio_exit(void);
hidden void __stdio_exit_needed(void);
#if defined(__PIC__) && (100*__GNUC__+__GNUC_MINOR__ >= 303)
__attribute__((visibility("protected")))
#endif
int __overflow(FILE *, int), __uflow(FILE *);
hidden int __fseeko(FILE *, off_t, int);
hidden int __fseeko_unlocked(FILE *, off_t, int);
hidden off_t __ftello(FILE *);
hidden off_t __ftello_unlocked(FILE *);
hidden size_t __fwritex(const unsigned char *, size_t, FILE *);
hidden int __putc_unlocked(int, FILE *);
hidden FILE *__fdopen(int, const char *);
hidden int __fmodeflags(const char *);
hidden FILE *__ofl_add(FILE *f);
hidden FILE **__ofl_lock(void);
hidden void __ofl_unlock(void);
struct __pthread;
hidden void __register_locked_file(FILE *, struct __pthread *);
hidden void __unlist_locked_file(FILE *);
hidden void __do_orphaned_stdio_locks(void);
#define MAYBE_WAITERS 0x40000000
hidden void __getopt_msg(const char *, const char *, const char *, size_t);
#define feof(f) ((f)->flags & F_EOF)
#define ferror(f) ((f)->flags & F_ERR)
#define getc_unlocked(f) \
( ((f)->rpos != (f)->rend) ? *(f)->rpos++ : __uflow((f)) )
#define putc_unlocked(c, f) \
( (((unsigned char)(c)!=(f)->lbf && (f)->wpos!=(f)->wend)) \
? *(f)->wpos++ = (unsigned char)(c) \
: __overflow((f),(unsigned char)(c)) )
/* Caller-allocated FILE * operations */
hidden FILE *__fopen_rb_ca(const char *, FILE *, unsigned char *, size_t);
hidden int __fclose_ca(FILE *);
#endif
......@@ -93,6 +93,7 @@ FILE *__fdopen(int fd, const char *mode)
f->write = __stdio_write;
f->seek = __stdio_seek;
f->close = __stdio_close;
f->readx = __stdio_readx;
if (!libc.threaded) {
f->lock = -1;
......
#include "stdio_impl.h"
#include <sys/uio.h>
size_t __stdio_readx(FILE *f, unsigned char *buf, size_t len)
{
return syscall(SYS_read, f->fd, buf, len);
}
size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
{
struct iovec iov_buf[2] = {
{ .iov_base = buf, .iov_len = len - !!f->buf_size },
{ .iov_base = f->buf, .iov_len = f->buf_size }
};
ssize_t cnt;
cnt = iov_buf[0].iov_len ? syscall(SYS_readv, f->fd, iov_buf, 2)
: syscall(SYS_read, f->fd, iov_buf[1].iov_base, iov_buf[1].iov_len);
if (cnt <= 0) {
f->flags |= cnt ? F_ERR : F_EOF;
return 0;
}
if (cnt <= iov_buf[0].iov_len) {
return cnt;
}
cnt -= iov_buf[0].iov_len;
f->rpos = f->buf;
f->rend = f->buf + cnt;
if (f->buf_size) buf[len-1] = *f->rpos++;
return len;
}
#include "stdio_impl.h"
#include <string.h>
#define MIN(a,b) ((a)<(b) ? (a) : (b))
int __fill_buffer(FILE *f)
{
int r = __toread(f);
if (r != 0) {
return r;
}
int k = f->readx(f, f->buf, f->buf_size);
if (k <= 0) {
f->flags |= (k == 0) ? F_EOF : F_ERR;
f->rpos = f->rend;
return k;
}
f->rpos = f->buf;
f->rend = f->rpos + k;
return 0;
}
size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f)
{
unsigned char *dest = destv;
size_t len = size * nmemb, l = len, k;
if (!size) {
nmemb = 0;
}
FLOCK(f);
f->mode |= f->mode-1;
while (l > 0) {
if (f->rpos != f->rend) {
/* First exhaust the buffer. */
k = MIN(f->rend - f->rpos, l);
memcpy(dest, f->rpos, k);
f->rpos += k;
dest += k;
l -= k;
}
/* done */
if (l == 0) {
goto exit;
}
/* if user buffer is longer than file buffer, read directly */
if (l > f->buf_size) {
break;
}
if (__fill_buffer(f)) {
goto exit;
}
}
/* Read the remainder directly */
for (; l; l-=k, dest+=k) {
k = f->readx(f, dest, l);
if (k <= 0) {
f->flags |= (k == 0 ? F_EOF : F_ERR);
break;
}
}
exit:
FUNLOCK(f);
return (len - l) / size;
}
weak_alias(fread, fread_unlocked);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册