chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Memory mapped I/O and CHICKEN ports


From: felix winkelmann
Subject: Re: [Chicken-users] Memory mapped I/O and CHICKEN ports
Date: Sun, 5 Dec 2004 14:32:51 +0100

On Sat, 04 Dec 2004 19:46:51 +0100, Thomas Chust <address@hidden> wrote:
> 
> currently I just open the file normally, read it blockwise into a string
> buffer and (display ...) it on the target port. This of course works
> fine for all kinds and sizes of files. But for relatively large files,
> it would be much faster to just map the file into address space and to
> somehow tell CHICKEN to push everything in the mapped memory region
> through the given port. If I copy the data from the mapped region to a
> buffer though, I lose the whole speed and memory footprint advantage
> that I might have had by using mmap!
> 
> Isn't there some dirty trick to coerce a foreign pointer together with
> length information into a CHICKEN string? I think that would be a good
> solution.
> 

Ah, now I understand.

Yes, if the pointer is prefixed by a prioper header, you can use it like
any other data object. All pointers that address data outside of the normal
garbage collected heap are ignored by the garbage collector, but can
be used like any other data, provided they have a proper header:

Here a stupid little example:

;;; Included in the C file:
#>
typedef struct my_string {
  C_header header;
  char data[ 1 ]; 
} MYSTRING;
<#

;;;; included and parsed:
#>!
___scheme_value make_my_string(int n)
{
  MYSTRING *s = (MYSTRING *)malloc(sizeof(C_header) + n);
  s->header = C_STRING_TYPE | n;
  strcpy(s->data, "this is a test.");
  return (___scheme_value)s;
}
<#

(define s (make_my_string 256))
(pp s)  ; will print a string with "this is a test." and lots of junk
(gc #t)
(pp s)

Effectively, just put a 4-byte (on 32-bit platforms) header at the start
of the mmapped region and set it to (C_STRING_TYPE | <length>).


cheers,
felix




reply via email to

[Prev in Thread] Current Thread [Next in Thread]