smob.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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, 2012, 2015 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. /* This is the internal representation of a smob type */
  25. typedef struct scm_smob_descriptor
  26. {
  27. char const *name;
  28. size_t size;
  29. SCM (*mark) (SCM);
  30. size_t (*free) (SCM);
  31. int (*print) (SCM exp, SCM port, scm_print_state *pstate);
  32. SCM (*equalp) (SCM, SCM);
  33. scm_t_subr apply;
  34. SCM apply_trampoline;
  35. } scm_smob_descriptor;
  36. #define SCM_SMOB_TYPE_MASK 0xffff
  37. #define SCM_SMOB_TYPE_BITS(tc) (tc)
  38. #define SCM_TC2SMOBNUM(x) (0x0ff & ((x) >> 8))
  39. #define SCM_SMOBNUM(x) (SCM_TC2SMOBNUM (SCM_CELL_TYPE (x)))
  40. /* SCM_SMOBNAME can be 0 if name is missing */
  41. #define SCM_SMOBNAME(smobnum) (scm_smobs[smobnum].name)
  42. #define SCM_SMOB_PREDICATE(tag, obj) SCM_HAS_TYP16 (obj, tag)
  43. #define SCM_SMOB_DESCRIPTOR(x) (scm_smobs[SCM_SMOBNUM (x)])
  44. #define SCM_SMOB_APPLICABLE_P(x) (SCM_SMOB_DESCRIPTOR (x).apply)
  45. /* Maximum number of SMOB types. */
  46. #define SCM_I_MAX_SMOB_TYPE_COUNT 256
  47. SCM_API long scm_numsmob;
  48. SCM_API scm_smob_descriptor scm_smobs[];
  49. SCM_API SCM scm_i_new_smob (scm_t_bits tc, scm_t_bits);
  50. SCM_API SCM scm_i_new_double_smob (scm_t_bits tc, scm_t_bits,
  51. scm_t_bits, scm_t_bits);
  52. SCM_INLINE SCM scm_new_smob (scm_t_bits tc, scm_t_bits);
  53. SCM_INLINE SCM scm_new_double_smob (scm_t_bits tc, scm_t_bits,
  54. scm_t_bits, scm_t_bits);
  55. #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
  56. SCM_INLINE_IMPLEMENTATION SCM
  57. scm_new_smob (scm_t_bits tc, scm_t_bits data)
  58. {
  59. scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
  60. if (SCM_UNLIKELY (scm_smobs[smobnum].mark || scm_smobs[smobnum].free))
  61. return scm_i_new_smob (tc, data);
  62. else
  63. return scm_cell (tc, data);
  64. }
  65. SCM_INLINE_IMPLEMENTATION SCM
  66. scm_new_double_smob (scm_t_bits tc, scm_t_bits data1,
  67. scm_t_bits data2, scm_t_bits data3)
  68. {
  69. scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
  70. if (SCM_UNLIKELY (scm_smobs[smobnum].mark || scm_smobs[smobnum].free))
  71. return scm_i_new_double_smob (tc, data1, data2, data3);
  72. else
  73. return scm_double_cell (tc, data1, data2, data3);
  74. }
  75. #endif
  76. #define SCM_NEWSMOB(z, tc, data) \
  77. z = scm_new_smob ((tc), (scm_t_bits)(data))
  78. #define SCM_RETURN_NEWSMOB(tc, data) \
  79. return scm_new_smob ((tc), (scm_t_bits)(data))
  80. #define SCM_NEWSMOB2(z, tc, data1, data2) \
  81. z = scm_new_double_smob ((tc), (scm_t_bits)(data1), \
  82. (scm_t_bits)(data2), 0)
  83. #define SCM_RETURN_NEWSMOB2(tc, data1, data2) \
  84. return scm_new_double_smob ((tc), (scm_t_bits)(data1), \
  85. (scm_t_bits)(data2), 0)
  86. #define SCM_NEWSMOB3(z, tc, data1, data2, data3) \
  87. z = scm_new_double_smob ((tc), (scm_t_bits)(data1), \
  88. (scm_t_bits)(data2), (scm_t_bits)(data3))
  89. #define SCM_RETURN_NEWSMOB3(tc, data1, data2, data3) \
  90. return scm_new_double_smob ((tc), (scm_t_bits)(data1), \
  91. (scm_t_bits)(data2), (scm_t_bits)(data3))
  92. #define SCM_SMOB_DATA_N(x, n) (SCM_CELL_WORD ((x), (n)))
  93. #define SCM_SET_SMOB_DATA_N(x, n, data) (SCM_SET_CELL_WORD ((x), (n), (data)))
  94. #define SCM_SMOB_DATA_0(x) (SCM_SMOB_DATA_N ((x), 0))
  95. #define SCM_SMOB_DATA_1(x) (SCM_SMOB_DATA_N ((x), 1))
  96. #define SCM_SMOB_DATA_2(x) (SCM_SMOB_DATA_N ((x), 2))
  97. #define SCM_SMOB_DATA_3(x) (SCM_SMOB_DATA_N ((x), 3))
  98. #define SCM_SET_SMOB_DATA_0(x, data) (SCM_SET_SMOB_DATA_N ((x), 0, (data)))
  99. #define SCM_SET_SMOB_DATA_1(x, data) (SCM_SET_SMOB_DATA_N ((x), 1, (data)))
  100. #define SCM_SET_SMOB_DATA_2(x, data) (SCM_SET_SMOB_DATA_N ((x), 2, (data)))
  101. #define SCM_SET_SMOB_DATA_3(x, data) (SCM_SET_SMOB_DATA_N ((x), 3, (data)))
  102. #define SCM_SMOB_FLAGS(x) (SCM_SMOB_DATA_0 (x) >> 16)
  103. #define SCM_SMOB_DATA(x) (SCM_SMOB_DATA_1 (x))
  104. #define SCM_SET_SMOB_FLAGS(x, data) (SCM_SET_SMOB_DATA_0 ((x), (SCM_CELL_TYPE (x)&0xffff)|((data)<<16)))
  105. #define SCM_SET_SMOB_DATA(x, data) (SCM_SET_SMOB_DATA_1 ((x), (data)))
  106. #define SCM_SMOB_OBJECT_N(x,n) (SCM_CELL_OBJECT ((x), (n)))
  107. #define SCM_SET_SMOB_OBJECT_N(x,n,obj) (SCM_SET_CELL_OBJECT ((x), (n), (obj)))
  108. #define SCM_SMOB_OBJECT_N_LOC(x,n) (SCM_CELL_OBJECT_LOC ((x), (n)))
  109. /*#define SCM_SMOB_OBJECT_0(x) (SCM_SMOB_OBJECT_N ((x), 0))*/
  110. #define SCM_SMOB_OBJECT_1(x) (SCM_SMOB_OBJECT_N ((x), 1))
  111. #define SCM_SMOB_OBJECT_2(x) (SCM_SMOB_OBJECT_N ((x), 2))
  112. #define SCM_SMOB_OBJECT_3(x) (SCM_SMOB_OBJECT_N ((x), 3))
  113. /*#define SCM_SET_SMOB_OBJECT_0(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 0, (obj)))*/
  114. #define SCM_SET_SMOB_OBJECT_1(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 1, (obj)))
  115. #define SCM_SET_SMOB_OBJECT_2(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 2, (obj)))
  116. #define SCM_SET_SMOB_OBJECT_3(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 3, (obj)))
  117. #define SCM_SMOB_OBJECT_0_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 0))
  118. #define SCM_SMOB_OBJECT_1_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 1))
  119. #define SCM_SMOB_OBJECT_2_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 2))
  120. #define SCM_SMOB_OBJECT_3_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 3))
  121. #define SCM_SMOB_OBJECT(x) (SCM_SMOB_OBJECT_1 (x))
  122. #define SCM_SET_SMOB_OBJECT(x,obj) (SCM_SET_SMOB_OBJECT_1 ((x), (obj)))
  123. #define SCM_SMOB_OBJECT_LOC(x) (SCM_SMOB_OBJECT_1_LOC (x))
  124. #define SCM_SMOB_APPLY_0(x) (scm_call_0 (x))
  125. #define SCM_SMOB_APPLY_1(x, a1) (scm_call_1 (x, a1))
  126. #define SCM_SMOB_APPLY_2(x, a1, a2) (scm_call_2 (x, a1, a2))
  127. #define SCM_SMOB_APPLY_3(x, a1, a2, rst) (scm_call_3 (x, a1, a2, a3))
  128. SCM_API SCM scm_mark0 (SCM ptr);
  129. SCM_API SCM scm_markcdr (SCM ptr);
  130. SCM_API size_t scm_free0 (SCM ptr);
  131. SCM_API int scm_smob_print (SCM exp, SCM port, scm_print_state *pstate);
  132. /* The following set of functions is the standard way to create new
  133. * SMOB types.
  134. *
  135. * Create a type tag using `scm_make_smob_type', accept default values
  136. * for mark, free, print and/or equalp functions, or set your own
  137. * values using `scm_set_smob_xxx'.
  138. */
  139. SCM_API scm_t_bits scm_make_smob_type (char const *name, size_t size);
  140. SCM_API void scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM));
  141. SCM_API void scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM));
  142. SCM_API void scm_set_smob_print (scm_t_bits tc,
  143. int (*print) (SCM, SCM, scm_print_state*));
  144. SCM_API void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
  145. SCM_API void scm_set_smob_apply (scm_t_bits tc,
  146. scm_t_subr apply,
  147. unsigned int req,
  148. unsigned int opt,
  149. unsigned int rst);
  150. SCM_API SCM scm_smob_type_class (scm_t_bits tc);
  151. SCM_API void scm_assert_smob_type (scm_t_bits tag, SCM val);
  152. /* Function for creating smobs */
  153. SCM_API SCM scm_make_smob (scm_t_bits tc);
  154. SCM_API void scm_smob_prehistory (void);
  155. #endif /* SCM_SMOB_H */
  156. /*
  157. Local Variables:
  158. c-file-style: "gnu"
  159. End:
  160. */