Navigation Menu

Skip to content

Commit

Permalink
Use separate flags for adding read/write buffers to handles by defaul…
Browse files Browse the repository at this point in the history
…t. Remove the ad hoc buffer STRING nonsense from socket.pmc, replaced by actual buffer logic.
  • Loading branch information
Whiteknight committed Jun 15, 2012
1 parent 9aeede6 commit 82035c2
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 38 deletions.
9 changes: 5 additions & 4 deletions include/parrot/io.h
Expand Up @@ -52,11 +52,12 @@
#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 */
#define PIO_VF_PATH_NOT_REQUIRED 0x0004 /* This handle does not require a path
#define PIO_VF_DEFAULT_READ_BUF 0x0001 /* This type uses read buffers by default */
#define PIO_VF_DEFAULT_WRITE_BUF 0x0002 /* This type uses write buffers by default */
#define PIO_VF_FLUSH_ON_CLOSE 0x0004 /* Flush before closing */
#define PIO_VF_PATH_NOT_REQUIRED 0x0008 /* This handle does not require a path
for .open() */
#define PIO_VF_AWAYS_READABLE 0x0008 /* Handle can always be read */
#define PIO_VF_AWAYS_READABLE 0x0010 /* Handle can always be read */

/*
* pioctl argument constants. These don't have to
Expand Down
4 changes: 2 additions & 2 deletions src/io/api.c
Expand Up @@ -316,9 +316,9 @@ Parrot_io_open(PARROT_INTERP, ARGIN(PMC *pmc), ARGIN(STRING *path),

/* If this type uses buffers by default, set them up, and if we're
in an acceptable mode, set up buffers. */
if (vtable->flags & PIO_VF_DEFAULT_BUFFERS && flags & PIO_F_READ)
if (vtable->flags & PIO_VF_DEFAULT_READ_BUF && 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)
if (vtable->flags & PIO_VF_DEFAULT_WRITE_BUF && flags & PIO_F_WRITE)
Parrot_io_buffer_add_to_handle(interp, handle, IO_PTR_IDX_WRITE_BUFFER, BUFFER_SIZE_ANY, PIO_BF_BLKBUF);
}

Expand Down
4 changes: 3 additions & 1 deletion src/io/filehandle.c
Expand Up @@ -198,7 +198,9 @@ io_filehandle_setup_vtable(PARROT_INTERP, ARGMOD_NULLOK(IO_VTABLE *vtable), INTV
if (vtable == NULL)
vtable = &(interp->piodata->vtables[idx]);
vtable->number = idx;
vtable->flags = PIO_VF_DEFAULT_BUFFERS | PIO_VF_FLUSH_ON_CLOSE;
vtable->flags = PIO_VF_DEFAULT_READ_BUF
| PIO_VF_DEFAULT_WRITE_BUF
| PIO_VF_FLUSH_ON_CLOSE;
vtable->name = "FileHandle";
vtable->read_b = io_filehandle_read_b;
vtable->write_b = io_filehandle_write_b;
Expand Down
3 changes: 2 additions & 1 deletion src/io/pipe.c
Expand Up @@ -180,7 +180,8 @@ io_pipe_setup_vtable(PARROT_INTERP, ARGMOD_NULLOK(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->flags = PIO_VF_DEFAULT_READ_BUF
| PIO_VF_FLUSH_ON_CLOSE;
vtable->name = "Pipe";
vtable->read_b = io_pipe_read_b;
vtable->write_b = io_pipe_write_b;
Expand Down
3 changes: 2 additions & 1 deletion src/io/socket.c
Expand Up @@ -202,7 +202,8 @@ io_socket_setup_vtable(PARROT_INTERP, ARGMOD_NULLOK(IO_VTABLE *vtable), INTVAL i
if (vtable == NULL)
vtable = &(interp->piodata->vtables[idx]);
vtable->number = idx;
vtable->flags = PIO_VF_FLUSH_ON_CLOSE;
vtable->flags = PIO_VF_DEFAULT_READ_BUF
| PIO_VF_FLUSH_ON_CLOSE;
vtable->name = "Socket";
vtable->read_b = io_socket_read_b;
vtable->write_b = io_socket_write_b;
Expand Down
32 changes: 3 additions & 29 deletions src/pmc/socket.pmc
Expand Up @@ -32,7 +32,6 @@ pmclass Socket extends Handle provides socket auto_attrs {
ATTR INTVAL family;
ATTR INTVAL type;
ATTR INTVAL protocol;
ATTR STRING *buf;
ATTR STRING *encoding;

/*
Expand All @@ -51,7 +50,6 @@ Initializes a newly created Socket object.

data_struct->local = PMCNULL;
data_struct->remote = PMCNULL;
data_struct->buf = STRINGNULL;
data_struct->io_vtable = Parrot_io_get_vtable(interp,
IO_VTABLE_SOCKET, NULL);

Expand Down Expand Up @@ -99,7 +97,6 @@ Mark active socket handle data as live.
if (data) {
Parrot_gc_mark_PMC_alive(INTERP, data->local);
Parrot_gc_mark_PMC_alive(INTERP, data->remote);
Parrot_gc_mark_STRING_alive(INTERP, data->buf);
Parrot_io_buffer_mark(interp, data->read_buffer);
Parrot_io_buffer_mark(interp, data->write_buffer);
}
Expand Down Expand Up @@ -340,15 +337,7 @@ string containing the received message.
*/

METHOD recv() {
STRING * result;
GET_ATTR_buf(INTERP, SELF, result);

if (result != STRINGNULL && Parrot_str_length(INTERP, result) > 0) {
SET_ATTR_buf(INTERP, SELF, STRINGNULL);
}
else {
result = Parrot_io_read_s(INTERP, SELF, CHUNK_SIZE);
}
STRING * const result = Parrot_io_read_s(INTERP, SELF, CHUNK_SIZE);
RETURN(STRING * result);
}

Expand Down Expand Up @@ -449,29 +438,14 @@ Read the given number of bytes from the socket and return them in a string.
*/

METHOD read(INTVAL nb) {
STRING *result;
STRING *buf;
GET_ATTR_buf(INTERP, SELF, buf);

if (Parrot_io_is_closed(INTERP, SELF))
RETURN(STRING * STRINGNULL);

if (buf == STRINGNULL)
buf = Parrot_io_read_s(INTERP, SELF, CHUNK_SIZE);
buf = Parrot_io_read_s(INTERP, SELF, nb);

while (Parrot_str_length(INTERP, buf) < nb) {
STRING * const more = Parrot_io_read_s(INTERP, SELF, CHUNK_SIZE);
if (Parrot_str_length(INTERP, more) == 0) {
SET_ATTR_buf(INTERP, SELF, STRINGNULL);
RETURN(STRING *buf);
}
buf = Parrot_str_concat(INTERP, buf, more);
}

result = Parrot_str_substr(INTERP, buf, 0, nb);
buf = Parrot_str_substr(INTERP, buf, nb, Parrot_str_length(INTERP, buf) - nb);
SET_ATTR_buf(INTERP, SELF, buf);
RETURN(STRING *result);
RETURN(STRING *buf);
}


Expand Down

0 comments on commit 82035c2

Please sign in to comment.