|
88 | 88 | (set! cffi-enable-log t) |
89 | 89 | ) |
90 | 90 |
|
91 | | - (define $ffi-alloc-list '() ) |
92 | 91 |
|
93 | 92 | (define lib-name |
94 | 93 | (case (machine-type) |
|
118 | 117 | ;;(display (format "addr=~x\n" addr)))) |
119 | 118 |
|
120 | 119 |
|
121 | | - (define (ffi-cif-alloc) |
| 120 | + (define (ffi-cif-alloc $ffi-alloc-list) |
122 | 121 | (let ((m ($ffi-cif-alloc))) |
123 | 122 | (set! $ffi-alloc-list(append! $ffi-alloc-list (list m) )) |
124 | 123 | m |
125 | 124 | ) |
126 | 125 | ) |
127 | | - (define (ffi-types-alloc size) |
| 126 | + (define (ffi-types-alloc $ffi-alloc-list size) |
128 | 127 | (let ((m ($ffi-types-alloc size))) |
129 | 128 | (set! $ffi-alloc-list(append! $ffi-alloc-list (list m) )) |
130 | 129 | m |
131 | 130 | ) |
132 | 131 | ) |
133 | | - (define (ffi-values-alloc size) |
| 132 | + (define (ffi-values-alloc $ffi-alloc-list size) |
134 | 133 | (let ((m ($ffi-values-alloc size))) |
135 | 134 | (set! $ffi-alloc-list(append! $ffi-alloc-list (list m) )) |
136 | 135 | m |
137 | 136 | ) |
138 | 137 | ) |
139 | | - (define (ffi-alloc size) |
| 138 | + (define (ffi-alloc $ffi-alloc-list size) |
140 | 139 | (let ((m ($ffi-alloc size))) |
141 | 140 | ;;(display (format "$ffi-alloc addr=~x\n" m)) |
142 | 141 | (set! $ffi-alloc-list(append! $ffi-alloc-list (list m) )) |
143 | 142 | m |
144 | 143 | ) |
145 | 144 | ) |
146 | 145 |
|
147 | | - (define (ffi-free-all) |
| 146 | + (define (ffi-free-all $ffi-alloc-list) |
148 | 147 | ;;(display (format "ffi-free-all=~a\n" (length $ffi-alloc-list))) |
149 | 148 | (let loop ((l $ffi-alloc-list)) |
150 | 149 | (if (pair? l) |
|
155 | 154 | ) |
156 | 155 | ) |
157 | 156 | ) |
| 157 | + ;;(lock-object $ffi-alloc-list) |
158 | 158 | (set! $ffi-alloc-list '() ) |
159 | 159 | ) |
160 | 160 |
|
|
448 | 448 | (let loop ((h handlers)) |
449 | 449 | (if (pair? h) |
450 | 450 | (let ((sym (ffi-dlsym (car h) name))) |
451 | | - (printf " sym=~a handler=~a name=~a\n" sym (car h ) name) |
| 451 | + ;;(printf " sym=~a handler=~a name=~a\n" sym (car h ) name) |
452 | 452 | (if (= sym 0) |
453 | 453 | (loop (cdr h)) |
454 | 454 | sym)) |
|
522 | 522 | #t |
523 | 523 | ) |
524 | 524 | ;; |
525 | | - (define (create-carg-type arg-type) |
| 525 | + (define (create-carg-type alloc-list arg-type ) |
526 | 526 | ;;(display (format "creat-carg-type arg-type=~a len=~a \n" arg-type (length arg-type) )) |
527 | | - (let ((carg-type (ffi-types-alloc (length arg-type) ))) |
| 527 | + (let ((carg-type (ffi-types-alloc alloc-list (length arg-type) ))) |
528 | 528 | (let loop ((type arg-type) (i 0)) |
529 | 529 | (if (pair? type) |
530 | 530 | (begin |
531 | 531 | ;;(display (format " type=~a index=~a \n" (car type) i )) |
532 | | - (ffi-types-set carg-type i (create-cret-type (car type)) ) |
| 532 | + (ffi-types-set carg-type i (create-cret-type alloc-list (car type)) ) |
533 | 533 |
|
534 | 534 | (loop (cdr type) (+ i 1) ) |
535 | 535 | ) |
|
539 | 539 | carg-type |
540 | 540 | ) |
541 | 541 | ) |
| 542 | + |
542 | 543 |
|
543 | | - (define (process-struct ret-type) |
| 544 | + (define (process-struct alloc-list ret-type) |
544 | 545 | (let ((ret-struct-val '()) |
545 | 546 | (typelist '() ) |
546 | 547 | (typeelement '() ) |
547 | 548 | (alloc 0) |
548 | 549 | ) |
549 | | - (set! alloc (ffi-alloc (+ 64 16 16 64))) |
| 550 | + (set! alloc (ffi-alloc alloc-list (+ 64 16 16 64))) |
550 | 551 | (set! ret-struct-val ((top-level-value |
551 | 552 | (string->symbol (format "make-~a" ret-type ) )) )) |
552 | 553 | (set! typelist (struct-ref ret-struct-val 0) ) |
553 | | - (set! typeelement (ffi-alloc (* 64 (+ (length typelist ) 1) ) )) |
| 554 | + (set! typeelement (ffi-alloc alloc-list (* 64 (+ (length typelist ) 1) ) )) |
554 | 555 | (ffi-init-struct alloc 0 0 FFI_TYPE_STRUCT typeelement) |
555 | 556 | (let loop ((type typelist) (i 0)) |
556 | 557 | (if (pair? type) |
|
571 | 572 | [(string ) (ffi-types-set typeelement i ffi-type-pointer ) ] |
572 | 573 | [else |
573 | 574 | ;;(display (format " ###$$$else type=~a\n" (car type) ) ) |
574 | | - (ffi-types-set typeelement i (process-struct (car type) ) ) |
| 575 | + (ffi-types-set typeelement i (process-struct alloc-list (car type) ) ) |
575 | 576 | ] |
576 | 577 | ) |
577 | 578 | (loop (cdr type) (+ i 1) ) |
|
584 | 585 |
|
585 | 586 | ) |
586 | 587 |
|
587 | | - (define (create-cret-type ret-type) |
| 588 | + (define (create-cret-type alloc-list ret-type) |
588 | 589 | (let ((alloc 0) (ret-struct-val 0) (typeelement 0) (typelist '() ) ) |
589 | 590 | (case ret-type |
590 | 591 | [(short ) ffi-type-sint16] |
|
599 | 600 | [(void) ffi-type-void ] |
600 | 601 | [else |
601 | 602 | ;;(display (format " $$$else type=~a\n" ret-type) ) |
602 | | - (process-struct ret-type ) |
| 603 | + (process-struct alloc-list ret-type ) |
603 | 604 | ] |
604 | 605 | ))) |
605 | 606 |
|
606 | | - (define (create-cret ret-type) |
| 607 | + (define (create-cret alloc-list ret-type) |
607 | 608 | ;;(display (format "ret-type-size=~a\n" (cffi-size ret-type) )) |
608 | 609 | (let ( (ret-fun (lambda (x) x) ) |
609 | 610 | (ret-type-s ret-type ) |
|
630 | 631 | ) |
631 | 632 | ] |
632 | 633 | ) ) |
633 | | - (list (ffi-alloc (cffi-size ret-type-s) ) ret-fun ) |
| 634 | + (list (ffi-alloc alloc-list (cffi-size ret-type-s) ) ret-fun ) |
634 | 635 |
|
635 | 636 | ) |
636 | 637 | ) |
637 | 638 |
|
638 | | - (define (create-cargs arg-type args carg-type) |
| 639 | + (define (create-cargs alloc-list arg-type args carg-type) |
639 | 640 | ;;(display (format "creat-cargs args=~a len=~a carg-type=~a\n" args (length args) carg-type)) |
640 | | - (let ((cargs (ffi-values-alloc (length args))) |
| 641 | + (let ((cargs (ffi-values-alloc alloc-list (length args))) |
641 | 642 | (alloc 0) |
642 | 643 | ) |
643 | 644 | (let loop ((arg args) (type arg-type) (i 0)) |
|
646 | 647 | ;;(display (format " type=~a value=~a index=~a \n" (car type) (car arg) i )) |
647 | 648 | (case (car type) |
648 | 649 | [(ushort) |
649 | | - (set! alloc (ffi-alloc 16) ) |
| 650 | + (set! alloc (ffi-alloc alloc-list 16) ) |
650 | 651 | (ffi-set-ushort alloc (car arg) ) |
651 | 652 | (ffi-values-set cargs i alloc) ] |
652 | 653 | [(short) |
653 | | - (set! alloc (ffi-alloc 16) ) |
| 654 | + (set! alloc (ffi-alloc alloc-list 16) ) |
654 | 655 | (ffi-set-short alloc (car arg) ) |
655 | 656 | (ffi-values-set cargs i alloc) ] |
656 | 657 | [(uint) |
657 | | - (set! alloc (ffi-alloc 32) ) |
| 658 | + (set! alloc (ffi-alloc alloc-list 32) ) |
658 | 659 | (ffi-set-uint alloc (car arg) ) |
659 | 660 | (ffi-values-set cargs i alloc) ] |
660 | 661 | [(int) |
661 | | - (set! alloc (ffi-alloc 32) ) |
| 662 | + (set! alloc (ffi-alloc alloc-list 32) ) |
662 | 663 | (ffi-set-int alloc (car arg) ) |
663 | 664 | (ffi-values-set cargs i alloc) ] |
664 | 665 | [(int64 long) |
665 | | - (set! alloc (ffi-alloc 64) ) |
| 666 | + (set! alloc (ffi-alloc alloc-list 64) ) |
666 | 667 | (ffi-set-long alloc (car arg)) |
667 | 668 | (ffi-values-set cargs i alloc) ] |
668 | 669 | [(float) |
669 | | - (set! alloc (ffi-alloc 32) ) |
| 670 | + (set! alloc (ffi-alloc alloc-list 32) ) |
670 | 671 | (ffi-set-float alloc (+ 0.0 (car arg) )) |
671 | 672 | (ffi-values-set cargs i alloc) ] |
672 | 673 | [(double) |
673 | | - (set! alloc (ffi-alloc 64) ) |
| 674 | + (set! alloc (ffi-alloc alloc-list 64) ) |
674 | 675 | (ffi-set-double alloc (+ 0.0 (car arg))) |
675 | 676 | (ffi-values-set cargs i alloc) ] |
676 | 677 | [(void) |
|
680 | 681 | 1 |
681 | 682 | ] |
682 | 683 | [(string ) |
683 | | - (set! alloc (ffi-alloc 64) ) |
| 684 | + (set! alloc (ffi-alloc alloc-list 64) ) |
684 | 685 | (if (number? (car arg)) |
685 | 686 | (ffi-set-pointer alloc (car arg)) |
686 | 687 | (ffi-set-string alloc (car arg)) |
687 | 688 | ) |
688 | 689 | (ffi-values-set cargs i alloc) ] |
689 | 690 | [(void* ) |
690 | | - (set! alloc (ffi-alloc 64) ) |
| 691 | + (set! alloc (ffi-alloc alloc-list 64) ) |
691 | 692 | (if (string? (car arg)) |
692 | 693 | (ffi-set-string alloc (car arg)) |
693 | 694 | (ffi-set-pointer alloc (car arg))) |
|
696 | 697 | (ffi-values-set cargs i alloc) ] |
697 | 698 | [else |
698 | 699 | ;(display (format " %%%else type=~a size=~a \n" (car type) (cffi-size (struct-ref (car arg) 1) ) ) ) |
699 | | - (set! alloc (ffi-alloc (cffi-size (struct-ref (car arg) 1) )) ) |
| 700 | + (set! alloc (ffi-alloc alloc-list (cffi-size (struct-ref (car arg) 1) )) ) |
700 | 701 | (lisp2struct (car arg) alloc) |
701 | 702 | (ffi-values-set cargs i alloc) |
702 | 703 | ] |
|
719 | 720 | (begin |
720 | 721 | (display "\n")(display (format "cffi-call ~a arg-type=~a ret-type=~a args=~a \n" sym arg-type ret-type args) ) ) ) |
721 | 722 | (let* ( |
722 | | - (carg-type (create-carg-type arg-type) ) |
723 | | - (cret-type (create-cret-type ret-type) ) |
724 | | - (cargs (create-cargs arg-type args carg-type) ) |
| 723 | + (alloc-list '()) |
| 724 | + (carg-type (create-carg-type alloc-list arg-type) ) |
| 725 | + (cret-type (create-cret-type alloc-list ret-type) ) |
| 726 | + (cargs (create-cargs alloc-list arg-type args carg-type) ) |
725 | 727 |
|
726 | | - (cret-info (create-cret ret-type) ) |
| 728 | + (cret-info (create-cret alloc-list ret-type) ) |
727 | 729 | (cret '() ) |
728 | | - (cif (ffi-cif-alloc) ) |
| 730 | + (cif (ffi-cif-alloc alloc-list) ) |
729 | 731 | (fptr (cffi-sym sym )) |
730 | 732 | (call-ret '() ) |
731 | 733 | (ret-val '() ) |
|
753 | 755 | (if (procedure? call-ret) |
754 | 756 | (set! ret-val (call-ret cret)) |
755 | 757 | ) |
756 | | - (ffi-free-all) |
| 758 | + (ffi-free-all alloc-list) |
757 | 759 | (if cffi-enable-log |
758 | 760 | (display (format "ffi-call ret=~x\n" ret-val))) |
759 | 761 | ret-val |
|
0 commit comments