| Good Afternoon Ladies and Gents, I was wondering if you could help me in using the Racket ffi to define a C structure and functions that operate on it. What is the correct process to define and use the structure? This is what I have so far: I have create a header and c file, below i've only included the header. #ifndef STACKARRAY_H
#define STACKARRAY_H
#define MAX 10
typedef struct _stackarray{
int array[MAX];
int size;
} StackArray;
void pushStackArray(StackArray *, int );
int popStackArray(StackArray *);
int fullStackArray(StackArray *);
int emptyStackArray(StackArray *);
StackArray * newStackArray();
void displayStackArray(StackArray *);
void delete(StackArray *);
#endif
To create the shared library file libStackArray.so I compiled with gcc:
gcc -c -fPIC StackArray.c
gcc -g -Wall -shared -o libStackArray.so StackArray.o
Using anarki and the racket ffi should I write:
($:require ffi/unsafe
ffi/unsafe/define
ffi/unsafe/alloc)
($:define-ffi-definer define-StackArray (ffi-lib "/path/libStackArray"))
($:define-StackArray newStackArray (_fun -> _pointer ))
($:define-StackArray delete (_fun _pointer -> _void ))
($:define-StackArray pushStackArray (_fun _pointer _int -> _void))
($:define-StackArray popStackArray (_fun _pointer -> _int))
($:define-StackArray fullStackArray (_fun _pointer -> _int))
($:define-StackArray emptyStackArray (_fun _pointer -> _int))
($:define-StackArray displayStackArray (_fun _pointer -> _void))
Then I would like to clean them up abit with macros in anarki:
(mac newSA()
`($:newStackArray))
(mac deleteSA(stack)
`($:delete ,stack))
(mac pushSA(stack x)
`($:pushStackArray ,stack ,x))
(mac popSA(stack)
`($:popStackArray ,stack))
(mac fullSA?(stack)
`($:fullStackArray ,stack))
(mac emptySA?(stack)
`($:emptyStackArray ,stack))
(mac displaySA(stack)
`($:displayStackArray ,stack))
To use my StackArray I would like to write:
(= stack (newSA))
(pushSA stack 1)
(pushSA stack 2)
(fullSA? stack)
(emptySA? stack)
(displaySA stack)
(popSA stack)
(deleteSA stack)
|