Hello,
Bruce Butterfield bruce@open-tek.com writes:
Still having a problem and I think it's related to the allocation of the buffer. Here is C code that works fine:
Yes, sort of.
(BTW, I'm running SBCL 0.9.5, cffi-luis-051002-1333, on Ubuntu Hoary/x86)
================================= typedef struct { unsigned char* bp; unsigned int size; unsigned int allocated; unsigned int next; } TidyBuffer;
typedef void* TidyDoc;
TidyDoc tidyCreate(void); int tidyParseString(TidyDoc tdoc, const char* str); int tidySaveBuffer(TidyDoc tdoc, TidyBuffer* buf);
int main(int argc, char **argv ) { const char* input = "<title>Foo</title><p>Foo!"; TidyBuffer output = {0};
The equivalent lisp code to this assignment was missing:
(loop for i below (cffi:foreign-type-size 'tidy-buffer) do (setf (mem-ref output :char i) 0))
TidyDoc tdoc = tidyCreate(); tidyParseString( tdoc, input ); // Parse the input tidySaveBuffer( tdoc, &output ); // Pretty Print
printf( "\n%s\n", output.bp ); }
[snip]
when it gets to the call to tidy-save-buffer I get a stack trace:
memory fault [Condition of type SB-KERNEL::MEMORY-FAULT-ERROR]
Restarts: 0: [ABORT] Abort SLIME compilation. 1: [ABORT-REQUEST] Abort handling SLIME request. 2: [TERMINATE-THREAD] Terminate this thread (#<THREAD "worker" {9E2F069}>)
Backtrace: 0: (SB-KERNEL::MEMORY-FAULT-ERROR) 1: ("foreign function: call_into_lisp") 2: ("foreign function: post_signal_tramp") 3: ("foreign function: tidyBufPutByte")
^^^^^^^^^^^^^^ And somewhere around there a random pointer was being realloc()'ed. (or something like that)
4: ("foreign function: #xB5EF69F7") 5: ("foreign function: tidyPutByte") 6: ("foreign function: #xB5EF8359") 7: ("foreign function: WriteChar") 8: ("foreign function: PFlushLine") 9: ("foreign function: PPrintTree") 10: ("foreign function: PPrintTree") 11: ("foreign function: tidyDocSaveStream") 12: ("foreign function: tidyDocSaveBuffer") 13: ("foreign function: tidySaveBuffer") 14: (TIDY-SAVE-BUFFER #.(SB-SYS:INT-SAP #X0827B590) #.(SB-SYS:INT-SAP #XB5A3DFEC)) 15: (TIDY-STRING "<title>Foo</title><p>Foo!") 16: ((SB-PCL::FAST-METHOD SWANK-BACKEND:SWANK-COMPILE-STRING (T)) #<unused argument> #<unused argument> "(tidy-string "<title>Foo</title><p>Foo!") " (:BUFFER "tinytidy.lisp" :POSITION 624 :DIRECTORY "/home/bruce/src/cl/"))
HTH