smob.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #ifndef SCM_SMOB_H
  2. #define SCM_SMOB_H
  3. /* Copyright 1995-1996,1998-2001,2004,2006,2009-2012,2015,2018
  4. Free Software Foundation, Inc.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. #include <libguile/error.h>
  18. #include <libguile/gc.h>
  19. #include "libguile/inline.h"
  20. #include "libguile/print.h"
  21. #include <libguile/snarf.h>
  22. /* This is the internal representation of a smob type */
  23. typedef struct scm_smob_descriptor
  24. {
  25. char const *name;
  26. size_t size;
  27. SCM (*mark) (SCM);
  28. size_t (*free) (SCM);
  29. int (*print) (SCM exp, SCM port, scm_print_state *pstate);
  30. SCM (*equalp) (SCM, SCM);
  31. scm_t_subr apply;
  32. SCM apply_trampoline;
  33. } scm_smob_descriptor;
  34. #define SCM_SMOB_TYPE_MASK 0xffff
  35. #define SCM_SMOB_TYPE_BITS(tc) (tc)
  36. #define SCM_TC2SMOBNUM(x) (0x0ff & ((x) >> 8))
  37. #define SCM_SMOBNUM(x) (SCM_TC2SMOBNUM (SCM_CELL_TYPE (x)))
  38. /* SCM_SMOBNAME can be 0 if name is missing */
  39. #define SCM_SMOBNAME(smobnum) (scm_smobs[smobnum].name)
  40. #define SCM_SMOB_PREDICATE(tag, obj) SCM_HAS_TYP16 (obj, tag)
  41. #define SCM_SMOB_DESCRIPTOR(x) (scm_smobs[SCM_SMOBNUM (x)])
  42. #define SCM_SMOB_APPLICABLE_P(x) (SCM_SMOB_DESCRIPTOR (x).apply)
  43. #define SCM_VALIDATE_SMOB(pos, obj, type) \
  44. do { \
  45. SCM_ASSERT (SCM_SMOB_PREDICATE (scm_tc16_ ## type, obj), \
  46. obj, pos, FUNC_NAME); \
  47. } while (0)
  48. /* Maximum number of SMOB types. */
  49. #define SCM_I_MAX_SMOB_TYPE_COUNT 256
  50. SCM_API long scm_numsmob;
  51. SCM_API scm_smob_descriptor scm_smobs[];
  52. #define SCM_SMOB(tag, scheme_name, size) \
  53. SCM_SNARF_HERE(static scm_t_bits tag) \
  54. SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
  55. #define SCM_GLOBAL_SMOB(tag, scheme_name, size) \
  56. SCM_SNARF_HERE(scm_t_bits tag) \
  57. SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
  58. #define SCM_SMOB_MARK(tag, c_name, arg) \
  59. SCM_SNARF_HERE(static SCM c_name(SCM arg)) \
  60. SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
  61. #define SCM_GLOBAL_SMOB_MARK(tag, c_name, arg) \
  62. SCM_SNARF_HERE(SCM c_name(SCM arg)) \
  63. SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
  64. #define SCM_SMOB_FREE(tag, c_name, arg) \
  65. SCM_SNARF_HERE(static size_t c_name(SCM arg)) \
  66. SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
  67. #define SCM_GLOBAL_SMOB_FREE(tag, c_name, arg) \
  68. SCM_SNARF_HERE(size_t c_name(SCM arg)) \
  69. SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
  70. #define SCM_SMOB_PRINT(tag, c_name, obj, port, pstate) \
  71. SCM_SNARF_HERE(static int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
  72. SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
  73. #define SCM_GLOBAL_SMOB_PRINT(tag, c_name, obj, port, pstate) \
  74. SCM_SNARF_HERE(int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
  75. SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
  76. #define SCM_SMOB_EQUALP(tag, c_name, obj1, obj2) \
  77. SCM_SNARF_HERE(static SCM c_name(SCM obj1, SCM obj2)) \
  78. SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
  79. #define SCM_GLOBAL_SMOB_EQUALP(tag, c_name, obj1, obj2) \
  80. SCM_SNARF_HERE(SCM c_name(SCM obj1, SCM obj2)) \
  81. SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
  82. #define SCM_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
  83. SCM_SNARF_HERE(static SCM c_name arglist) \
  84. SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
  85. #define SCM_GLOBAL_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
  86. SCM_SNARF_HERE(SCM c_name arglist) \
  87. SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
  88. SCM_API SCM scm_i_new_smob (scm_t_bits tc, scm_t_bits);
  89. SCM_API SCM scm_i_new_double_smob (scm_t_bits tc, scm_t_bits,
  90. scm_t_bits, scm_t_bits);
  91. SCM_INLINE SCM scm_new_smob (scm_t_bits tc, scm_t_bits);
  92. SCM_INLINE SCM scm_new_double_smob (scm_t_bits tc, scm_t_bits,
  93. scm_t_bits, scm_t_bits);
  94. #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
  95. SCM_INLINE_IMPLEMENTATION SCM
  96. scm_new_smob (scm_t_bits tc, scm_t_bits data)
  97. {
  98. scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
  99. if (SCM_UNLIKELY (scm_smobs[smobnum].mark || scm_smobs[smobnum].free))
  100. return scm_i_new_smob (tc, data);
  101. else
  102. return scm_cell (tc, data);
  103. }
  104. SCM_INLINE_IMPLEMENTATION SCM
  105. scm_new_double_smob (scm_t_bits tc, scm_t_bits data1,
  106. scm_t_bits data2, scm_t_bits data3)
  107. {
  108. scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
  109. if (SCM_UNLIKELY (scm_smobs[smobnum].mark || scm_smobs[smobnum].free))
  110. return scm_i_new_double_smob (tc, data1, data2, data3);
  111. else
  112. return scm_double_cell (tc, data1, data2, data3);
  113. }
  114. #endif
  115. #define SCM_NEWSMOB(z, tc, data) \
  116. z = scm_new_smob ((tc), (scm_t_bits)(data))
  117. #define SCM_RETURN_NEWSMOB(tc, data) \
  118. return scm_new_smob ((tc), (scm_t_bits)(data))
  119. #define SCM_NEWSMOB2(z, tc, data1, data2) \
  120. z = scm_new_double_smob ((tc), (scm_t_bits)(data1), \
  121. (scm_t_bits)(data2), 0)
  122. #define SCM_RETURN_NEWSMOB2(tc, data1, data2) \
  123. return scm_new_double_smob ((tc), (scm_t_bits)(data1), \
  124. (scm_t_bits)(data2), 0)
  125. #define SCM_NEWSMOB3(z, tc, data1, data2, data3) \
  126. z = scm_new_double_smob ((tc), (scm_t_bits)(data1), \
  127. (scm_t_bits)(data2), (scm_t_bits)(data3))
  128. #define SCM_RETURN_NEWSMOB3(tc, data1, data2, data3) \
  129. return scm_new_double_smob ((tc), (scm_t_bits)(data1), \
  130. (scm_t_bits)(data2), (scm_t_bits)(data3))
  131. #define SCM_SMOB_DATA_N(x, n) (SCM_CELL_WORD ((x), (n)))
  132. #define SCM_SET_SMOB_DATA_N(x, n, data) (SCM_SET_CELL_WORD ((x), (n), (data)))
  133. #define SCM_SMOB_DATA_0(x) (SCM_SMOB_DATA_N ((x), 0))
  134. #define SCM_SMOB_DATA_1(x) (SCM_SMOB_DATA_N ((x), 1))
  135. #define SCM_SMOB_DATA_2(x) (SCM_SMOB_DATA_N ((x), 2))
  136. #define SCM_SMOB_DATA_3(x) (SCM_SMOB_DATA_N ((x), 3))
  137. #define SCM_SET_SMOB_DATA_0(x, data) (SCM_SET_SMOB_DATA_N ((x), 0, (data)))
  138. #define SCM_SET_SMOB_DATA_1(x, data) (SCM_SET_SMOB_DATA_N ((x), 1, (data)))
  139. #define SCM_SET_SMOB_DATA_2(x, data) (SCM_SET_SMOB_DATA_N ((x), 2, (data)))
  140. #define SCM_SET_SMOB_DATA_3(x, data) (SCM_SET_SMOB_DATA_N ((x), 3, (data)))
  141. #define SCM_SMOB_FLAGS(x) (SCM_SMOB_DATA_0 (x) >> 16)
  142. #define SCM_SMOB_DATA(x) (SCM_SMOB_DATA_1 (x))
  143. #define SCM_SET_SMOB_FLAGS(x, data) (SCM_SET_SMOB_DATA_0 ((x), (SCM_CELL_TYPE (x)&0xffff)|((data)<<16)))
  144. #define SCM_SET_SMOB_DATA(x, data) (SCM_SET_SMOB_DATA_1 ((x), (data)))
  145. #define SCM_SMOB_OBJECT_N(x,n) (SCM_CELL_OBJECT ((x), (n)))
  146. #define SCM_SET_SMOB_OBJECT_N(x,n,obj) (SCM_SET_CELL_OBJECT ((x), (n), (obj)))
  147. #define SCM_SMOB_OBJECT_N_LOC(x,n) (SCM_CELL_OBJECT_LOC ((x), (n)))
  148. /*#define SCM_SMOB_OBJECT_0(x) (SCM_SMOB_OBJECT_N ((x), 0))*/
  149. #define SCM_SMOB_OBJECT_1(x) (SCM_SMOB_OBJECT_N ((x), 1))
  150. #define SCM_SMOB_OBJECT_2(x) (SCM_SMOB_OBJECT_N ((x), 2))
  151. #define SCM_SMOB_OBJECT_3(x) (SCM_SMOB_OBJECT_N ((x), 3))
  152. /*#define SCM_SET_SMOB_OBJECT_0(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 0, (obj)))*/
  153. #define SCM_SET_SMOB_OBJECT_1(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 1, (obj)))
  154. #define SCM_SET_SMOB_OBJECT_2(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 2, (obj)))
  155. #define SCM_SET_SMOB_OBJECT_3(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 3, (obj)))
  156. #define SCM_SMOB_OBJECT_0_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 0))
  157. #define SCM_SMOB_OBJECT_1_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 1))
  158. #define SCM_SMOB_OBJECT_2_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 2))
  159. #define SCM_SMOB_OBJECT_3_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 3))
  160. #define SCM_SMOB_OBJECT(x) (SCM_SMOB_OBJECT_1 (x))
  161. #define SCM_SET_SMOB_OBJECT(x,obj) (SCM_SET_SMOB_OBJECT_1 ((x), (obj)))
  162. #define SCM_SMOB_OBJECT_LOC(x) (SCM_SMOB_OBJECT_1_LOC (x))
  163. #define SCM_SMOB_APPLY_0(x) (scm_call_0 (x))
  164. #define SCM_SMOB_APPLY_1(x, a1) (scm_call_1 (x, a1))
  165. #define SCM_SMOB_APPLY_2(x, a1, a2) (scm_call_2 (x, a1, a2))
  166. #define SCM_SMOB_APPLY_3(x, a1, a2, rst) (scm_call_3 (x, a1, a2, a3))
  167. SCM_API SCM scm_mark0 (SCM ptr);
  168. SCM_API SCM scm_markcdr (SCM ptr);
  169. SCM_API size_t scm_free0 (SCM ptr);
  170. SCM_API int scm_smob_print (SCM exp, SCM port, scm_print_state *pstate);
  171. /* The following set of functions is the standard way to create new
  172. * SMOB types.
  173. *
  174. * Create a type tag using `scm_make_smob_type', accept default values
  175. * for mark, free, print and/or equalp functions, or set your own
  176. * values using `scm_set_smob_xxx'.
  177. */
  178. SCM_API scm_t_bits scm_make_smob_type (char const *name, size_t size);
  179. SCM_API void scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM));
  180. SCM_API void scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM));
  181. SCM_API void scm_set_smob_print (scm_t_bits tc,
  182. int (*print) (SCM, SCM, scm_print_state*));
  183. SCM_API void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
  184. SCM_API void scm_set_smob_apply (scm_t_bits tc,
  185. scm_t_subr apply,
  186. unsigned int req,
  187. unsigned int opt,
  188. unsigned int rst);
  189. SCM_API SCM scm_smob_type_class (scm_t_bits tc);
  190. SCM_API void scm_assert_smob_type (scm_t_bits tag, SCM val);
  191. /* Function for creating smobs */
  192. SCM_API SCM scm_make_smob (scm_t_bits tc);
  193. SCM_API void scm_smob_prehistory (void);
  194. #endif /* SCM_SMOB_H */