* validate.h
[bpt/guile.git] / libguile / smob.c
index 5b9a070..a487b97 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1995, 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,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
@@ -45,6 +45,8 @@
 \f
 
 #include <stdio.h>
+#include <errno.h>
+
 #include "libguile/_scm.h"
 
 #include "libguile/objects.h"
 \f
 
 /* scm_smobs scm_numsmob
- * implement a dynamicly resized array of smob records.
+ * implement a fixed sized array of smob records.
  * Indexes into this table are used when generating type
  * tags for smobjects (if you know a tag you can get an index and conversely).
  */
-int scm_numsmob;
-scm_smob_descriptor *scm_smobs;
+
+#define MAX_SMOB_COUNT 256
+scm_bits_t scm_numsmob;
+scm_smob_descriptor scm_smobs[MAX_SMOB_COUNT];
 
 /* {Mark}
  */
@@ -86,21 +90,23 @@ scm_mark0 (SCM ptr)
 }
 
 SCM 
+/* Dirk::FIXME: The name markcdr is misleading, since the term cdr should only
+   be used for real pairs. */
 scm_markcdr (SCM ptr)
 {
-  return SCM_CDR (ptr);
+  return SCM_CELL_OBJECT_1 (ptr);
 }
 
 /* {Free}
  */
 
-scm_size
+size_
 scm_free0 (SCM ptr)
 {
   return 0;
 }
 
-scm_sizet
+size_t
 scm_smob_free (SCM obj)
 {
   scm_must_free ((char *) SCM_CELL_WORD_1 (obj));
@@ -113,11 +119,14 @@ scm_smob_free (SCM obj)
 int
 scm_smob_print (SCM exp, SCM port, scm_print_state *pstate)
 {
-  int n = SCM_SMOBNUM (exp);
+  size_t n = SCM_SMOBNUM (exp);
   scm_puts ("#<", port);
   scm_puts (SCM_SMOBNAME (n) ? SCM_SMOBNAME (n) : "smob", port);
   scm_putc (' ', port);
-  scm_intprint (SCM_UNPACK (scm_smobs[n].size ? SCM_CDR (exp) : exp), 16, port);
+  if (scm_smobs[n].size)
+    scm_intprint (SCM_CELL_WORD_1 (exp), 16, port);
+  else
+    scm_intprint (SCM_UNPACK (exp), 16, port);
   scm_putc ('>', port);
   return 1;
 }
@@ -275,72 +284,64 @@ scm_smob_apply_3_error (SCM smob, SCM a1, SCM a2, SCM rst)
 }
 
 \f
-long 
-scm_make_smob_type (char *name, scm_sizet size)
-{
-  char *tmp;
-  if (255 <= scm_numsmob)
-    goto smoberr;
-  SCM_DEFER_INTS;
-  SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_smobs,
-                                      (1 + scm_numsmob)
-                                      * sizeof (scm_smob_descriptor)));
-  if (tmp)
-    {
-      scm_smobs = (scm_smob_descriptor *) tmp;
-      scm_smobs[scm_numsmob].name = name;
-      scm_smobs[scm_numsmob].size = size;
-      scm_smobs[scm_numsmob].mark = 0;
-      scm_smobs[scm_numsmob].free = (size == 0 ? scm_free0 : scm_smob_free);
-      scm_smobs[scm_numsmob].print = scm_smob_print;
-      scm_smobs[scm_numsmob].equalp = 0;
-      scm_smobs[scm_numsmob].apply = 0;
-      scm_smobs[scm_numsmob].apply_0 = 0;
-      scm_smobs[scm_numsmob].apply_1 = 0;
-      scm_smobs[scm_numsmob].apply_2 = 0;
-      scm_smobs[scm_numsmob].apply_3 = 0;
-      scm_smobs[scm_numsmob].gsubr_type = 0;
-      scm_numsmob++;
-    }
-  SCM_ALLOW_INTS;
-  if (!tmp) 
+
+scm_bits_t 
+scm_make_smob_type (char *name, size_t size)
+#define FUNC_NAME "scm_make_smob_type"
+{
+  size_t new_smob;
+
+  SCM_ENTER_A_SECTION;  /* scm_numsmob */
+  new_smob = scm_numsmob;
+  if (scm_numsmob != MAX_SMOB_COUNT)
+    ++scm_numsmob;
+  SCM_EXIT_A_SECTION;
+
+  if (new_smob == MAX_SMOB_COUNT)
+    scm_misc_error (FUNC_NAME, "maximum number of smobs exceeded", SCM_EOL);
+
+  scm_smobs[new_smob].name = name;
+  if (size != 0)
     {
-    smoberr:
-      scm_memory_error ("scm_make_smob_type");
+      scm_smobs[new_smob].size = size;
+      scm_smobs[new_smob].free = scm_smob_free;
     }
+
   /* Make a class object if Goops is present. */
   if (scm_smob_class)
-    scm_smob_class[scm_numsmob - 1]
-      = scm_make_extended_class (SCM_SMOBNAME (scm_numsmob - 1));
-  return scm_tc7_smob + (scm_numsmob - 1) * 256;
+    scm_smob_class[new_smob] = scm_make_extended_class (name);
+
+  return scm_tc7_smob + new_smob * 256;
 }
+#undef FUNC_NAME
+
 
 void
-scm_set_smob_mark (long tc, SCM (*mark) (SCM))
+scm_set_smob_mark (scm_bits_t tc, SCM (*mark) (SCM))
 {
   scm_smobs[SCM_TC2SMOBNUM (tc)].mark = mark;
 }
 
 void
-scm_set_smob_free (long tc, scm_sizet (*free) (SCM))
+scm_set_smob_free (scm_bits_t tc, size_t (*free) (SCM))
 {
   scm_smobs[SCM_TC2SMOBNUM (tc)].free = free;
 }
 
 void
-scm_set_smob_print (long tc, int (*print) (SCM, SCM, scm_print_state*))
+scm_set_smob_print (scm_bits_t tc, int (*print) (SCM, SCM, scm_print_state*))
 {
   scm_smobs[SCM_TC2SMOBNUM (tc)].print = print;
 }
 
 void
-scm_set_smob_equalp (long tc, SCM (*equalp) (SCM, SCM))
+scm_set_smob_equalp (scm_bits_t tc, SCM (*equalp) (SCM, SCM))
 {
   scm_smobs[SCM_TC2SMOBNUM (tc)].equalp = equalp;
 }
 
 void
-scm_set_smob_apply (long tc, SCM (*apply) (),
+scm_set_smob_apply (scm_bits_t tc, SCM (*apply) (),
                    unsigned int req, unsigned int opt, unsigned int rst)
 {
   SCM (*apply_0) (SCM);
@@ -441,7 +442,7 @@ scm_set_smob_apply (long tc, SCM (*apply) (),
       apply_3 = scm_smob_apply_3_error; break;
     }
 
-  scm_smobs[SCM_TC2SMOBNUM (tc)].apply = apply;
+  scm_smobs[SCM_TC2SMOBNUM (tc)].apply   = apply;
   scm_smobs[SCM_TC2SMOBNUM (tc)].apply_0 = apply_0;
   scm_smobs[SCM_TC2SMOBNUM (tc)].apply_1 = apply_1;
   scm_smobs[SCM_TC2SMOBNUM (tc)].apply_2 = apply_2;
@@ -450,19 +451,22 @@ scm_set_smob_apply (long tc, SCM (*apply) (),
 }
 
 SCM
-scm_make_smob (long tc)
+scm_make_smob (scm_bits_t tc)
 {
-  int n = SCM_TC2SMOBNUM (tc);
-  scm_sizet size = scm_smobs[n].size;
+  size_t n = SCM_TC2SMOBNUM (tc);
+  size_t size = scm_smobs[n].size;
   SCM z;
   SCM_NEWCELL (z);
   if (size != 0)
     {
 #if 0
-      SCM_ASSERT (scm_smobs[n].mark == 0,
-                 0,
-                 "forbidden operation for smobs with GC data, use SCM_NEWSMOB",
-                 SCM_SMOBNAME (n));
+      if (scm_smobs[n].mark != 0)
+       {
+         fprintf
+           (stderr,
+            "forbidden operation for smobs with GC data, use SCM_NEWSMOB\n");
+         abort ();
+       }
 #endif
       SCM_SET_SMOB_DATA (z, scm_must_malloc (size, SCM_SMOBNAME (n)));
     }
@@ -477,13 +481,13 @@ scm_make_smob (long tc)
 #if (SCM_DEBUG_DEPRECATED == 0)
 
 long
-scm_make_smob_type_mfpe (char *name, scm_sizet size,
+scm_make_smob_type_mfpe (char *name, size_t size,
                         SCM (*mark) (SCM),
-                        scm_sizet (*free) (SCM),
+                        size_t (*free) (SCM),
                         int (*print) (SCM, SCM, scm_print_state *),
                         SCM (*equalp) (SCM, SCM))
 {
-  long answer = scm_make_smob_type (name, size);
+  scm_bits_t answer = scm_make_smob_type (name, size);
   scm_set_smob_mfpe (answer, mark, free, print, equalp);
   return answer;
 }
@@ -491,7 +495,7 @@ scm_make_smob_type_mfpe (char *name, scm_sizet size,
 void
 scm_set_smob_mfpe (long tc, 
                   SCM (*mark) (SCM),
-                  scm_sizet (*free) (SCM),
+                  size_t (*free) (SCM),
                   int (*print) (SCM, SCM, scm_print_state *),
                   SCM (*equalp) (SCM, SCM))
 {
@@ -508,38 +512,55 @@ scm_set_smob_mfpe (long tc,
  */
 
 static int
-freeprint (SCM exp,
-          SCM port,
-          scm_print_state *pstate)
+free_print (SCM exp, SCM port, scm_print_state *pstate)
 {
   char buf[100];
 
-  sprintf (buf, "#<freed cell %p; GC missed a reference>", (void *) SCM_UNPACK (exp));
+  sprintf (buf, "#<freed cell %p; GC missed a reference>",
+          (void *) SCM_UNPACK (exp));
   scm_puts (buf, port);
 
   return 1;
 }
 
-
 void
 scm_smob_prehistory ()
 {
+  size_t i;
+  scm_bits_t tc;
+
   scm_numsmob = 0;
-  scm_smobs = ((scm_smob_descriptor *)
-              malloc (7 * sizeof (scm_smob_descriptor)));
+  for (i = 0; i < MAX_SMOB_COUNT; ++i)
+    {
+      scm_smobs[i].name       = 0;
+      scm_smobs[i].size       = 0;
+      scm_smobs[i].mark       = 0;
+      scm_smobs[i].free       = 0;
+      scm_smobs[i].print      = scm_smob_print;
+      scm_smobs[i].equalp     = 0;
+      scm_smobs[i].apply      = 0;
+      scm_smobs[i].apply_0    = 0;
+      scm_smobs[i].apply_1    = 0;
+      scm_smobs[i].apply_2    = 0;
+      scm_smobs[i].apply_3    = 0;
+      scm_smobs[i].gsubr_type = 0;
+    }
 
   /* WARNING: These scm_make_smob_type calls must be done in this order */
-  scm_make_smob_type_mfpe ("free", 0,
-                          NULL, NULL, freeprint, NULL);
+  tc = scm_make_smob_type ("free", 0);
+  scm_set_smob_print (tc, free_print);
 
-  scm_make_smob_type_mfpe ("big", 0,     /* freed in gc */
-                          NULL, NULL, scm_bigprint, scm_bigequal);
+  tc = scm_make_smob_type ("big", 0);  /* freed in gc */
+  scm_set_smob_print (tc, scm_bigprint);
+  scm_set_smob_equalp (tc, scm_bigequal);
 
-  scm_make_smob_type_mfpe ("real", 0,    /* freed in gc */
-                          NULL, NULL, scm_print_real, scm_real_equalp);
+  tc = scm_make_smob_type ("real", 0); /* freed in gc */
+  scm_set_smob_print (tc, scm_print_real);
+  scm_set_smob_equalp (tc, scm_real_equalp);
 
-  scm_make_smob_type_mfpe ("complex", 0,    /* freed in gc */
-                          NULL, NULL, scm_print_complex, scm_complex_equalp);
+  tc = scm_make_smob_type ("complex", 0);  /* freed in gc */
+  scm_set_smob_print (tc, scm_print_complex);
+  scm_set_smob_equalp (tc, scm_complex_equalp);
 }
 
 /*