]> gitweb.factorcode.org Git - factor.git/commitdiff
mapped-file no longer supports sequence protocol. Instead, io.mmap.functor generates...
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Wed, 3 Dec 2008 03:50:34 +0000 (21:50 -0600)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Wed, 3 Dec 2008 03:50:34 +0000 (21:50 -0600)
19 files changed:
basis/help/cookbook/cookbook.factor
basis/io/mmap/alien/alien.factor [new file with mode: 0644]
basis/io/mmap/bool/bool.factor [new file with mode: 0644]
basis/io/mmap/char/char.factor [new file with mode: 0644]
basis/io/mmap/double/double.factor [new file with mode: 0644]
basis/io/mmap/float/float.factor [new file with mode: 0644]
basis/io/mmap/functor/functor.factor
basis/io/mmap/int/int.factor [new file with mode: 0644]
basis/io/mmap/long/long.factor [new file with mode: 0644]
basis/io/mmap/longlong/longlong.factor [new file with mode: 0644]
basis/io/mmap/mmap-docs.factor
basis/io/mmap/mmap-tests.factor
basis/io/mmap/mmap.factor
basis/io/mmap/short/short.factor [new file with mode: 0644]
basis/io/mmap/uchar/uchar.factor [new file with mode: 0644]
basis/io/mmap/uint/uint.factor [new file with mode: 0644]
basis/io/mmap/ulong/ulong.factor [new file with mode: 0644]
basis/io/mmap/ulonglong/ulonglong.factor [new file with mode: 0644]
basis/io/mmap/ushort/ushort.factor [new file with mode: 0644]

index 65b40543af6e0ca8e2862becc9eb240202807fe1..4b806ec8e2c5e9f2b49b2d0a496dbaf0ad67ead6 100644 (file)
@@ -205,10 +205,10 @@ ARTICLE: "cookbook-io" "Input and output cookbook"
 }
 "Convert a file of 4-byte cells from little to big endian or vice versa, by directly mapping it into memory and operating on it with sequence words:"
 { $code
-    "USING: accessors grouping io.files io.mmap kernel sequences ;"
-    "\"mydata.dat\" dup file-info size>> ["
+    "USING: accessors grouping io.files io.mmap.char kernel sequences ;"
+    "\"mydata.dat\" ["
     "    4 <sliced-groups> [ reverse-here ] change-each"
-    "] with-mapped-file"
+    "] with-mapped-char-file"
 }
 "Send some bytes to a remote host:"
 { $code
diff --git a/basis/io/mmap/alien/alien.factor b/basis/io/mmap/alien/alien.factor
new file mode 100644 (file)
index 0000000..4b0a532
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.alien ;
+IN: io.mmap.alien
+
+<< "void*" define-mapped-array >>
\ No newline at end of file
diff --git a/basis/io/mmap/bool/bool.factor b/basis/io/mmap/bool/bool.factor
new file mode 100644 (file)
index 0000000..a2b596f
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.bool ;
+IN: io.mmap.bool
+
+<< "bool" define-mapped-array >>
\ No newline at end of file
diff --git a/basis/io/mmap/char/char.factor b/basis/io/mmap/char/char.factor
new file mode 100644 (file)
index 0000000..453e7e9
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.char ;
+IN: io.mmap.char
+
+<< "char" define-mapped-array >>
\ No newline at end of file
diff --git a/basis/io/mmap/double/double.factor b/basis/io/mmap/double/double.factor
new file mode 100644 (file)
index 0000000..919c006
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.double ;
+IN: io.mmap.double
+
+<< "double" define-mapped-array >>
\ No newline at end of file
diff --git a/basis/io/mmap/float/float.factor b/basis/io/mmap/float/float.factor
new file mode 100644 (file)
index 0000000..33cf16c
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.float ;
+IN: io.mmap.float
+
+<< "float" define-mapped-array >>
\ No newline at end of file
index b60a1c0bf2596b3e053b14bf5fc1d62c87ce2912..4587a75fd9d2b9c97018013f385e7e12bfa62e49 100644 (file)
@@ -1,6 +1,7 @@
 ! Copyright (C) 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: functors accessors alien.c-types math kernel words ;
+USING: io.mmap functors accessors alien.c-types math kernel
+words fry ;
 IN: io.mmap.functor
 
 SLOT: address
@@ -10,13 +11,18 @@ SLOT: length
     [ [ address>> ] [ length>> ] bi ] dip
     heap-size [ 1- + ] keep /i ;
 
-FUNCTOR: mapped-array-functor ( T -- )
+FUNCTOR: define-mapped-array ( T -- )
 
-C   DEFINES <mapped-${T}-array>
-<A> IS      <direct-${T}-array>
+<mapped-A>         DEFINES <mapped-${T}-array>
+<A>                IS      <direct-${T}-array>
+with-mapped-A-file DEFINES with-mapped-${T}-file
 
 WHERE
 
-: C mapped-file>direct <A> execute ; inline
+: <mapped-A> ( mapped-file -- direct-array )
+    T mapped-file>direct <A> execute ; inline
+
+: with-mapped-A-file ( path length quot -- )
+    '[ <mapped-A> execute @ ] with-mapped-file ; inline
 
 ;FUNCTOR
diff --git a/basis/io/mmap/int/int.factor b/basis/io/mmap/int/int.factor
new file mode 100644 (file)
index 0000000..400e81e
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.int ;
+IN: io.mmap.int
+
+<< "int" define-mapped-array >>
\ No newline at end of file
diff --git a/basis/io/mmap/long/long.factor b/basis/io/mmap/long/long.factor
new file mode 100644 (file)
index 0000000..190dd28
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.long ;
+IN: io.mmap.long
+
+<< "long" define-mapped-array >>
\ No newline at end of file
diff --git a/basis/io/mmap/longlong/longlong.factor b/basis/io/mmap/longlong/longlong.factor
new file mode 100644 (file)
index 0000000..4d0a2aa
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.longlong ;
+IN: io.mmap.longlong
+
+<< "longlong" define-mapped-array >>
\ No newline at end of file
index 09922fc9290f5c8d9dd725ef0d6afae14ed4100d..a613c4711f56423346761a9545ffb074967cd931 100644 (file)
@@ -11,9 +11,9 @@ HELP: mapped-file
 } ;
 
 HELP: <mapped-file>
-{ $values { "path" "a pathname string" } { "length" integer } { "mmap" mapped-file } }
-{ $contract "Opens a file and maps the first " { $snippet "length" } " bytes into memory. The length is permitted to exceed the length of the file on disk, in which case the remaining space is padded with zero bytes." }
-{ $notes "You must call " { $link close-mapped-file } " when you are finished working with the returned object, to reclaim resources. The " { $link with-mapped-file } " provides an abstraction which can close the mapped file for you." }
+{ $values { "path" "a pathname string" }  { "mmap" mapped-file } }
+{ $contract "Opens a file and maps its contents into memory. The length is permitted to exceed the length of the file on disk, in which case the remaining space is padded with zero bytes." }
+{ $notes "You must call " { $link dispose } " when you are finished working with the returned object, to reclaim resources. The " { $link with-mapped-file } " provides an abstraction which can close the mapped file for you." }
 { $errors "Throws an error if a memory mapping could not be established." } ;
 
 HELP: with-mapped-file
@@ -26,6 +26,33 @@ HELP: close-mapped-file
 { $contract "Releases system resources associated with the mapped file. This word should not be called by user code; use " { $link dispose } " instead." }
 { $errors "Throws an error if a memory mapping could not be established." } ;
 
+ARTICLE: "io.mmap.arrays" "Memory-mapped arrays"
+"Mapped file can be viewed as a sequence using the words in sub-vocabularies of " { $vocab-link "io.mmap" } ". For each primitive C type " { $snippet "T" } ", a set of words are defined in the vocabulary named " { $snippet "io.mmap.T" } ":"
+{ $table
+    { { $snippet "<mapped-T-array>" } { "Wraps a " { $link mapped-file } " in a sequence; stack effect " { $snippet "( mapped-file -- direct-array )" } } }
+    { { $snippet "with-mapped-T-file" } { "Maps a file into memory and wraps it in a sequence by combining " { $link with-mapped-file } " and " { $snippet "<mapped-T-array>" } "; stack effect " { $snippet "( path quot -- )" } } }
+}
+"The primitive C types for which mapped arrays exist:"
+{ $list
+    { $snippet "char" }
+    { $snippet "uchar" }
+    { $snippet "short" }
+    { $snippet "ushort" }
+    { $snippet "int" }
+    { $snippet "uint" }
+    { $snippet "long" }
+    { $snippet "ulong" }
+    { $snippet "longlong" }
+    { $snippet "ulonglong" }
+    { $snippet "float" }
+    { $snippet "double" }
+    { $snippet "void*" }
+    { $snippet "bool" }
+} ;
+
+ARTICLE: "io.mmap.low-level" "Reading and writing mapped files directly"
+"Data can be read and written from the " { $link mapped-file } " by applying low-level alien words to the " { $slot "address" } " slot. See " { $link "reading-writing-memory" } "." ;
+
 ARTICLE: "io.mmap" "Memory-mapped files"
 "The " { $vocab-link "io.mmap" } " vocabulary implements support for memory-mapped files."
 { $subsection <mapped-file> }
@@ -33,7 +60,8 @@ ARTICLE: "io.mmap" "Memory-mapped files"
 $nl
 "A utility combinator which wraps the above:"
 { $subsection with-mapped-file }
-"Memory mapped files implement the " { $link "sequence-protocol" } " and present themselves as a sequence of bytes. The underlying memory area can also be accessed directly with the " { $snippet "address" } " slot." $nl
-"Data can be read and written from the memory area using alien words. See " { $link "reading-writing-memory" } "." ;
+"Instances of " { $link mapped-file } " don't support any interesting operations in themselves. There are two facilities for accessing their contents:"
+{ $subsection "io.mmap.arrays" }
+{ $subsection "io.mmap.low-level" } ;
 
 ABOUT: "io.mmap"
index 57faca01c734e8812b01c436e26d64f8a3247da4..dc2f0b4687a76c48b5928ad803c8d2c34625e726 100644 (file)
@@ -1,10 +1,10 @@
-USING: io io.mmap io.files kernel tools.test continuations
-sequences io.encodings.ascii accessors ;
+USING: io io.mmap io.mmap.char io.files kernel tools.test
+continuations sequences io.encodings.ascii accessors ;
 IN: io.mmap.tests
 
 [ "mmap-test-file.txt" temp-file delete-file ] ignore-errors
 [ ] [ "12345" "mmap-test-file.txt" temp-file ascii set-file-contents ] unit-test
-[ ] [ "mmap-test-file.txt" temp-file dup file-info size>> [ CHAR: 2 0 pick set-nth drop ] with-mapped-file ] unit-test
-[ 5 ] [ "mmap-test-file.txt" temp-file dup file-info size>> [ length ] with-mapped-file ] unit-test
+[ ] [ "mmap-test-file.txt" temp-file [ CHAR: 2 0 pick set-nth drop ] with-mapped-char-file ] unit-test
+[ 5 ] [ "mmap-test-file.txt" temp-file [ length ] with-mapped-char-file ] unit-test
 [ "22345" ] [ "mmap-test-file.txt" temp-file ascii file-contents ] unit-test
 [ "mmap-test-file.txt" temp-file delete-file ] ignore-errors
index 9be6303e6f7344e512fe0d92c7b2ae5fa3b9c119..3cf451bd036f432af4ae12aecbf38a691cd04164 100644 (file)
@@ -1,39 +1,25 @@
 ! Copyright (C) 2007, 2008 Doug Coleman, Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: continuations destructors io.backend kernel quotations
-sequences system alien alien.accessors accessors
-sequences.private system vocabs.loader combinators
-specialized-arrays.direct functors alien.c-types
-io.mmap.functor ;
+USING: continuations destructors io.files io.backend kernel
+quotations system alien alien.accessors accessors system
+vocabs.loader combinators alien.c-types ;
 IN: io.mmap
 
 TUPLE: mapped-file address handle length disposed ;
 
-M: mapped-file length dup check-disposed length>> ;
-
-M: mapped-file nth-unsafe
-    dup check-disposed address>> swap alien-unsigned-1 ;
-
-M: mapped-file set-nth-unsafe
-    dup check-disposed address>> swap set-alien-unsigned-1 ;
-
-INSTANCE: mapped-file sequence
-
 HOOK: (mapped-file) io-backend ( path length -- address handle )
 
-: <mapped-file> ( path length -- mmap )
-    [ [ normalize-path ] dip (mapped-file) ] keep
+: <mapped-file> ( path -- mmap )
+    [ normalize-path ] [ file-info size>> ] bi [ (mapped-file) ] keep
     f mapped-file boa ;
 
 HOOK: close-mapped-file io-backend ( mmap -- )
 
 M: mapped-file dispose* ( mmap -- ) close-mapped-file ;
 
-: with-mapped-file ( path length quot -- )
+: with-mapped-file ( path quot -- )
     [ <mapped-file> ] dip with-disposal ; inline
 
-APPLY: mapped-array-functor primitive-types
-
 {
     { [ os unix? ] [ "io.unix.mmap" require ] }
     { [ os winnt? ] [ "io.windows.mmap" require ] }
diff --git a/basis/io/mmap/short/short.factor b/basis/io/mmap/short/short.factor
new file mode 100644 (file)
index 0000000..add5815
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.short ;
+IN: io.mmap.short
+
+<< "short" define-mapped-array >>
\ No newline at end of file
diff --git a/basis/io/mmap/uchar/uchar.factor b/basis/io/mmap/uchar/uchar.factor
new file mode 100644 (file)
index 0000000..d30fb60
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.uchar ;
+IN: io.mmap.uchar
+
+<< "uchar" define-mapped-array >>
\ No newline at end of file
diff --git a/basis/io/mmap/uint/uint.factor b/basis/io/mmap/uint/uint.factor
new file mode 100644 (file)
index 0000000..926a0f4
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.uint ;
+IN: io.mmap.uint
+
+<< "uint" define-mapped-array >>
\ No newline at end of file
diff --git a/basis/io/mmap/ulong/ulong.factor b/basis/io/mmap/ulong/ulong.factor
new file mode 100644 (file)
index 0000000..80f70b3
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.ulong ;
+IN: io.mmap.ulong
+
+<< "ulong" define-mapped-array >>
\ No newline at end of file
diff --git a/basis/io/mmap/ulonglong/ulonglong.factor b/basis/io/mmap/ulonglong/ulonglong.factor
new file mode 100644 (file)
index 0000000..91f481c
--- /dev/null
@@ -0,0 +1,4 @@
+USING: io.mmap.functor specialized-arrays.direct.ulonglong ;
+IN: io.mmap.ulonglong
+
+<< "ulonglong" define-mapped-array >>
\ No newline at end of file
diff --git a/basis/io/mmap/ushort/ushort.factor b/basis/io/mmap/ushort/ushort.factor
new file mode 100644 (file)
index 0000000..e0989aa
--- /dev/null
@@ -0,0 +1,4 @@
+USE: specialized-arrays.functor
+IN: specialized-arrays.ushort
+
+<< "ushort" define-array >>
\ No newline at end of file