foreign.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef SCM_FOREIGN_H
  2. #define SCM_FOREIGN_H
  3. /* Copyright (C) 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 3 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. */
  20. #include "libguile/__scm.h"
  21. /* A "foreign pointer" is a wrapped C pointer. It is represented by a
  22. cell whose second word is a pointer. The first word has the
  23. `scm_tc7_pointer' type code.
  24. The basic idea is that we can help the programmer to avoid cutting herself,
  25. but we won't take away her knives. */
  26. enum scm_t_foreign_type
  27. {
  28. SCM_FOREIGN_TYPE_VOID,
  29. SCM_FOREIGN_TYPE_FLOAT,
  30. SCM_FOREIGN_TYPE_DOUBLE,
  31. SCM_FOREIGN_TYPE_UINT8,
  32. SCM_FOREIGN_TYPE_INT8,
  33. SCM_FOREIGN_TYPE_UINT16,
  34. SCM_FOREIGN_TYPE_INT16,
  35. SCM_FOREIGN_TYPE_UINT32,
  36. SCM_FOREIGN_TYPE_INT32,
  37. SCM_FOREIGN_TYPE_UINT64,
  38. SCM_FOREIGN_TYPE_INT64,
  39. SCM_FOREIGN_TYPE_LAST = SCM_FOREIGN_TYPE_INT64
  40. };
  41. typedef enum scm_t_foreign_type scm_t_foreign_type;
  42. typedef void (*scm_t_pointer_finalizer) (void *);
  43. #define SCM_POINTER_P(x) (SCM_HAS_TYP7 (x, scm_tc7_pointer))
  44. #define SCM_VALIDATE_POINTER(pos, x) \
  45. SCM_MAKE_VALIDATE (pos, x, POINTER_P)
  46. #define SCM_POINTER_VALUE(x) \
  47. ((void *) SCM_CELL_WORD_1 (x))
  48. SCM_API void *scm_to_pointer (SCM pointer);
  49. SCM_API SCM scm_from_pointer (void *, scm_t_pointer_finalizer);
  50. SCM_API SCM scm_alignof (SCM type);
  51. SCM_API SCM scm_sizeof (SCM type);
  52. SCM_API SCM scm_pointer_address (SCM pointer);
  53. SCM_API SCM scm_pointer_to_scm (SCM pointer);
  54. SCM_API SCM scm_scm_to_pointer (SCM scm);
  55. SCM_API SCM scm_pointer_to_bytevector (SCM pointer, SCM type,
  56. SCM offset, SCM len);
  57. SCM_API SCM scm_set_pointer_finalizer_x (SCM pointer, SCM finalizer);
  58. SCM_API SCM scm_bytevector_to_pointer (SCM bv, SCM offset);
  59. SCM_INTERNAL SCM scm_pointer_p (SCM obj);
  60. SCM_INTERNAL SCM scm_make_pointer (SCM address, SCM finalizer);
  61. SCM_INTERNAL void scm_i_pointer_print (SCM pointer, SCM port,
  62. scm_print_state *pstate);
  63. SCM_INTERNAL SCM scm_dereference_pointer (SCM pointer);
  64. SCM_INTERNAL SCM scm_string_to_pointer (SCM string, SCM encoding);
  65. SCM_INTERNAL SCM scm_pointer_to_string (SCM pointer, SCM length, SCM encoding);
  66. /* Foreign functions */
  67. /* The goal is to make it so that calling a foreign function doesn't cause any
  68. heap allocation. That means we need native Scheme formats for all kinds of
  69. arguments.
  70. For "value" types like s64 or f32, we just use native Scheme value types.
  71. (Note that in both these cases, allocation is possible / likely, as the
  72. value might need to be boxed, but perhaps we won't worry about that. Hmm.)
  73. For everything else, we use foreign pointers. This includes arrays, pointer
  74. arguments and return vals, struct args and return vals, and out and in/out
  75. arguments.
  76. */
  77. union scm_vm_stack_element;
  78. SCM_API SCM scm_pointer_to_procedure (SCM return_type, SCM func_ptr,
  79. SCM arg_types);
  80. SCM_API SCM scm_procedure_to_pointer (SCM return_type, SCM func_ptr,
  81. SCM arg_types);
  82. SCM_INTERNAL SCM scm_i_foreign_call (SCM foreign,
  83. const union scm_vm_stack_element *argv);
  84. SCM_INTERNAL int scm_i_foreign_arity (SCM foreign,
  85. int *req, int *opt, int *rest);
  86. SCM_INTERNAL void scm_register_foreign (void);
  87. #endif /* SCM_FOREIGN_H */