dynwind.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006, 2008, 2010, 2011, 2012 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 <assert.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/dynstack.h"
  24. #include "libguile/eval.h"
  25. #include "libguile/ports.h"
  26. #include "libguile/dynwind.h"
  27. SCM
  28. scm_dynamic_wind (SCM in_guard, SCM thunk, SCM out_guard)
  29. #define FUNC_NAME "dynamic-wind"
  30. {
  31. SCM ans;
  32. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  33. SCM_ASSERT (scm_is_true (scm_thunk_p (out_guard)), out_guard,
  34. SCM_ARG3, FUNC_NAME);
  35. scm_call_0 (in_guard);
  36. scm_dynstack_push_dynwind (&thread->dynstack, in_guard, out_guard);
  37. ans = scm_call_0 (thunk);
  38. scm_dynstack_pop (&thread->dynstack);
  39. scm_call_0 (out_guard);
  40. return ans;
  41. }
  42. #undef FUNC_NAME
  43. void
  44. scm_dynwind_begin (scm_t_dynwind_flags flags)
  45. {
  46. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  47. scm_dynstack_push_frame (&thread->dynstack, flags);
  48. }
  49. void
  50. scm_dynwind_end (void)
  51. {
  52. scm_dynstack_unwind_frame (&SCM_I_CURRENT_THREAD->dynstack);
  53. }
  54. void
  55. scm_dynwind_unwind_handler (void (*proc) (void *), void *data,
  56. scm_t_wind_flags flags)
  57. {
  58. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  59. scm_t_dynstack *dynstack = &thread->dynstack;
  60. scm_dynstack_push_unwinder (dynstack, flags, proc, data);
  61. }
  62. void
  63. scm_dynwind_rewind_handler (void (*proc) (void *), void *data,
  64. scm_t_wind_flags flags)
  65. {
  66. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  67. scm_t_dynstack *dynstack = &thread->dynstack;
  68. scm_dynstack_push_rewinder (dynstack, 0, proc, data);
  69. if (flags & SCM_F_WIND_EXPLICITLY)
  70. proc (data);
  71. }
  72. void
  73. scm_dynwind_unwind_handler_with_scm (void (*proc) (SCM), SCM data,
  74. scm_t_wind_flags flags)
  75. {
  76. /* FIXME: This is not a safe cast. */
  77. scm_dynwind_unwind_handler ((scm_t_guard) proc, SCM2PTR (data), flags);
  78. }
  79. void
  80. scm_dynwind_rewind_handler_with_scm (void (*proc) (SCM), SCM data,
  81. scm_t_wind_flags flags)
  82. {
  83. /* FIXME: This is not a safe cast. */
  84. scm_dynwind_rewind_handler ((scm_t_guard) proc, SCM2PTR (data), flags);
  85. }
  86. void
  87. scm_dynwind_free (void *mem)
  88. {
  89. scm_dynwind_unwind_handler (free, mem, SCM_F_WIND_EXPLICITLY);
  90. }
  91. void
  92. scm_swap_bindings (SCM vars, SCM vals)
  93. {
  94. SCM tmp;
  95. while (scm_is_pair (vals))
  96. {
  97. tmp = SCM_VARIABLE_REF (SCM_CAR (vars));
  98. SCM_VARIABLE_SET (SCM_CAR (vars), SCM_CAR (vals));
  99. SCM_SETCAR (vals, tmp);
  100. vars = SCM_CDR (vars);
  101. vals = SCM_CDR (vals);
  102. }
  103. }
  104. void
  105. scm_init_dynwind ()
  106. {
  107. #include "libguile/dynwind.x"
  108. }
  109. /*
  110. Local Variables:
  111. c-file-style: "gnu"
  112. End:
  113. */