mescc: Mes C Library: Add buffered read.

* lib/mes/__buffered_read.c: New file.
* build-aux/configure-lib.sh (libc_SOURCES): Add it.  Also add memmove.c.
(libc_tcc_SOURCES): Remove memmove.c
* lib/linux/close.c (close): Clear read buffer.
* lib/linux/_open3.c (_open3): Likewise.
* lib/linux/lseek.c (lseek): Correct for read buffer.
* lib/stdio/fwrite.c (fwrite): Likewise.
* lib/posix/read.c (read): Call __buffered_read.
* lib/posix/write.c (write): Add FIXME note about buffered reads.
* simple.sh: Update.
This commit is contained in:
Jan Nieuwenhuizen 2019-07-27 22:58:49 +02:00
parent c7d86eb955
commit ef29ade04b
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
14 changed files with 133 additions and 24 deletions

View File

@ -78,6 +78,7 @@ fi
libc_SOURCES="
$libmes_SOURCES
lib/mes/__assert_fail.c
lib/mes/__buffered_read.c
lib/mes/__mes_debug.c
lib/posix/execv.c
lib/posix/getcwd.c
@ -101,6 +102,7 @@ lib/stdlib/realloc.c
lib/string/memchr.c
lib/string/memcmp.c
lib/string/memcpy.c
lib/string/memmove.c
lib/string/memset.c
lib/string/strcmp.c
lib/string/strcpy.c
@ -171,7 +173,6 @@ lib/stdlib/strtoll.c
lib/stdlib/strtoul.c
lib/stdlib/strtoull.c
lib/string/memmem.c
lib/string/memmove.c
lib/string/strcat.c
lib/string/strchr.c
lib/string/strlwr.c

View File

@ -54,11 +54,14 @@ ssize_t _read (int fd, void *buffer, size_t size);
extern char *__brk;
extern void (*__call_at_exit) (void);
#define __FILEDES_MAX 512
#if !SYSTEM_LIBC
void __assert_fail (char *s);
ssize_t __buffered_read (int filedes, void *buffer, size_t size);
size_t __buffered_read_clear (int filedes);
void _exit (int code);
long brk (void *addr);
#endif // !SYSTEM_LIBC
#endif //__MES_LIB_H

View File

@ -29,6 +29,9 @@ _open3 (char const *file_name, int flags, int mask)
int r = _sys_call3 (SYS_open, (long) file_name, (int) flags, (int) mask);
__ungetc_init ();
if (r > 2)
__ungetc_clear (r);
{
__ungetc_clear (r);
__buffered_read_clear (r);
}
return r;
}

View File

@ -25,7 +25,7 @@
int
close (int filedes)
{
if (filedes > 2)
__ungetc_clear (filedes);
__ungetc_clear (filedes);
__buffered_read_clear (filedes);
return _sys_call1 (SYS_close, (int) filedes);
}

View File

@ -18,12 +18,17 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
#include <linux/syscall.h>
#include <syscall.h>
#include <stdio.h>
#include <sys/types.h>
off_t
lseek (int filedes, off_t offset, int whence)
{
size_t skip = __buffered_read_clear (filedes);
if (whence == SEEK_CUR)
offset -= skip;
return _sys_call3 (SYS_lseek, (int) filedes, (long) offset, (int) whence);
}

79
lib/mes/__buffered_read.c Normal file
View File

@ -0,0 +1,79 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
* GNU Mes is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* GNU Mes is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
#include <stdlib.h>
#include <string.h>
#define __READ_BUFFER_MAX 100
struct __read_buffer
{
ssize_t size;
char string[__READ_BUFFER_MAX];
};
struct __read_buffer *__read_cache = 0;
void
__buffered_read_init (int filedes)
{
if (!__read_cache)
__read_cache = (struct __read_buffer *) malloc (sizeof (struct __read_buffer) * __FILEDES_MAX);
}
size_t
__buffered_read_clear (int filedes)
{
__buffered_read_init (filedes);
size_t size = __read_cache[filedes].size;
__read_cache[filedes].size = 0;
return size;
}
ssize_t
__buffered_read (int filedes, void *buffer, size_t size)
{
size_t todo = size;
__buffered_read_init (filedes);
struct __read_buffer *cache = &__read_cache[filedes];
char *p = buffer;
if (!cache->size && size > __READ_BUFFER_MAX)
return _read (filedes, buffer, size);
while (cache->size > 0 && todo)
{
todo--;
*p++ = cache->string[__READ_BUFFER_MAX - cache->size--];
}
if (todo)
{
ssize_t bytes = _read (filedes, cache->string, __READ_BUFFER_MAX);
if (bytes < 0)
return -1;
if (bytes)
{
cache->size = bytes;
if (bytes < __READ_BUFFER_MAX)
memmove (cache->string + __READ_BUFFER_MAX - bytes, cache->string, bytes);
return size - todo + __buffered_read (filedes, p, todo);
}
}
return size - todo;
}

View File

@ -24,9 +24,7 @@
#include <sys/resource.h>
#include <unistd.h>
#define __UNGETC_MAX 1024
int __ungetc_buf[__UNGETC_MAX + 1] = { 0 };
int __ungetc_buf[__FILEDES_MAX + 1] = { 0 };
int
__ungetc_p (int filedes)
@ -37,8 +35,8 @@ __ungetc_p (int filedes)
void
__ungetc_init ()
{
if (__ungetc_buf[__UNGETC_MAX] == 0)
memset (__ungetc_buf, -1, (__UNGETC_MAX + 1) * sizeof (int));
if (__ungetc_buf[__FILEDES_MAX] == 0)
memset (__ungetc_buf, -1, (__FILEDES_MAX + 1) * sizeof (int));
}
void

View File

@ -24,7 +24,7 @@
ssize_t
read (int filedes, void *buffer, size_t size)
{
ssize_t bytes = _read (filedes, buffer, size);
ssize_t bytes = __buffered_read (filedes, buffer, size);
if (__mes_debug () > 4)
{
if (bytes == 1)

View File

@ -18,12 +18,21 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <mes/lib-mini.h>
#include <errno.h>
ssize_t
write (int filedes, void const *buffer, size_t size)
{
#if 0 // !MES_MINI
// FIXME: libc-mini has no __buffered_read_clear, lseek.
// and libc includes libc-mini...how to override?
// Let's hope everyone uses fwrite, or lseek for RDWR
// semantics...
size_t skip = __buffered_read_clear (filedes);
if (skip)
lseek (filedes, -skip, SEEK_CUR);
#endif
int r = _write (filedes, buffer, size);
if (r < 0)
{

View File

@ -41,9 +41,10 @@ fread (void *data, size_t size, size_t count, FILE * stream)
int bytes = 0;
while (__fungetc_p (stream) && todo-- && ++bytes)
*buf++ = fgetc (stream);
int filedes = (long) stream;
if (todo)
{
int r = read ((int) (long) stream, buf, todo);
int r = read (filedes, buf, todo);
if (r < 0 && !bytes)
bytes = r;
else
@ -52,18 +53,18 @@ fread (void *data, size_t size, size_t count, FILE * stream)
if (__mes_debug ())
{
static char debug_buf[4096];
eputs ("fread fd=");
eputs (itoa ((int) (long) stream));
eputs (itoa (filedes));
eputs (" bytes=");
eputs (itoa (bytes));
eputs ("\n");
static char buf[4096];
if (bytes > 0 && bytes < sizeof (buf))
if (bytes > 0 && bytes < sizeof (debug_buf))
{
strncpy (buf, data, bytes);
strncpy (debug_buf, data, bytes);
buf[bytes] = 0;
eputs ("fread buf=");
eputs (buf);
eputs (debug_buf);
eputs ("\n");
}
}

View File

@ -1,6 +1,6 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
* Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
@ -25,11 +25,12 @@
int
fseek (FILE * stream, long offset, int whence)
{
off_t pos = lseek ((int) (long) stream, offset, whence);
int filedes = (long) stream;
off_t pos = lseek (filedes, offset, whence);
if (__mes_debug ())
{
eputs ("fread fd=");
eputs (itoa ((int) (long) stream));
eputs (itoa (filedes));
eputs (" =>");
eputs (itoa (pos));
eputs ("\n");

View File

@ -20,6 +20,7 @@
#include <mes/lib.h>
#include <stdio.h>
#include <unistd.h>
size_t
fwrite (void const *data, size_t size, size_t count, FILE * stream)
@ -35,7 +36,12 @@ fwrite (void const *data, size_t size, size_t count, FILE * stream)
if (!size || !count)
return 0;
int bytes = write ((int) (long) stream, data, size * count);
// FIXME: should be in write, but that's libc-mini.
int filedes = (long) stream;
size_t skip = __buffered_read_clear (filedes);
if (skip)
lseek (filedes, -skip, SEEK_CUR);
int bytes = write (filedes, data, size * count);
if (__mes_debug () > 2)
{

View File

@ -29,7 +29,7 @@
(define welcome
(string-append "GNU Mes " %version "
Copyright (C) 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
Copyright (C) 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
GNU Mes comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it

View File

@ -167,12 +167,14 @@ $CC -g -D HAVE_CONFIG_H=1 -I include -I include/$mes_kernel/$mes_cpu\
lib/ctype/isxdigit.c\
\
lib/mes/__assert_fail.c\
lib/mes/__buffered_read.c\
lib/mes/__mes_debug.c\
lib/posix/execv.c\
lib/posix/getcwd.c\
lib/posix/getenv.c\
lib/posix/isatty.c\
lib/posix/open.c\
lib/posix/read.c\
lib/posix/setenv.c\
lib/posix/wait.c\
lib/stdio/fgetc.c\
@ -189,6 +191,7 @@ $CC -g -D HAVE_CONFIG_H=1 -I include -I include/$mes_kernel/$mes_cpu\
lib/string/memchr.c\
lib/string/memcmp.c\
lib/string/memcpy.c\
lib/string/memmove.c\
lib/string/memset.c\
lib/string/strcmp.c\
lib/string/strcpy.c\
@ -207,7 +210,7 @@ $CC -g -D HAVE_CONFIG_H=1 -I include -I include/$mes_kernel/$mes_cpu\
lib/linux/gettimeofday.c\
lib/linux/ioctl.c\
lib/linux/_open3.c\
lib/linux/read.c\
lib/linux/_read.c\
lib/linux/time.c\
lib/linux/unlink.c\
lib/linux/waitpid.c\