smob.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/objects.h"
  44. #include "libguile/ports.h"
  45. #ifdef HAVE_MALLOC_H
  46. #include <malloc.h>
  47. #endif
  48. #include "libguile/smob.h"
  49. /* scm_smobs scm_numsmob
  50. * implement a dynamicly resized array of smob records.
  51. * Indexes into this table are used when generating type
  52. * tags for smobjects (if you know a tag you can get an index and conversely).
  53. */
  54. int scm_numsmob;
  55. scm_smob_descriptor *scm_smobs;
  56. /* {Mark}
  57. */
  58. /* This function is vestigial. It used to be the mark function's
  59. responsibility to set the mark bit on the smob or port, but now the
  60. generic marking routine in gc.c takes care of that, and a zero
  61. pointer for a mark function means "don't bother". So you never
  62. need scm_mark0.
  63. However, we leave it here because it's harmless to call it, and
  64. people out there have smob code that uses it, and there's no reason
  65. to make their links fail. */
  66. SCM
  67. scm_mark0 (SCM ptr)
  68. {
  69. return SCM_BOOL_F;
  70. }
  71. SCM
  72. scm_markcdr (SCM ptr)
  73. {
  74. return SCM_CDR (ptr);
  75. }
  76. /* {Free}
  77. */
  78. scm_sizet
  79. scm_free0 (SCM ptr)
  80. {
  81. return 0;
  82. }
  83. scm_sizet
  84. scm_smob_free (SCM obj)
  85. {
  86. scm_must_free ((char *) SCM_CELL_WORD_1 (obj));
  87. return scm_smobs[SCM_SMOBNUM (obj)].size;
  88. }
  89. /* {Print}
  90. */
  91. int
  92. scm_smob_print (SCM exp, SCM port, scm_print_state *pstate)
  93. {
  94. int n = SCM_SMOBNUM (exp);
  95. scm_puts ("#<", port);
  96. scm_puts (SCM_SMOBNAME (n) ? SCM_SMOBNAME (n) : "smob", port);
  97. scm_putc (' ', port);
  98. scm_intprint (SCM_UNPACK (scm_smobs[n].size ? SCM_CDR (exp) : exp), 16, port);
  99. scm_putc ('>', port);
  100. return 1;
  101. }
  102. long
  103. scm_make_smob_type (char *name, scm_sizet size)
  104. {
  105. char *tmp;
  106. if (255 <= scm_numsmob)
  107. goto smoberr;
  108. SCM_DEFER_INTS;
  109. SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_smobs,
  110. (1 + scm_numsmob)
  111. * sizeof (scm_smob_descriptor)));
  112. if (tmp)
  113. {
  114. scm_smobs = (scm_smob_descriptor *) tmp;
  115. scm_smobs[scm_numsmob].name = name;
  116. scm_smobs[scm_numsmob].size = size;
  117. scm_smobs[scm_numsmob].mark = 0;
  118. scm_smobs[scm_numsmob].free = (size == 0 ? scm_free0 : scm_smob_free);
  119. scm_smobs[scm_numsmob].print = scm_smob_print;
  120. scm_smobs[scm_numsmob].equalp = 0;
  121. scm_numsmob++;
  122. }
  123. SCM_ALLOW_INTS;
  124. if (!tmp)
  125. smoberr:scm_wta (SCM_MAKINUM ((long) scm_numsmob),
  126. (char *) SCM_NALLOC, "scm_make_smob_type");
  127. /* Make a class object if Goops is present. */
  128. if (scm_smob_class)
  129. scm_smob_class[scm_numsmob - 1]
  130. = scm_make_extended_class (SCM_SMOBNAME (scm_numsmob - 1));
  131. return scm_tc7_smob + (scm_numsmob - 1) * 256;
  132. }
  133. long
  134. scm_make_smob_type_mfpe (char *name, scm_sizet size,
  135. SCM (*mark) (SCM),
  136. scm_sizet (*free) (SCM),
  137. int (*print) (SCM, SCM, scm_print_state *),
  138. SCM (*equalp) (SCM, SCM))
  139. {
  140. long answer = scm_make_smob_type (name, size);
  141. scm_set_smob_mfpe (answer, mark, free, print, equalp);
  142. return answer;
  143. }
  144. void
  145. scm_set_smob_mark (long tc, SCM (*mark) (SCM))
  146. {
  147. scm_smobs[SCM_TC2SMOBNUM (tc)].mark = mark;
  148. }
  149. void
  150. scm_set_smob_free (long tc, scm_sizet (*free) (SCM))
  151. {
  152. scm_smobs[SCM_TC2SMOBNUM (tc)].free = free;
  153. }
  154. void
  155. scm_set_smob_print (long tc, int (*print) (SCM, SCM, scm_print_state*))
  156. {
  157. scm_smobs[SCM_TC2SMOBNUM (tc)].print = print;
  158. }
  159. void
  160. scm_set_smob_equalp (long tc, SCM (*equalp) (SCM, SCM))
  161. {
  162. scm_smobs[SCM_TC2SMOBNUM (tc)].equalp = equalp;
  163. }
  164. void
  165. scm_set_smob_mfpe (long tc,
  166. SCM (*mark) (SCM),
  167. scm_sizet (*free) (SCM),
  168. int (*print) (SCM, SCM, scm_print_state *),
  169. SCM (*equalp) (SCM, SCM))
  170. {
  171. if (mark) scm_set_smob_mark (tc, mark);
  172. if (free) scm_set_smob_free (tc, free);
  173. if (print) scm_set_smob_print (tc, print);
  174. if (equalp) scm_set_smob_equalp (tc, equalp);
  175. }
  176. SCM
  177. scm_make_smob (long tc)
  178. {
  179. int n = SCM_TC2SMOBNUM (tc);
  180. scm_sizet size = scm_smobs[n].size;
  181. SCM z;
  182. SCM_NEWCELL (z);
  183. if (size != 0)
  184. {
  185. #if 0
  186. SCM_ASSERT (scm_smobs[n].mark == 0,
  187. 0,
  188. "forbidden operation for smobs with GC data, use SCM_NEWSMOB",
  189. SCM_SMOBNAME (n));
  190. #endif
  191. SCM_SET_SMOB_DATA (z, scm_must_malloc (size, SCM_SMOBNAME (n)));
  192. }
  193. SCM_SET_CELL_TYPE (z, tc);
  194. return z;
  195. }
  196. /* {Initialization for i/o types, float, bignum, the type of free cells}
  197. */
  198. static int
  199. freeprint (SCM exp,
  200. SCM port,
  201. scm_print_state *pstate)
  202. {
  203. char buf[100];
  204. sprintf (buf, "#<freed cell %p; GC missed a reference>", (void *) SCM_UNPACK (exp));
  205. scm_puts (buf, port);
  206. return 1;
  207. }
  208. void
  209. scm_smob_prehistory ()
  210. {
  211. scm_numsmob = 0;
  212. scm_smobs = ((scm_smob_descriptor *)
  213. malloc (7 * sizeof (scm_smob_descriptor)));
  214. /* WARNING: These scm_make_smob_type calls must be done in this order */
  215. scm_make_smob_type_mfpe ("free", 0,
  216. NULL, NULL, freeprint, NULL);
  217. scm_make_smob_type_mfpe ("big", 0, /* freed in gc */
  218. NULL, NULL, scm_bigprint, scm_bigequal);
  219. scm_make_smob_type_mfpe ("real", 0, /* freed in gc */
  220. NULL, NULL, scm_print_real, scm_real_equalp);
  221. scm_make_smob_type_mfpe ("complex", 0, /* freed in gc */
  222. NULL, NULL, scm_print_complex, scm_complex_equalp);
  223. scm_make_smob_type ("allocated", 0);
  224. }
  225. /*
  226. Local Variables:
  227. c-file-style: "gnu"
  228. End:
  229. */