foreign-object.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Copyright 2014,2017-2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include "eval.h"
  19. #include "extensions.h"
  20. #include "finalizers.h"
  21. #include "goops.h"
  22. #include "gsubr.h"
  23. #include "list.h"
  24. #include "modules.h"
  25. #include "numbers.h"
  26. #include "procs.h"
  27. #include "threads.h"
  28. #include "variable.h"
  29. #include "version.h"
  30. #include "foreign-object.h"
  31. static SCM make_fobj_type_var;
  32. static void
  33. init_make_fobj_type_var (void)
  34. {
  35. make_fobj_type_var = scm_c_private_lookup ("system foreign-object",
  36. "make-foreign-object-type");
  37. }
  38. SCM
  39. scm_make_foreign_object_type (SCM name, SCM slot_names,
  40. scm_t_struct_finalize finalizer)
  41. {
  42. SCM type;
  43. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  44. scm_i_pthread_once (&once, init_make_fobj_type_var);
  45. type = scm_call_2 (scm_variable_ref (make_fobj_type_var), name, slot_names);
  46. if (finalizer)
  47. SCM_SET_VTABLE_INSTANCE_FINALIZER (type, finalizer);
  48. return type;
  49. }
  50. void
  51. scm_assert_foreign_object_type (SCM type, SCM val)
  52. {
  53. /* FIXME: Add fast path for when type == struct vtable */
  54. if (!SCM_IS_A_P (val, type))
  55. scm_error (scm_arg_type_key, NULL, "Wrong type (expecting ~A): ~S",
  56. scm_list_2 (scm_class_name (type), val), scm_list_1 (val));
  57. }
  58. SCM
  59. scm_make_foreign_object_0 (SCM type)
  60. {
  61. return scm_make_foreign_object_n (type, 0, NULL);
  62. }
  63. SCM
  64. scm_make_foreign_object_1 (SCM type, void *val0)
  65. {
  66. return scm_make_foreign_object_n (type, 1, &val0);
  67. }
  68. SCM
  69. scm_make_foreign_object_2 (SCM type, void *val0, void *val1)
  70. {
  71. void *vals[2];
  72. vals[0] = val0;
  73. vals[1] = val1;
  74. return scm_make_foreign_object_n (type, 2, vals);
  75. }
  76. SCM
  77. scm_make_foreign_object_3 (SCM type, void *val0, void *val1, void *val2)
  78. {
  79. void *vals[3];
  80. vals[0] = val0;
  81. vals[1] = val1;
  82. vals[2] = val2;
  83. return scm_make_foreign_object_n (type, 3, vals);
  84. }
  85. SCM
  86. scm_make_foreign_object_n (SCM type, size_t n, void *vals[])
  87. #define FUNC_NAME "make-foreign-object"
  88. {
  89. SCM obj;
  90. size_t i;
  91. SCM_VALIDATE_VTABLE (SCM_ARG1, type);
  92. if (SCM_VTABLE_SIZE (type) < n)
  93. scm_out_of_range (FUNC_NAME, scm_from_size_t (n));
  94. for (i = 0; i < n; i++)
  95. if (!SCM_VTABLE_FIELD_IS_UNBOXED (type, i))
  96. scm_wrong_type_arg_msg (FUNC_NAME, 0, type, "foreign object type");
  97. obj = scm_c_make_structv (type, 0, 0, NULL);
  98. for (i = 0; i < n; i++)
  99. SCM_STRUCT_DATA_SET (obj, i, (scm_t_bits) vals[i]);
  100. return obj;
  101. }
  102. #undef FUNC_NAME
  103. scm_t_bits
  104. scm_foreign_object_unsigned_ref (SCM obj, size_t n)
  105. #define FUNC_NAME "foreign-object-ref"
  106. {
  107. SCM_VALIDATE_STRUCT (SCM_ARG1, obj);
  108. if (SCM_STRUCT_SIZE (obj) <= n)
  109. scm_out_of_range (FUNC_NAME, scm_from_size_t (n));
  110. if (!SCM_STRUCT_FIELD_IS_UNBOXED (obj, n))
  111. scm_wrong_type_arg_msg (FUNC_NAME, 0, scm_from_size_t (n), "unboxed field");
  112. return SCM_STRUCT_DATA_REF (obj, n);
  113. }
  114. #undef FUNC_NAME
  115. void
  116. scm_foreign_object_unsigned_set_x (SCM obj, size_t n, scm_t_bits val)
  117. #define FUNC_NAME "foreign-object-set!"
  118. {
  119. SCM_VALIDATE_STRUCT (SCM_ARG1, obj);
  120. if (SCM_STRUCT_SIZE (obj) <= n)
  121. scm_out_of_range (FUNC_NAME, scm_from_size_t (n));
  122. if (!SCM_STRUCT_FIELD_IS_UNBOXED (obj, n))
  123. scm_wrong_type_arg_msg (FUNC_NAME, 0, scm_from_size_t (n), "unboxed 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. }