foreign-object.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Copyright (C) 2014 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include "libguile/goops.h"
  23. #include "libguile/foreign-object.h"
  24. static SCM make_fobj_type_var;
  25. static void
  26. init_make_fobj_type_var (void)
  27. {
  28. make_fobj_type_var = scm_c_private_lookup ("system foreign-object",
  29. "make-foreign-object-type");
  30. }
  31. SCM
  32. scm_make_foreign_object_type (SCM name, SCM slot_names,
  33. scm_t_struct_finalize finalizer)
  34. {
  35. SCM type;
  36. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  37. scm_i_pthread_once (&once, init_make_fobj_type_var);
  38. type = scm_call_2 (scm_variable_ref (make_fobj_type_var), name, slot_names);
  39. if (finalizer)
  40. SCM_SET_VTABLE_INSTANCE_FINALIZER (type, finalizer);
  41. return type;
  42. }
  43. void
  44. scm_assert_foreign_object_type (SCM type, SCM val)
  45. {
  46. if (!SCM_IS_A_P (val, type))
  47. scm_error (scm_arg_type_key, NULL, "Wrong type (expecting ~A): ~S",
  48. scm_list_2 (scm_class_name (type), val), scm_list_1 (val));
  49. }
  50. SCM
  51. scm_make_foreign_object_0 (SCM type)
  52. {
  53. return scm_make_foreign_object_n (type, 0, NULL);
  54. }
  55. SCM
  56. scm_make_foreign_object_1 (SCM type, void *val0)
  57. {
  58. return scm_make_foreign_object_n (type, 1, &val0);
  59. }
  60. SCM
  61. scm_make_foreign_object_2 (SCM type, void *val0, void *val1)
  62. {
  63. void *vals[2];
  64. vals[0] = val0;
  65. vals[1] = val1;
  66. return scm_make_foreign_object_n (type, 2, vals);
  67. }
  68. SCM
  69. scm_make_foreign_object_3 (SCM type, void *val0, void *val1, void *val2)
  70. {
  71. void *vals[3];
  72. vals[0] = val0;
  73. vals[1] = val1;
  74. vals[2] = val2;
  75. return scm_make_foreign_object_n (type, 3, vals);
  76. }
  77. SCM
  78. scm_make_foreign_object_n (SCM type, size_t n, void *vals[])
  79. #define FUNC_NAME "make-foreign-object"
  80. {
  81. SCM obj;
  82. SCM layout;
  83. size_t i;
  84. const char *layout_chars;
  85. SCM_VALIDATE_VTABLE (SCM_ARG1, type);
  86. layout = SCM_VTABLE_LAYOUT (type);
  87. if (scm_i_symbol_length (layout) / 2 < n)
  88. scm_out_of_range (FUNC_NAME, scm_from_size_t (n));
  89. layout_chars = scm_i_symbol_chars (layout);
  90. for (i = 0; i < n; i++)
  91. if (layout_chars[i * 2] != 'u')
  92. scm_wrong_type_arg_msg (FUNC_NAME, 0, layout, "'u' field");
  93. obj = scm_c_make_structv (type, 0, 0, NULL);
  94. for (i = 0; i < n; i++)
  95. SCM_STRUCT_DATA_SET (obj, i, (scm_t_bits) vals[i]);
  96. return obj;
  97. }
  98. #undef FUNC_NAME
  99. scm_t_bits
  100. scm_foreign_object_unsigned_ref (SCM obj, size_t n)
  101. #define FUNC_NAME "foreign-object-ref"
  102. {
  103. SCM layout;
  104. SCM_VALIDATE_STRUCT (SCM_ARG1, obj);
  105. layout = SCM_STRUCT_LAYOUT (obj);
  106. if (scm_i_symbol_length (layout) / 2 < n)
  107. scm_out_of_range (FUNC_NAME, scm_from_size_t (n));
  108. if (scm_i_symbol_ref (layout, n * 2) != 'u')
  109. scm_wrong_type_arg_msg (FUNC_NAME, 0, layout, "'u' field");
  110. return SCM_STRUCT_DATA_REF (obj, n);
  111. }
  112. #undef FUNC_NAME
  113. void
  114. scm_foreign_object_unsigned_set_x (SCM obj, size_t n, scm_t_bits val)
  115. #define FUNC_NAME "foreign-object-set!"
  116. {
  117. SCM layout;
  118. SCM_VALIDATE_STRUCT (SCM_ARG1, obj);
  119. layout = SCM_STRUCT_LAYOUT (obj);
  120. if (scm_i_symbol_length (layout) / 2 < n)
  121. scm_out_of_range (FUNC_NAME, scm_from_size_t (n));
  122. if (scm_i_symbol_ref (layout, n * 2) != 'u')
  123. scm_wrong_type_arg_msg (FUNC_NAME, 0, layout, "'u' field");
  124. SCM_STRUCT_DATA_SET (obj, n, val);
  125. }
  126. #undef FUNC_NAME
  127. scm_t_signed_bits
  128. scm_foreign_object_signed_ref (SCM obj, size_t n)
  129. {
  130. scm_t_bits bits = scm_foreign_object_unsigned_ref (obj, n);
  131. return (scm_t_signed_bits) bits;
  132. }
  133. void
  134. scm_foreign_object_signed_set_x (SCM obj, size_t n, scm_t_signed_bits val)
  135. {
  136. scm_t_bits bits = (scm_t_bits) val;
  137. scm_foreign_object_unsigned_set_x (obj, n, bits);
  138. }
  139. void*
  140. scm_foreign_object_ref (SCM obj, size_t n)
  141. {
  142. scm_t_bits bits = scm_foreign_object_unsigned_ref (obj, n);
  143. return (void *) bits;
  144. }
  145. void
  146. scm_foreign_object_set_x (SCM obj, size_t n, void *val)
  147. {
  148. scm_t_bits bits = (scm_t_bits) val;
  149. scm_foreign_object_unsigned_set_x (obj, n, bits);
  150. }
  151. static void
  152. invoke_finalizer (void *obj, void *data)
  153. {
  154. scm_call_1 (PTR2SCM (data), PTR2SCM (obj));
  155. }
  156. static SCM
  157. sys_add_finalizer_x (SCM obj, SCM finalizer)
  158. #define FUNC_NAME "%add-finalizer!"
  159. {
  160. SCM_VALIDATE_PROC (SCM_ARG2, finalizer);
  161. scm_i_add_finalizer (SCM2PTR (obj), invoke_finalizer, SCM2PTR (finalizer));
  162. return SCM_UNSPECIFIED;
  163. }
  164. #undef FUNC_NAME
  165. static void
  166. scm_init_foreign_object (void)
  167. {
  168. scm_c_define_gsubr ("%add-finalizer!", 2, 0, 0,
  169. (scm_t_subr) sys_add_finalizer_x);
  170. }
  171. void
  172. scm_register_foreign_object (void)
  173. {
  174. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  175. "scm_init_foreign_object",
  176. (scm_t_extension_init_func)scm_init_foreign_object,
  177. NULL);
  178. }