[Openmcl-devel] Allocate heap and call C question

Andrew P. Lentvorski, Jr. bsder at mail.allcaps.org
Mon Jul 5 00:55:22 PDT 2004


I have been trying to allocate a chunk of memory from the heap rather 
than the stack (I would eventually like to move a vector around of 
reasonable size) and pass it back and forth to C.

What I would like to do is:

CL-USER> (setq a (make-array 4 :element-type :signed-long))
#(0 0 0 0)
CL-USER> (setq amp (unknown-function-to-create-a-macptr-to-memory a))
#<A Mac Pointer #x641DE66>
;; Put long int values 0-3 into memory chunk
CL-USER> (set-mem amp)
0
1
2
3

CL-USER> (external-call "_ip_ip_test" :address ap :address)
Entered ip_ip_test:
Data In: 0x641DE66
C:I:0 *(p+i):0
C:I:1 *(p+i):1
C:I:2 *(p+i):2
C:I:3 *(p+i):3
Exited  ip_ip_test:
#<A Mac Pointer #x641DE66>

What I currently have and get is:

Welcome to OpenMCL Version (Beta: Darwin) 0.14.2-040506!
? ;; Set the memory elements consecutively
(defun set-mem (a)
   (let ((alen 4))
     (do ((i 0 (+ i 1)))
         ((= i alen) nil)
       (setf (%get-signed-long a i) i)
       (format t "~A~%" (%get-signed-long a i)))))
SET-MEM
? ;; Dump the memory elements out afterward
(defun print-mem (a)
   (let ((alen 4))
     (do ((i 0 (+ i 1)))
         ((= i alen) nil)
       (format t "~A~%" (%get-signed-long a i)))))
PRINT-MEM
? ;; Overallocate in case this is bytes rather than objects
(setq am (ccl::%alloc-misc 16 :signed-long))
#<BOGUS object @ #x63886E6>
? ;; Stuff the memory address into a macptr
(setq ap (%int-to-ptr (ccl::%address-of am)))
#<A Mac Pointer #x63886E6>
? (open-shared-library 
"/Users/andrewl/openmcl/openmcl/gtk/libptrtest.dylib")
#<SHLIB /Users/andrewl/openmcl/openmcl/gtk/libptrtest.dylib #x638849E>
? (set-mem ap)
0
1
2
3
NIL
? (external-call "_ip_ip_test" :address ap :address)
Entered ip_ip_test:
Data In: 0x63886e6
C:I:0 *(p+i):0
C:I:1 *(p+i):811
C:I:2 *(p+i):1271793208
C:I:3 *(p+i):-2031550464
Exited  ip_ip_test:
#<A Mac Pointer #x63886E6>
? (print-mem ap)
0
0
0
3
NIL

What do I need to do in order to make this work?

Thanks,
-a


The external C program is included here:

#include <stdio.h>

int *
ip_ip_test(int * data)
{
   int i;

   printf("Entered %s:\n", __FUNCTION__);
   printf("Data In: %p\n", data);

   for(i=0; i<4; i++)
     {
       printf("C:I:%d *(p+i):%d\n", i, *(data+i));
     }

   printf("Exited  %s:\n", __FUNCTION__);
   return data;
}




More information about the Openmcl-devel mailing list