ia64/xen-unstable

view tools/libxc/xg_private.c @ 17705:7325ea8445cb

libxc: drop double un?lock_pages definition to fix static linking

Signed-off-by: Samuel Thibault <samuel.thibault@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu May 22 15:09:49 2008 +0100 (2008-05-22)
parents 6235c85a145f
children b73f3646a17f
line source
1 /******************************************************************************
2 * xg_private.c
3 *
4 * Helper functions for the rest of the library.
5 */
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <zlib.h>
10 #include <strings.h>
11 #include <malloc.h>
13 #include "xg_private.h"
15 char *xc_read_image(const char *filename, unsigned long *size)
16 {
17 int kernel_fd = -1;
18 gzFile kernel_gfd = NULL;
19 char *image = NULL, *tmp;
20 unsigned int bytes;
22 if ( (filename == NULL) || (size == NULL) )
23 return NULL;
25 if ( (kernel_fd = open(filename, O_RDONLY)) < 0 )
26 {
27 PERROR("Could not open kernel image");
28 goto out;
29 }
31 if ( (kernel_gfd = gzdopen(kernel_fd, "rb")) == NULL )
32 {
33 PERROR("Could not allocate decompression state for state file");
34 goto out;
35 }
37 *size = 0;
39 #define CHUNK 1*1024*1024
40 while(1)
41 {
42 if ( (tmp = realloc(image, *size + CHUNK)) == NULL )
43 {
44 PERROR("Could not allocate memory for kernel image");
45 free(image);
46 image = NULL;
47 goto out;
48 }
49 image = tmp;
51 bytes = gzread(kernel_gfd, image + *size, CHUNK);
52 switch (bytes)
53 {
54 case -1:
55 PERROR("Error reading kernel image");
56 free(image);
57 image = NULL;
58 goto out;
59 case 0: /* EOF */
60 goto out;
61 default:
62 *size += bytes;
63 break;
64 }
65 }
66 #undef CHUNK
68 out:
69 if ( *size == 0 )
70 {
71 PERROR("Could not read kernel image");
72 free(image);
73 image = NULL;
74 }
75 else if ( image )
76 {
77 /* Shrink allocation to fit image. */
78 tmp = realloc(image, *size);
79 if ( tmp )
80 image = tmp;
81 }
83 if ( kernel_gfd != NULL )
84 gzclose(kernel_gfd);
85 else if ( kernel_fd >= 0 )
86 close(kernel_fd);
87 return image;
88 }
90 char *xc_inflate_buffer(const char *in_buf, unsigned long in_size,
91 unsigned long *out_size)
92 {
93 int sts;
94 z_stream zStream;
95 unsigned long out_len;
96 char *out_buf;
98 /* Not compressed? Then return the original buffer. */
99 if ( ((unsigned char)in_buf[0] != 0x1F) ||
100 ((unsigned char)in_buf[1] != 0x8B) )
101 {
102 if ( out_size != NULL )
103 *out_size = in_size;
104 return (char *)in_buf;
105 }
107 out_len = (unsigned char)in_buf[in_size-4] +
108 (256 * ((unsigned char)in_buf[in_size-3] +
109 (256 * ((unsigned char)in_buf[in_size-2] +
110 (256 * (unsigned char)in_buf[in_size-1])))));
112 bzero(&zStream, sizeof(zStream));
113 out_buf = malloc(out_len + 16); /* Leave a little extra space */
114 if ( out_buf == NULL )
115 {
116 ERROR("Error mallocing buffer\n");
117 return NULL;
118 }
120 zStream.next_in = (unsigned char *)in_buf;
121 zStream.avail_in = in_size;
122 zStream.next_out = (unsigned char *)out_buf;
123 zStream.avail_out = out_len+16;
124 sts = inflateInit2(&zStream, (MAX_WBITS+32)); /* +32 means "handle gzip" */
125 if ( sts != Z_OK )
126 {
127 ERROR("inflateInit failed, sts %d\n", sts);
128 free(out_buf);
129 return NULL;
130 }
132 /* Inflate in one pass/call */
133 sts = inflate(&zStream, Z_FINISH);
134 if ( sts != Z_STREAM_END )
135 {
136 ERROR("inflate failed, sts %d\n", sts);
137 free(out_buf);
138 return NULL;
139 }
141 if ( out_size != NULL )
142 *out_size = out_len;
144 return out_buf;
145 }
147 /*******************/
149 int pin_table(
150 int xc_handle, unsigned int type, unsigned long mfn, domid_t dom)
151 {
152 struct mmuext_op op;
154 op.cmd = type;
155 op.arg1.mfn = mfn;
157 if ( xc_mmuext_op(xc_handle, &op, 1, dom) < 0 )
158 return 1;
160 return 0;
161 }
163 /* This is shared between save and restore, and may generally be useful. */
164 unsigned long csum_page(void *page)
165 {
166 int i;
167 unsigned long *p = page;
168 unsigned long long sum=0;
170 for ( i = 0; i < (PAGE_SIZE/sizeof(unsigned long)); i++ )
171 sum += p[i];
173 return sum ^ (sum>>32);
174 }
176 __attribute__((weak))
177 int xc_hvm_build(int xc_handle,
178 uint32_t domid,
179 int memsize,
180 const char *image_name)
181 {
182 errno = ENOSYS;
183 return -1;
184 }
186 void *xg_memalign(size_t alignment, size_t size)
187 {
188 #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
189 int ret;
190 void *ptr;
191 ret = posix_memalign(&ptr, alignment, size);
192 if (ret != 0)
193 return NULL;
194 return ptr;
195 #elif defined(__NetBSD__) || defined(__OpenBSD__)
196 return valloc(size);
197 #else
198 return memalign(alignment, size);
199 #endif
200 }
202 /*
203 * Local variables:
204 * mode: C
205 * c-set-style: "BSD"
206 * c-basic-offset: 4
207 * tab-width: 4
208 * indent-tabs-mode: nil
209 * End:
210 */