* gc.c (scm_must_malloc): changed the comment explaining when
authorGary Houston <ghouston@arglist.com>
Sat, 17 Mar 2001 16:59:48 +0000 (16:59 +0000)
committerGary Houston <ghouston@arglist.com>
Sat, 17 Mar 2001 16:59:48 +0000 (16:59 +0000)
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.

libguile/ChangeLog
libguile/fports.c
libguile/gc.c
libguile/ports.c

index b6e4a09..502325d 100644 (file)
@@ -1,3 +1,16 @@
+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,
index 06dae4e..c062525 100644 (file)
@@ -1,4 +1,4 @@
-/*     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
@@ -80,10 +80,10 @@ static const int default_buffer_size = 1024;
    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)
     {
@@ -104,9 +104,7 @@ scm_fport_buffer_add (SCM port, int read_size, int write_size)
 
   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;
     }
@@ -118,9 +116,7 @@ scm_fport_buffer_add (SCM port, int read_size, int write_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;
     }
@@ -136,6 +132,7 @@ scm_fport_buffer_add (SCM port, int read_size, int 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),
@@ -189,9 +186,9 @@ SCM_DEFINE (scm_setvbuf, "setvbuf", 2, 1, 0,
 
   /* 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;
@@ -386,9 +383,9 @@ scm_fdes_to_port (int fdes, char *mode, SCM name)
 
   {
     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);
@@ -768,10 +765,10 @@ fport_close (SCM port)
   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;
 }
 
index 264b9f3..c99e3f0 100644 (file)
@@ -1839,10 +1839,11 @@ scm_gc_sweep ()
  * 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.
index 898de99..04725b8 100644 (file)
@@ -431,11 +431,14 @@ int scm_port_table_room = 20;     /* Size of the array.  */
 
 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));
@@ -444,9 +447,7 @@ scm_add_to_port_table (SCM port)
       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;
@@ -465,6 +466,7 @@ scm_add_to_port_table (SCM port)
 
   return entry;
 }
+#undef FUNC_NAME
 
 /* Remove a port from the table and destroy it.  */
 
@@ -478,8 +480,8 @@ scm_remove_from_port_table (SCM port)
   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)
@@ -1101,6 +1103,7 @@ scm_end_input (SCM port)
 
 void 
 scm_ungetc (int c, SCM port)
+#define FUNC_NAME "scm_ungetc"
 {
   scm_port *pt = SCM_PTAB_ENTRY (port);
 
@@ -1112,11 +1115,10 @@ scm_ungetc (int c, SCM 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;
@@ -1141,9 +1143,8 @@ scm_ungetc (int c, SCM port)
       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;
        }
 
@@ -1172,6 +1173,7 @@ scm_ungetc (int c, SCM port)
   else
     SCM_COL(port) -= 1;
 }
+#undef FUNC_NAME
 
 
 void