Author: petdance
Date: Sat Apr 22 20:40:38 2006
New Revision: 12400
Modified:
trunk/src/io/io.c
trunk/src/io/io_buf.c
trunk/src/io/io_layers.c
Log:
Consting and localizing. This time without segfaults!
Modified: trunk/src/io/io.c
==============================================================================
--- trunk/src/io/io.c (original)
+++ trunk/src/io/io.c Sat Apr 22 20:40:38 2006
@@ -66,14 +66,9 @@
PMC *
new_io_pmc(theINTERP, ParrotIO *io)
{
- PMC *new_pmc;
- new_pmc = pmc_new(interpreter, enum_class_ParrotIO);
+ PMC * const new_pmc = pmc_new(interpreter, enum_class_ParrotIO);
PMC_data(new_pmc) = io;
- /* io could be NULL */
- if(io)
- PMC_struct_val(new_pmc) = io->stack;
- else
- PMC_struct_val(new_pmc) = NULL;
+ PMC_struct_val(new_pmc) = io ? io->stack : NULL;
return new_pmc;
}
@@ -130,10 +125,8 @@
ParrotIOTable
alloc_pio_array(int numhandles)
{
- ParrotIOTable newhandles;
- size_t size = numhandles * sizeof(ParrotIO *);
- newhandles = (ParrotIOTable)mem_sys_allocate_zeroed(size);
- return newhandles;
+ const size_t size = numhandles * sizeof(ParrotIO *);
+ return (ParrotIOTable)mem_sys_allocate_zeroed(size);
}
/*
@@ -173,11 +166,9 @@
ParrotIO *
PIO_new(theINTERP, INTVAL iotype, INTVAL flags, INTVAL mode)
{
- ParrotIO *new_io;
-
+ ParrotIO * const new_io = (ParrotIO *)mem_sys_allocate(sizeof(ParrotIO));
UNUSED(iotype);
- new_io = (ParrotIO *)mem_sys_allocate(sizeof(ParrotIO));
new_io->fpos = new_io->lpos = piooffsetzero;
new_io->flags = flags;
new_io->mode = mode;
@@ -205,7 +196,7 @@
void
PIO_destroy(theINTERP, PMC *pmc)
{
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
UNUSED(interpreter);
if(!io)
@@ -571,8 +562,8 @@
INTVAL
PIO_peek(theINTERP, PMC *pmc, STRING **buffer)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
if(!io)
return -1;
return PIO_peek_down(interpreter, l, io, buffer);
@@ -603,7 +594,7 @@
PIO_pioctl(theINTERP, PMC *pmc, INTVAL cmd, INTVAL arg)
{
- ParrotIO * io = PMC_data0(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
ParrotIOBuf * b;
if(!io) return -1;
b = &io->b;
@@ -654,8 +645,8 @@
INTVAL
PIO_setbuf(theINTERP, PMC *pmc, size_t bufsize)
{
- ParrotIOLayer *layer = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIOLayer * const layer = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
if(!io)
return -1;
PIO_flush(interpreter, pmc);
@@ -677,8 +668,8 @@
INTVAL
PIO_setlinebuf(theINTERP, PMC *pmc)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
if(!io)
return -1;
@@ -702,7 +693,7 @@
const char *sflags)
{
ParrotIO *io;
- INTVAL flags = PIO_parse_open_flags(sflags);
+ const INTVAL flags = PIO_parse_open_flags(sflags);
if (!layer) {
layer = interpreter->piodata->default_stack;
@@ -775,8 +766,8 @@
PIO_close(theINTERP, PMC *pmc)
{
INTVAL res;
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
if(!io)
return -1;
PIO_flush(interpreter, pmc); /* XXX boe: is this neccessary here? */
@@ -800,8 +791,8 @@
void
PIO_flush(theINTERP, PMC *pmc)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
if(!io)
return;
@@ -829,8 +820,8 @@
PIO_reads(theINTERP, PMC *pmc, size_t len)
{
STRING *res = NULL;
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
if (!io)
return new_string_header(interpreter, 0);
@@ -852,8 +843,8 @@
INTVAL
PIO_read(theINTERP, PMC *pmc, void *buffer, size_t len)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
STRING *res = new_string_header(interpreter, 0);
if (!io)
return -1;
@@ -877,9 +868,8 @@
INTVAL
PIO_write(theINTERP, PMC *pmc, const void *buffer, size_t len)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data0(pmc);
- STRING fake;
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
union {
const void * __c_ptr;
void * __ptr;
@@ -889,6 +879,7 @@
return -1;
if (io->flags & PIO_F_WRITE) {
+ STRING fake;
/* TODO skip utf8 translation layers if any */
fake.strstart = const_cast(buffer);
fake.strlen = fake.bufused = len;
@@ -916,8 +907,8 @@
PIOOFF_T
PIO_seek(theINTERP, PMC *pmc, PIOOFF_T offset, INTVAL w)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
if(!io)
return -1;
@@ -938,8 +929,8 @@
PIOOFF_T
PIO_tell(theINTERP, PMC *pmc)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
if(!io)
return -1;
@@ -961,7 +952,7 @@
INTVAL
PIO_eof(theINTERP, PMC *pmc)
{
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
UNUSED(interpreter);
@@ -991,6 +982,7 @@
return PIO_write(interpreter, pmc, s, strlen(s));
}
+/* XXX Should be in an external header file */
void *Parrot_utf8_encode(void *ptr, UINTVAL c);
/*
@@ -1007,8 +999,8 @@
INTVAL
PIO_putps(theINTERP, PMC *pmc, STRING *s)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
assert((unsigned long)l != 0xdeadbeefUL);
assert(io != 0);
#if ! DISABLE_GC_DEBUG
@@ -1034,7 +1026,7 @@
PIO_fprintf(theINTERP, PMC *pmc, const char *s, ...)
{
va_list args;
- INTVAL ret=-1;
+ INTVAL ret;
va_start(args, s);
@@ -1059,13 +1051,12 @@
INTVAL
PIO_printf(theINTERP, const char *s, ...) {
va_list args;
- STRING *str;
- INTVAL ret=-1;
+ INTVAL ret;
va_start(args, s);
if(interpreter) {
- str=Parrot_vsprintf_c(interpreter, s, args);
+ STRING * const str = Parrot_vsprintf_c(interpreter, s, args);
ret=PIO_putps(interpreter, PIO_STDOUT(interpreter), str);
}
else {
@@ -1094,13 +1085,12 @@
INTVAL
PIO_eprintf(theINTERP, const char *s, ...) {
va_list args;
- STRING *str;
- INTVAL ret=-1;
+ INTVAL ret;
va_start(args, s);
if(interpreter) {
- str=Parrot_vsprintf_c(interpreter, s, args);
+ STRING * const str = Parrot_vsprintf_c(interpreter, s, args);
ret=PIO_putps(interpreter, PIO_STDERR(interpreter), str);
}
@@ -1324,8 +1314,8 @@
INTVAL
PIO_poll(theINTERP, PMC *pmc, INTVAL which, INTVAL sec, INTVAL usec)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data0(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data0(pmc);
return PIO_poll_down(interpreter, l, io, which, sec, usec);
}
@@ -1345,9 +1335,8 @@
PMC *
PIO_socket(theINTERP, INTVAL fam, INTVAL type, INTVAL proto)
{
- ParrotIO *io;
- ParrotIOLayer *l = interpreter->piodata->default_stack;
- io = PIO_socket_down(interpreter, l, fam, type, proto);
+ ParrotIOLayer * const l = interpreter->piodata->default_stack;
+ ParrotIO * const io = PIO_socket_down(interpreter, l, fam, type, proto);
/* We have to create a PMC here even if the IO handle
* didn't create because caller has to be able to
* check with a bool test. Can't use a NULL PMC in a bool
@@ -1370,8 +1359,8 @@
INTVAL
PIO_recv(theINTERP, PMC *pmc, STRING **buf)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data(pmc);
if(!io)
return -1;
@@ -1392,8 +1381,8 @@
INTVAL
PIO_send(theINTERP, PMC *pmc, STRING *buf)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data(pmc);
if(!io)
return -1;
@@ -1414,8 +1403,8 @@
INTVAL
PIO_connect(theINTERP, PMC *pmc, STRING *address)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data(pmc);
if(!io)
return -1;
@@ -1436,8 +1425,8 @@
INTVAL
PIO_bind(theINTERP, PMC *pmc, STRING *address)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data(pmc);
if(!io)
return -1;
@@ -1458,8 +1447,8 @@
INTVAL
PIO_listen(theINTERP, PMC *pmc, INTVAL backlog)
{
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data(pmc);
if(!io)
return -1;
@@ -1480,8 +1469,8 @@
PIO_accept(theINTERP, PMC *pmc)
{
ParrotIO *io2;
- ParrotIOLayer *l = PMC_struct_val(pmc);
- ParrotIO *io = PMC_data(pmc);
+ ParrotIOLayer * const l = PMC_struct_val(pmc);
+ ParrotIO * const io = PMC_data(pmc);
if(!io)
return NULL;
@@ -1503,7 +1492,7 @@
INTVAL
PIO_isatty(theINTERP, PMC *pmc)
{
- ParrotIO *io = PMC_data(pmc);
+ ParrotIO * const io = PMC_data(pmc);
UNUSED(interpreter);
@@ -1518,7 +1507,7 @@
int
PIO_softspace(theINTERP, PMC *pmc, int new)
{
- ParrotIO *io = PMC_data(pmc);
+ ParrotIO * const io = PMC_data(pmc);
int ret;
UNUSED(interpreter);
Modified: trunk/src/io/io_buf.c
==============================================================================
--- trunk/src/io/io_buf.c (original)
+++ trunk/src/io/io_buf.c Sat Apr 22 20:40:38 2006
@@ -110,10 +110,8 @@
PIO_buf_open(theINTERP, ParrotIOLayer *layer,
const char *path, INTVAL flags)
{
- ParrotIO *io;
- ParrotIOLayer *l = PIO_DOWNLAYER(layer);
-
- io = PIO_open_down(interpreter, l, path, flags);
+ ParrotIOLayer * const l = PIO_DOWNLAYER(layer);
+ ParrotIO * const io = PIO_open_down(interpreter, l, path, flags);
if (!io) {
/* error creating IO stream */
return NULL;
@@ -147,10 +145,9 @@
static INTVAL
PIO_buf_setbuf(theINTERP, ParrotIOLayer *layer, ParrotIO *io, size_t bufsize)
{
- ParrotIOLayer *l = layer;
- ParrotIOBuf *b = &io->b;
- if(!l)
- l = io->stack;
+ ParrotIOLayer * const l = layer ? layer : io->stack;
+ ParrotIOBuf * const b = &io->b;
+
/* If there is a buffer, make sure we flush before
* dinking around with the buffer.
*/
@@ -207,11 +204,7 @@
PIO_buf_setlinebuf(theINTERP, ParrotIOLayer *layer, ParrotIO *io)
{
int err;
- ParrotIOLayer * l;
-
- l = layer;
- if(!l)
- l = io->stack;
+ ParrotIOLayer * const l = layer ? layer : io->stack;
/* already linebuffering */
if (io->flags & PIO_F_LINEBUF)
@@ -242,10 +235,9 @@
static ParrotIO *
PIO_buf_fdopen(theINTERP, ParrotIOLayer *layer, PIOHANDLE fd, INTVAL flags)
{
- ParrotIO *io;
- ParrotIOLayer *l = PIO_DOWNLAYER(layer);
+ ParrotIOLayer * const l = PIO_DOWNLAYER(layer);
+ ParrotIO * const io = PIO_fdopen_down(interpreter, l, fd, flags);
- io = PIO_fdopen_down(interpreter, l, fd, flags);
if (!io) {
/* error creating IO stream */
return NULL;
@@ -273,7 +265,7 @@
static INTVAL
PIO_buf_close(theINTERP, ParrotIOLayer *layer, ParrotIO *io)
{
- ParrotIOLayer *l = PIO_DOWNLAYER(layer);
+ ParrotIOLayer * const l = PIO_DOWNLAYER(layer);
PIO_buf_flush(interpreter, layer, io);
return PIO_close_down (interpreter, l, io);
@@ -307,7 +299,7 @@
* Write flush
*/
if (io->b.flags & PIO_BF_WRITEBUF) {
- ParrotIOLayer *l = layer;
+ ParrotIOLayer * const l = layer;
to_write = io->b.next - io->b.startb;
/* Flush to next layer */
@@ -421,7 +413,7 @@
out_buf = s->strstart;
/* read Data from buffer */
if (b->flags & PIO_BF_READBUF) {
- size_t avail = b->endb - b->next;
+ const size_t avail = b->endb - b->next;
current = avail < len ? avail : len;
memcpy(out_buf, b->next, current);
@@ -451,10 +443,11 @@
if (!(b->flags & PIO_BF_READBUF)) {
size_t got;
if (len >= io->b.size) {
- STRING fake, *sf;
+ STRING fake;
+ STRING *sf = &fake;
+
fake.strstart = out_buf;
fake.bufused = len;
- sf = &fake;
got = PIO_read_down(interpreter, PIO_DOWNLAYER(l), io, &sf);
s->strlen = s->bufused = current + got;
io->fpos += got;
@@ -488,10 +481,8 @@
ParrotIOLayer *l = layer;
ParrotIOBuf *b;
size_t len = 1;
- STRING *s;
- size_t avail;
- s = PIO_make_io_string(interpreter, buf, 1);
+ STRING * const s = PIO_make_io_string(interpreter, buf, 1);
/* write buffer flush */
if (io->b.flags & PIO_BF_WRITEBUF) {
@@ -502,7 +493,7 @@
/* read Data from buffer */
if (b->flags & PIO_BF_READBUF) {
- avail = b->endb - b->next;
+ const size_t avail = b->endb - b->next;
/* if we have data available, copy out the next byte */
if (avail) {
@@ -549,7 +540,7 @@
size_t l;
unsigned char *out_buf;
unsigned char *buf_start;
- ParrotIOBuf *b = &io->b;
+ ParrotIOBuf * const b = &io->b;
size_t len;
STRING *s;
@@ -632,7 +623,7 @@
{
size_t avail;
long wrote;
- void *buffer = s->strstart;
+ void * const buffer = s->strstart;
size_t len = s->bufused;
int need_flush;
@@ -658,10 +649,10 @@
char *p = (char*)buffer + len - 1;
size_t i;
for (i = 0; i < len; ++i, --p)
- if (IS_EOL(io, p)) {
- need_flush = 1;
- break;
- }
+ if (IS_EOL(io, p)) {
+ need_flush = 1;
+ break;
+ }
}
/*
@@ -690,7 +681,7 @@
return len;
}
else {
- unsigned int diff = (int)(len - avail);
+ const unsigned int diff = (int)(len - avail);
io->b.flags |= PIO_BF_WRITEBUF;
/* Fill remainder, flush, then try to buffer more */
Modified: trunk/src/io/io_layers.c
==============================================================================
--- trunk/src/io/io_layers.c (original)
+++ trunk/src/io/io_layers.c Sat Apr 22 20:40:38 2006
@@ -1,5 +1,5 @@
/*
-Copyright: 2001-2003 The Perl Foundation. All Rights Reserved.
+Copyright: 2001-2006 The Perl Foundation. All Rights Reserved.
$Id$
=head1 NAME
@@ -39,12 +39,11 @@
ParrotIOLayer *
PIO_base_new_layer(ParrotIOLayer *proto)
{
- ParrotIOLayer *new_layer;
+ ParrotIOLayer * const new_layer = mem_sys_allocate(sizeof(ParrotIOLayer));
/*
* XXX use managed memory here ?
*/
- new_layer = mem_sys_allocate(sizeof(ParrotIOLayer));
if (proto) {
/* FIXME: Flag here to indicate whether to free strings */
new_layer->name = proto->name;
@@ -100,12 +99,12 @@
INTVAL
PIO_push_layer(theINTERP, PMC *pmc, ParrotIOLayer *layer)
{
- ParrotIOLayer *t;
if (layer == NULL)
return -1;
if (!PMC_IS_NULL(pmc)) {
- ParrotIO *io = PMC_data(pmc);
+ ParrotIOLayer *t;
+ ParrotIO * const io = PMC_data(pmc);
if (!io)
return -1;
if (io->stack == NULL && (layer->flags & PIO_L_TERMINAL) == 0) {
@@ -137,7 +136,8 @@
(*layer->api->Pushed) (layer, io);
}
else {
- ParrotIOData *d = interpreter->piodata;
+ ParrotIOLayer *t;
+ ParrotIOData * const d = interpreter->piodata;
if (d->default_stack == NULL && (layer->flags & PIO_L_TERMINAL) == 0) {
/* Error( 1st layer must be terminal) */
return -1;
@@ -163,7 +163,7 @@
UNUSED(interpreter);
for (t = pio_registered_layers; *t; ++t)
- if (!strcmp(name, (*t)->name))
+ if (strcmp(name, (*t)->name) == 0)
return *t;
return NULL;
}
@@ -171,18 +171,18 @@
void
PIO_push_layer_str(Interp *interpreter, PMC *pmc, STRING *ls)
{
- ParrotIOLayer *l;
- char *cls = string_to_cstring(interpreter, ls);
+ char * const cls = string_to_cstring(interpreter, ls);
+ ParrotIOLayer * const l = PIO_get_layer(interpreter, cls);
+ ParrotIOLayer * newlayer;
- l = PIO_get_layer(interpreter, cls);
string_cstring_free(cls);
if (!l)
internal_exception(1, "Layer not found");
/* make private copy */
- l = PIO_base_new_layer(l);
- l->flags |= PIO_L_LAYER_COPIED;
- PIO_push_layer(interpreter, pmc, l);
+ newlayer = PIO_base_new_layer(l);
+ newlayer->flags |= PIO_L_LAYER_COPIED;
+ PIO_push_layer(interpreter, pmc, newlayer);
}
/*
@@ -205,10 +205,10 @@
ParrotIOLayer *
PIO_pop_layer(theINTERP, PMC *pmc)
{
- ParrotIOLayer *layer;
- ParrotIO *io = PMC_data(pmc);
+ ParrotIO * const io = PMC_data(pmc);
if (!PMC_IS_NULL(pmc)) {
+ ParrotIOLayer *layer;
if (!io)
return 0;
/*
@@ -232,9 +232,8 @@
}
/* Null io object - use default stack */
else {
- ParrotIOData *d;
- d = interpreter->piodata;
- layer = d->default_stack;
+ ParrotIOData * const d = interpreter->piodata;
+ ParrotIOLayer * const layer = d->default_stack;
if (layer) {
d->default_stack = layer->down;
d->default_stack->up = NULL;
@@ -244,17 +243,14 @@
}
}
- return 0;
+ return NULL;
}
STRING *
PIO_pop_layer_str(Interp *interpreter, PMC *pmc)
{
- ParrotIOLayer *layer;
- STRING *ls;
-
- layer = PIO_pop_layer(interpreter, pmc);
- ls = string_make(interpreter, layer->name, strlen(layer->name),
+ ParrotIOLayer * const layer = PIO_pop_layer(interpreter, pmc);
+ STRING * const ls = string_make(interpreter, layer->name,
strlen(layer->name),
"iso-8859-1", 0);
mem_sys_free(layer);
return ls;