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

[Xen-changelog] Merge iostream fixes.



ChangeSet 1.1402, 2005/05/13 16:01:20+01:00, mjw@xxxxxxxxxxxxxxxxxxx

        Merge iostream fixes.
        Signed-off-by: Mike Wray <mike.wray@xxxxxx>



 b/tools/libxutil/file_stream.c   |   25 +--
 b/tools/libxutil/gzip_stream.c   |   13 -
 b/tools/libxutil/iostream.h      |   44 ++---
 b/tools/libxutil/kernel_stream.c |   49 +++---
 b/tools/libxutil/string_stream.c |   35 +++-
 tools/xfrd/http.h                |   50 ------
 tools/xfrd/xdr.c                 |  316 ---------------------------------------
 tools/xfrd/xdr.h                 |   30 ---
 8 files changed, 95 insertions(+), 467 deletions(-)


diff -Nru a/tools/libxutil/file_stream.c b/tools/libxutil/file_stream.c
--- a/tools/libxutil/file_stream.c      2005-05-13 16:07:32 -04:00
+++ b/tools/libxutil/file_stream.c      2005-05-13 16:07:32 -04:00
@@ -46,18 +46,21 @@
 static IOStream _iostdin = {
     methods: &file_methods,
     data: (void*)1,
+    nofree: 1,
 };
 
 /** IOStream for stdout. */
 static IOStream _iostdout = {
     methods: &file_methods,
     data: (void*)2,
+    nofree: 1,
 };
 
 /** IOStream for stderr. */
 static IOStream _iostderr = {
     methods: &file_methods,
     data: (void*)3,
+    nofree: 1,
 };
 
 /** IOStream for stdin. */
@@ -152,10 +155,7 @@
  */
 static int file_close(IOStream *s){
     int result = 0;
-    if (s->data){
-        result = fclose(get_file(s));
-        s->data = (void*)0;
-    }
+    result = fclose(get_file(s));
     return result;
 }
 
@@ -164,7 +164,7 @@
  * @param s file stream
  */
 static void file_free(IOStream *s){
-    file_close(s);
+    // Nothing extra to do - close did it all.
 }
 
 /** Create an IOStream for a stream.
@@ -175,8 +175,8 @@
 IOStream *file_stream_new(FILE *f){
     IOStream *io = ALLOCATE(IOStream);
     if(io){
-       io->methods = &file_methods;
-       io->data = (void*)f;
+        io->methods = &file_methods;
+        io->data = (void*)f;
     }
     return io;
 }
@@ -191,10 +191,10 @@
     IOStream *io = 0;
     FILE *fin = fopen(file, flags);
     if(fin){
-       io = file_stream_new(fin);
-       if(!io){
-           fclose(fin);
-       }
+        io = file_stream_new(fin);
+        if(!io){
+            fclose(fin);
+        }
     }
     return io;
 }
@@ -211,8 +211,9 @@
     FILE *fin = fdopen(fd, flags);
     if(fin){
         io = file_stream_new(fin);
-        if(!io)
+        if(!io){
             fclose(fin);
+        }
     }
     return io;
 }
diff -Nru a/tools/libxutil/gzip_stream.c b/tools/libxutil/gzip_stream.c
--- a/tools/libxutil/gzip_stream.c      2005-05-13 16:07:32 -04:00
+++ b/tools/libxutil/gzip_stream.c      2005-05-13 16:07:32 -04:00
@@ -39,7 +39,7 @@
 
 /** Methods used by a gzFile* IOStream. */
 static const IOMethods gzip_methods = {
-    read: gzip_read,
+    read:  gzip_read,
     write: gzip_write,
     error: gzip_error,
     close: gzip_close,
@@ -108,10 +108,7 @@
  */
 static int gzip_close(IOStream *s){
     int result = 0;
-    if (s->data){
-        result = gzclose(get_gzfile(s));
-        s->data = (void*)0;
-    }
+    result = gzclose(get_gzfile(s));
     return result;
 }
 
@@ -120,7 +117,7 @@
  * @param s gzip stream
  */
 static void gzip_free(IOStream *s){
-    gzip_close(s);
+    // Nothing to do - close did it all.
 }
 
 /** Create an IOStream for a gzip stream.
@@ -131,8 +128,8 @@
 IOStream *gzip_stream_new(gzFile *f){
     IOStream *io = ALLOCATE(IOStream);
     if(io){
-       io->methods = &gzip_methods;
-       io->data = (void*)f;
+        io->methods = &gzip_methods;
+        io->data = (void*)f;
     }
     return io;
 }
diff -Nru a/tools/libxutil/iostream.h b/tools/libxutil/iostream.h
--- a/tools/libxutil/iostream.h 2005-05-13 16:07:32 -04:00
+++ b/tools/libxutil/iostream.h 2005-05-13 16:07:32 -04:00
@@ -33,7 +33,7 @@
 
 #include "allocate.h"
 
-/** End of input return value. */
+/** End of input return value (for getc). */
 #define IOSTREAM_EOF -1
 
 /** An input/output abstraction.
@@ -82,6 +82,8 @@
     int written;
     /** Number of bytes read. */
     int read;
+    /** Flag indicating whether not to free when closed. */
+    int nofree;
 };
 
 
@@ -107,7 +109,7 @@
 static inline int IOStream_read(IOStream *stream, void *buf, size_t n){
     int result;
     if(stream->closed){
-        result = IOSTREAM_EOF;
+        result = -EIO;
         goto exit;
     }
     if(!stream->methods || !stream->methods->read){
@@ -132,7 +134,7 @@
 static inline int IOStream_write(IOStream *stream, const void *buf, size_t n){
     int result;
     if(stream->closed){
-        result = IOSTREAM_EOF;
+        result = -EIO;
         goto exit;
     }
     if(!stream->methods || !stream->methods->write){
@@ -150,15 +152,14 @@
 /** Flush the stream.
  *
  * @param stream stream
- * @return 0 on success, IOSTREAM_EOF otherwise
+ * @return 0 on success, negative error code otherwise
  */
 static inline int IOStream_flush(IOStream *stream){
     int result = 0;
     if(stream->closed){
-        result = IOSTREAM_EOF;
+        result = -EIO;
     } else if(stream->methods->flush){
         result = (stream->methods->flush)(stream);
-        if(result < 0) result = IOSTREAM_EOF;
     }
     return result;
 }
@@ -179,14 +180,25 @@
 /** Close the stream.
  *
  * @param stream to close
- * @return 1 for error, 0 otherwise
+ * @return 0 on success, negative error code otherwise
  */
 static inline int IOStream_close(IOStream *stream){
-    int err = 1;
+    int err = 0;
+    if(!stream || stream->closed){
+        err = -EIO;
+        goto exit;
+    }
     if(stream->methods && stream->methods->close){
         err = (stream->methods->close)(stream);
         stream->closed = 1;
     }
+    if(stream->nofree) goto exit;
+    if(stream->methods && stream->methods->free){
+        (stream->methods->free)(stream);
+    }
+    *stream = (IOStream){};
+    deallocate(stream);
+  exit:
     return err;
 }
 
@@ -198,22 +210,6 @@
 static inline int IOStream_is_closed(IOStream *stream){
     return stream->closed;
 }
-
-/** Free the memory used by the stream.
- *
- * @param stream to free
- */
-static inline void IOStream_free(IOStream *stream){
-    if(!stream->closed && stream->methods && stream->methods->close){
-        (stream->methods->close)(stream);
-    }
-    if(stream->methods && stream->methods->free){
-        (stream->methods->free)(stream);
-    }
-    *stream = (IOStream){};
-    deallocate(stream);
-}
-
 
 /** Print a character to a stream, like fputc().
  *
diff -Nru a/tools/libxutil/kernel_stream.c b/tools/libxutil/kernel_stream.c
--- a/tools/libxutil/kernel_stream.c    2005-05-13 16:07:32 -04:00
+++ b/tools/libxutil/kernel_stream.c    2005-05-13 16:07:32 -04:00
@@ -57,10 +57,10 @@
 
 /** Methods for a kernel stream. Output only. */
 static const IOMethods kernel_methods = {
-  write:  kernel_write,
-  free:   kernel_free,
-  lock:   kernel_stream_lock,
-  unlock: kernel_stream_unlock,
+    write:  kernel_write,
+    free:   kernel_free,
+    lock:   kernel_stream_lock,
+    unlock: kernel_stream_unlock,

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.