Paste: ffi question

Author: rien
Mode: factor
Date: Sun, 16 Oct 2011 15:32:49
Plain Text |
! PCRE_EXP_DECL pcre *pcre_compile(const char *, int, const char **, int *,
!                   const unsigned char *);
FUNCTION: pcre* pcre_compile
    ( c-string pattern,
      int options,
      c-string* errptr,
      int* erroffset,
      uchar* tableptr ) ;

: pcre-compile ( pat opt errp erro tablep -- pcre* )
    pointer: pcre "libpcre" "pcre_compile"
    { c-string
      int
      pointer: c-string
      pointer: int
      pointer: uchar }
    alien-invoke ;

Now how do I write this?:

PCRE_EXP_DECL void *(*pcre_malloc)(size_t);

Annotation: how to invoke pcre_free

Author: blei
Mode: factor
Date: Sun, 16 Oct 2011 16:17:37
Plain Text |
CALLBACK: void pcre_free_t ( void* ptr ) ;

C-GLOBAL: pcre_free_t pcre_free

: pcre-free ( ptr -- )
    pcre_free \ void
    { pointer: void }
    cdecl
    alien-indirect ;

Annotation: thanks

Author: rien
Mode: factor
Date: Wed, 19 Oct 2011 04:10:37
Plain Text |
! thank you, Blei. I'm not sure why it wasn't working before
! when I tried it while talking to you, but now it works fine.
! here is what I'm using now. I had tried this same thing
! when you showed me your code (adapting \ void to void
! and pointer: void to void*) but I was still crashing my
! instance of Factor. must've been code gremlins.
! Either way, thanks, and here goes what I have now.

CALLBACK: void* pcre_malloc_t ( size_t size ) ;
C-GLOBAL: pcre_malloc_t pcre_malloc
: pcre-malloc ( size -- ptr )
    pcre_malloc void* { size_t } cdecl alien-indirect ;

CALLBACK: void pcre_free_t ( void* ptr ) ;
C-GLOBAL: pcre_free_t pcre_free
: pcre-free ( ptr -- )
    pcre_free void { void* } cdecl alien-indirect ;
DESTRUCTOR: pcre-free

! now this works:
[ "some pattern" new-regex &pcre-free ] with-destructors
! returns a now-invalid (just-freed) pointer, which is correct.

Annotation: and now for something completely different

Author: rien
Mode: factor
Date: Wed, 19 Oct 2011 06:19:08
Plain Text |
! here's the code that redefines pcre_malloc and pcre_free to
! use byte-arrays. this code is tested to work. I was in a
! brute-force trial-and-error frenzy trying all combinations
! of the alien functions to make this work, and couldn't.
! the suggestions I was offered at the irc channel also
! wouldn't work. so I got more rational and started thinking
! about how all the alien functions can work together.
! the result is a bit more repetitive than what I'd like to
! write to make this work, but it works and most importantly
! it makes perfect sense in my head and that of anyone who
! reads the docs for CALLBACK: C-GLOBAL: alien-callback and
! alien-indirect . No example in the docs mention this
! construct and I could not find an existing library that
! does this. maybe this is a first!

CALLBACK: void* pcre_malloc_t ( size_t size ) ;
C-GLOBAL: pcre_malloc_t pcre_malloc
: setup-pcre_malloc ( -- )
    void* { size_t } cdecl [ <byte-array> ] alien-callback set-pcre_malloc ;
setup-pcre_malloc ! set it up once
: pcre-malloc ( size -- ptr )
    pcre_malloc void* { size_t } cdecl alien-indirect ;

CALLBACK: void pcre_free_t ( void* ptr ) ;
C-GLOBAL: pcre_free_t pcre_free
: setup-pcre_free ( -- )
    void { void* } cdecl [ drop ] alien-callback set-pcre_free ;
setup-pcre_free ! set it up once
: pcre-free ( ptr -- )
    pcre_free void { void* } cdecl alien-indirect ;


! There are two ways to implicitly check that this is works.
! one is that calling (free) on what is returned from
! pcre-malloc crashes Factor in the same way that
! B{ 0 0 0 } (free) crashes it.
! the other way to verify it is by looking at the address
! of the returned alien. The code in the previous
! annotation always returns an alien like this:
! ALIEN: 7fdd8b0c80c0
! while this code returns an alien like this:
! ALIEN: 110b1b570
! which to me seem like two very different address spaces,
! and the latter I suspect is Factor's address space,
! although I couldn't get >c-ptr to work in order to verify
! this.
! this should be included in the docs somehow, as it took me
! a while to figure out and no-one in the IRC channel knew
! exactly how to go about it.

Annotation: but are pcre regexes relocatable?

Author: rien
Mode: factor
Date: Wed, 19 Oct 2011 06:24:03
Plain Text |
! and to answer the question you have in your head,
! here's an excerpt from man pcreapi:

Although  the  compiled  code  of  a PCRE regex is relocatable, that is, it does not depend on memory location, the complete pcre data block is not fully
       relocatable, because it may contain a copy of the tableptr argument, which is an address (see below).

! most pcre code I've seen so far always pass a null pointer
! for the tableptr argument. if that's indeed the usual
! way to do it then we may have a clear winner here.

New Annotation

Summary:
Author:
Mode:
Body: