diff --git a/libguile/ports.c b/libguile/ports.c index 3d9c1eb11..96dcd2fe6 100644 --- a/libguile/ports.c +++ b/libguile/ports.c @@ -724,21 +724,12 @@ SCM_DEFINE (scm_close_output_port, "close-output-port", 1, 0, 0, } #undef FUNC_NAME -SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0, - (SCM proc), - "Apply @var{proc} to each port in the Guile port table\n" - "in turn. The return value is unspecified. More specifically,\n" - "@var{proc} is applied exactly once to every port that exists\n" - "in the system at the time @var{port-for-each} is invoked.\n" - "Changes to the port table while @var{port-for-each} is running\n" - "have no effect as far as @var{port-for-each} is concerned.") -#define FUNC_NAME s_scm_port_for_each +void +scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data) { long i; SCM ports; - SCM_VALIDATE_PROC (1, proc); - /* Even without pre-emptive multithreading, running arbitrary code while scanning the port table is unsafe because the port table can change arbitrarily (from a GC, for example). So we build a @@ -754,14 +745,29 @@ SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0, while (ports != SCM_EOL) { - scm_call_1 (proc, SCM_CAR (ports)); + proc (data, SCM_CAR (ports)); ports = SCM_CDR (ports); } +} +SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0, + (SCM proc), + "Apply @var{proc} to each port in the Guile port table\n" + "in turn. The return value is unspecified. More specifically,\n" + "@var{proc} is applied exactly once to every port that exists\n" + "in the system at the time @var{port-for-each} is invoked.\n" + "Changes to the port table while @var{port-for-each} is running\n" + "have no effect as far as @var{port-for-each} is concerned.") +#define FUNC_NAME s_scm_port_for_each +{ + SCM_VALIDATE_PROC (1, proc); + + scm_c_port_for_each ((void (*)(void*,SCM))scm_call_1, proc); return SCM_UNSPECIFIED; } #undef FUNC_NAME + /* Utter miscellany. Gosh, we should clean this up some time. */ diff --git a/libguile/ports.h b/libguile/ports.h index 01717a5a7..164a2bed3 100644 --- a/libguile/ports.h +++ b/libguile/ports.h @@ -250,6 +250,7 @@ SCM_API SCM scm_close_input_port (SCM port); SCM_API SCM scm_close_output_port (SCM port); SCM_API SCM scm_close_port (SCM port); SCM_API SCM scm_port_for_each (SCM proc); +SCM_API void scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data); SCM_API SCM scm_input_port_p (SCM x); SCM_API SCM scm_output_port_p (SCM x); SCM_API SCM scm_port_p (SCM x);