Skip to content

Commit

Permalink
Add in vtable-level flags to specify some per-type default behaviors.…
Browse files Browse the repository at this point in the history
… For types that need it, set up buffers automatically.
  • Loading branch information
Whiteknight committed Jun 3, 2012
1 parent d8932cb commit a54a224
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions include/parrot/io.h
Expand Up @@ -51,6 +51,10 @@
#define PIO_F_ASYNC 01000000 /* Handle is asynchronous */
#define PIO_F_BINARY 02000000 /* Open in binary mode */

/* IO VTABLE Flags */
#define PIO_VF_DEFAULT_BUFFERS 0x0001 /* This type uses buffers by default */
#define PIO_VF_FLUSH_ON_CLOSE 0x0002 /* Flush before closing */

/*
* pioctl argument constants. These don't have to
* be unique across io commands.
Expand Down Expand Up @@ -138,6 +142,7 @@ typedef const STR_VTABLE *(*io_vtable_get_encoding) (PARROT_INTERP, PMC *handle)
typedef struct _io_vtable {
const char * name;
INTVAL number;
INTVAL flags;
io_vtable_read_b read_b;
io_vtable_write_b write_b;
io_vtable_flush flush;
Expand Down
12 changes: 10 additions & 2 deletions src/io/api.c
Expand Up @@ -309,6 +309,12 @@ Parrot_io_open(PARROT_INTERP, ARGIN(PMC *pmc), ARGIN(STRING *path),
if (!status)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
"Unable to open %s from path '%Ss'", vtable->name, path);

/* If this type uses buffers by default, set them up. */
if (vtable->flags & PIO_VF_DEFAULT_BUFFERS && flags & PIO_F_READ)
Parrot_io_buffer_add_to_handle(interp, handle, IO_PTR_IDX_READ_BUFFER, BUFFER_SIZE_ANY, PIO_BF_BLKBUF);
if (vtable->flags & PIO_VF_DEFAULT_BUFFERS && flags & PIO_F_WRITE)
Parrot_io_buffer_add_to_handle(interp, handle, IO_PTR_IDX_WRITE_BUFFER, BUFFER_SIZE_ANY, PIO_BF_BLKBUF);
}

return handle;
Expand Down Expand Up @@ -468,7 +474,7 @@ INTVAL
Parrot_io_close_handle(PARROT_INTERP, ARGMOD(PMC *pmc))
{
ASSERT_ARGS(Parrot_io_close_handle)
return Parrot_io_close(interp, pmc, 1);
return Parrot_io_close(interp, pmc, -1);
}

PARROT_EXPORT
Expand All @@ -480,7 +486,9 @@ Parrot_io_close(PARROT_INTERP, ARGMOD(PMC *handle), INTVAL autoflush)
return 0;
else {
IO_VTABLE * const vtable = IO_GET_VTABLE(interp, handle);
if (autoflush) {
if (autoflush == -1)
autoflush == (vtable->flags && PIO_VF_FLUSH_ON_CLOSE) ? 1 : 0;
if (autoflush == 1) {
IO_BUFFER * const write_buffer = IO_GET_WRITE_BUFFER(interp, handle);
Parrot_io_buffer_flush(interp, write_buffer, handle, vtable, 0);
}
Expand Down
1 change: 1 addition & 0 deletions src/io/filehandle.c
Expand Up @@ -169,6 +169,7 @@ io_filehandle_setup_vtable(PARROT_INTERP, IO_VTABLE *vtable, INTVAL idx)
if (vtable == NULL)
vtable = &(interp->piodata->vtables[idx]);
vtable->number = idx;
vtable->flags = PIO_VF_DEFAULT_BUFFERS | PIO_VF_FLUSH_ON_CLOSE;
vtable->name = "FileHandle";
vtable->read_b = io_filehandle_read_b;
vtable->write_b = io_filehandle_write_b;
Expand Down
1 change: 1 addition & 0 deletions src/io/pipe.c
Expand Up @@ -152,6 +152,7 @@ io_pipe_setup_vtable(PARROT_INTERP, IO_VTABLE *vtable, INTVAL idx)
if (vtable == NULL)
vtable = &(interp->piodata->vtables[idx]);
vtable->number = idx;
vtable->flags = PIO_VF_DEFAULT_BUFFERS | PIO_VF_FLUSH_ON_CLOSE;
vtable->name = "Pipe";
vtable->read_b = io_pipe_read_b;
vtable->write_b = io_pipe_write_b;
Expand Down
1 change: 1 addition & 0 deletions src/io/socket.c
Expand Up @@ -174,6 +174,7 @@ io_socket_setup_vtable(PARROT_INTERP, ARGIN_NULLOK(IO_VTABLE *vtable), INTVAL id
if (vtable == NULL)
vtable = &(interp->piodata->vtables[idx]);
vtable->number = idx;
vtable->flags = PIO_VF_DEFAULT_BUFFERS | PIO_VF_FLUSH_ON_CLOSE;
vtable->name = "Socket";
vtable->read_b = io_socket_read_b;
vtable->write_b = io_socket_write_b;
Expand Down
2 changes: 1 addition & 1 deletion src/io/stringhandle.c
Expand Up @@ -166,6 +166,7 @@ io_stringhandle_setup_vtable(PARROT_INTERP, IO_VTABLE *vtable, INTVAL idx)
if (vtable == NULL)
vtable = &(interp->piodata->vtables[idx]);
vtable->number = idx;
vtable->flags = 0;
vtable->name = "StringHandle";
vtable->read_b = io_stringhandle_read_b;
vtable->write_b = io_stringhandle_write_b;
Expand Down Expand Up @@ -306,7 +307,6 @@ static INTVAL
io_stringhandle_close(PARROT_INTERP, ARGMOD(PMC *handle))
{
ASSERT_ARGS(io_stringhandle_close)
//SETATTR_StringHandle_stringhandle(interp, handle, STRINGNULL);
SETATTR_StringHandle_read_offset(interp, handle, 0);
return 1;
}
Expand Down

0 comments on commit a54a224

Please sign in to comment.