remove pic->files

This commit is contained in:
Yuichi Nishiwaki 2016-07-10 23:50:41 +09:00
parent 05cb5aec22
commit eff3475cde
7 changed files with 31 additions and 50 deletions

View File

@ -587,7 +587,9 @@ gc_finalize_object(pic_state *pic, struct object *obj)
break; break;
} }
case PIC_TYPE_PORT: { case PIC_TYPE_PORT: {
pic_fflush(pic, pic_obj_value(obj)); /* FIXME */ if (obj->u.port.file.flag != 0) {
pic_fflush(pic, pic_obj_value(obj)); /* FIXME */
}
break; break;
} }

View File

@ -18,5 +18,4 @@
/* #define PIC_ABORT(pic) abort() */ /* #define PIC_ABORT(pic) abort() */
/** I/O configuration */ /** I/O configuration */
/* #define PIC_OPEN_MAX 1024 */
/* #define PIC_BUFSIZ 1024 */ /* #define PIC_BUFSIZ 1024 */

View File

@ -9,6 +9,7 @@
extern "C" { extern "C" {
#endif #endif
#include "picrin/private/file.h"
#include "picrin/private/khash.h" #include "picrin/private/khash.h"
#include "picrin/private/gc.h" #include "picrin/private/gc.h"
@ -119,7 +120,7 @@ struct error {
struct port { struct port {
OBJECT_HEADER OBJECT_HEADER
struct file *file; struct file file;
}; };
struct checkpoint { struct checkpoint {

View File

@ -10,7 +10,6 @@ extern "C" {
#endif #endif
#include "picrin/private/khash.h" #include "picrin/private/khash.h"
#include "picrin/private/file.h"
#include "picrin/private/vm.h" #include "picrin/private/vm.h"
#include "picrin/private/gc.h" #include "picrin/private/gc.h"
@ -60,8 +59,6 @@ struct pic_state {
khash_t(ltable) ltable; khash_t(ltable) ltable;
struct list_head ireps; struct list_head ireps;
struct file files[PIC_OPEN_MAX];
bool gc_enable; bool gc_enable;
struct heap *heap; struct heap *heap;
struct object **arena; struct object **arena;

View File

@ -36,10 +36,6 @@ void abort(void);
# define PIC_ABORT(pic) abort() # define PIC_ABORT(pic) abort()
#endif #endif
#ifndef PIC_OPEN_MAX
# define PIC_OPEN_MAX 1024
#endif
#ifndef PIC_BUFSIZ #ifndef PIC_BUFSIZ
# define PIC_BUFSIZ 1024 # define PIC_BUFSIZ 1024
#endif #endif

View File

@ -15,28 +15,17 @@
pic_value pic_value
pic_funopen(pic_state *pic, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)) pic_funopen(pic_state *pic, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *))
{ {
struct file *fp;
struct port *port; struct port *port;
for (fp = pic->files; fp < pic->files + PIC_OPEN_MAX; fp++)
if ((fp->flag & (FILE_READ | FILE_WRITE)) == 0)
break; /* found free slot */
if (fp >= pic->files + PIC_OPEN_MAX) /* no free slots */
pic_error(pic, "too many files open", 0);
fp->cnt = 0;
fp->base = NULL;
fp->flag = read? FILE_READ : FILE_WRITE;
fp->vtable.cookie = cookie;
fp->vtable.read = read;
fp->vtable.write = write;
fp->vtable.seek = seek;
fp->vtable.close = close;
port = (struct port *)pic_obj_alloc(pic, sizeof(struct port), PIC_TYPE_PORT); port = (struct port *)pic_obj_alloc(pic, sizeof(struct port), PIC_TYPE_PORT);
port->file = fp; port->file.cnt = 0;
port->file.base = NULL;
port->file.flag = read? FILE_READ : FILE_WRITE;
port->file.vtable.cookie = cookie;
port->file.vtable.read = read;
port->file.vtable.write = write;
port->file.vtable.seek = seek;
port->file.vtable.close = close;
return pic_obj_value(port); return pic_obj_value(port);
} }
@ -44,7 +33,7 @@ pic_funopen(pic_state *pic, void *cookie, int (*read)(pic_state *, void *, char
int int
pic_fclose(pic_state *pic, pic_value port) pic_fclose(pic_state *pic, pic_value port)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
pic_fflush(pic, port); pic_fflush(pic, port);
fp->flag = 0; fp->flag = 0;
@ -56,7 +45,7 @@ pic_fclose(pic_state *pic, pic_value port)
void void
pic_clearerr(pic_state *PIC_UNUSED(pic), pic_value port) pic_clearerr(pic_state *PIC_UNUSED(pic), pic_value port)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
fp->flag &= ~(FILE_EOF | FILE_ERR); fp->flag &= ~(FILE_EOF | FILE_ERR);
} }
@ -64,7 +53,7 @@ pic_clearerr(pic_state *PIC_UNUSED(pic), pic_value port)
int int
pic_feof(pic_state *PIC_UNUSED(pic), pic_value port) pic_feof(pic_state *PIC_UNUSED(pic), pic_value port)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
return (fp->flag & FILE_EOF) != 0; return (fp->flag & FILE_EOF) != 0;
} }
@ -72,7 +61,7 @@ pic_feof(pic_state *PIC_UNUSED(pic), pic_value port)
int int
pic_ferror(pic_state *PIC_UNUSED(pic), pic_value port) pic_ferror(pic_state *PIC_UNUSED(pic), pic_value port)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
return (fp->flag & FILE_ERR) != 0; return (fp->flag & FILE_ERR) != 0;
} }
@ -169,7 +158,7 @@ flushbuf(pic_state *pic, int x, struct file *fp)
int int
pic_fflush(pic_state *pic, pic_value port) pic_fflush(pic_state *pic, pic_value port)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
int retval; int retval;
retval = 0; retval = 0;
@ -193,7 +182,7 @@ pic_fflush(pic_state *pic, pic_value port)
int int
pic_fputc(pic_state *pic, int x, pic_value port) pic_fputc(pic_state *pic, int x, pic_value port)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
return putc_(pic, x, fp); return putc_(pic, x, fp);
} }
@ -201,7 +190,7 @@ pic_fputc(pic_state *pic, int x, pic_value port)
int int
pic_fgetc(pic_state *pic, pic_value port) pic_fgetc(pic_state *pic, pic_value port)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
return getc_(pic, fp); return getc_(pic, fp);
} }
@ -209,7 +198,7 @@ pic_fgetc(pic_state *pic, pic_value port)
int int
pic_fputs(pic_state *pic, const char *s, pic_value port) pic_fputs(pic_state *pic, const char *s, pic_value port)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
const char *ptr = s; const char *ptr = s;
while(*ptr != '\0') { while(*ptr != '\0') {
@ -223,7 +212,7 @@ pic_fputs(pic_state *pic, const char *s, pic_value port)
char * char *
pic_fgets(pic_state *pic, char *s, int size, pic_value port) pic_fgets(pic_state *pic, char *s, int size, pic_value port)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
int c = 0; int c = 0;
char *buf; char *buf;
@ -245,7 +234,7 @@ pic_fgets(pic_state *pic, char *s, int size, pic_value port)
int int
pic_ungetc(pic_state *PIC_UNUSED(pic), int c, pic_value port) pic_ungetc(pic_state *PIC_UNUSED(pic), int c, pic_value port)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
unsigned char uc = c; unsigned char uc = c;
if (c == EOF || fp->base == fp->ptr) { if (c == EOF || fp->base == fp->ptr) {
@ -258,7 +247,7 @@ pic_ungetc(pic_state *PIC_UNUSED(pic), int c, pic_value port)
size_t size_t
pic_fread(pic_state *pic, void *ptr, size_t size, size_t count, pic_value port) pic_fread(pic_state *pic, void *ptr, size_t size, size_t count, pic_value port)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
char *bptr = ptr; char *bptr = ptr;
long nbytes; long nbytes;
int c; int c;
@ -284,7 +273,7 @@ pic_fread(pic_state *pic, void *ptr, size_t size, size_t count, pic_value port)
size_t size_t
pic_fwrite(pic_state *pic, const void *ptr, size_t size, size_t count, pic_value port) pic_fwrite(pic_state *pic, const void *ptr, size_t size, size_t count, pic_value port)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
const char *bptr = ptr; const char *bptr = ptr;
long nbytes; long nbytes;
@ -307,7 +296,7 @@ pic_fwrite(pic_state *pic, const void *ptr, size_t size, size_t count, pic_value
long long
pic_fseek(pic_state *pic, pic_value port, long offset, int whence) pic_fseek(pic_state *pic, pic_value port, long offset, int whence)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
long s; long s;
pic_fflush(pic, port); pic_fflush(pic, port);
@ -512,7 +501,7 @@ pic_fmemopen(pic_state *pic, const char *data, int size, const char *mode)
int int
pic_fgetbuf(pic_state *pic, pic_value port, const char **buf, int *len) pic_fgetbuf(pic_state *pic, pic_value port, const char **buf, int *len)
{ {
struct file *fp = pic_port_ptr(pic, port)->file; struct file *fp = &pic_port_ptr(pic, port)->file;
xbuf_t *s; xbuf_t *s;
pic_fflush(pic, port); pic_fflush(pic, port);
@ -533,7 +522,7 @@ pic_port_input_port_p(pic_state *pic)
pic_get_args(pic, "o", &v); pic_get_args(pic, "o", &v);
if (pic_port_p(pic, v) && (pic_port_ptr(pic, v)->file->flag & FILE_READ) != 0) { if (pic_port_p(pic, v) && (pic_port_ptr(pic, v)->file.flag & FILE_READ) != 0) {
return pic_true_value(pic); return pic_true_value(pic);
} else { } else {
return pic_false_value(pic); return pic_false_value(pic);
@ -547,7 +536,7 @@ pic_port_output_port_p(pic_state *pic)
pic_get_args(pic, "o", &v); pic_get_args(pic, "o", &v);
if (pic_port_p(pic, v) && (pic_port_ptr(pic, v)->file->flag & FILE_WRITE) != 0) { if (pic_port_p(pic, v) && (pic_port_ptr(pic, v)->file.flag & FILE_WRITE) != 0) {
return pic_true_value(pic); return pic_true_value(pic);
} }
else { else {
@ -590,7 +579,7 @@ pic_port_port_open_p(pic_state *pic)
pic_get_args(pic, "p", &port); pic_get_args(pic, "p", &port);
return pic_bool_value(pic, pic_port_ptr(pic, port)->file->flag != 0); return pic_bool_value(pic, pic_port_ptr(pic, port)->file.flag != 0);
} }
static pic_value static pic_value
@ -606,7 +595,7 @@ pic_port_close_port(pic_state *pic)
} }
#define assert_port_profile(port, flags, caller) do { \ #define assert_port_profile(port, flags, caller) do { \
int flag = pic_port_ptr(pic, port)->file->flag; \ int flag = pic_port_ptr(pic, port)->file.flag; \
if ((flag & (flags)) != (flags)) { \ if ((flag & (flags)) != (flags)) { \
switch (flags) { \ switch (flags) { \
case FILE_WRITE: \ case FILE_WRITE: \

View File

@ -240,9 +240,6 @@ pic_open(pic_allocf allocf, void *userdata)
pic->panicf = NULL; pic->panicf = NULL;
pic->err = pic_invalid_value(pic); pic->err = pic_invalid_value(pic);
/* file pool */
memset(pic->files, 0, sizeof pic->files);
/* root tables */ /* root tables */
pic->globals = pic_make_weak(pic); pic->globals = pic_make_weak(pic);
pic->macros = pic_make_weak(pic); pic->macros = pic_make_weak(pic);