pointerless backing buffers for string ports
[bpt/guile.git] / libguile / objcodes.c
index cd5506f..f4e20f8 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc.
+/* Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
 #include "programs.h"
 #include "objcodes.h"
 
-/* SCM_OBJCODE_COOKIE is defined in _scm.h */
-/* The length of the header must be a multiple of 8 bytes.  */
+/* SCM_OBJCODE_COOKIE, defined in _scm.h, is a magic value prepended
+   to objcode on disk but not in memory.
+
+   The length of the header must be a multiple of 8 bytes.  */
 verify (((sizeof (SCM_OBJCODE_COOKIE) - 1) & 7) == 0);
 
 \f
@@ -42,7 +44,13 @@ verify (((sizeof (SCM_OBJCODE_COOKIE) - 1) & 7) == 0);
  * Objcode type
  */
 
-scm_t_bits scm_tc16_objcode;
+/* The words in an objcode SCM object are as follows:
+     - scm_tc7_objcode | type | flags
+     - the struct scm_objcode C object
+     - the parent of this objcode: either another objcode, a bytevector,
+       or, in the case of mmap types, file descriptors (as an inum)
+     - "native code" -- not currently used.
+ */
 
 static SCM
 make_objcode_by_mmap (int fd)
@@ -69,15 +77,31 @@ make_objcode_by_mmap (int fd)
       SCM_SYSERROR;
     }
 
-  if (memcmp (addr, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE)))
+  /* The cookie ends with a version of the form M.N, where M is the
+     major version and N is the minor version.  For this Guile to be
+     able to load an objcode, M must be SCM_OBJCODE_MAJOR_VERSION, and N
+     must be less than or equal to SCM_OBJCODE_MINOR_VERSION.  Since N
+     is the last character, we do a strict comparison on all but the
+     last, then a <= on the last one.  */
+  if (memcmp (addr, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE) - 1))
     {
-      SCM args = scm_list_1 (scm_from_locale_stringn
+      SCM args = scm_list_1 (scm_from_latin1_stringn
                              (addr, strlen (SCM_OBJCODE_COOKIE)));
       (void) close (fd);
       (void) munmap (addr, st.st_size);
       scm_misc_error (FUNC_NAME, "bad header on object file: ~s", args);
     }
 
+  {
+    char minor_version = addr[strlen (SCM_OBJCODE_COOKIE) - 1];
+
+    if (minor_version > SCM_OBJCODE_MINOR_VERSION_STRING[0])
+      scm_misc_error (FUNC_NAME, "objcode minor version too new (~a > ~a)",
+                      scm_list_2 (scm_from_latin1_stringn (&minor_version, 1),
+                                  scm_from_latin1_string
+                                  (SCM_OBJCODE_MINOR_VERSION_STRING)));
+  }
+
   data = (struct scm_objcode*)(addr + strlen (SCM_OBJCODE_COOKIE));
 
   if (data->len + data->metalen != (st.st_size - sizeof (*data) - strlen (SCM_OBJCODE_COOKIE)))
@@ -90,9 +114,9 @@ make_objcode_by_mmap (int fd)
                                                   + data->metalen)));
     }
 
-  SCM_NEWSMOB3 (sret, scm_tc16_objcode, addr + strlen (SCM_OBJCODE_COOKIE),
-                SCM_PACK (SCM_BOOL_F), fd);
-  SCM_SET_SMOB_FLAGS (sret, SCM_F_OBJCODE_IS_MMAP);
+  sret = scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_MMAP, 0),
+                          (scm_t_bits)(addr + strlen (SCM_OBJCODE_COOKIE)),
+                          SCM_UNPACK (scm_from_int (fd)), 0);
 
   /* FIXME: we leak ourselves and the file descriptor. but then again so does
      dlopen(). */
@@ -106,7 +130,6 @@ scm_c_make_objcode_slice (SCM parent, const scm_t_uint8 *ptr)
 {
   const struct scm_objcode *data, *parent_data;
   const scm_t_uint8 *parent_base;
-  SCM ret;
 
   SCM_VALIDATE_OBJCODE (1, parent);
   parent_data = SCM_OBJCODE_DATA (parent);
@@ -115,11 +138,12 @@ scm_c_make_objcode_slice (SCM parent, const scm_t_uint8 *ptr)
   if (ptr < parent_base
       || ptr >= (parent_base + parent_data->len + parent_data->metalen
                  - sizeof (struct scm_objcode)))
-    scm_misc_error (FUNC_NAME, "offset out of bounds (~a vs ~a + ~a + ~a)",
-                   scm_list_4 (scm_from_ulong ((unsigned long) ptr),
-                               scm_from_ulong ((unsigned long) parent_base),
-                               scm_from_uint32 (parent_data->len),
-                               scm_from_uint32 (parent_data->metalen)));
+    scm_misc_error
+      (FUNC_NAME, "offset out of bounds (~a vs ~a + ~a + ~a)",
+       scm_list_4 (scm_from_unsigned_integer ((scm_t_bits) ptr),
+                   scm_from_unsigned_integer ((scm_t_bits) parent_base),
+                   scm_from_uint32 (parent_data->len),
+                   scm_from_uint32 (parent_data->metalen)));
 
   /* Make sure bytecode for the objcode-meta is suitable aligned.  Failing to
      do so leads to SIGBUS/SIGSEGV on some arches (e.g., SPARC).  */
@@ -130,9 +154,8 @@ scm_c_make_objcode_slice (SCM parent, const scm_t_uint8 *ptr)
   assert (SCM_C_OBJCODE_BASE (data) + data->len + data->metalen
          <= parent_base + parent_data->len + parent_data->metalen);
 
-  SCM_NEWSMOB2 (ret, scm_tc16_objcode, data, parent);
-  SCM_SET_SMOB_FLAGS (ret, SCM_F_OBJCODE_IS_SLICE);
-  return ret;
+  return scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_SLICE, 0),
+                          (scm_t_bits)data, SCM_UNPACK (parent), 0);
 }
 #undef FUNC_NAME
 
@@ -173,7 +196,6 @@ SCM_DEFINE (scm_bytecode_to_objcode, "bytecode->objcode", 1, 0, 0,
   size_t size;
   const scm_t_uint8 *c_bytecode;
   struct scm_objcode *data;
-  SCM objcode;
 
   if (!scm_is_bytevector (bytecode))
     scm_wrong_type_arg (FUNC_NAME, 1, bytecode);
@@ -189,13 +211,10 @@ SCM_DEFINE (scm_bytecode_to_objcode, "bytecode->objcode", 1, 0, 0,
                    scm_list_2 (scm_from_size_t (size),
                                scm_from_uint32 (sizeof (*data) + data->len + data->metalen)));
 
-  SCM_NEWSMOB2 (objcode, scm_tc16_objcode, data, bytecode);
-  SCM_SET_SMOB_FLAGS (objcode, SCM_F_OBJCODE_IS_BYTEVECTOR);
-  
   /* foolishly, we assume that as long as bytecode is around, that c_bytecode
      will be of the same length; perhaps a bad assumption? */
-
-  return objcode;
+  return scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_BYTEVECTOR, 0),
+                          (scm_t_bits)data, SCM_UNPACK (bytecode), 0);
 }
 #undef FUNC_NAME
 
@@ -228,7 +247,7 @@ SCM_DEFINE (scm_objcode_to_bytecode, "objcode->bytecode", 1, 0, 0,
 
   SCM_VALIDATE_OBJCODE (1, objcode);
 
-  len = sizeof(struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode);
+  len = sizeof (struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode);
 
   s8vector = scm_malloc (len);
   memcpy (s8vector, SCM_OBJCODE_DATA (objcode), len);
@@ -253,12 +272,20 @@ SCM_DEFINE (scm_write_objcode, "write-objcode", 2, 0, 0,
 }
 #undef FUNC_NAME
 
+void
+scm_i_objcode_print (SCM objcode, SCM port, scm_print_state *pstate)
+{
+  scm_puts ("#<objcode ", port);
+  scm_uintprint ((scm_t_bits)SCM_OBJCODE_BASE (objcode), 16, port);
+  scm_puts (">", port);
+}
+
 \f
 void
 scm_bootstrap_objcodes (void)
 {
-  scm_tc16_objcode = scm_make_smob_type ("objcode", 0);
-  scm_c_register_extension ("libguile", "scm_init_objcodes",
+  scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
+                            "scm_init_objcodes",
                             (scm_t_extension_init_func)scm_init_objcodes, NULL);
 }