[plt-scheme] Re: large blocks inside cstructs

From: Dave Herman (dherman at ccs.neu.edu)
Date: Mon Dec 18 00:33:54 EST 2006

>> How should I do this?  Other than putting 260 / 4 dummy _int32 slots
>> in a row, I mean. :)
> There's no good solution for that, sorry.  One way would be to
> generate dummy slots, which would be a hassle to generate, but will
> allow you to address slots conveniently.  Another way is to just use
> malloc directly, which is a different kind of a hassle.

Okay, well then I guess this is better than nothing:

   (require-for-syntax (lib "etc.ss"))
   (define-syntax (define-inline-cstruct stx)
     (syntax-case stx ()
       [(_ typename elttype len)
        (and (identifier? #'typename) (number? (syntax-object->datum 
        (with-syntax ([(fields ...)
                       (build-list (syntax-object->datum #'len)
                                   (lambda (i)
                                     (let ([name (string->symbol (format 
"Chunk~a" (add1 i)))])
                                       #`[#,name elttype])))])
          #'(define-cstruct typename (fields ...)))]))

With this you can create a separate ctype for each inline field, e.g. 
for my previous example:

   (define-inline-cstruct _ADAPTER_NAME _byte 260)
   (define-inline-cstruct _ADAPTER_DESCRIPTION _byte 132)
   (define-cstruct _IP_ADAPTER_INFO (...
                                     [AdapterName _ADAPTER_NAME]
                                     [Description _ADAPTER_DESCRIPTION]

A serious down-side is the enormous code-bloat from generating a 
separate field for every single byte in the array. :(

I'm considering creating a small fixed prefix of the sequence of blocks 
of size 2^n bytes (say, from _byte up to _kilobyte) and use the largest 
chunks possible in the implementation of define-inline-cstruct.


Posted on the users mailing list.