File: ffi.c

package info (click to toggle)
scheme48 1.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,332 kB
  • sloc: lisp: 88,907; ansic: 87,519; sh: 3,224; makefile: 771
file content (876 lines) | stat: -rw-r--r-- 18,049 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
/*
 * Part of Scheme 48 1.9.  See file COPYING for notices and license.
 *
 * Authors: Marcus Crestani, Harald Glab-Phlak
 */

/* Modelled on Jim Blandy's foreign function interface that he put in
   his Scheme implementation called Minor. */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "scheme48.h"
#include "scheme48vm.h"
#include "scheme48heap.h"
#include "ffi.h"

/* structs */

struct ref_group;

struct s48_ref_s
{
  s48_value obj;
  struct ref_group *group;
};

struct ref;

struct ref
{
  struct s48_ref_s x;
  struct ref *next, *prev;
};

#define NUM_REFS_PER_CLUMP 85

struct ref_clump;

struct ref_clump
{
  struct ref_clump *next;
  struct ref refs[NUM_REFS_PER_CLUMP];
};

struct ref_group
{
  struct ref_clump *clumps;
  struct ref *free;
  struct ref *last_free;
  short first_never_used;
  struct ref allocated;
};

struct buf_group;

struct buf_group
{
  void *buffer;
  struct buf_group *next, *prev;
};

enum BV_MODE { READWRITE, READONLY };

struct bv_group;

struct bv_group
{
  char *buffer;
  s48_ref_t byte_vector;
  enum BV_MODE mode;
  struct bv_group *next, *prev;
};

struct s48_call_s
{
  s48_call_t older_call;
  s48_call_t subcall_parent;
  s48_call_t child;
  s48_call_t next_subcall, prev_subcall;
  struct ref_group *local_refs;
  struct buf_group *local_bufs;
  struct bv_group *local_bvs;
};


/* global states */
static s48_call_t current_call = NULL;
static struct ref_group *global_ref_group = NULL;

#define GLOBAL_REF_P(ref) (ref->group == global_ref_group)

/* REFS */

static struct ref_group *
make_ref_group (void)
{
  struct ref_group *g = (struct ref_group *) malloc (sizeof (struct ref_group));
  if (g == NULL)
    s48_out_of_memory_error();
  memset (g, 0, sizeof (*g));

  g->clumps = 0;
  g->free = 0;
  g->allocated.next = &g->allocated;
  g->allocated.prev = &g->allocated;
  return g;
}

static void
free_ref_group (struct ref_group *g)
{
  struct ref_clump *c, *next;
  for (c = g->clumps; c; c = next) {
    next = c->next;
    free (c);
  }
  free (g);
}

static s48_ref_t
make_ref (struct ref_group *g, s48_value obj)
{
  struct ref *r;

  if (g->clumps && (g->first_never_used < NUM_REFS_PER_CLUMP))
    r = &g->clumps->refs[g->first_never_used++];
  else if (g->free) {
    r = g->free;
    g->free = r->next;
  } else {
    struct ref_clump *new =
      (struct ref_clump *) malloc (sizeof (struct ref_clump));
    if (new == NULL)
      s48_out_of_memory_error();

    new->next = g->clumps;
    g->clumps = new;
    r = &new->refs[0];
    g->first_never_used = 1;
  }

  r->next = g->allocated.next;
  r->prev = &g->allocated;
  r->next->prev = r;
  r->prev->next = r;
  r->x.group = g;
  r->x.obj = obj;

  return &r->x;
}

static void
free_ref (s48_ref_t x)
{
#ifdef DEBUG_FFI
  fprintf (stderr, "free ref with scheme value %x\n", s48_deref(x));
#endif
  struct ref *r = (struct ref *) x;
  struct ref_group *g = r->x.group;

  r->next->prev = r->prev;
  r->prev->next = r->next;
  r->next = 0;
  if (g->free) {
    g->last_free->next = r;
    g->last_free = r;
  } else
    g->free = g->last_free = r;
  r->x.obj = S48_FALSE;
}

static void
walk_ref_group (struct ref_group *g,
		void (*func) (s48_ref_t ref, void *closure),
		void *closure)
{
  struct ref *r;
  struct ref *head = &g->allocated;
  for (r = head->next; r != head; r = r->next)
    func (&r->x, closure);
}


/* LOCAL REFS */

s48_ref_t
s48_make_local_ref (s48_call_t call, s48_value obj)
{
#ifdef DEBUG_FFI
  fprintf (stderr, "make local ref from scheme value %x\n", obj);
#endif
  return make_ref (call->local_refs, obj);
}

s48_ref_t
s48_copy_local_ref (s48_call_t call, s48_ref_t ref)
{
  s48_ref_t r = s48_make_local_ref (call, s48_deref(ref));
  return r;
}

void
s48_free_local_ref (s48_call_t call, s48_ref_t ref)
{
#ifdef DEBUG_FFI
  fprintf (stderr, "free local ref with scheme value %x\n", s48_deref(ref));
#endif
  if (!GLOBAL_REF_P (ref))
    free_ref (ref);
  else 
    s48_assertion_violation ("s48_free_localref", "ref is not local", 0);
}

void
s48_free_local_ref_array (s48_call_t call, s48_ref_t *refs, size_t len)
{
  size_t i;
  for (i = 0; i < len; i++)
    s48_free_local_ref (call, refs[i]);
}


/* GLOBAL REFS */

s48_ref_t
s48_make_global_ref (s48_value obj)
{
#ifdef DEBUG_FFI
  fprintf (stderr, "make global ref from scheme value %x\n", obj);
#endif
  return make_ref (global_ref_group, obj);
}

void
s48_free_global_ref (s48_ref_t ref)
{
#ifdef DEBUG_FFI
  fprintf (stderr, "free global ref from scheme value %x\n", s48_deref(ref));
#endif
  if (GLOBAL_REF_P (ref))
    free_ref (ref);
  else
    s48_assertion_violation ("s48_free_global_ref", "ref is not global", 0);
}

s48_ref_t
s48_local_to_global_ref(s48_ref_t ref)
{
  s48_value temp = s48_deref(ref);
#ifdef DEBUG_FFI
  fprintf (stderr, "local to global ref from scheme value %x\n", s48_deref(ref));
#endif
  free_ref (ref);
  return s48_make_global_ref(temp);
}

static void
walk_global_refs (void (*func) (s48_ref_t ref, void *closure),
		  void *closure)
{
  walk_ref_group (global_ref_group, func, closure);
}


/* BUFS */

struct buf_group *
make_buf_group (void)
{
  struct buf_group *g = (struct buf_group *) malloc (sizeof (struct buf_group));
  if (g == NULL)
    s48_out_of_memory_error();
#ifdef DEBUG_FFI
  fprintf (stderr, "make buf group %x\n", g);
#endif
  return g;
}

void
free_buf (struct buf_group *b)
{
#ifdef DEBUG_FFI
  fprintf (stderr, "free buf %x\n", b);
#endif
  free (b->buffer);
  free (b);
}

void
free_buf_group (struct buf_group *g)
{
  struct buf_group *b, *next;
#ifdef DEBUG_FFI
  fprintf (stderr, "free buf group %x\n", g);
#endif
  for (b = g; b; b = next) {
    next = b->next;
    free_buf (b);
  }
}

void *
s48_make_local_buf (s48_call_t call, size_t s)
{
  struct buf_group *g = make_buf_group ();
#ifdef DEBUG_FFI
  fprintf (stderr, "make buf with size %x\n", s);
#endif
  g->buffer = (void *) calloc (1, s);
  if (g->buffer == NULL)
    s48_out_of_memory_error();
  g->prev = NULL;
  g->next = call->local_bufs;
  if (g->next)
    g->next->prev = g;
  call->local_bufs = g;
  return g->buffer;
}

void
s48_free_local_buf (s48_call_t call, void *buffer)
{
  struct buf_group *prev, *b, *next;

  if (! call->local_bufs)
    return;

#ifdef DEBUG_FFI
  fprintf (stderr, "free buf %x\n", buffer);
#endif

  if (buffer == call->local_bufs->buffer) {
    b = call->local_bufs;
    call->local_bufs = call->local_bufs->next;
    if (call->local_bufs)
      call->local_bufs->prev = NULL;
    free_buf (b);
    return;
  }
  
  prev = call->local_bufs;
  b = call->local_bufs->next;
  while (b) {
    if (buffer == b->buffer) {
      next = b->next;
      prev = b->prev;
      prev->next = next;
      if (next)
	next->prev = prev;
      free_buf (b);
      b = NULL;
    } else {
      b = b->next;
    }
  }
}


/* BYTE VECTORS */

struct bv_group *
make_bv_group (void)
{
  struct bv_group *g = (struct bv_group *) malloc (sizeof (struct bv_group));
  if (g == NULL)
    s48_out_of_memory_error();
#ifdef DEBUG_FFI
  fprintf (stderr, "make bv group %x\n", g);
#endif
  return g;
}


static void
copy_to_bv (s48_call_t call, struct bv_group *bv, void *closure)
{
  if (bv->mode != READONLY)
    s48_copy_to_byte_vector_2(call, bv->byte_vector, bv->buffer);
}

static void
copy_from_bv (s48_call_t call, struct bv_group *bv, void *closure)
{
  s48_copy_from_byte_vector_2(call, bv->byte_vector, bv->buffer);
}

void
free_bv (s48_call_t call, struct bv_group *b)
{
#ifdef DEBUG_FFI
  fprintf (stderr, "free bv %x\n", b);
#endif
  copy_to_bv (call, b, NULL);
  free (b->buffer);
  free (b);
}

void
free_bv_group (s48_call_t call, struct bv_group *g)
{
  struct bv_group *b, *next;
#ifdef DEBUG_FFI
  fprintf (stderr, "free bv group %x\n", g);
#endif
  for (b = g; b; b = next) {
    next = b->next;
    free_bv (call, b);
  }
}

struct bv_group *
s48_find_local_bv (s48_call_t call, s48_ref_t byte_vector, long s)
{
  struct bv_group *b;

  if (! call->local_bvs)
    return NULL;

  if (s48_eq_p_2 (call, byte_vector, call->local_bvs->byte_vector)) {
    return call->local_bvs;
  }
  
  b = call->local_bvs->next;
  while (b) {
    if (s48_eq_p_2 (call, byte_vector, b->byte_vector)) {
      return b;
    } else {
      b = b->next;
    }
  }

  return NULL;
}

char *
s48_really_make_local_bv (s48_call_t call, s48_ref_t byte_vector, long s, enum BV_MODE mode)
{
  struct bv_group *g = make_bv_group ();
#ifdef DEBUG_FFI
  fprintf (stderr, "make bv with size %x\n", s);
#endif
  g->buffer = (char *) calloc (1, s);
  if (g->buffer == NULL)
    s48_out_of_memory_error();
  g->byte_vector = byte_vector;
  g->mode = mode;
  g->prev = NULL;
  g->next = call->local_bvs;
  if (g->next)
    g->next->prev = g;
  call->local_bvs = g;
  return g->buffer;
}

psbool     s48_unmovable_p (s48_call_t, s48_ref_t);

char *
s48_maybe_make_local_bv (s48_call_t call, s48_ref_t byte_vector, long s, enum BV_MODE mode)
{
  char *buf;
  struct bv_group *b;

  if (s48_unmovable_p(call, byte_vector))
    {
      return s48_extract_unmovable_byte_vector_2(call, byte_vector);
    }

  b = s48_find_local_bv (call, byte_vector, s);
  if (b)
    {
      b->mode = mode;
      return b->buffer;
    }
  else
    {
      buf = s48_really_make_local_bv (call, byte_vector, s, mode);
      s48_extract_byte_vector_region_2(call, byte_vector, 0, s, buf);
      return buf;
    }
}

char *
s48_make_local_bv (s48_call_t call, s48_ref_t byte_vector, long s)
{
  return s48_maybe_make_local_bv(call, byte_vector, s, READWRITE);
}

char *
s48_make_local_bv_readonly (s48_call_t call, s48_ref_t byte_vector, long s)
{
  return s48_maybe_make_local_bv(call, byte_vector, s, READONLY);
}

void
s48_free_local_bv (s48_call_t call, char *buffer)
{
  struct bv_group *prev, *b, *next;

  if (! call->local_bvs)
    return;

#ifdef DEBUG_FFI
  fprintf (stderr, "free bv %x\n", buffer);
#endif

  if (buffer == call->local_bvs->buffer) {
    b = call->local_bvs;
    call->local_bvs = call->local_bvs->next;
    if (call->local_bvs)
      call->local_bvs->prev = NULL;
    free_bv (call, b);
    return;
  }
  
  prev = call->local_bvs;
  b = call->local_bvs->next;
  while (b) {
    if (buffer == b->buffer) {
      next = b->next;
      prev = b->prev;
      prev->next = next;
      if (next)
	next->prev = prev;
      free_bv (call, b);
      b = NULL;
    } else {
      b = b->next;
    }
  }
}

static void
walk_local_bvs (s48_call_t call,
		void (*func) (s48_call_t call, struct bv_group *bv, void *closure),
		void *closure)
{
  struct bv_group *b;

  for (b = call->local_bvs; b; b = b->next)
    func (call, b, closure);
}

void
s48_copy_local_bvs_to_scheme (s48_call_t call)
{
  walk_local_bvs (call, copy_to_bv, NULL);
}

void
s48_copy_local_bvs_from_scheme (s48_call_t call)
{
  walk_local_bvs (call, copy_from_bv, NULL);
}


/* CALLS */

static s48_call_t
really_make_call (s48_call_t older_call)
{
  s48_call_t new = (s48_call_t ) malloc (sizeof (struct s48_call_s));
  if (new == NULL)
    s48_out_of_memory_error();
  memset (new, 0, sizeof (*new));
  new->local_refs = make_ref_group ();
  new->older_call = older_call;
  new->subcall_parent = NULL;
  new->child = NULL;
  new->local_bufs = NULL;
  new->local_bvs = NULL;
  return new;
}

s48_call_t
s48_push_call (s48_call_t call)
{
#ifdef DEBUG_FFI
    fprintf (stderr, "push\n");
#endif
  current_call = really_make_call (call);
  return current_call;
}

static void
free_call (s48_call_t call)
{
  if (call->child) {
    s48_call_t c = call->child;

    do {
      s48_call_t temp = c;
      c = c->next_subcall;
      free_call (temp);
    } while (c != call->child);
  }
  free_bv_group (call, call->local_bvs);
  free_ref_group (call->local_refs);
  free_buf_group (call->local_bufs);
#ifdef DEBUG_FFI
  fprintf (stderr, "free_call\n");
  fprintf(stderr, "  count calls: %d, localrefs: %d, globalrefs: %d\n",
	  count_calls(), count_local_refs (), count_global_refs());
#endif
  free (call);
}

void
s48_pop_to (s48_call_t call)
{
  while (current_call != call) {
    s48_call_t here = current_call;
    if (!here)
      s48_assertion_violation ("s48_pop_to", "current_call is null", 0);
    current_call = here->older_call;
    free_call (here);
#ifdef DEBUG_FFI
    fprintf (stderr, "pop\n");
#endif
  }
}


/* SUBCALLS */

s48_call_t
s48_make_subcall (s48_call_t call)
{
  s48_call_t new = (s48_call_t ) malloc (sizeof (struct s48_call_s));
  if (new == NULL)
    s48_out_of_memory_error();
  memset (new, 0, sizeof (*new));
  new->local_refs = make_ref_group ();
  new->older_call = NULL;
  new->subcall_parent = call;
  new->child = NULL;

  if (call->child) {
    new->next_subcall = call->child->next_subcall;
    new->prev_subcall = call->child;
    new->next_subcall->prev_subcall = new;
    new->prev_subcall->next_subcall = new;
  } else {
    new->next_subcall = new->prev_subcall = new;
    call->child = new;
  }

  return new;
}

void
s48_free_subcall (s48_call_t subcall)
{
  s48_call_t parent = subcall->subcall_parent;
  if (subcall->next_subcall == subcall) {
    parent->child = NULL;
  } else {
    parent->child = subcall->next_subcall;
    subcall->prev_subcall->next_subcall = subcall->next_subcall;
    subcall->next_subcall->prev_subcall = subcall->prev_subcall;
  }
  free_call (subcall);
}

s48_ref_t
s48_finish_subcall (s48_call_t call, s48_call_t subcall, s48_ref_t ref)
{
  s48_ref_t result = ref ? s48_copy_local_ref (call, ref) : NULL;
  s48_free_subcall (subcall);
  return result;
}

static void
walk_call (s48_call_t call,
	   void (*func) (s48_ref_t, void *closure),
	   void *closure)
{
  s48_call_t c = NULL;
  walk_ref_group (call->local_refs, func, closure);
  c = call->child;
  if (c)
    do
      walk_call (c, func, closure);
    while ((c = c->next_subcall) != call->child);
}

static void
walk_local_refs (void (*func) (s48_ref_t, void *closure), void *closure)
{
  s48_call_t c;
  for (c = current_call; c; c = c->older_call)
    walk_call (c, func, closure);
}

#ifdef DEBUG_FFI /* for debugging */
static void
count_a_ref (s48_ref_t ref, void *closure)
{
  size_t *count_p = closure;
  (*count_p)++;
}

static size_t
count_global_refs ()
{
  size_t count = 0;
  walk_global_refs (count_a_ref, &count);
  return count;
}

static size_t
count_local_refs ()
{
  size_t count = 0;
  walk_local_refs (count_a_ref, &count);
  return count;
}

static size_t
count_calls ()
{
  size_t count;
  s48_call_t c;
  for (c = current_call, count = 0; c; c = c->older_call, count++);
  return count;
}
#endif

void
s48_setref (s48_ref_t ref, s48_value obj)
{
  ref->obj = obj;
}

s48_value 
s48_deref (s48_ref_t ref)
{
  return ref->obj;
}

s48_call_t
s48_first_call (void)
{
  return really_make_call (NULL);
}

s48_call_t
s48_get_current_call (void)
{
  return current_call;
}

void
s48_initialize_ffi (void)
{
  if (current_call)
    s48_assertion_violation ("s48_init_ffi", "current_call is already set", 0);
  current_call = s48_first_call ();

  if (global_ref_group)
    s48_assertion_violation ("s48_init_ffi", "global_ref_group is already set", 0);
  global_ref_group = make_ref_group ();
}

static void
trace_a_ref (s48_ref_t ref, void *closure)
{
  (*(size_t *) closure)++;
  s48_setref(ref, s48_trace_value (s48_deref(ref)));
}

void
s48_trace_external_calls (void)
{
  size_t cnt_locals = 0;
  size_t cnt_globals = 0;
  walk_local_refs (trace_a_ref, &cnt_locals);
  walk_global_refs (trace_a_ref, &cnt_globals);
#ifdef DEBUG_FFI
  fprintf(stderr, "### TRACED locals %d    globals %d ###\n", cnt_locals, cnt_globals);
#endif
}


#ifdef DEBUG_FFI
/* TESTS */

static s48_ref_t
test_0 (s48_call_t call)
{
  fprintf(stderr, "test_0\n");
  fprintf(stderr, "  count calls: %d, localrefs: %d, globalrefs: %d\n",
	  count_calls(), count_local_refs (), count_global_refs());
  return s48_make_local_ref (call, _s48_value_true);
}

static s48_ref_t
test_1 (s48_call_t call, s48_ref_t ref_1)
{
  s48_ref_t result;

  fprintf(stderr, ">>> %d <<<\n", s48_extract_fixnum (s48_deref(ref_1)));
  /*
  s48_ref_t proc =
    s48_make_local_ref (call,
			S48_SHARED_BINDING_REF(s48_get_imported_binding ("display")));
  fprintf(stderr, "> test_1\n");
  fprintf(stderr, "  count calls: %d, localrefs: %d, globalrefs: %d\n",
	  count_calls(), count_local_refs (), count_global_refs());
  result = s48_call_scheme_2 (call, proc, 1, ref_1);
  fprintf(stderr, "  count calls: %d, localrefs: %d, globalrefs: %d\n",
	  count_calls(), count_local_refs (), count_global_refs());
  fprintf(stderr, "< test_1\n");
  */
  return result;
}

static s48_ref_t
call_thunk (s48_call_t call, s48_ref_t thunk)
{
  s48_ref_t result;
  fprintf(stderr, "> call_thunk\n");
  fprintf(stderr, "  count calls: %d, localrefs: %d, globalrefs: %d\n",
	  count_calls(), count_local_refs (), count_global_refs());
  result = s48_call_scheme_2 (call, thunk, 0);
  fprintf(stderr, "  count calls: %d, localrefs: %d, globalrefs: %d\n",
	  count_calls(), count_local_refs (), count_global_refs());
  fprintf(stderr, "< call_thunk\n");
  return result;
}

static s48_ref_t
call_unary (s48_call_t call, s48_ref_t unary, s48_ref_t arg)
{
  s48_ref_t result;
  fprintf(stderr, "> call_unary\n");
  fprintf(stderr, "  count calls: %d, localrefs: %d, globalrefs: %d\n",
	  count_calls(), count_local_refs (), count_global_refs());
  result = s48_call_scheme_2 (call, unary, 1, arg);
  fprintf(stderr, "  count calls: %d, localrefs: %d, globalrefs: %d\n",
	  count_calls(), count_local_refs (), count_global_refs());
  fprintf(stderr, "< call_unary\n");
  return result;
}

void
init_debug_ffi (void)
{
  S48_EXPORT_FUNCTION(test_0);
  S48_EXPORT_FUNCTION(test_1);
  S48_EXPORT_FUNCTION(call_thunk);
  S48_EXPORT_FUNCTION(call_unary);
  S48_EXPORT_FUNCTION(s48_length_2);
}

/*
; ,open external-calls primitives

(import-lambda-definition-2 call-thunk (thunk))
(import-lambda-definition-2 call-unary (proc arg))

(call-thunk
 (lambda ()
   (call-with-current-continuation
    (lambda (cont)
      (call-thunk
       (lambda ()
         (call-thunk
          (lambda ()
	    (collect)
            (call-unary cont 23)))))))))

*/

#endif