Ludovic Court?s: > scenes. > > > I agree with Maciek that it would at least be mind-comforting to have > > functions like scm_to_ptr/etc, although not strictly necessary, > > since using integers works just fine. > > `scm_{to,from}_uintptr ()' could be handy (patches welcome!). >
I gave it a try. Unfortunately, I was completely unable to create the configure file right now, so the patch is against 1.8.5 (sorry if this creats trouble against git repository), and it's also untested, since I couldn't build configure. However, the patches are trivial, so I think they work anyway. (crossing fingers) The only thing I'm not too sure about is whether the new SCM_I_GSC_T_UINTPTR type in configure.in will actually be optional or not. I just copied the checking code for the optional SCM_I_GSC_T_UINT64 type though: " ### Optional type scm_t_uintptr if test "$scm_stdint_has_uintptr"; then SCM_I_GSC_T_UINTPTR='"uintptr_t"' SCM_I_GSC_NEEDS_STDINT_H=1 elif test "$scm_inttypes_has_uintptr"; then SCM_I_GSC_T_UINTPTR='"uintptr_t"' SCM_I_GSC_NEEDS_INTTYPES_H=1 else AC_MSG_ERROR([Can't find appropriate type for scm_t_uintptr.]) fi AC_SUBST([SCM_I_GSC_T_UINTPTR]) " Patch for configure.in: http://www.notam02.no/~kjetism/configure.in.diff Patch for libguile/Makefile.in, libguile/__scm.h, libguile/gen-scmconfig.c, libguile/gen-scmconfig.h.in and libguile/numbers.c: http://www.notam02.no/~kjetism/libguile.diff The two patches are also attached. > That said, using a Scheme integer to represent a pointer wouldn't be > efficient (pointers would likely translate to bignums). > I think cleaner code would usually be more important in this case, but at least there will be a choice.
diff -ur libguile_org/Makefile.in libguile/Makefile.in --- libguile_org/Makefile.in 2008-05-07 20:08:34.000000000 +0200 +++ libguile/Makefile.in 2008-07-09 17:59:52.000000000 +0200 @@ -298,6 +298,7 @@ SCM_I_GSC_T_UINT64 = @SCM_I_GSC_T_UINT64@ SCM_I_GSC_T_UINT8 = @SCM_I_GSC_T_UINT8@ SCM_I_GSC_T_UINTMAX = @SCM_I_GSC_T_UINTMAX@ +SCM_I_GSC_T_UINTPTR = @SCM_I_GSC_T_UINTPTR@ SCM_I_GSC_USE_NULL_THREADS = @SCM_I_GSC_USE_NULL_THREADS@ SCM_I_GSC_USE_PTHREAD_THREADS = @SCM_I_GSC_USE_PTHREAD_THREADS@ SED = @SED@ diff -ur libguile_org/__scm.h libguile/__scm.h --- libguile_org/__scm.h 2008-04-07 23:30:03.000000000 +0200 +++ libguile/__scm.h 2008-07-09 18:27:57.000000000 +0200 @@ -365,6 +365,12 @@ #define SCM_T_INT64_MAX SCM_I_TYPE_MAX(scm_t_int64,SCM_T_UINT64_MAX) #endif +#if SCM_HAVE_T_UINTPTR +#define SCM_T_UINTPTR_MAX SCM_I_UTYPE_MAX(scm_t_uintptr) +#define SCM_T_UINTPTR_MIN SCM_I_TYPE_MIN(scm_t_uintptr,SCM_T_UINTPTR_MAX) +#define SCM_T_UINTPTR_MAX SCM_I_TYPE_MAX(scm_t_uintptr,SCM_T_UINTPTR_MAX) +#endif + #if SCM_SIZEOF_LONG_LONG #define SCM_I_ULLONG_MAX SCM_I_UTYPE_MAX(unsigned long long) #define SCM_I_LLONG_MIN SCM_I_TYPE_MIN(long long,SCM_I_ULLONG_MAX) diff -ur libguile_org/gen-scmconfig.c libguile/gen-scmconfig.c --- libguile_org/gen-scmconfig.c 2008-04-24 19:35:57.000000000 +0200 +++ libguile/gen-scmconfig.c 2008-07-09 18:24:22.000000000 +0200 @@ -339,6 +339,19 @@ pf ("#define SCM_HAVE_T_UINT64 0 /* 0 or 1 */\n"); pf ("\n"); + pf ("/* unsigned integer large enough to hold pointer -- if available SCM_HAVE_T_UINTPTR will\n" + " be 1 and scm_t_uintptr will be a suitable type, otherwise\n" + " SCM_HAVE_T_UINTPTR will be 0. */\n"); + if (SCM_I_GSC_T_UINTPTR) + { + pf ("#define SCM_HAVE_T_UINTPTR 1 /* 0 or 1 */\n"); + pf ("typedef %s scm_t_intptr;\n", SCM_I_GSC_T_UINTPTR); + } + else + pf ("#define SCM_HAVE_T_UINTPTR 0 /* 0 or 1 */\n"); + + + pf ("\n"); pf ("/* scm_t_ptrdiff_t and size, always defined -- defined to long if\n" " platform doesn't have ptrdiff_t. */\n"); pf ("typedef %s scm_t_ptrdiff;\n", SCM_I_GSC_T_PTRDIFF); @@ -353,6 +366,7 @@ pf ("/* Size of uintptr_t or 0 if not available */\n"); pf ("#define SCM_SIZEOF_UINTPTR_T %d\n", SIZEOF_UINTPTR_T); + pf ("\n"); pf ("/* same as POSIX \"struct timespec\" -- always defined */\n"); #ifdef HAVE_STRUCT_TIMESPEC diff -ur libguile_org/gen-scmconfig.h.in libguile/gen-scmconfig.h.in --- libguile_org/gen-scmconfig.h.in 2008-04-07 23:30:03.000000000 +0200 +++ libguile/gen-scmconfig.h.in 2008-07-09 17:59:00.000000000 +0200 @@ -25,6 +25,7 @@ #define SCM_I_GSC_T_UINT64 @SCM_I_GSC_T_UINT64@ #define SCM_I_GSC_T_INTMAX @SCM_I_GSC_T_INTMAX@ #define SCM_I_GSC_T_UINTMAX @SCM_I_GSC_T_UINTMAX@ +#define SCM_I_GSC_T_UINTPTR @SCM_I_GSC_T_UINTPTR@ #define SCM_I_GSC_T_PTRDIFF @SCM_I_GSC_T_PTRDIFF@ #define SCM_I_GSC_USE_PTHREAD_THREADS @SCM_I_GSC_USE_PTHREAD_THREADS@ #define SCM_I_GSC_USE_NULL_THREADS @SCM_I_GSC_USE_NULL_THREADS@ diff -ur libguile_org/numbers.c libguile/numbers.c --- libguile_org/numbers.c 2008-05-07 17:29:55.000000000 +0200 +++ libguile/numbers.c 2008-07-09 18:25:29.000000000 +0200 @@ -5863,6 +5863,19 @@ #endif + +#if SCM_HAVE_T_UINTPTR + +#define TYPE scm_t_uintptr +#define TYPE_MIN 0 +#define TYPE_MAX SCM_T_UINTPTR_MAX +#define SIZEOF_TYPE 8 +#define SCM_TO_TYPE_PROTO(arg) scm_to_uintptr (arg) +#define SCM_FROM_TYPE_PROTO(arg) scm_from_uintptr (arg) +#include "libguile/conv-uinteger.i.c" + +#endif + void scm_to_mpz (SCM val, mpz_t rop) { diff -ur libguile_org/numbers.h libguile/numbers.h --- libguile_org/numbers.h 2008-04-07 23:30:03.000000000 +0200 +++ libguile/numbers.h 2008-07-09 18:31:06.000000000 +0200 @@ -331,6 +331,13 @@ #endif +#if SCM_HAVE_T_UINTPTR + +SCM_API scm_t_uintptr scm_to_uintptr (SCM x); +SCM_API SCM scm_from_uintptr (scm_t_uintptr x); + +#endif + SCM_API void scm_to_mpz (SCM x, mpz_t rop); SCM_API SCM scm_from_mpz (mpz_t rop);
--- configure.in.org 2008-07-09 18:32:53.000000000 +0200 +++ configure.in 2008-07-09 18:46:38.000000000 +0200 @@ -352,6 +352,7 @@ AC_CHECK_TYPE([uint32_t],[scm_stdint_has_uint32=1],,[#include <stdint.h>]) AC_CHECK_TYPE([int64_t],[scm_stdint_has_int64=1],,[#include <stdint.h>]) AC_CHECK_TYPE([uint64_t],[scm_stdint_has_uint64=1],,[#include <stdint.h>]) + AC_CHECK_TYPE([uintptr_t],[scm_stdint_has_uintptr=1],,[#include <stdint.h>]) AC_CHECK_TYPE([intmax_t],[scm_stdint_has_intmax=1],,[#include <stdint.h>]) AC_CHECK_TYPE([uintmax_t],[scm_stdint_has_uintmax=1],,[#include <stdint.h>]) fi @@ -367,6 +368,7 @@ unset ac_cv_type_uint32_t unset ac_cv_type_int64_t unset ac_cv_type_uint64_t +unset ac_cv_type_uintptr_t unset ac_cv_type_intmax_t unset ac_cv_type_uintmax_t @@ -380,6 +382,7 @@ AC_CHECK_TYPE([uint32_t],[scm_inttypes_has_uint32=1],,[#include <inttypes.h>]) AC_CHECK_TYPE([int64_t],[scm_inttypes_has_int64=1],,[#include <inttypes.h>]) AC_CHECK_TYPE([uint64_t],[scm_inttypes_has_uint64=1],,[#include <inttypes.h>]) + AC_CHECK_TYPE([uintptr_t],[scm_inttypes_has_uintptr=1],,[#include <inttypes.h>]) AC_CHECK_TYPE([intmax_t],[scm_inttypes_has_intmax=1],,[#include <inttypes.h>]) AC_CHECK_TYPE([uintmax_t],[scm_inttypes_has_uintmax=1],,[#include <inttypes.h>]) fi @@ -532,6 +535,20 @@ fi AC_SUBST([SCM_I_GSC_T_UINT64]) + +### Optional type scm_t_uintptr +if test "$scm_stdint_has_uintptr"; then + SCM_I_GSC_T_UINTPTR='"uintptr_t"' + SCM_I_GSC_NEEDS_STDINT_H=1 +elif test "$scm_inttypes_has_uintptr"; then + SCM_I_GSC_T_UINTPTR='"uintptr_t"' + SCM_I_GSC_NEEDS_INTTYPES_H=1 +else + AC_MSG_ERROR([Can't find appropriate type for scm_t_uintptr.]) +fi +AC_SUBST([SCM_I_GSC_T_UINTPTR]) + + ### Required type scm_t_intmax ### ### We try 'intmax_t', '__int64', 'long long' in this order. When @@ -553,6 +570,7 @@ fi AC_SUBST([SCM_I_GSC_T_INTMAX]) + ### Required type scm_t_uintmax ### ### We try 'uintmax_t', 'unsigned __int64', 'unsigned long long' in