+2001-03-17 Gary Houston <ghouston@arglist.com>
+
+ * gc.c (scm_must_malloc): changed the comment explaining when
+ scm_must variants of malloc/free etc., should be used, based on
+ explanation from Dirk Herrmann.
+ * fports.c (scm_fport_buffer_add): use FUNC_NAME instead of a local
+ string with procedure name. use scm_must_malloc instead of malloc.
+ (scm_setvbuf, scm_fdes_to_port, fport_close): use scm_must variants
+ of malloc/free.
+ * ports.c (scm_add_to_port_table, scm_remove_from_port_table,
+ scm_ungetc): use scm_must variants of malloc/realloc/free.
+ (scm_add_to_port_table, scm_ungetc): define FUNC_NAME.
+
2001-03-17 Dirk Herrmann <D.Herrmann@tu-bs.de>
* __scm.h (SCM_ASSERT, SCM_WTA_DISPATCH_0, SCM_WTA_DISPATCH_1,
-/* Copyright (C) 1995,1996,1997,1998,1999, 2000 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
0 for no buffer. */
static void
scm_fport_buffer_add (SCM port, int read_size, int write_size)
+#define FUNC_NAME "scm_fport_buffer_add"
{
struct scm_fport *fp = SCM_FSTREAM (port);
scm_port *pt = SCM_PTAB_ENTRY (port);
- char *s_scm_fport_buffer_add = "scm_fport_buffer_add";
if (read_size == -1 || write_size == -1)
{
if (SCM_INPUT_PORT_P (port) && read_size > 0)
{
- pt->read_buf = malloc (read_size);
- if (pt->read_buf == NULL)
- scm_memory_error (s_scm_fport_buffer_add);
+ pt->read_buf = scm_must_malloc (read_size, FUNC_NAME);
pt->read_pos = pt->read_end = pt->read_buf;
pt->read_buf_size = read_size;
}
if (SCM_OUTPUT_PORT_P (port) && write_size > 0)
{
- pt->write_buf = malloc (write_size);
- if (pt->write_buf == NULL)
- scm_memory_error (s_scm_fport_buffer_add);
+ pt->write_buf = scm_must_malloc (write_size, FUNC_NAME);
pt->write_pos = pt->write_buf;
pt->write_buf_size = write_size;
}
else
SCM_SET_CELL_WORD_0 (port, SCM_CELL_WORD_0 (port) | SCM_BUF0);
}
+#undef FUNC_NAME
SCM_DEFINE (scm_setvbuf, "setvbuf", 2, 1, 0,
(SCM port, SCM mode, SCM size),
/* silently discards buffered chars. */
if (pt->read_buf != &pt->shortbuf)
- free (pt->read_buf);
+ scm_must_free (pt->read_buf);
if (pt->write_buf != &pt->shortbuf)
- free (pt->write_buf);
+ scm_must_free (pt->write_buf);
scm_fport_buffer_add (port, csize, csize);
return SCM_UNSPECIFIED;
{
struct scm_fport *fp
- = (struct scm_fport *) malloc (sizeof (struct scm_fport));
- if (fp == NULL)
- SCM_MEMORY_ERROR;
+ = (struct scm_fport *) scm_must_malloc (sizeof (struct scm_fport),
+ FUNC_NAME);
+
fp->fdes = fdes;
pt->rw_random = SCM_FDES_RANDOM_P (fdes);
SCM_SETSTREAM (port, fp);
if (pt->read_buf == pt->putback_buf)
pt->read_buf = pt->saved_read_buf;
if (pt->read_buf != &pt->shortbuf)
- free (pt->read_buf);
+ scm_must_free (pt->read_buf);
if (pt->write_buf != &pt->shortbuf)
- free (pt->write_buf);
- free ((char *) fp);
+ scm_must_free (pt->write_buf);
+ scm_must_free ((char *) fp);
return rv;
}
* scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
* scm_done_free
*
- * These functions provide services comperable to malloc, realloc, and
- * free. They are for allocating malloced parts of scheme objects.
- * The primary purpose of the front end is to impose calls to gc. */
-
+ * These functions provide services comparable to malloc, realloc, and
+ * free. They should be used when allocating memory that will be under
+ * control of the garbage collector, i.e., if the memory may be freed
+ * during garbage collection.
+ */
/* scm_must_malloc
* Return newly malloced storage or throw an error.
scm_port *
scm_add_to_port_table (SCM port)
+#define FUNC_NAME "scm_add_to_port_table"
{
scm_port *entry;
if (scm_port_table_size == scm_port_table_room)
{
+ /* initial malloc is in gc.c. this doesn't use scm_must_malloc etc.,
+ since it can never be freed during gc. */
void *newt = realloc ((char *) scm_port_table,
(scm_sizet) (sizeof (scm_port *)
* scm_port_table_room * 2));
scm_port_table = (scm_port **) newt;
scm_port_table_room *= 2;
}
- entry = (scm_port *) malloc (sizeof (scm_port));
- if (entry == NULL)
- scm_memory_error ("scm_add_to_port_table");
+ entry = (scm_port *) scm_must_malloc (sizeof (scm_port), FUNC_NAME);
entry->port = port;
entry->entry = scm_port_table_size;
return entry;
}
+#undef FUNC_NAME
/* Remove a port from the table and destroy it. */
if (i >= scm_port_table_size)
SCM_MISC_ERROR ("Port not in table: ~S", SCM_LIST1 (port));
if (p->putback_buf)
- free (p->putback_buf);
- free (p);
+ scm_must_free (p->putback_buf);
+ scm_must_free (p);
/* Since we have just freed slot i we can shrink the table by moving
the last entry to that slot... */
if (i < scm_port_table_size - 1)
void
scm_ungetc (int c, SCM port)
+#define FUNC_NAME "scm_ungetc"
{
scm_port *pt = SCM_PTAB_ENTRY (port);
&& pt->read_buf == pt->read_pos)
{
int new_size = pt->read_buf_size * 2;
- unsigned char *tmp =
- (unsigned char *) realloc (pt->putback_buf, new_size);
+ unsigned char *tmp = (unsigned char *)
+ scm_must_realloc (pt->putback_buf, pt->read_buf_size, new_size,
+ FUNC_NAME);
- if (tmp == NULL)
- scm_memory_error ("scm_ungetc");
pt->read_pos = pt->read_buf = pt->putback_buf = tmp;
pt->read_end = pt->read_buf + pt->read_buf_size;
pt->read_buf_size = pt->putback_buf_size = new_size;
if (pt->putback_buf == NULL)
{
pt->putback_buf
- = (unsigned char *) malloc (SCM_INITIAL_PUTBACK_BUF_SIZE);
- if (pt->putback_buf == NULL)
- scm_memory_error ("scm_ungetc");
+ = (unsigned char *) scm_must_malloc (SCM_INITIAL_PUTBACK_BUF_SIZE,
+ FUNC_NAME);
pt->putback_buf_size = SCM_INITIAL_PUTBACK_BUF_SIZE;
}
else
SCM_COL(port) -= 1;
}
+#undef FUNC_NAME
void