[macruby-changes] [1412] MacRuby/branches/experimental

source_changes at macosforge.org source_changes at macosforge.org
Sun Apr 12 21:10:49 PDT 2009


Revision: 1412
          http://trac.macosforge.org/projects/ruby/changeset/1412
Author:   lsansonetti at apple.com
Date:     2009-04-12 21:10:49 -0700 (Sun, 12 Apr 2009)
Log Message:
-----------
better fast-parsing API

Modified Paths:
--------------
    MacRuby/branches/experimental/io.c
    MacRuby/branches/experimental/parse.y

Modified: MacRuby/branches/experimental/io.c
===================================================================
--- MacRuby/branches/experimental/io.c	2009-04-13 03:37:54 UTC (rev 1411)
+++ MacRuby/branches/experimental/io.c	2009-04-13 04:10:49 UTC (rev 1412)
@@ -888,12 +888,12 @@
 	}
     }
 	
-	if (io_struct->pipe != -1) {
-		int status;
-		waitpid(io_struct->pid, &status, 0);
-		close(io_struct->pipe);
-		io_struct->pipe = -1;
-	}
+    if (io_struct->pipe != -1) {
+	int status;
+	waitpid(io_struct->pid, &status, 0);
+	close(io_struct->pipe);
+	io_struct->pipe = -1;
+    }
 
     // Read from the stream.
     data_read += rb_io_stream_read_internal(io_struct->readStream,
@@ -919,22 +919,25 @@
     return bytestring_buffer; 
 }
 
-UInt8 *
-rb_io_read_all_file(VALUE io, size_t *buflen)
+// Called by parse.y
+size_t rb_io_file_size(VALUE io)
 {
     rb_io_t *io_struct = ExtractIOStruct(io);
     struct stat buf;
     if (fstat(io_struct->fd, &buf) == -1) {
-	return NULL;
+	return 0;
     }
-    UInt8 *str = (UInt8 *)xmalloc(buf.st_size);
-    rb_io_read_internal(io_struct, str, buf.st_size);
-    if (buflen != NULL) {
-	*buflen = buf.st_size;
-    }
-    return str; 
+    return buf.st_size;
 }
 
+// Called by parse.y
+bool
+rb_io_read_all_file(VALUE io, UInt8 *buffer, size_t buffer_len)
+{
+    rb_io_t *io_struct = ExtractIOStruct(io);
+    return rb_io_read_internal(io_struct, buffer, buffer_len) > 0;
+}
+
 /*
  *  call-seq:
  *     ios.readpartial(maxlen)              => string

Modified: MacRuby/branches/experimental/parse.y
===================================================================
--- MacRuby/branches/experimental/parse.y	2009-04-13 03:37:54 UTC (rev 1411)
+++ MacRuby/branches/experimental/parse.y	2009-04-13 04:10:49 UTC (rev 1412)
@@ -5157,7 +5157,8 @@
     return rb_parser_compile_file(vparser, f, file, start);
 }
 
-UInt8 *rb_io_read_all_file(VALUE io, size_t *buflen);
+size_t rb_io_file_size(VALUE io);
+bool rb_io_read_all_file(VALUE io, UInt8 *buf, size_t buflen);
 
 NODE*
 rb_parser_compile_file(volatile VALUE vparser, const char *f, VALUE file, int start)
@@ -5168,11 +5169,15 @@
 
     Data_Get_Struct(vparser, struct parser_params, parser);
 
-    size_t buflen = 0;
-    UInt8 *buf = rb_io_read_all_file(file, &buflen);
-    if (buf == NULL || buflen == 0) {
+    size_t buflen = rb_io_file_size(file);
+    if (buflen == 0) {
 	return NULL;
     }
+    // TODO use mmap() if the file is too big
+    UInt8 *buf = (UInt8 *)xmalloc(buflen);
+    if (!rb_io_read_all_file(file, buf, buflen)) {
+	return NULL;
+    }
 
     struct lex_io_gets_data data;
     data.buf = buf;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090412/b7aba9f5/attachment-0001.html>


More information about the macruby-changes mailing list