smob.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* classes: h_files */
  2. #ifndef SCM_SMOB_H
  3. #define SCM_SMOB_H
  4. /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2004, 2006, 2009,
  5. * 2010, 2011 Free Software Foundation, Inc.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * as published by the Free Software Foundation; either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. */
  22. #include "libguile/__scm.h"
  23. #include "libguile/print.h"
  24. #include "libguile/bdw-gc.h"
  25. /* This is the internal representation of a smob type */
  26. typedef struct scm_smob_descriptor
  27. {
  28. char const *name;
  29. size_t size;
  30. SCM (*mark) (SCM);
  31. size_t (*free) (SCM);
  32. int (*print) (SCM exp, SCM port, scm_print_state *pstate);
  33. SCM (*equalp) (SCM, SCM);
  34. scm_t_subr apply;
  35. SCM apply_trampoline_objcode;
  36. } scm_smob_descriptor;
  37. SCM_API SCM scm_i_new_smob_with_mark_proc (scm_t_bits tc,
  38. scm_t_bits, scm_t_bits, scm_t_bits);
  39. #define SCM_NEWSMOB(z, tc, data) \
  40. do \
  41. { \
  42. register scm_t_bits _smobnum = SCM_TC2SMOBNUM (tc); \
  43. z = (scm_smobs[_smobnum].mark \
  44. ? scm_i_new_smob_with_mark_proc ((tc), (scm_t_bits)(data), \
  45. 0, 0) \
  46. : scm_cell (tc, (scm_t_bits)(data))); \
  47. if (scm_smobs[_smobnum].free) \
  48. { \
  49. GC_finalization_proc _prev_finalizer; \
  50. GC_PTR _prev_finalizer_data; \
  51. \
  52. GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (z), scm_i_finalize_smob, \
  53. NULL, \
  54. &_prev_finalizer, \
  55. &_prev_finalizer_data); \
  56. } \
  57. } \
  58. while (0)
  59. #define SCM_RETURN_NEWSMOB(tc, data) \
  60. do { SCM __SCM_smob_answer; \
  61. SCM_NEWSMOB (__SCM_smob_answer, (tc), (data)); \
  62. return __SCM_smob_answer; \
  63. } while (0)
  64. #define SCM_NEWSMOB2(z, tc, data1, data2) \
  65. SCM_NEWSMOB3 (z, tc, data1, data2, 0)
  66. #define SCM_RETURN_NEWSMOB2(tc, data1, data2) \
  67. do { SCM __SCM_smob_answer; \
  68. SCM_NEWSMOB2 (__SCM_smob_answer, (tc), (data1), (data2)); \
  69. return __SCM_smob_answer; \
  70. } while (0)
  71. #define SCM_NEWSMOB3(z, tc, data1, data2, data3) \
  72. do \
  73. { \
  74. register scm_t_bits _smobnum = SCM_TC2SMOBNUM (tc); \
  75. z = (scm_smobs[_smobnum].mark \
  76. ? scm_i_new_smob_with_mark_proc (tc, (scm_t_bits)(data1), \
  77. (scm_t_bits)(data2), \
  78. (scm_t_bits)(data3)) \
  79. : scm_double_cell ((tc), (scm_t_bits)(data1), \
  80. (scm_t_bits)(data2), \
  81. (scm_t_bits)(data3))); \
  82. if (scm_smobs[_smobnum].free) \
  83. { \
  84. GC_finalization_proc _prev_finalizer; \
  85. GC_PTR _prev_finalizer_data; \
  86. \
  87. GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (z), scm_i_finalize_smob, \
  88. NULL, \
  89. &_prev_finalizer, \
  90. &_prev_finalizer_data); \
  91. } \
  92. } \
  93. while (0)
  94. #define SCM_RETURN_NEWSMOB3(tc, data1, data2, data3) \
  95. do { SCM __SCM_smob_answer; \
  96. SCM_NEWSMOB3 (__SCM_smob_answer, (tc), (data1), (data2), (data3)); \
  97. return __SCM_smob_answer; \
  98. } while (0)
  99. #define SCM_SMOB_DATA_N(x, n) (SCM_CELL_WORD ((x), (n)))
  100. #define SCM_SET_SMOB_DATA_N(x, n, data) (SCM_SET_CELL_WORD ((x), (n), (data)))
  101. #define SCM_SMOB_DATA_0(x) (SCM_SMOB_DATA_N ((x), 0))
  102. #define SCM_SMOB_DATA_1(x) (SCM_SMOB_DATA_N ((x), 1))
  103. #define SCM_SMOB_DATA_2(x) (SCM_SMOB_DATA_N ((x), 2))
  104. #define SCM_SMOB_DATA_3(x) (SCM_SMOB_DATA_N ((x), 3))
  105. #define SCM_SET_SMOB_DATA_0(x, data) (SCM_SET_SMOB_DATA_N ((x), 0, (data)))
  106. #define SCM_SET_SMOB_DATA_1(x, data) (SCM_SET_SMOB_DATA_N ((x), 1, (data)))
  107. #define SCM_SET_SMOB_DATA_2(x, data) (SCM_SET_SMOB_DATA_N ((x), 2, (data)))
  108. #define SCM_SET_SMOB_DATA_3(x, data) (SCM_SET_SMOB_DATA_N ((x), 3, (data)))
  109. #define SCM_SMOB_FLAGS(x) (SCM_SMOB_DATA_0 (x) >> 16)
  110. #define SCM_SMOB_DATA(x) (SCM_SMOB_DATA_1 (x))
  111. #define SCM_SET_SMOB_FLAGS(x, data) (SCM_SET_SMOB_DATA_0 ((x), (SCM_CELL_TYPE (x)&0xffff)|((data)<<16)))
  112. #define SCM_SET_SMOB_DATA(x, data) (SCM_SET_SMOB_DATA_1 ((x), (data)))
  113. #define SCM_SMOB_OBJECT_N(x,n) (SCM_CELL_OBJECT ((x), (n)))
  114. #define SCM_SET_SMOB_OBJECT_N(x,n,obj) (SCM_SET_CELL_OBJECT ((x), (n), (obj)))
  115. #define SCM_SMOB_OBJECT_N_LOC(x,n) (SCM_CELL_OBJECT_LOC ((x), (n)))
  116. /*#define SCM_SMOB_OBJECT_0(x) (SCM_SMOB_OBJECT_N ((x), 0))*/
  117. #define SCM_SMOB_OBJECT_1(x) (SCM_SMOB_OBJECT_N ((x), 1))
  118. #define SCM_SMOB_OBJECT_2(x) (SCM_SMOB_OBJECT_N ((x), 2))
  119. #define SCM_SMOB_OBJECT_3(x) (SCM_SMOB_OBJECT_N ((x), 3))
  120. /*#define SCM_SET_SMOB_OBJECT_0(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 0, (obj)))*/
  121. #define SCM_SET_SMOB_OBJECT_1(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 1, (obj)))
  122. #define SCM_SET_SMOB_OBJECT_2(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 2, (obj)))
  123. #define SCM_SET_SMOB_OBJECT_3(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 3, (obj)))
  124. #define SCM_SMOB_OBJECT_0_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 0)))
  125. #define SCM_SMOB_OBJECT_1_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 1)))
  126. #define SCM_SMOB_OBJECT_2_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 2)))
  127. #define SCM_SMOB_OBJECT_3_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 3)))
  128. #define SCM_SMOB_OBJECT(x) (SCM_SMOB_OBJECT_1 (x))
  129. #define SCM_SET_SMOB_OBJECT(x,obj) (SCM_SET_SMOB_OBJECT_1 ((x), (obj)))
  130. #define SCM_SMOB_OBJECT_LOC(x) (SCM_SMOB_OBJECT_1_LOC (x)))
  131. #define SCM_SMOB_TYPE_MASK 0xffff
  132. #define SCM_SMOB_TYPE_BITS(tc) (tc)
  133. #define SCM_TC2SMOBNUM(x) (0x0ff & ((x) >> 8))
  134. #define SCM_SMOBNUM(x) (SCM_TC2SMOBNUM (SCM_CELL_TYPE (x)))
  135. /* SCM_SMOBNAME can be 0 if name is missing */
  136. #define SCM_SMOBNAME(smobnum) (scm_smobs[smobnum].name)
  137. #define SCM_SMOB_PREDICATE(tag, obj) SCM_TYP16_PREDICATE (tag, obj)
  138. #define SCM_SMOB_DESCRIPTOR(x) (scm_smobs[SCM_SMOBNUM (x)])
  139. #define SCM_SMOB_APPLICABLE_P(x) (SCM_SMOB_DESCRIPTOR (x).apply)
  140. #define SCM_SMOB_APPLY_0(x) (scm_call_0 (x))
  141. #define SCM_SMOB_APPLY_1(x, a1) (scm_call_1 (x, a1))
  142. #define SCM_SMOB_APPLY_2(x, a1, a2) (scm_call_2 (x, a1, a2))
  143. #define SCM_SMOB_APPLY_3(x, a1, a2, rst) (scm_call_3 (x, a1, a2, a3))
  144. /* Maximum number of SMOB types. */
  145. #define SCM_I_MAX_SMOB_TYPE_COUNT 256
  146. SCM_API long scm_numsmob;
  147. SCM_API scm_smob_descriptor scm_smobs[];
  148. SCM_API void scm_i_finalize_smob (GC_PTR obj, GC_PTR data);
  149. SCM_API SCM scm_mark0 (SCM ptr);
  150. SCM_API SCM scm_markcdr (SCM ptr);
  151. SCM_API size_t scm_free0 (SCM ptr);
  152. SCM_API int scm_smob_print (SCM exp, SCM port, scm_print_state *pstate);
  153. /* The following set of functions is the standard way to create new
  154. * SMOB types.
  155. *
  156. * Create a type tag using `scm_make_smob_type', accept default values
  157. * for mark, free, print and/or equalp functions, or set your own
  158. * values using `scm_set_smob_xxx'.
  159. */
  160. SCM_API scm_t_bits scm_make_smob_type (char const *name, size_t size);
  161. SCM_API void scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM));
  162. SCM_API void scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM));
  163. SCM_API void scm_set_smob_print (scm_t_bits tc,
  164. int (*print) (SCM, SCM, scm_print_state*));
  165. SCM_API void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
  166. SCM_API void scm_set_smob_apply (scm_t_bits tc,
  167. scm_t_subr apply,
  168. unsigned int req,
  169. unsigned int opt,
  170. unsigned int rst);
  171. SCM_API void scm_assert_smob_type (scm_t_bits tag, SCM val);
  172. /* Function for creating smobs */
  173. SCM_API SCM scm_make_smob (scm_t_bits tc);
  174. SCM_INTERNAL SCM scm_i_smob_apply_trampoline (SCM smob);
  175. SCM_API void scm_smob_prehistory (void);
  176. #endif /* SCM_SMOB_H */
  177. /*
  178. Local Variables:
  179. c-file-style: "gnu"
  180. End:
  181. */