additional C api functions

Started by ryuo, May 24, 2014, 02:17:32 PM

Previous topic - Next topic

ryuo

I find there is a lack of C API functions for dereferencing non-aggregate pointer types. There already exist enough of them to write workarounds for the missing functionality, but I think these should be builtin so we don't have to resort to such hacks to get a basic feature. What I think would good to add is:



1) "get-pointer" which dereferences a pointer to a pointer, thus returning the address its argument points to.



2) An integer constant which is equal to "sizeof(void *)" for the platform the interpreter is running on.



3) "get-short" which dereferences a pointer to a short int, thus returning the 16 bit integer its argument points to.



4) An unsigned variant of all the functions that currently dereference a pointer to a signed integer.



These all add features which I think are essential to having the ability to interface with more C libraries. I also noticed the interface only has a function for derefencing double-precision floating point. While I don't often see floating point used, especially pointers to floating point, I would think for completeness that there should also be functions to handle pointers to "float" and "long double". Thank you.

Lutz

#1
As long as these are not available you could use unpack for all signed and unsigned types. E.g. for a definition of get-short:

> (define (get-short addr) (first (unpack "u" addr)))
(lambda (addr) (first (unpack "u" addr)))
> (get-short (pack "u" 12345))
12345
>

pack can take a list and unpack always returns a list and can take a raw number as an address or a string buffer. Both are used frequently for structure packing and unpacking structures from pointer references.



And a short expression for pointer size:
(if ((bits (sys-info -1) true) 8) 64 32)