ia64/xen-unstable

view tools/vnet/libxutil/gzip_stream.c @ 19848:5839491bbf20

[IA64] replace MAX_VCPUS with d->max_vcpus where necessary.

don't use MAX_VCPUS, and use vcpu::max_vcpus.
The changeset of 2f9e1348aa98 introduced max_vcpus to allow more vcpus
per guest. This patch is ia64 counter part.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Isaku Yamahata <yamahata@valinux.co.jp>
date Mon Jun 29 11:26:05 2009 +0900 (2009-06-29)
parents 9f22db685802
children
line source
1 /*
2 * Copyright (C) 2003 Hewlett-Packard Company.
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
19 /** @file
20 * An IOStream implementation using zlib gzFile to provide
21 * compression and decompression.
22 */
23 #ifndef __KERNEL__
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "zlib.h"
30 #include "allocate.h"
31 #include "gzip_stream.h"
33 static int gzip_read(IOStream *s, void *buf, size_t n);
34 static int gzip_write(IOStream *s, const void *buf, size_t n);
35 static int gzip_error(IOStream *s);
36 static int gzip_close(IOStream *s);
37 static void gzip_free(IOStream *s);
38 static int gzip_flush(IOStream *s);
40 /** Methods used by a gzFile* IOStream. */
41 static const IOMethods gzip_methods = {
42 read: gzip_read,
43 write: gzip_write,
44 error: gzip_error,
45 close: gzip_close,
46 free: gzip_free,
47 flush: gzip_flush,
48 };
50 /** Get the underlying gzFile*.
51 *
52 * @param s gzip stream
53 * @return the stream s wraps
54 */
55 static inline gzFile get_gzfile(IOStream *s){
56 return (gzFile)s->data;
57 }
59 /** Write to the underlying stream.
60 *
61 * @param stream destination
62 * @param buf data
63 * @param n number of bytes to write
64 * @return number of bytes written
65 */
66 static int gzip_write(IOStream *s, const void *buf, size_t n){
67 return gzwrite(get_gzfile(s), (void*)buf, n);
68 }
70 /** Read from the underlying stream.
71 *
72 * @param stream input
73 * @param buf where to put input
74 * @param n number of bytes to read
75 * @return number of bytes read
76 */
77 static int gzip_read(IOStream *s, void *buf, size_t n){
78 return gzread(get_gzfile(s), buf, n);
79 }
81 /** Flush the underlying stream.
82 *
83 * @param s gzip stream
84 * @return 0 on success, error code otherwise
85 */
86 static int gzip_flush(IOStream *s){
87 //return gzflush(get_gzfile(s), Z_NO_FLUSH);
88 return gzflush(get_gzfile(s), Z_SYNC_FLUSH);
89 //return gzflush(get_gzfile(s), Z_FULL_FLUSH);
90 }
92 /** Check if a stream has an error.
93 *
94 * @param s gzip stream
95 * @return 1 if has an error, 0 otherwise
96 */
97 static int gzip_error(IOStream *s){
98 int err;
99 gzFile *gz = get_gzfile(s);
100 gzerror(gz, &err);
101 return (err == Z_ERRNO ? 1 /* ferror(gzfile(gz)) */ : err);
102 }
104 /** Close a gzip stream.
105 *
106 * @param s gzip stream to close
107 * @return result of the close
108 */
109 static int gzip_close(IOStream *s){
110 int result = 0;
111 result = gzclose(get_gzfile(s));
112 return result;
113 }
115 /** Free a gzip stream.
116 *
117 * @param s gzip stream
118 */
119 static void gzip_free(IOStream *s){
120 // Nothing to do - close did it all.
121 }
123 /** Create an IOStream for a gzip stream.
124 *
125 * @param f stream to wrap
126 * @return new IOStream using f for i/o
127 */
128 IOStream *gzip_stream_new(gzFile *f){
129 IOStream *io = ALLOCATE(IOStream);
130 if(io){
131 io->methods = &gzip_methods;
132 io->data = (void*)f;
133 }
134 return io;
135 }
137 /** IOStream version of fopen().
138 *
139 * @param file name of the file to open
140 * @param flags giving the mode to open in (as for fopen())
141 * @return new stream for the open file, or NULL if failed
142 */
143 IOStream *gzip_stream_fopen(const char *file, const char *flags){
144 IOStream *io = NULL;
145 gzFile *fgz;
146 fgz = gzopen(file, flags);
147 if(fgz){
148 io = gzip_stream_new(fgz);
149 if(!io){
150 gzclose(fgz);
151 }
152 }
153 return io;
154 }
156 /** IOStream version of fdopen().
157 *
158 * @param fd file descriptor
159 * @param flags giving the mode to open in (as for fdopen())
160 * @return new stream for the open file, or NULL if failed. Always takes
161 * ownership of fd.
162 */
163 IOStream *gzip_stream_fdopen(int fd, const char *flags){
164 IOStream *io = NULL;
165 gzFile *fgz;
166 fgz = gzdopen(fd, flags);
167 if(fgz){
168 io = gzip_stream_new(fgz);
169 if(!io)
170 gzclose(fgz);
171 }
172 return io;
173 }
174 #endif